|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from hcloud.core.client import ClientEntityBase, BoundModelBase, GetEntityByNameMixin |
| 3 | + |
| 4 | +from hcloud.certificates.domain import Certificate |
| 5 | + |
| 6 | + |
| 7 | +class BoundCertificate(BoundModelBase): |
| 8 | + model = Certificate |
| 9 | + |
| 10 | + def update(self, name=None, labels=None): |
| 11 | + # type: (Optional[str], Optional[Dict[str, str]]) -> BoundCertificate |
| 12 | + """Updates an certificate. You can update an certificate name and the certificate labels. |
| 13 | +
|
| 14 | + :param name: str (optional) |
| 15 | + New name to set |
| 16 | + :param labels: Dict[str, str] (optional) |
| 17 | + User-defined labels (key-value pairs) |
| 18 | + :return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate> |
| 19 | + """ |
| 20 | + return self._client.update(self, name, labels) |
| 21 | + |
| 22 | + def delete(self): |
| 23 | + # type: () -> bool |
| 24 | + """Deletes a certificate. |
| 25 | + :return: boolean |
| 26 | + """ |
| 27 | + return self._client.delete(self) |
| 28 | + |
| 29 | + |
| 30 | +class CertificatesClient(ClientEntityBase, GetEntityByNameMixin): |
| 31 | + results_list_attribute_name = 'certificates' |
| 32 | + |
| 33 | + def get_by_id(self, id): |
| 34 | + # type: (int) -> BoundCertificate |
| 35 | + """Get a specific certificate by its ID. |
| 36 | +
|
| 37 | + :param id: int |
| 38 | + :return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>` |
| 39 | + """ |
| 40 | + response = self._client.request(url="/certificates/{certificate_id}".format(certificate_id=id), method="GET") |
| 41 | + return BoundCertificate(self, response['certificate']) |
| 42 | + |
| 43 | + def get_list(self, |
| 44 | + name=None, # type: Optional[str] |
| 45 | + label_selector=None, # type: Optional[str] |
| 46 | + page=None, # type: Optional[int] |
| 47 | + per_page=None # type: Optional[int] |
| 48 | + ): |
| 49 | + # type: (...) -> PageResults[List[BoundCertificate], Meta] |
| 50 | + """Get a list of certificates |
| 51 | +
|
| 52 | + :param name: str (optional) |
| 53 | + Can be used to filter certificates by their name. |
| 54 | + :param label_selector: str (optional) |
| 55 | + Can be used to filter certificates by labels. The response will only contain certificates matching the label selector. |
| 56 | + :param page: int (optional) |
| 57 | + Specifies the page to fetch |
| 58 | + :param per_page: int (optional) |
| 59 | + Specifies how many results are returned by page |
| 60 | + :return: (List[:class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>`], :class:`Meta <hcloud.core.domain.Meta>`) |
| 61 | + """ |
| 62 | + params = {} |
| 63 | + if name is not None: |
| 64 | + params["name"] = name |
| 65 | + |
| 66 | + if label_selector: |
| 67 | + params["label_selector"] = label_selector |
| 68 | + |
| 69 | + if page is not None: |
| 70 | + params['page'] = page |
| 71 | + |
| 72 | + if per_page is not None: |
| 73 | + params['per_page'] = per_page |
| 74 | + |
| 75 | + response = self._client.request(url="/certificates", method="GET", params=params) |
| 76 | + |
| 77 | + certificates = [BoundCertificate(self, certificate_data) for certificate_data in response['certificates']] |
| 78 | + |
| 79 | + return self._add_meta_to_result(certificates, response) |
| 80 | + |
| 81 | + def get_all(self, name=None, label_selector=None): |
| 82 | + # type: (Optional[str]) -> List[BoundCertificate] |
| 83 | + """Get all certificates |
| 84 | +
|
| 85 | + :param name: str (optional) |
| 86 | + Can be used to filter certificates by their name. |
| 87 | + :param label_selector: str (optional) |
| 88 | + Can be used to filter certificates by labels. The response will only contain certificates matching the label selector. |
| 89 | + :return: List[:class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>`] |
| 90 | + """ |
| 91 | + return super(CertificatesClient, self).get_all(name=name, label_selector=label_selector) |
| 92 | + |
| 93 | + def get_by_name(self, name): |
| 94 | + # type: (str) -> BoundCertificate |
| 95 | + """Get certificate by name |
| 96 | +
|
| 97 | + :param name: str |
| 98 | + Used to get certificate by name. |
| 99 | + :return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>` |
| 100 | + """ |
| 101 | + return super(CertificatesClient, self).get_by_name(name) |
| 102 | + |
| 103 | + def create(self, name, certificate, private_key, labels=None): |
| 104 | + # type: (str, str, Optional[Dict[str, str]]) -> BoundCertificate |
| 105 | + """Creates a new Certificate with the given name, certificate and private_key. |
| 106 | +
|
| 107 | + :param name: str |
| 108 | + :param certificate: str |
| 109 | + Certificate and chain in PEM format, in order so that each record directly certifies the one preceding |
| 110 | + :param private_key: str |
| 111 | + Certificate key in PEM format |
| 112 | + :param labels: Dict[str, str] (optional) |
| 113 | + User-defined labels (key-value pairs) |
| 114 | + :return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>` |
| 115 | + """ |
| 116 | + data = { |
| 117 | + 'name': name, |
| 118 | + 'certificate': certificate, |
| 119 | + 'private_key': private_key |
| 120 | + } |
| 121 | + if labels is not None: |
| 122 | + data['labels'] = labels |
| 123 | + response = self._client.request(url="/certificates", method="POST", json=data) |
| 124 | + return BoundCertificate(self, response['certificate']) |
| 125 | + |
| 126 | + def update(self, certificate, name=None, labels=None): |
| 127 | + # type: (Certificate, Optional[str], Optional[Dict[str, str]]) -> BoundCertificate |
| 128 | + """Updates a Certificate. You can update a certificate name and labels. |
| 129 | +
|
| 130 | + :param certificate: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>` or :class:`Certificate <hcloud.certificates.domain.Certificate>` |
| 131 | + :param name: str (optional) |
| 132 | + New name to set |
| 133 | + :param labels: Dict[str, str] (optional) |
| 134 | + User-defined labels (key-value pairs) |
| 135 | + :return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>` |
| 136 | + """ |
| 137 | + data = {} |
| 138 | + if name is not None: |
| 139 | + data['name'] = name |
| 140 | + if labels is not None: |
| 141 | + data['labels'] = labels |
| 142 | + response = self._client.request(url="/certificates/{certificate_id}".format(certificate_id=certificate.id), |
| 143 | + method="PUT", |
| 144 | + json=data) |
| 145 | + return BoundCertificate(self, response['certificate']) |
| 146 | + |
| 147 | + def delete(self, certificate): |
| 148 | + # type: (Certificate) -> bool |
| 149 | + self._client.request(url="/certificates/{certificate_id}".format(certificate_id=certificate.id), |
| 150 | + method="DELETE") |
| 151 | + """Deletes a certificate. |
| 152 | +
|
| 153 | + :param certificate: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>` or :class:`Certificate <hcloud.certificates.domain.Certificate>` |
| 154 | + :return: True |
| 155 | + """ |
| 156 | + # Return always true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised |
| 157 | + return True |
0 commit comments