Add managed certs support (#72)

This commit is contained in:
Lukas Kämmerling 2021-04-06 11:59:59 +02:00 committed by GitHub
parent cf4b59257a
commit e16ae06ea3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 8 deletions

View file

@ -49,6 +49,7 @@ ansible-devel-4/4:
ansible-210-1/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:
@ -59,6 +60,7 @@ ansible-210-1/4:
ansible-210-2/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:
@ -69,6 +71,7 @@ ansible-210-2/4:
ansible-210-3/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:
@ -79,6 +82,7 @@ ansible-210-3/4:
ansible-210-4/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:
@ -89,6 +93,7 @@ ansible-210-4/4:
ansible-29-1/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:
@ -99,6 +104,7 @@ ansible-29-1/4:
ansible-29-2/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:
@ -109,6 +115,7 @@ ansible-29-2/4:
ansible-29-3/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:
@ -119,6 +126,7 @@ ansible-29-3/4:
ansible-29-4/4:
stage: sanity
image: python:3.6-buster
allow_failure: true
except:
- tags
script:

View file

@ -46,6 +46,18 @@ options:
- Certificate key in PEM format.
- Required if certificate does not exists.
type: str
domain_names:
description:
- Certificate key in PEM format.
- Required if certificate does not exists.
type: list
elements: str
type:
description:
- Choose between uploading a Certificate in PEM format or requesting a managed Let's Encrypt Certificate.
default: uploaded
choices: [ uploaded, managed ]
type: str
state:
description:
- State of the certificate.
@ -146,6 +158,7 @@ class AnsibleHcloudCertificate(Hcloud):
return {
"id": to_native(self.hcloud_certificate.id),
"name": to_native(self.hcloud_certificate.name),
"type": to_native(self.hcloud_certificate.type),
"fingerprint": to_native(self.hcloud_certificate.fingerprint),
"certificate": to_native(self.hcloud_certificate.certificate),
"not_valid_before": to_native(self.hcloud_certificate.not_valid_before),
@ -170,20 +183,36 @@ class AnsibleHcloudCertificate(Hcloud):
def _create_certificate(self):
self.module.fail_on_missing_params(
required_params=["name", "certificate", "private_key"]
required_params=["name"]
)
params = {
"name": self.module.params.get("name"),
"certificate": self.module.params.get("certificate"),
"private_key": self.module.params.get("private_key"),
"labels": self.module.params.get("labels")
}
if self.module.params.get('type') == 'uploaded':
self.module.fail_on_missing_params(
required_params=["certificate", "private_key"]
)
params["certificate"] = self.module.params.get("certificate")
params["private_key"] = self.module.params.get("private_key")
if not self.module.check_mode:
try:
self.client.certificates.create(**params)
except Exception as e:
self.module.fail_json(msg=e.message)
else:
self.module.fail_on_missing_params(
required_params=["domain_names"]
)
params["domain_names"] = self.module.params.get("domain_names")
if not self.module.check_mode:
try:
resp = self.client.certificates.create_managed(**params)
resp.action.wait_until_finished(max_retries=1000)
except Exception as e:
self.module.fail_json(msg=e.message)
if not self.module.check_mode:
try:
self.client.certificates.create(**params)
except Exception as e:
self.module.fail_json(msg=e.message)
self._mark_as_changed()
self._get_certificate()
@ -231,6 +260,11 @@ class AnsibleHcloudCertificate(Hcloud):
argument_spec=dict(
id={"type": "int"},
name={"type": "str"},
type={
"choices": ["uploaded", "managed"],
"default": "uploaded",
},
domain_names={"type": "list", "elements": "str", "default": []},
certificate={"type": "str"},
private_key={"type": "str", "no_log": True},
labels={"type": "dict"},

View file

@ -3,3 +3,4 @@
---
hcloud_prefix: "tests"
hcloud_certificate_name: "{{hcloud_prefix}}-integration"
hcloud_dns_test_domain: "{{hcloud_prefix | truncate(19, False, 'ans')}}.hc-certs.de"

View file

@ -121,3 +121,29 @@
assert:
that:
- result is success
- name: test create managed certificate
hcloud_certificate:
name: "{{ hcloud_certificate_name }}"
domain_names:
- "{{ hcloud_dns_test_domain }}"
type: managed
labels:
HC-Use-Staging-CA: "true"
register: result
- name: test rename certificate
assert:
that:
- result is changed
- result.hcloud_certificate.name == "{{ hcloud_certificate_name }}"
- result.hcloud_certificate.domain_names[0] == "{{ hcloud_dns_test_domain }}"
- name: absent certificate
hcloud_certificate:
id: "{{ result.hcloud_certificate.id }}"
state: absent
register: result
- name: verify absent server
assert:
that:
- result is success