2020-05-12 10:20:09 -05:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Copyright (c) 2016 Hewlett-Packard Enterprise
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: recordset
|
|
|
|
short_description: Manage OpenStack DNS recordsets
|
2020-06-17 12:35:09 -05:00
|
|
|
author: OpenStack Ansible SIG
|
2020-05-12 10:20:09 -05:00
|
|
|
description:
|
|
|
|
- Manage OpenStack DNS recordsets. Recordsets can be created, deleted or
|
|
|
|
updated. Only the I(records), I(description), and I(ttl) values
|
|
|
|
can be updated.
|
|
|
|
options:
|
|
|
|
zone:
|
|
|
|
description:
|
|
|
|
- Zone managing the recordset
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
name:
|
|
|
|
description:
|
2020-12-10 21:38:49 +03:00
|
|
|
- Name of the recordset. It must be ended with name of dns zone.
|
2020-05-12 10:20:09 -05:00
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
recordset_type:
|
|
|
|
description:
|
|
|
|
- Recordset type
|
|
|
|
- Required when I(state=present).
|
2020-12-10 21:38:49 +03:00
|
|
|
choices: ['a', 'aaaa', 'mx', 'cname', 'txt', 'ns', 'srv', 'ptr', 'caa']
|
2020-05-12 10:20:09 -05:00
|
|
|
type: str
|
|
|
|
records:
|
|
|
|
description:
|
|
|
|
- List of recordset definitions.
|
|
|
|
- Required when I(state=present).
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Description of the recordset
|
|
|
|
type: str
|
|
|
|
ttl:
|
|
|
|
description:
|
|
|
|
- TTL (Time To Live) value in seconds
|
|
|
|
type: int
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Should the resource be present or absent.
|
|
|
|
choices: [present, absent]
|
|
|
|
default: present
|
|
|
|
type: str
|
|
|
|
requirements:
|
|
|
|
- "python >= 3.6"
|
|
|
|
- "openstacksdk"
|
|
|
|
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- openstack.cloud.openstack
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a recordset named "www.example.net."
|
|
|
|
- openstack.cloud.recordset:
|
|
|
|
cloud: mycloud
|
|
|
|
state: present
|
|
|
|
zone: example.net.
|
2020-12-10 21:38:49 +03:00
|
|
|
name: www.example.net.
|
|
|
|
recordset_type: "a"
|
2020-05-12 10:20:09 -05:00
|
|
|
records: ['10.1.1.1']
|
|
|
|
description: test recordset
|
|
|
|
ttl: 3600
|
|
|
|
|
|
|
|
# Update the TTL on existing "www.example.net." recordset
|
|
|
|
- openstack.cloud.recordset:
|
|
|
|
cloud: mycloud
|
|
|
|
state: present
|
|
|
|
zone: example.net.
|
2020-12-10 21:38:49 +03:00
|
|
|
name: www.example.net.
|
2021-01-08 23:36:53 +01:00
|
|
|
recordset_type: "a"
|
|
|
|
records: ['10.1.1.1']
|
2020-05-12 10:20:09 -05:00
|
|
|
ttl: 7200
|
|
|
|
|
|
|
|
# Delete recordset named "www.example.net."
|
|
|
|
- openstack.cloud.recordset:
|
|
|
|
cloud: mycloud
|
|
|
|
state: absent
|
|
|
|
zone: example.net.
|
2020-12-10 21:38:49 +03:00
|
|
|
name: www.example.net.
|
2020-05-12 10:20:09 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
recordset:
|
|
|
|
description: Dictionary describing the recordset.
|
|
|
|
returned: On success when I(state) is 'present'.
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Unique recordset ID
|
|
|
|
type: str
|
|
|
|
sample: "c1c530a3-3619-46f3-b0f6-236927b2618c"
|
|
|
|
name:
|
|
|
|
description: Recordset name
|
|
|
|
type: str
|
|
|
|
sample: "www.example.net."
|
|
|
|
zone_id:
|
|
|
|
description: Zone id
|
|
|
|
type: str
|
|
|
|
sample: 9508e177-41d8-434e-962c-6fe6ca880af7
|
|
|
|
type:
|
|
|
|
description: Recordset type
|
|
|
|
type: str
|
|
|
|
sample: "A"
|
|
|
|
description:
|
|
|
|
description: Recordset description
|
|
|
|
type: str
|
|
|
|
sample: "Test description"
|
|
|
|
ttl:
|
|
|
|
description: Zone TTL value
|
|
|
|
type: int
|
|
|
|
sample: 3600
|
|
|
|
records:
|
|
|
|
description: Recordset records
|
|
|
|
type: list
|
|
|
|
sample: ['10.0.0.1']
|
|
|
|
'''
|
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
2020-05-12 10:20:09 -05:00
|
|
|
|
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
class DnsRecordsetModule(OpenStackModule):
|
|
|
|
argument_spec = dict(
|
2020-05-12 10:20:09 -05:00
|
|
|
zone=dict(required=True),
|
|
|
|
name=dict(required=True),
|
2020-12-10 21:38:49 +03:00
|
|
|
recordset_type=dict(required=False, choices=['a', 'aaaa', 'mx', 'cname', 'txt', 'ns', 'srv', 'ptr', 'caa']),
|
2020-05-12 10:20:09 -05:00
|
|
|
records=dict(required=False, type='list', elements='str'),
|
|
|
|
description=dict(required=False, default=None),
|
2020-12-10 21:38:49 +03:00
|
|
|
ttl=dict(required=False, type='int'),
|
2020-05-12 10:20:09 -05:00
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
|
|
|
)
|
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
module_kwargs = dict(
|
|
|
|
required_if=[
|
|
|
|
('state', 'present',
|
|
|
|
['recordset_type', 'records'])],
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
2020-05-12 10:20:09 -05:00
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
module_min_sdk_version = '0.28.0'
|
|
|
|
|
|
|
|
def _system_state_change(self, state, records, description, ttl, recordset):
|
|
|
|
if state == 'present':
|
|
|
|
if recordset is None:
|
|
|
|
return True
|
|
|
|
if records is not None and recordset['records'] != records:
|
|
|
|
return True
|
|
|
|
if description is not None and recordset['description'] != description:
|
|
|
|
return True
|
|
|
|
if ttl is not None and recordset['ttl'] != ttl:
|
|
|
|
return True
|
|
|
|
if state == 'absent' and recordset:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
zone = self.params.get('zone')
|
|
|
|
name = self.params.get('name')
|
|
|
|
state = self.params.get('state')
|
|
|
|
|
|
|
|
recordsets = self.conn.search_recordsets(zone, name_or_id=name)
|
|
|
|
|
|
|
|
if recordsets:
|
|
|
|
recordset = recordsets[0]
|
|
|
|
try:
|
|
|
|
recordset_id = recordset['id']
|
|
|
|
except KeyError as e:
|
|
|
|
self.fail_json(msg=str(e))
|
2020-12-10 21:38:49 +03:00
|
|
|
else:
|
2021-05-20 14:42:40 +02:00
|
|
|
# recordsets is filtered by type and should never be more than 1 return
|
|
|
|
recordset = None
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
recordset_type = self.params.get('recordset_type').upper()
|
|
|
|
records = self.params.get('records')
|
|
|
|
description = self.params.get('description')
|
|
|
|
ttl = self.params.get('ttl')
|
|
|
|
|
|
|
|
kwargs = {}
|
|
|
|
if description:
|
|
|
|
kwargs['description'] = description
|
|
|
|
kwargs['records'] = records
|
|
|
|
|
|
|
|
if self.ansible.check_mode:
|
|
|
|
self.exit_json(
|
|
|
|
changed=self._system_state_change(
|
|
|
|
state, records, description, ttl, recordset))
|
|
|
|
|
|
|
|
if recordset is None:
|
|
|
|
if ttl:
|
|
|
|
kwargs['ttl'] = ttl
|
|
|
|
else:
|
|
|
|
kwargs['ttl'] = 300
|
|
|
|
|
|
|
|
recordset = self.conn.create_recordset(
|
|
|
|
zone=zone, name=name, recordset_type=recordset_type,
|
|
|
|
**kwargs)
|
|
|
|
changed = True
|
|
|
|
else:
|
2020-12-10 21:38:49 +03:00
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
if ttl:
|
|
|
|
kwargs['ttl'] = ttl
|
2020-12-10 21:38:49 +03:00
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
pre_update_recordset = recordset
|
|
|
|
changed = self._system_state_change(
|
|
|
|
state, records, description, ttl, pre_update_recordset)
|
|
|
|
if changed:
|
|
|
|
recordset = self.conn.update_recordset(
|
|
|
|
zone=zone, name_or_id=recordset_id, **kwargs)
|
2020-12-10 21:38:49 +03:00
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
self.exit_json(changed=changed, recordset=recordset)
|
2020-12-10 21:38:49 +03:00
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
elif state == 'absent':
|
|
|
|
if self.ansible.check_mode:
|
|
|
|
self.exit_json(changed=self._system_state_change(
|
|
|
|
state, None, None, None, recordset))
|
2020-12-10 21:38:49 +03:00
|
|
|
|
2021-05-20 14:42:40 +02:00
|
|
|
if recordset is None:
|
|
|
|
changed = False
|
|
|
|
else:
|
|
|
|
self.conn.delete_recordset(zone, recordset_id)
|
|
|
|
changed = True
|
|
|
|
self.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = DnsRecordsetModule()
|
|
|
|
module()
|
2020-05-12 10:20:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|