Enable update for recordset and add tests for dns and recordset module

Update for recordset wasn't working properly and there are no tests
for dns and recordset modules, minor fix in dns_zone

Change-Id: I7f78f6038dfb858e795b1954eae11cff47f697ad
This commit is contained in:
Polina Gubina 2020-12-10 21:38:49 +03:00 committed by Shnaidman Sagi (Sergey)
parent c532560d1b
commit 87c305907e
5 changed files with 167 additions and 70 deletions

View File

@ -0,0 +1,4 @@
dns_zone_name: test.dns.zone.
recordset_name: testrecordset.test.dns.zone.
records: ['10.0.0.0']
updated_records: ['10.1.1.1']

View File

@ -0,0 +1,79 @@
---
- name: Create dns zone
openstack.cloud.dns_zone:
cloud: "{{ cloud }}"
name: "{{ dns_zone_name }}"
zone_type: "primary"
email: test@example.net
register: dns_zone
- debug: var=dns_zone
- name: Update dns zone
openstack.cloud.dns_zone:
cloud: "{{ cloud }}"
name: "{{ dns_zone.zone.name }}"
description: "New descirption"
register: updated_dns_zone
- debug: var=updated_dns_zone
- name: Create a recordset
openstack.cloud.recordset:
cloud: "{{ cloud }}"
zone: "{{ updated_dns_zone.zone.name }}"
name: "{{ recordset_name }}"
recordset_type: "a"
records: "{{ records }}"
register: recordset
- name: Verify recordset info
assert:
that:
- recordset["recordset"].name == recordset_name
- recordset["recordset"].zone_name == dns_zone.zone.name
- recordset["recordset"].records == records
- name: Update a recordset
openstack.cloud.recordset:
cloud: "{{ cloud }}"
zone: "{{ updated_dns_zone.zone.name }}"
name: "{{ recordset_name }}"
recordset_type: "a"
records: "{{ updated_records }}"
description: "new test recordset"
register: updated_recordset
- name: Verify recordset info
assert:
that:
- updated_recordset["recordset"].zone_name == dns_zone.zone.name
- updated_recordset["recordset"].name == recordset_name
- updated_recordset["recordset"].records == updated_records
- name: Delete recordset
openstack.cloud.recordset:
cloud: "{{ cloud }}"
zone: "{{ updated_dns_zone.zone.name }}"
name: "{{ recordset.recordset.name }}"
state: absent
register: deleted_recordset
- name: Verify recordset deletion
assert:
that:
- deleted_recordset is successful
- deleted_recordset is changed
- name: Delete dns zone
openstack.cloud.dns_zone:
cloud: "{{ cloud }}"
name: "{{ updated_dns_zone.zone.name }}"
state: absent
register: deleted_dns_zone
- name: Verify dns zone
assert:
that:
- deleted_dns_zone is successful
- deleted_dns_zone is changed

View File

@ -7,6 +7,9 @@
- { role: auth, tags: auth }
- { role: client_config, tags: client_config }
- { role: group, tags: group }
- role: dns
tags: dns
when: sdk_version is version(0.28, '>=')
- { role: image, tags: image }
- { role: keypair, tags: keypair }
- { role: keystone_domain, tags: keystone_domain }

View File

@ -173,12 +173,24 @@ class DnsZoneModule(OpenStackModule):
zone = self.conn.get_zone(name)
if state == 'present':
zone_type = self.params['zone_type']
email = self.params['email']
description = self.params['description']
ttl = self.params['ttl']
masters = self.params['masters']
kwargs = {}
if email:
kwargs['email'] = email
if description:
kwargs['description'] = description
if ttl:
kwargs['ttl'] = ttl
if masters:
kwargs['masters'] = masters
if self.ansible.check_mode:
self.exit_json(changed=self._system_state_change(state, email,
description, ttl,
@ -186,8 +198,7 @@ class DnsZoneModule(OpenStackModule):
if zone is None:
zone = self.conn.create_zone(
name=name, zone_type=zone_type, email=email,
description=description, ttl=ttl, masters=masters)
name=name, zone_type=zone_type, **kwargs)
changed = True
else:
if masters is None:
@ -199,9 +210,7 @@ class DnsZoneModule(OpenStackModule):
masters, pre_update_zone)
if changed:
zone = self.conn.update_zone(
name, email=email,
description=description,
ttl=ttl, masters=masters)
name, **kwargs)
if wait:
self._wait(timeout, zone, state)

View File

@ -19,13 +19,14 @@ options:
type: str
name:
description:
- Name of the recordset
- Name of the recordset. It must be ended with name of dns zone.
required: true
type: str
recordset_type:
description:
- Recordset type
- Required when I(state=present).
choices: ['a', 'aaaa', 'mx', 'cname', 'txt', 'ns', 'srv', 'ptr', 'caa']
type: str
records:
description:
@ -61,8 +62,8 @@ EXAMPLES = '''
cloud: mycloud
state: present
zone: example.net.
name: www
recordset_type: primary
name: www.example.net.
recordset_type: "a"
records: ['10.1.1.1']
description: test recordset
ttl: 3600
@ -72,7 +73,7 @@ EXAMPLES = '''
cloud: mycloud
state: present
zone: example.net.
name: www
name: www.example.net.
ttl: 7200
# Delete recordset named "www.example.net."
@ -80,7 +81,7 @@ EXAMPLES = '''
cloud: mycloud
state: absent
zone: example.net.
name: www
name: www.example.net.
'''
RETURN = '''
@ -125,7 +126,7 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (
openstack_cloud_from_module)
def _system_state_change(state, records, description, ttl, zone, recordset):
def _system_state_change(state, records, description, ttl, recordset):
if state == 'present':
if recordset is None:
return True
@ -144,10 +145,10 @@ def main():
argument_spec = openstack_full_argument_spec(
zone=dict(required=True),
name=dict(required=True),
recordset_type=dict(required=False),
recordset_type=dict(required=False, choices=['a', 'aaaa', 'mx', 'cname', 'txt', 'ns', 'srv', 'ptr', 'caa']),
records=dict(required=False, type='list', elements='str'),
description=dict(required=False, default=None),
ttl=dict(required=False, default=None, type='int'),
ttl=dict(required=False, type='int'),
state=dict(default='present', choices=['absent', 'present']),
)
@ -159,76 +160,77 @@ def main():
supports_check_mode=True,
**module_kwargs)
module.module_min_sdk_version = '0.28.0'
zone = module.params.get('zone')
name = module.params.get('name')
state = module.params.get('state')
sdk, cloud = openstack_cloud_from_module(module)
try:
recordset_type = module.params.get('recordset_type')
recordset_filter = {'type': recordset_type}
recordsets = cloud.search_recordsets(zone, name_or_id=name)
recordsets = cloud.search_recordsets(zone, name_or_id=name, filters=recordset_filter)
if recordsets:
recordset = recordsets[0]
try:
recordset_id = recordset['id']
except KeyError as e:
module.fail_json(msg=str(e))
else:
# recordsets is filtered by type and should never be more than 1 return
recordset = None
if len(recordsets) == 1:
recordset = recordsets[0]
try:
recordset_id = recordset['id']
except KeyError as e:
module.fail_json(msg=str(e))
if state == 'present':
recordset_type = module.params.get('recordset_type').upper()
records = module.params.get('records')
description = module.params.get('description')
ttl = module.params.get('ttl')
kwargs = {}
if description:
kwargs['description'] = description
kwargs['records'] = records
if module.check_mode:
module.exit_json(changed=_system_state_change(state,
records, description,
ttl, recordset))
if recordset is None:
if ttl:
kwargs['ttl'] = ttl
else:
kwargs['ttl'] = 300
recordset = cloud.create_recordset(
zone=zone, name=name, recordset_type=recordset_type,
**kwargs)
changed = True
else:
# recordsets is filtered by type and should never be more than 1 return
recordset = None
if state == 'present':
records = module.params.get('records')
description = module.params.get('description')
ttl = module.params.get('ttl')
if ttl:
kwargs['ttl'] = ttl
if module.check_mode:
module.exit_json(changed=_system_state_change(state,
records, description,
ttl, zone,
recordset))
pre_update_recordset = recordset
changed = _system_state_change(state, records,
description, ttl,
pre_update_recordset)
if changed:
recordset = cloud.update_recordset(
zone=zone, name_or_id=recordset_id, **kwargs)
if recordset is None:
recordset = cloud.create_recordset(
zone=zone, name=name, recordset_type=recordset_type,
records=records, description=description, ttl=ttl)
changed = True
else:
if records is None:
records = []
module.exit_json(changed=changed, recordset=recordset)
pre_update_recordset = recordset
changed = _system_state_change(state, records,
description, ttl,
zone, pre_update_recordset)
if changed:
zone = cloud.update_recordset(
zone, recordset_id,
records=records,
description=description,
ttl=ttl)
elif state == 'absent':
if module.check_mode:
module.exit_json(changed=_system_state_change(state,
None, None,
None, recordset))
module.exit_json(changed=changed, recordset=recordset)
elif state == 'absent':
if module.check_mode:
module.exit_json(changed=_system_state_change(state,
None, None,
None,
None, recordset))
if recordset is None:
changed = False
else:
cloud.delete_recordset(zone, recordset_id)
changed = True
module.exit_json(changed=changed)
except sdk.exceptions.OpenStackCloudException as e:
module.fail_json(msg=str(e))
if recordset is None:
changed = False
else:
cloud.delete_recordset(zone, recordset_id)
changed = True
module.exit_json(changed=changed)
if __name__ == '__main__':