| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Hetzner Cloud GmbH <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: certificate_info |
| 12 |
short_description: Gather infos about your Hetzner Cloud certificates. |
| 13 |
description: |
| 14 |
- Gather facts about your Hetzner Cloud certificates. |
| 15 |
author: |
| 16 |
- Lukas Kaemmerling (@LKaemmerling) |
| 17 |
options: |
| 18 |
id: |
| 19 |
description: |
| 20 |
- The ID of the certificate you want to get. |
| 21 |
- The module will fail if the provided ID is invalid. |
| 22 |
type: int |
| 23 |
name: |
| 24 |
description: |
| 25 |
- The name of the certificate you want to get. |
| 26 |
type: str |
| 27 |
label_selector: |
| 28 |
description: |
| 29 |
- The label selector for the certificate you want to get. |
| 30 |
type: str |
| 31 |
extends_documentation_fragment: |
| 32 |
- hetzner.hcloud.hcloud |
| 33 |
|
| 34 |
""" |
| 35 |
|
| 36 |
EXAMPLES = """ |
| 37 |
- name: Gather hcloud certificate infos |
| 38 |
hetzner.hcloud.certificate_info: |
| 39 |
register: output |
| 40 |
- name: Print the gathered infos |
| 41 |
debug: |
| 42 |
var: output.hcloud_certificate_info |
| 43 |
""" |
| 44 |
|
| 45 |
RETURN = """ |
| 46 |
hcloud_certificate_info: |
| 47 |
description: The certificate instances |
| 48 |
returned: Always |
| 49 |
type: list |
| 50 |
contains: |
| 51 |
id: |
| 52 |
description: Numeric identifier of the certificate |
| 53 |
returned: always |
| 54 |
type: int |
| 55 |
sample: 1937415 |
| 56 |
name: |
| 57 |
description: Name of the certificate |
| 58 |
returned: always |
| 59 |
type: str |
| 60 |
sample: my website cert |
| 61 |
fingerprint: |
| 62 |
description: Fingerprint of the certificate |
| 63 |
returned: always |
| 64 |
type: str |
| 65 |
sample: "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f" |
| 66 |
certificate: |
| 67 |
description: Certificate and chain in PEM format |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
sample: "-----BEGIN CERTIFICATE-----..." |
| 71 |
domain_names: |
| 72 |
description: List of Domains and Subdomains covered by the Certificate |
| 73 |
returned: always |
| 74 |
type: dict |
| 75 |
not_valid_before: |
| 76 |
description: Point in time when the Certificate becomes valid (in ISO-8601 format) |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
not_valid_after: |
| 80 |
description: Point in time when the Certificate stops being valid (in ISO-8601 format) |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
labels: |
| 84 |
description: User-defined labels (key-value pairs) |
| 85 |
returned: always |
| 86 |
type: dict |
| 87 |
""" |
| 88 |
|
| 89 |
from ansible.module_utils.basic import AnsibleModule |
| 90 |
|
| 91 |
from ..module_utils._base import AnsibleHCloud |
| 92 |
from ..module_utils._vendor.hcloud import HCloudException |
| 93 |
from ..module_utils._vendor.hcloud.certificates import BoundCertificate |
| 94 |
|
| 95 |
|
| 96 |
class AnsibleHCloudCertificateInfo(AnsibleHCloud): |
| 97 |
represent = "hcloud_certificate_info" |
| 98 |
|
| 99 |
hcloud_certificate_info: list[BoundCertificate] | None = None |
| 100 |
|
| 101 |
def _prepare_result(self): |
| 102 |
tmp = [] |
| 103 |
|
| 104 |
for certificate in self.hcloud_certificate_info: |
| 105 |
if certificate is None: |
| 106 |
continue |
| 107 |
|
| 108 |
tmp.append( |
| 109 |
{ |
| 110 |
"id": certificate.id, |
| 111 |
"name": certificate.name, |
| 112 |
"fingerprint": certificate.fingerprint, |
| 113 |
"certificate": certificate.certificate, |
| 114 |
"not_valid_before": certificate.not_valid_before.isoformat(), |
| 115 |
"not_valid_after": certificate.not_valid_after.isoformat(), |
| 116 |
"domain_names": certificate.domain_names, |
| 117 |
"labels": certificate.labels, |
| 118 |
} |
| 119 |
) |
| 120 |
return tmp |
| 121 |
|
| 122 |
def get_certificates(self): |
| 123 |
try: |
| 124 |
if self.module.params.get("id") is not None: |
| 125 |
self.hcloud_certificate_info = [self.client.certificates.get_by_id(self.module.params.get("id"))] |
| 126 |
elif self.module.params.get("name") is not None: |
| 127 |
self.hcloud_certificate_info = [self.client.certificates.get_by_name(self.module.params.get("name"))] |
| 128 |
elif self.module.params.get("label_selector") is not None: |
| 129 |
self.hcloud_certificate_info = self.client.certificates.get_all( |
| 130 |
label_selector=self.module.params.get("label_selector") |
| 131 |
) |
| 132 |
else: |
| 133 |
self.hcloud_certificate_info = self.client.certificates.get_all() |
| 134 |
|
| 135 |
except HCloudException as exception: |
| 136 |
self.fail_json_hcloud(exception) |
| 137 |
|
| 138 |
@classmethod |
| 139 |
def define_module(cls): |
| 140 |
return AnsibleModule( |
| 141 |
argument_spec=dict( |
| 142 |
id={"type": "int"}, |
| 143 |
name={"type": "str"}, |
| 144 |
label_selector={"type": "str"}, |
| 145 |
**super().base_module_arguments(), |
| 146 |
), |
| 147 |
supports_check_mode=True, |
| 148 |
) |
| 149 |
|
| 150 |
|
| 151 |
def main(): |
| 152 |
module = AnsibleHCloudCertificateInfo.define_module() |
| 153 |
hcloud = AnsibleHCloudCertificateInfo(module) |
| 154 |
|
| 155 |
hcloud.get_certificates() |
| 156 |
result = hcloud.get_result() |
| 157 |
|
| 158 |
ansible_info = {"hcloud_certificate_info": result["hcloud_certificate_info"]} |
| 159 |
module.exit_json(**ansible_info) |
| 160 |
|
| 161 |
|
| 162 |
if __name__ == "__main__": |
| 163 |
main() |