Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
markuman committed Apr 19, 2021
1 parent d0211a3 commit 6180bf3
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.4.0

* add support to add/delete muliple DNS records for one Name
* new paramter `purge` with alias parameter `replace`, `overwrite` and `solo` to be compatible with other ansible dns modules.


# 1.3.0

* Grafeful error handling
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ install: ## install collection localy
remove: ## remove collection localy
rm -rf markuman* ~/.ansible/collections/ansible_collections/markuman/hetzner_dns

syntax: ## test compile
python -m py_compile plugins/modules/record.py
python -m py_compile plugins/modules/record_info.py
python -m py_compile plugins/modules/zone_info.py
python -m py_compile plugins/module_utils/helper.py

round: ## remove, build install
$(MAKE) syntax
$(MAKE) remove
$(MAKE) build
$(MAKE) install
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The API token can be set via ansible parameter or ENV variable.

| **Ansible Parameter** | **ENV Variable** |
| --- | --- |
| `api_token` | `HETZNER_DNS_TOKEN` |
| `api_token` (_alias `access_token`_) | `HETZNER_DNS_TOKEN` |

All modules supports and respects `check_mode: yes`.

Expand All @@ -26,6 +26,7 @@ All modules supports and respects `check_mode: yes`.
| parameters | default | comment |
| --- | --- | --- |
| `name` | - | name of a record |
| `purge` | `true` | Wether a existing value should be replaced (`true`) or appended (`false`) to achive multiple DNS records for one name. Alias parameter are: `replace`, `overwrite`, `solo`.
| `value` | - | required with `state: present` |
| `type` | - | type of record. valid records: "A" "AAAA" "NS" "MX" "CNAME" "RP" "TXT" "SOA" "HINFO" "SRV" "DANE" "TLSA" "DS" "CAA" |
| `ttl` | `300` | TTL of a record |
Expand Down Expand Up @@ -69,6 +70,50 @@ All modules supports and respects `check_mode: yes`.
type: CNAME
state: absent
register: RECORD

- name: add single A record
markuman.hetzner_dns.record:
zone_name: osuv.de
name: dns
value: 1.1.1.1
type: A
ttl: 60

- name: append A record
markuman.hetzner_dns.record:
zone_name: osuv.de
name: dns
value: 8.8.8.8
type: A
purge: no
ttl: 60

- name: append A record
markuman.hetzner_dns.record:
zone_name: osuv.de
name: dns
value: 8.8.4.4
type: A
purge: no
ttl: 60

- name: delete single A record
markuman.hetzner_dns.record:
zone_name: osuv.de
name: dns
value: 8.8.8.8
type: A
state: absent

- name: >
delete all A records
when no `value` is given
markuman.hetzner_dns.record:
zone_name: osuv.de
name: dns
type: A
state: absent

```

#### mx priority
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace: "markuman"
description: hetzner dns collection
name: "hetzner_dns"
version: "1.3.0"
version: "1.4.0"
readme: "README.md"
authors:
- "Markus Bergholz <[email protected]>"
Expand Down
47 changes: 33 additions & 14 deletions plugins/modules/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def main():
value = dict(type='str'),
ttl = dict(default=300, type='int'),
type = dict(required=True, type='str', choices=["A","AAAA","NS","MX","CNAME","RP","TXT","SOA","HINFO","SRV","DANE","TLSA","DS","CAA"]),
state = dict(type='str', default='present', choices=['present', 'absent'])
state = dict(type='str', default='present', choices=['present', 'absent']),
purge = dict(required=False, type='bool', default=True, aliases=['replace', 'overwrite', 'solo'])
)

module = AnsibleModule(
Expand All @@ -77,6 +78,7 @@ def main():
zone_id = module.params.get("zone_id")
zone_name = module.params.get("zone_name")
state = module.params.get("state")
purge = module.params.get("purge")

if zone_id is None:
zones = dns.get_zone_info()
Expand All @@ -97,34 +99,47 @@ def main():
'type': future_record.get('type')
}

if module.params.get("value"):
find_record['value'] = module.params.get("value")

records = dns.get_record_info(zone_id)

record_changed = False
record_exists = False
change = False
past_record = None
past_record = []
record_ids = []
for record in records.json()['records']:

if not record.get('ttl'):
record['ttl'] = 300

if all(item in record.items() for item in find_record.items()):
record_exists = True
record_id = record.get('id')
past_record = record
record_ids.append(record.get('id'))
past_record.append(record)

if not all(item in record.items() for item in future_record.items()):
record_changed = True
else:
this_record = { 'record': record }
break

if record_changed and not purge:
record_exists = False
record_changed = False

if state == 'present':
if not record_exists:
record_id = None
this_record = { 'record': future_record }
change = True

if not module.check_mode:
r = dns.create_record(future_record)
record_id = r.json()['record']['id']
this_record = r.json()
r = dns.create_record(future_record)
record_id = r.json()['record']['id']
this_record = r.json()

elif record_changed:
change = True
this_record = { 'record': future_record }
Expand All @@ -137,18 +152,22 @@ def main():
if record_exists:
change = True
if not module.check_mode:
r = dns.delete_record(record_id)
if not module.params.get("value"):
# when value was not defined, delete all records of this type
for id in record_ids:
r = dns.delete_record(id)
else:
r = dns.delete_record(record_id)

else:
change = False
this_record = { 'record': None }
record_id = None

if past_record is None:
past_record = {}
else:
past_record.pop('id')
past_record.pop('created')
past_record.pop('modified')
for idx in range(len(past_record)):
past_record[idx].pop('id')
past_record[idx].pop('created')
past_record[idx].pop('modified')

diff = dict(
before=yaml.safe_dump(past_record),
Expand Down
112 changes: 108 additions & 4 deletions tests/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
assert:
that:
- RECORD is not changed
- RECORD.past_record.ttl == 300
- RECORD.past_record[0].ttl == 300
- RECORD.record_info.record.ttl == 300

- name: modify record change in check_mode
Expand All @@ -102,7 +102,7 @@
that:
- RECORD is changed
- RECORD.record_info.record.ttl == 60
- RECORD.past_record.ttl == 300
- RECORD.past_record[0].ttl == 300

- name: modify record change
markuman.hetzner_dns.record:
Expand All @@ -118,7 +118,7 @@
that:
- RECORD is changed
- RECORD.record_info.record.ttl == 60
- RECORD.past_record.ttl == 300
- RECORD.past_record[0].ttl == 300

- name: del record
markuman.hetzner_dns.record:
Expand Down Expand Up @@ -146,10 +146,114 @@
that:
- RECORD is not changed

- name: add single A record
markuman.hetzner_dns.record:
zone_id: "{{ ZONES.zone_id }}"
name: hetzner_dns_ansible_collection
value: 1.1.1.1
type: A
ttl: 60
register: RECORD

- name: append A record
markuman.hetzner_dns.record:
zone_id: "{{ ZONES.zone_id }}"
name: hetzner_dns_ansible_collection
value: 8.8.8.8
type: A
purge: no
ttl: 60
register: RECORD

- name: append A record
markuman.hetzner_dns.record:
zone_id: "{{ ZONES.zone_id }}"
name: hetzner_dns_ansible_collection
value: 8.8.4.4
type: A
purge: no
ttl: 60
register: RECORD

- name: fetch record info
markuman.hetzner_dns.record_info:
filter:
- name: hetzner_dns_ansible_collection
type: A
zone_name: "{{ TEST_ZONE }}"
register: RECORD

- name: assert 3 A recordds
assert:
that:
- RECORD.record_info | count == 3

- name: fetch one record
markuman.hetzner_dns.record_info:
filter:
- name: hetzner_dns_ansible_collection
type: A
value: 8.8.8.8
zone_name: "{{ TEST_ZONE }}"
register: RECORD

- name: assert 1 A recordds
assert:
that:
- RECORD.record_info | count == 1

- name: delete single A record
markuman.hetzner_dns.record:
zone_name: "{{ TEST_ZONE }}"
name: hetzner_dns_ansible_collection
value: 8.8.8.8
type: A
state: absent

- name: fetch record info
markuman.hetzner_dns.record_info:
filter:
- name: hetzner_dns_ansible_collection
type: A
zone_name: "{{ TEST_ZONE }}"
register: RECORD

- name: assert 2 A recordds
assert:
that:
- RECORD.record_info | count == 2

- name: delete all A records
markuman.hetzner_dns.record:
zone_name: "{{ TEST_ZONE }}"
name: hetzner_dns_ansible_collection
type: A
state: absent

- name: fetch record info
markuman.hetzner_dns.record_info:
filter:
- name: hetzner_dns_ansible_collection
type: A
zone_name: "{{ TEST_ZONE }}"
register: RECORD

- name: assert 2 A recordds
assert:
that:
- RECORD.record_info | count == 0

always:
- name: always del record
- name: always del CNAME record
markuman.hetzner_dns.record:
zone_name: "{{ TEST_ZONE }}"
name: hetzner_dns_ansible_collection
type: CNAME
state: absent

- name: always del A record
markuman.hetzner_dns.record:
zone_name: "{{ TEST_ZONE }}"
name: hetzner_dns_ansible_collection
type: A
state: absent

0 comments on commit 6180bf3

Please sign in to comment.