Skip to content

certificates

ArkDPACertificatesService

Bases: ArkService

Source code in ark_sdk_python/services/dpa/certificates/ark_dpa_certificates_service.py
 33
 34
 35
 36
 37
 38
 39
 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
class ArkDPACertificatesService(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')

    def add_certificate(self, create_certificate: ArkDPACreateCertificate) -> ArkDPACertificate:
        """
        Adds a new certificate through the Access Certificates Service.

        Args:
            create_certificate (ArkDPACreateCertificate): The certificate to add

        Raises:
            ArkServiceException: When the certificate could not be added

        Returns:
            ArkDPACertificate: The added certificate
        """
        self._logger.info("Adding new certificate.")
        cert_body = ''
        try:
            with open(create_certificate.file, 'r', encoding='utf-8') as f:
                cert_body = f.read()
        except Exception as ex:
            raise ArkServiceException(f'Failed to read certificate file [{create_certificate.file}] [{str(ex)}]') from ex
        if not cert_body:
            raise ArkServiceException(f'Certificate file [{create_certificate.file}] is empty')
        create_certificate_req = ArkDPACreateCertificateRequest(cert_body=cert_body, **create_certificate.dict()).dict()
        resp = self.__client.post(CERTIFICATES_API, json=create_certificate_req)
        if resp.status_code == HTTPStatus.CREATED:
            try:
                cert_id = resp.json()['certificate_id']
                return self.certificate(ArkDPAGetCertificate(certificate_id=cert_id))
            except (ValidationError, JSONDecodeError, KeyError) as ex:
                self._logger.exception(f"Failed to parse response from create certificate [{str(ex)}]")
                raise ArkServiceException(f"Failed to parse response from create certificate [{str(ex)}]") from ex
        raise ArkServiceException(f'Failed to add a new certificate [{resp.text}] - [{resp.status_code}]')

    def certificate(self, get_certificate: ArkDPAGetCertificate) -> ArkDPACertificate:
        """
        Retrieves a certificate from the Access Certificates Service.

        Args:
            get_certificate (ArkDPAGetCertificate): The ID of the certificate to retrieve

        Raises:
            ArkServiceException: When the certificate could not be retrieved

        Returns:
            ArkDPACertificate: The retrieved certificate
        """
        self._logger.info(f'Retrieving certificate [{get_certificate.certificate_id}]')
        resp = self.__client.get(CERTIFICATE_API.format(certificate_id=get_certificate.certificate_id))
        if resp.status_code != HTTPStatus.OK:
            raise ArkServiceException(f'Failed to retrieve certificate [{get_certificate.certificate_id}] [{resp.text}]')
        try:
            return ArkDPACertificate.parse_obj(resp.json())
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse certificate response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse policy response [{str(ex)}]') from ex

    def delete_certificate(self, cert: ArkDPADeleteCertificate) -> None:
        """
        Deletes an existing certificate.

        Args:
            cert (ArkDPADeleteCertificate): The ID of the certificate to delete

        Raises:
            ArkServiceException: _description_
        """
        self._logger.info(f"Deleting certificate [{cert.certificate_id}]")
        resp: Response = self.__client.delete(CERTIFICATE_API.format(certificate_id=cert.certificate_id))
        if resp.status_code != HTTPStatus.NO_CONTENT:
            raise ArkServiceException(f'Failed to delete certificate [{cert.certificate_id}] [{resp.status_code}]')

    def update_certificate(self, update_certificate: ArkDPAUpdateCertificate) -> None:
        """
        Updates an existing certificate.

        Args:
            update_certificate (ArkDPAUpdateCertificate): The ID of the certificate to update

        Raises:
            ArkServiceException: _description_
        """
        self._logger.info(f"Updating certificate [{update_certificate.certificate_id}]")
        try:
            with open(update_certificate.file, 'r', encoding='utf-8') as f:
                cert_body = f.read()
        except Exception as ex:
            raise ArkServiceException(f'Failed to read certificate file [{update_certificate.file}] [{str(ex)}]') from ex
        if not cert_body:
            raise ArkServiceException(f'Certificate file [{update_certificate.file}] is empty')
        update_certificate_req = ArkDPAUpdateCertificateRequest(cert_body=cert_body, **update_certificate.dict()).dict()
        resp = self.__client.put(CERTIFICATE_API.format(certificate_id=update_certificate.certificate_id), json=update_certificate_req)
        if resp.status_code != HTTPStatus.OK:
            raise ArkServiceException(f'Failed to update the certificate [{resp.text}] - [{resp.status_code}]')

    def list_certificates(self) -> List[ArkDPAShortCertificate]:
        """
        Lists all certificates.

        Raises:
            ArkServiceException: _description_

        Returns:
            list[ArkDPACertificate]: _description_
        """
        self._logger.info("Listing certificates.")
        resp = self.__client.get(CERTIFICATES_API)
        if resp.status_code != HTTPStatus.OK:
            raise ArkServiceException(f'Failed to list certificates [{resp.text}] - [{resp.status_code}]')
        try:
            return [ArkDPAShortCertificate.parse_obj(cert) for cert in resp.json()['certificates']['items']]
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse certificate response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse certificate response [{str(ex)}]') from ex

    def list_certificates_by(self, certificates_filter: ArkDPACertificatesFilter) -> List[ArkDPAShortCertificate]:
        """
        Lists certificates matching the specified filters.

        Args:
            certificates_filter (ArkDPACertificatesFilter): _description_

        Returns:
            List[ArkDPAShortCertificate]: _description_
        """
        self._logger.info(f'Retrieving certificates by filter [{certificates_filter}]')
        certs = self.list_certificates()

        if certificates_filter.domain_name:
            certs = [cert for cert in certs if certificates_filter.domain_name.lower() in cert.domain.lower()]

        if certificates_filter.cert_name:
            certs = [cert for cert in certs if certificates_filter.cert_name.lower() in cert.cert_name.lower()]

        return certs

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

add_certificate(create_certificate)

Adds a new certificate through the Access Certificates Service.

Parameters:

Name Type Description Default
create_certificate ArkDPACreateCertificate

The certificate to add

required

Raises:

Type Description
ArkServiceException

When the certificate could not be added

Returns:

Name Type Description
ArkDPACertificate ArkDPACertificate

The added certificate

Source code in ark_sdk_python/services/dpa/certificates/ark_dpa_certificates_service.py
39
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
def add_certificate(self, create_certificate: ArkDPACreateCertificate) -> ArkDPACertificate:
    """
    Adds a new certificate through the Access Certificates Service.

    Args:
        create_certificate (ArkDPACreateCertificate): The certificate to add

    Raises:
        ArkServiceException: When the certificate could not be added

    Returns:
        ArkDPACertificate: The added certificate
    """
    self._logger.info("Adding new certificate.")
    cert_body = ''
    try:
        with open(create_certificate.file, 'r', encoding='utf-8') as f:
            cert_body = f.read()
    except Exception as ex:
        raise ArkServiceException(f'Failed to read certificate file [{create_certificate.file}] [{str(ex)}]') from ex
    if not cert_body:
        raise ArkServiceException(f'Certificate file [{create_certificate.file}] is empty')
    create_certificate_req = ArkDPACreateCertificateRequest(cert_body=cert_body, **create_certificate.dict()).dict()
    resp = self.__client.post(CERTIFICATES_API, json=create_certificate_req)
    if resp.status_code == HTTPStatus.CREATED:
        try:
            cert_id = resp.json()['certificate_id']
            return self.certificate(ArkDPAGetCertificate(certificate_id=cert_id))
        except (ValidationError, JSONDecodeError, KeyError) as ex:
            self._logger.exception(f"Failed to parse response from create certificate [{str(ex)}]")
            raise ArkServiceException(f"Failed to parse response from create certificate [{str(ex)}]") from ex
    raise ArkServiceException(f'Failed to add a new certificate [{resp.text}] - [{resp.status_code}]')

certificate(get_certificate)

Retrieves a certificate from the Access Certificates Service.

Parameters:

Name Type Description Default
get_certificate ArkDPAGetCertificate

The ID of the certificate to retrieve

required

Raises:

Type Description
ArkServiceException

When the certificate could not be retrieved

Returns:

Name Type Description
ArkDPACertificate ArkDPACertificate

The retrieved certificate

Source code in ark_sdk_python/services/dpa/certificates/ark_dpa_certificates_service.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def certificate(self, get_certificate: ArkDPAGetCertificate) -> ArkDPACertificate:
    """
    Retrieves a certificate from the Access Certificates Service.

    Args:
        get_certificate (ArkDPAGetCertificate): The ID of the certificate to retrieve

    Raises:
        ArkServiceException: When the certificate could not be retrieved

    Returns:
        ArkDPACertificate: The retrieved certificate
    """
    self._logger.info(f'Retrieving certificate [{get_certificate.certificate_id}]')
    resp = self.__client.get(CERTIFICATE_API.format(certificate_id=get_certificate.certificate_id))
    if resp.status_code != HTTPStatus.OK:
        raise ArkServiceException(f'Failed to retrieve certificate [{get_certificate.certificate_id}] [{resp.text}]')
    try:
        return ArkDPACertificate.parse_obj(resp.json())
    except (ValidationError, JSONDecodeError) as ex:
        self._logger.exception(f'Failed to parse certificate response [{str(ex)}] - [{resp.text}]')
        raise ArkServiceException(f'Failed to parse policy response [{str(ex)}]') from ex

delete_certificate(cert)

Deletes an existing certificate.

Parameters:

Name Type Description Default
cert ArkDPADeleteCertificate

The ID of the certificate to delete

required

Raises:

Type Description
ArkServiceException

description

Source code in ark_sdk_python/services/dpa/certificates/ark_dpa_certificates_service.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def delete_certificate(self, cert: ArkDPADeleteCertificate) -> None:
    """
    Deletes an existing certificate.

    Args:
        cert (ArkDPADeleteCertificate): The ID of the certificate to delete

    Raises:
        ArkServiceException: _description_
    """
    self._logger.info(f"Deleting certificate [{cert.certificate_id}]")
    resp: Response = self.__client.delete(CERTIFICATE_API.format(certificate_id=cert.certificate_id))
    if resp.status_code != HTTPStatus.NO_CONTENT:
        raise ArkServiceException(f'Failed to delete certificate [{cert.certificate_id}] [{resp.status_code}]')

list_certificates()

Lists all certificates.

Raises:

Type Description
ArkServiceException

description

Returns:

Type Description
List[ArkDPAShortCertificate]

list[ArkDPACertificate]: description

Source code in ark_sdk_python/services/dpa/certificates/ark_dpa_certificates_service.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def list_certificates(self) -> List[ArkDPAShortCertificate]:
    """
    Lists all certificates.

    Raises:
        ArkServiceException: _description_

    Returns:
        list[ArkDPACertificate]: _description_
    """
    self._logger.info("Listing certificates.")
    resp = self.__client.get(CERTIFICATES_API)
    if resp.status_code != HTTPStatus.OK:
        raise ArkServiceException(f'Failed to list certificates [{resp.text}] - [{resp.status_code}]')
    try:
        return [ArkDPAShortCertificate.parse_obj(cert) for cert in resp.json()['certificates']['items']]
    except (ValidationError, JSONDecodeError) as ex:
        self._logger.exception(f'Failed to parse certificate response [{str(ex)}] - [{resp.text}]')
        raise ArkServiceException(f'Failed to parse certificate response [{str(ex)}]') from ex

list_certificates_by(certificates_filter)

Lists certificates matching the specified filters.

Parameters:

Name Type Description Default
certificates_filter ArkDPACertificatesFilter

description

required

Returns:

Type Description
List[ArkDPAShortCertificate]

List[ArkDPAShortCertificate]: description

Source code in ark_sdk_python/services/dpa/certificates/ark_dpa_certificates_service.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def list_certificates_by(self, certificates_filter: ArkDPACertificatesFilter) -> List[ArkDPAShortCertificate]:
    """
    Lists certificates matching the specified filters.

    Args:
        certificates_filter (ArkDPACertificatesFilter): _description_

    Returns:
        List[ArkDPAShortCertificate]: _description_
    """
    self._logger.info(f'Retrieving certificates by filter [{certificates_filter}]')
    certs = self.list_certificates()

    if certificates_filter.domain_name:
        certs = [cert for cert in certs if certificates_filter.domain_name.lower() in cert.domain.lower()]

    if certificates_filter.cert_name:
        certs = [cert for cert in certs if certificates_filter.cert_name.lower() in cert.cert_name.lower()]

    return certs

update_certificate(update_certificate)

Updates an existing certificate.

Parameters:

Name Type Description Default
update_certificate ArkDPAUpdateCertificate

The ID of the certificate to update

required

Raises:

Type Description
ArkServiceException

description

Source code in ark_sdk_python/services/dpa/certificates/ark_dpa_certificates_service.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def update_certificate(self, update_certificate: ArkDPAUpdateCertificate) -> None:
    """
    Updates an existing certificate.

    Args:
        update_certificate (ArkDPAUpdateCertificate): The ID of the certificate to update

    Raises:
        ArkServiceException: _description_
    """
    self._logger.info(f"Updating certificate [{update_certificate.certificate_id}]")
    try:
        with open(update_certificate.file, 'r', encoding='utf-8') as f:
            cert_body = f.read()
    except Exception as ex:
        raise ArkServiceException(f'Failed to read certificate file [{update_certificate.file}] [{str(ex)}]') from ex
    if not cert_body:
        raise ArkServiceException(f'Certificate file [{update_certificate.file}] is empty')
    update_certificate_req = ArkDPAUpdateCertificateRequest(cert_body=cert_body, **update_certificate.dict()).dict()
    resp = self.__client.put(CERTIFICATE_API.format(certificate_id=update_certificate.certificate_id), json=update_certificate_req)
    if resp.status_code != HTTPStatus.OK:
        raise ArkServiceException(f'Failed to update the certificate [{resp.text}] - [{resp.status_code}]')