Skip to content

db

ArkDPADBPoliciesService

Bases: ArkService

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
class ArkDPADBPoliciesService(ArkService):
    def __init__(self, isp_auth: ArkISPAuth) -> None:
        super().__init__(isp_auth)
        self.__isp_auth = isp_auth
        self.__client: ArkISPServiceClient = ArkISPServiceClient.from_isp_auth(self.__isp_auth, 'dpa')

    @property
    def isp_client(self) -> ArkISPServiceClient:
        return self.__client

    def __policy_id_by_name(self, policy_name: str) -> str:
        policies = self.list_policies_by(ArkDPADBPoliciesFilter(name=policy_name))
        if not policies:
            raise ArkServiceException(f'Failed to find db policy id by name [{policy_name}]')
        return policies[0].policy_id

    def add_policy(self, add_policy: ArkDPADBAddPolicy) -> ArkDPADBPolicy:
        """
        Adds a new DB policy with the specified information.

        Args:
            add_policy (ArkDPADBAddPolicy): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBPolicy: _description_
        """
        self._logger.info(f'Adding new db policy [{add_policy.policy_name}]')
        add_policy_dict = add_policy.dict(by_alias=True, exclude_none=True)
        resp: Response = self.__client.post(DB_POLICIES_API, json=add_policy_dict)
        if resp.status_code == HTTPStatus.CREATED:
            try:
                policy_id = resp.json()['policyId']
                return self.policy(ArkDPAGetPolicy(policy_id=policy_id))
            except (ValidationError, JSONDecodeError, KeyError) as ex:
                self._logger.exception(f'Failed to parse add db policy response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse add sb policy response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to add db policy [{resp.text}] - [{resp.status_code}]')

    def delete_policy(self, delete_policy: ArkDPADeletePolicy) -> None:
        """
        Deletes the specified (ID or name) DB policy.

        Args:
            delete_policy (ArkDPADeletePolicy): _description_

        Raises:
            ArkServiceException: _description_
        """
        if delete_policy.policy_name and not delete_policy.policy_id:
            delete_policy.policy_id = self.__policy_id_by_name(delete_policy.policy_name)
        self._logger.info(f'Deleting db policy [{delete_policy.policy_id}]')
        resp: Response = self.__client.delete(DB_POLICY_API.format(policy_id=delete_policy.policy_id))
        if resp.status_code != HTTPStatus.NO_CONTENT:
            raise ArkServiceException(f'Failed to delete db policy [{resp.text}] - [{resp.status_code}]')

    def update_policy(self, update_policy: ArkDPADBUpdatePolicy) -> ArkDPADBPolicy:
        """
        Updates a DB policy.

        Args:
            update_policy (ArkDPADBUpdatePolicy): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBPolicy: _description_
        """
        if update_policy.policy_name and not update_policy.policy_id:
            update_policy.policy_id = self.__policy_id_by_name(update_policy.policy_name)
        self._logger.info(f'Updating db policy [{update_policy.policy_id}]')
        update_dict = json.loads(update_policy.json(by_alias=True, exclude_none=True, exclude={'new_policy_name', 'policy_name'}))
        if update_policy.new_policy_name:
            update_dict['policyName'] = update_policy.new_policy_name
        else:
            update_dict['policyName'] = update_policy.policy_name
        resp: Response = self.__client.put(DB_POLICY_API.format(policy_id=update_policy.policy_id), json=update_dict)
        if resp.status_code == HTTPStatus.OK:
            try:
                return ArkDPADBPolicy.parse_obj(resp.json())
            except (ValidationError, JSONDecodeError) as ex:
                self._logger.exception(f'Failed to parse update db policy response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse update db policy response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to update db policy [{resp.text}] - [{resp.status_code}]')

    def update_policy_status(self, update_policy_status: ArkDPAUpdatePolicyStatus) -> ArkDPADBPolicy:
        """
        Updates the status of the specified (by ID) DB policy.

        Args:
            update_policy_status (ArkDPAUpdatePolicyStatus): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBPolicy: _description_
        """
        if update_policy_status.policy_name and not update_policy_status.policy_id:
            update_policy_status.policy_id = self.__policy_id_by_name(update_policy_status.policy_name)
        self._logger.info(f'Updating db policy status [{update_policy_status.policy_id}]')
        resp: Response = self.__client.put(
            DB_UPDATE_POLICY_STATUS_API.format(policy_id=update_policy_status.policy_id),
            json=update_policy_status.dict(exclude={'policy_id'}),
        )
        if resp.status_code == HTTPStatus.OK:
            return self.policy(ArkDPAGetPolicy(policy_id=update_policy_status.policy_id))
        raise ArkServiceException(f'Failed to update db policy status [{resp.text}] - [{resp.status_code}]')

    def list_policies(self) -> List[ArkDPADBPolicyListItem]:
        """
        Lists all of the tenants's DB policies.

        Raises:
            ArkServiceException: _description_

        Returns:
            List[ArkDPADBPolicyListItem]: _description_
        """
        self._logger.info('Retrieving all db policies')
        resp: Response = self.__client.get(DB_POLICIES_API)
        if resp.status_code == HTTPStatus.OK:
            try:
                return parse_obj_as(List[ArkDPADBPolicyListItem], resp.json()['items'])
            except (ValidationError, JSONDecodeError, KeyError) as ex:
                self._logger.exception(f'Failed to parse list db policies response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse list db policies response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to list db policies [{resp.text}] - [{resp.status_code}]')

    def list_policies_by(self, policies_filter: ArkDPADBPoliciesFilter) -> List[ArkDPADBPolicyListItem]:
        """
        Lists DB policies that match the specified filters.

        Args:
            policies_filter (ArkDPADBPoliciesFilter): _description_

        Returns:
            List[ArkDPADBPolicyListItem]: _description_
        """
        self._logger.info(f'Retrieving db policies by filter [{policies_filter}]')
        policies = self.list_policies()

        # Filter by statuses
        if policies_filter.statuses:
            policies = [p for p in policies if p.status in policies_filter.statuses]

        # Filter by name wildcard
        if policies_filter.name:
            policies = [p for p in policies if fnmatch(p.policy_name, policies_filter.name)]

        # Filter by cloud providers
        if policies_filter.providers:
            policies = [p for p in policies if all(cp.value in p.providers for cp in policies_filter.providers)]

        return policies

    def policy(self, get_policy: ArkDPAGetPolicy) -> ArkDPADBPolicy:
        """
        Retrieves a DB policy by ID.

        Args:
            get_policy (ArkDPAGetPolicy): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBPolicy: _description_
        """
        if get_policy.policy_name and not get_policy.policy_id:
            get_policy.policy_id = self.__policy_id_by_name(get_policy.policy_name)
        self._logger.info(f'Retrieving db policy [{get_policy.policy_id}]')
        resp: Response = self.__client.get(DB_POLICY_API.format(policy_id=get_policy.policy_id))
        if resp.status_code == HTTPStatus.OK:
            try:
                return ArkDPADBPolicy.parse_obj(resp.json())
            except (ValidationError, JSONDecodeError) as ex:
                self._logger.exception(f'Failed to parse db policy response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse db policy response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to retrieve db policy [{get_policy.policy_id}] [{resp.text}] - [{resp.status_code}]')

    def policies_stats(self) -> ArkDPADBPoliciesStats:
        """
        Calculates policy statistics.

        Returns:
            ArkDPADBPoliciesStats: _description_
        """
        self._logger.info('Calculating db policies stats')
        policies = self.list_policies()
        policies_stats = ArkDPADBPoliciesStats.construct()
        policies_stats.policies_count = len(policies)

        # Count policies per status
        status_types: Set[ArkDPARuleStatus] = {p.status for p in policies if p.status}
        policies_stats.policies_count_per_status = {st: len([p for p in policies if p.status and p.status == st]) for st in status_types}

        # Count policies per platforms
        policies_stats.policies_count_per_provider = {}
        for policy in policies:
            for platform in policy.providers:
                if platform not in policies_stats.policies_count_per_provider:
                    policies_stats.policies_count_per_provider[platform] = 0
                policies_stats.policies_count_per_provider[platform] += 1

        return policies_stats

    @staticmethod
    @overrides
    def service_config() -> ArkServiceConfig:
        return SERVICE_CONFIG

add_policy(add_policy)

Adds a new DB policy with the specified information.

Parameters:

Name Type Description Default
add_policy ArkDPADBAddPolicy

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBPolicy ArkDPADBPolicy

description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def add_policy(self, add_policy: ArkDPADBAddPolicy) -> ArkDPADBPolicy:
    """
    Adds a new DB policy with the specified information.

    Args:
        add_policy (ArkDPADBAddPolicy): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBPolicy: _description_
    """
    self._logger.info(f'Adding new db policy [{add_policy.policy_name}]')
    add_policy_dict = add_policy.dict(by_alias=True, exclude_none=True)
    resp: Response = self.__client.post(DB_POLICIES_API, json=add_policy_dict)
    if resp.status_code == HTTPStatus.CREATED:
        try:
            policy_id = resp.json()['policyId']
            return self.policy(ArkDPAGetPolicy(policy_id=policy_id))
        except (ValidationError, JSONDecodeError, KeyError) as ex:
            self._logger.exception(f'Failed to parse add db policy response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse add sb policy response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to add db policy [{resp.text}] - [{resp.status_code}]')

delete_policy(delete_policy)

Deletes the specified (ID or name) DB policy.

Parameters:

Name Type Description Default
delete_policy ArkDPADeletePolicy

description

required

Raises:

Type Description
ArkServiceException

description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def delete_policy(self, delete_policy: ArkDPADeletePolicy) -> None:
    """
    Deletes the specified (ID or name) DB policy.

    Args:
        delete_policy (ArkDPADeletePolicy): _description_

    Raises:
        ArkServiceException: _description_
    """
    if delete_policy.policy_name and not delete_policy.policy_id:
        delete_policy.policy_id = self.__policy_id_by_name(delete_policy.policy_name)
    self._logger.info(f'Deleting db policy [{delete_policy.policy_id}]')
    resp: Response = self.__client.delete(DB_POLICY_API.format(policy_id=delete_policy.policy_id))
    if resp.status_code != HTTPStatus.NO_CONTENT:
        raise ArkServiceException(f'Failed to delete db policy [{resp.text}] - [{resp.status_code}]')

list_policies()

Lists all of the tenants's DB policies.

Raises:

Type Description
ArkServiceException

description

Returns:

Type Description
List[ArkDPADBPolicyListItem]

List[ArkDPADBPolicyListItem]: description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def list_policies(self) -> List[ArkDPADBPolicyListItem]:
    """
    Lists all of the tenants's DB policies.

    Raises:
        ArkServiceException: _description_

    Returns:
        List[ArkDPADBPolicyListItem]: _description_
    """
    self._logger.info('Retrieving all db policies')
    resp: Response = self.__client.get(DB_POLICIES_API)
    if resp.status_code == HTTPStatus.OK:
        try:
            return parse_obj_as(List[ArkDPADBPolicyListItem], resp.json()['items'])
        except (ValidationError, JSONDecodeError, KeyError) as ex:
            self._logger.exception(f'Failed to parse list db policies response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse list db policies response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to list db policies [{resp.text}] - [{resp.status_code}]')

list_policies_by(policies_filter)

Lists DB policies that match the specified filters.

Parameters:

Name Type Description Default
policies_filter ArkDPADBPoliciesFilter

description

required

Returns:

Type Description
List[ArkDPADBPolicyListItem]

List[ArkDPADBPolicyListItem]: description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def list_policies_by(self, policies_filter: ArkDPADBPoliciesFilter) -> List[ArkDPADBPolicyListItem]:
    """
    Lists DB policies that match the specified filters.

    Args:
        policies_filter (ArkDPADBPoliciesFilter): _description_

    Returns:
        List[ArkDPADBPolicyListItem]: _description_
    """
    self._logger.info(f'Retrieving db policies by filter [{policies_filter}]')
    policies = self.list_policies()

    # Filter by statuses
    if policies_filter.statuses:
        policies = [p for p in policies if p.status in policies_filter.statuses]

    # Filter by name wildcard
    if policies_filter.name:
        policies = [p for p in policies if fnmatch(p.policy_name, policies_filter.name)]

    # Filter by cloud providers
    if policies_filter.providers:
        policies = [p for p in policies if all(cp.value in p.providers for cp in policies_filter.providers)]

    return policies

policies_stats()

Calculates policy statistics.

Returns:

Name Type Description
ArkDPADBPoliciesStats ArkDPADBPoliciesStats

description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def policies_stats(self) -> ArkDPADBPoliciesStats:
    """
    Calculates policy statistics.

    Returns:
        ArkDPADBPoliciesStats: _description_
    """
    self._logger.info('Calculating db policies stats')
    policies = self.list_policies()
    policies_stats = ArkDPADBPoliciesStats.construct()
    policies_stats.policies_count = len(policies)

    # Count policies per status
    status_types: Set[ArkDPARuleStatus] = {p.status for p in policies if p.status}
    policies_stats.policies_count_per_status = {st: len([p for p in policies if p.status and p.status == st]) for st in status_types}

    # Count policies per platforms
    policies_stats.policies_count_per_provider = {}
    for policy in policies:
        for platform in policy.providers:
            if platform not in policies_stats.policies_count_per_provider:
                policies_stats.policies_count_per_provider[platform] = 0
            policies_stats.policies_count_per_provider[platform] += 1

    return policies_stats

policy(get_policy)

Retrieves a DB policy by ID.

Parameters:

Name Type Description Default
get_policy ArkDPAGetPolicy

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBPolicy ArkDPADBPolicy

description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def policy(self, get_policy: ArkDPAGetPolicy) -> ArkDPADBPolicy:
    """
    Retrieves a DB policy by ID.

    Args:
        get_policy (ArkDPAGetPolicy): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBPolicy: _description_
    """
    if get_policy.policy_name and not get_policy.policy_id:
        get_policy.policy_id = self.__policy_id_by_name(get_policy.policy_name)
    self._logger.info(f'Retrieving db policy [{get_policy.policy_id}]')
    resp: Response = self.__client.get(DB_POLICY_API.format(policy_id=get_policy.policy_id))
    if resp.status_code == HTTPStatus.OK:
        try:
            return ArkDPADBPolicy.parse_obj(resp.json())
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse db policy response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse db policy response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to retrieve db policy [{get_policy.policy_id}] [{resp.text}] - [{resp.status_code}]')

update_policy(update_policy)

Updates a DB policy.

Parameters:

Name Type Description Default
update_policy ArkDPADBUpdatePolicy

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBPolicy ArkDPADBPolicy

description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def update_policy(self, update_policy: ArkDPADBUpdatePolicy) -> ArkDPADBPolicy:
    """
    Updates a DB policy.

    Args:
        update_policy (ArkDPADBUpdatePolicy): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBPolicy: _description_
    """
    if update_policy.policy_name and not update_policy.policy_id:
        update_policy.policy_id = self.__policy_id_by_name(update_policy.policy_name)
    self._logger.info(f'Updating db policy [{update_policy.policy_id}]')
    update_dict = json.loads(update_policy.json(by_alias=True, exclude_none=True, exclude={'new_policy_name', 'policy_name'}))
    if update_policy.new_policy_name:
        update_dict['policyName'] = update_policy.new_policy_name
    else:
        update_dict['policyName'] = update_policy.policy_name
    resp: Response = self.__client.put(DB_POLICY_API.format(policy_id=update_policy.policy_id), json=update_dict)
    if resp.status_code == HTTPStatus.OK:
        try:
            return ArkDPADBPolicy.parse_obj(resp.json())
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse update db policy response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse update db policy response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to update db policy [{resp.text}] - [{resp.status_code}]')

update_policy_status(update_policy_status)

Updates the status of the specified (by ID) DB policy.

Parameters:

Name Type Description Default
update_policy_status ArkDPAUpdatePolicyStatus

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBPolicy ArkDPADBPolicy

description

Source code in ark_sdk_python/services/dpa/policies/db/ark_dpa_db_policies_service.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def update_policy_status(self, update_policy_status: ArkDPAUpdatePolicyStatus) -> ArkDPADBPolicy:
    """
    Updates the status of the specified (by ID) DB policy.

    Args:
        update_policy_status (ArkDPAUpdatePolicyStatus): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBPolicy: _description_
    """
    if update_policy_status.policy_name and not update_policy_status.policy_id:
        update_policy_status.policy_id = self.__policy_id_by_name(update_policy_status.policy_name)
    self._logger.info(f'Updating db policy status [{update_policy_status.policy_id}]')
    resp: Response = self.__client.put(
        DB_UPDATE_POLICY_STATUS_API.format(policy_id=update_policy_status.policy_id),
        json=update_policy_status.dict(exclude={'policy_id'}),
    )
    if resp.status_code == HTTPStatus.OK:
        return self.policy(ArkDPAGetPolicy(policy_id=update_policy_status.policy_id))
    raise ArkServiceException(f'Failed to update db policy status [{resp.text}] - [{resp.status_code}]')