Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Metadata support to azure_rm_dnsrecordset.py #589

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 66 additions & 9 deletions plugins/modules/azure_rm_dnsrecordset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
description:
- Name of resource group.
required: true
type: str
zone_name:
description:
- Name of the existing DNS zone in which to manage the record set.
required: true
type: str
relative_name:
description:
- Relative name of the record set.
required: true
type: str
record_type:
description:
- The type of record set to create or delete.
Expand All @@ -47,27 +50,41 @@
- CAA
- SOA
required: true
type: str
record_mode:
description:
- Whether existing record values not sent to the module should be purged.
default: purge
type: str
choices:
- append
- purge
state:
description:
- Assert the state of the record set. Use C(present) to create or update and C(absent) to delete.
default: present
type: str
choices:
- absent
- present
metadata:
description:
- The metadata tags for the record sets.
type: dict
append_metadata:
description: Whether metadata should be appended or not
type: bool
default: True
time_to_live:
description:
- Time to live of the record set in seconds.
default: 3600
type: int
records:
description:
- List of records to be created depending on the type of record (set).
type: list
elements: dict
suboptions:
preference:
description:
Expand Down Expand Up @@ -115,6 +132,17 @@
zone_name: testing.com
state: absent

- name: create A record set with metadata information
azure_rm_dnsrecordset:
resource_group: myResourceGroup
relative_name: www
zone_name: zone1.com
record_type: A
records:
- entry: 192.168.100.104
metadata:
key1: "value1"

- name: create multiple "A" record sets with multiple records
azure_rm_dnsrecordset:
resource_group: myResourceGroup
Expand Down Expand Up @@ -183,43 +211,43 @@
fqdn:
description:
- Fully qualified domain name of the record set.
return: always
returned: always
type: str
sample: www.b57dc95985712e4523282.com
etag:
description:
- The etag of the record set.
return: always
returned: always
type: str
sample: 692c3e92-a618-46fc-aecd-8f888807cd6c
provisioning_state:
description:
- The DNS record set state.
return: always
returned: always
type: str
sample: Succeeded
target_resource:
description:
- The target resource of the record set.
return: always
returned: always
type: dict
sample: {}
ttl:
description:
- The TTL(time-to-live) of the records in the records set.
return: always
returned: always
type: int
sample: 3600
type:
description:
- The type of DNS record in this record set.
return: always
returned: always
type: str
sample: A
arecords:
description:
- A list of records in the record set.
return: always
returned: always
type: list
sample: [
{
Expand All @@ -236,6 +264,7 @@

import inspect
import sys
import copy

from ansible.module_utils.basic import _load_params
from ansible.module_utils.six import iteritems
Expand Down Expand Up @@ -324,7 +353,9 @@ def __init__(self):
record_mode=dict(choices=['append', 'purge'], default='purge'),
state=dict(choices=['present', 'absent'], default='present', type='str'),
time_to_live=dict(type='int', default=3600),
records=dict(type='list', elements='dict')
records=dict(type='list', elements='dict'),
metadata=dict(type='dict'),
append_metadata=dict(type='bool', default=True)
)

required_if = [
Expand Down Expand Up @@ -352,6 +383,7 @@ def __init__(self):
self.state = None
self.time_to_live = None
self.records = None
self.metadata = None

# rerun validation and actually run the module this time
super(AzureRMRecordSet, self).__init__(self.module_arg_spec, required_if=required_if, supports_check_mode=True)
Expand Down Expand Up @@ -392,7 +424,11 @@ def exec_module(self, **kwargs):
# also check top-level recordset properties
changed |= record_set.ttl != self.time_to_live

# FUTURE: add metadata/tag check on recordset
old_metadata = self.results['state']['metadata'] if 'metadata' in self.results['state'] else dict()
update_metadata, self.results['state']['metadata'] = self.update_metadata(old_metadata)
if update_metadata:
changed = True
self.metadata = self.results['state']['metadata']

self.results['changed'] |= changed

Expand All @@ -412,6 +448,8 @@ def exec_module(self, **kwargs):
record_set_args[record_type_metadata['attrname']] = self.input_sdk_records if record_type_metadata['is_list'] else self.input_sdk_records[0]

record_set = self.dns_models.RecordSet(**record_set_args)
if self.metadata:
record_set.metadata = self.metadata

self.results['state'] = self.create_or_update(record_set)

Expand Down Expand Up @@ -472,6 +510,25 @@ def recordset_to_dict(self, recordset):
result['type'] = result['type'].strip('Microsoft.Network/dnszones/')
return result

def update_metadata(self, metadata):
metadata = metadata or dict()
new_metadata = copy.copy(metadata) if isinstance(metadata, dict) else dict()
param_metadata = self.metadata if isinstance(self.metadata, dict) else dict()
append_metadata = self.append_metadata if self.metadata is not None else True
changed = False
# check add or update metadata
for key, value in param_metadata.items():
if not new_metadata.get(key) or new_metadata[key] != value:
changed = True
new_metadata[key] = value
# check remove
if not append_metadata:
for key, value in metadata.items():
if not param_metadata.get(key):
new_metadata.pop(key)
changed = True
return changed, new_metadata


def main():
AzureRMRecordSet()
Expand Down
14 changes: 13 additions & 1 deletion plugins/modules/azure_rm_dnsrecordset_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
relative_name:
description:
- Only show results for a Record Set.
type: str
resource_group:
description:
- Limit results by resource group. Required when filtering by name or type.
type: str
zone_name:
description:
- Limit results by zones. Required when filtering by name or type.
type: str
record_type:
description:
- Limit record sets by record type.
type: str
top:
description:
- Limit the maximum number of record sets to return.
Expand Down Expand Up @@ -96,24 +100,29 @@
id:
description:
- ID of the dns recordset.
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/dnszones/newzone.
com/A/servera"
relative_name:
description:
- Name of the dns recordset.
type: str
sample: servera
record_type:
description:
- The type of the record set.
- Can be C(A), C(AAAA), C(CNAME), C(MX), C(NS), C(SRV), C(TXT), C(PTR).
type: str
sample: A
time_to_live:
description:
- Time to live of the record set in seconds.
type: int
sample: 12900
records:
description:
- List of records depending on the type of recordset.
type: dict
sample: [
{
"ipv4Address": "10.4.5.7"
Expand All @@ -125,10 +134,12 @@
provisioning_state:
description:
- Provision state of the resource.
type: str
sample: Successed
fqdn:
description:
- Fully qualified domain name of the record set.
type: str
sample: www.newzone.com
'''

Expand Down Expand Up @@ -281,7 +292,8 @@ def record_to_dict(self, record):
records=[x.as_dict() for x in records],
time_to_live=record.ttl,
fqdn=record.fqdn,
provisioning_state=record.provisioning_state
provisioning_state=record.provisioning_state,
metadata=record.metadata
)


Expand Down
6 changes: 0 additions & 6 deletions tests/sanity/ignore-2.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ plugins/modules/azure_rm_deployment.py validate-modules:parameter-type-not-in-do
plugins/modules/azure_rm_deployment.py validate-modules:return-syntax-error
plugins/modules/azure_rm_deployment_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_deployment_info.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnsrecordset.py validate-modules:doc-missing-type
plugins/modules/azure_rm_dnsrecordset.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset.py validate-modules:doc-elements-mismatch
plugins/modules/azure_rm_dnsrecordset.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_dnsrecordset.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnsrecordset_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset_info.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnszone.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnszone.py validate-modules:doc-elements-mismatch
plugins/modules/azure_rm_dnszone_info.py validate-modules:doc-type-does-not-match-spec
Expand Down
6 changes: 0 additions & 6 deletions tests/sanity/ignore-2.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ plugins/modules/azure_rm_deployment.py validate-modules:parameter-type-not-in-do
plugins/modules/azure_rm_deployment.py validate-modules:return-syntax-error
plugins/modules/azure_rm_deployment_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_deployment_info.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnsrecordset.py validate-modules:doc-missing-type
plugins/modules/azure_rm_dnsrecordset.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset.py validate-modules:doc-elements-mismatch
plugins/modules/azure_rm_dnsrecordset.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_dnsrecordset.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnsrecordset_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset_info.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnszone.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnszone.py validate-modules:doc-elements-mismatch
plugins/modules/azure_rm_dnszone_info.py validate-modules:doc-type-does-not-match-spec
Expand Down
6 changes: 0 additions & 6 deletions tests/sanity/ignore-2.12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ plugins/modules/azure_rm_deployment.py validate-modules:parameter-type-not-in-do
plugins/modules/azure_rm_deployment.py validate-modules:return-syntax-error
plugins/modules/azure_rm_deployment_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_deployment_info.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnsrecordset.py validate-modules:doc-missing-type
plugins/modules/azure_rm_dnsrecordset.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset.py validate-modules:doc-elements-mismatch
plugins/modules/azure_rm_dnsrecordset.py validate-modules:invalid-ansiblemodule-schema
plugins/modules/azure_rm_dnsrecordset.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnsrecordset_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset_info.py validate-modules:return-syntax-error
plugins/modules/azure_rm_dnszone.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnszone.py validate-modules:doc-elements-mismatch
plugins/modules/azure_rm_dnszone_info.py validate-modules:doc-type-does-not-match-spec
Expand Down
3 changes: 0 additions & 3 deletions tests/sanity/ignore-2.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ plugins/modules/azure_rm_containerregistrywebhook_info.py validate-modules:undoc
plugins/modules/azure_rm_datalakestore.py validate-modules:nonexistent-parameter-documented
plugins/modules/azure_rm_deployment.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_deployment_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset.py validate-modules:doc-missing-type
plugins/modules/azure_rm_dnsrecordset.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnsrecordset_info.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnszone.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_dnszone_info.py validate-modules:doc-type-does-not-match-spec
plugins/modules/azure_rm_dnszone_info.py validate-modules:parameter-type-not-in-doc
Expand Down