new facts module for dns zone (#28803)
* new facts module for dns zone integration tests and new module for dns zone facts * use vairable for domain name * add nondeterministic piece to domain name * fix azure_rm_dnszone_facts examples
This commit is contained in:
parent
950165ba70
commit
3045c58c83
4 changed files with 220 additions and 0 deletions
181
lib/ansible/modules/cloud/azure/azure_rm_dnszone_facts.py
Normal file
181
lib/ansible/modules/cloud/azure/azure_rm_dnszone_facts.py
Normal file
|
@ -0,0 +1,181 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright (c) 2017 Obezimnaka Boms, <t-ozboms@microsoft.com>
|
||||
#
|
||||
#
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: azure_rm_dnszone_facts
|
||||
|
||||
version_added: "2.4"
|
||||
|
||||
short_description: Get DNS zone facts.
|
||||
|
||||
description:
|
||||
- Get facts for a specific DNS zone or all DNS zones within a resource group.
|
||||
|
||||
options:
|
||||
resource_group:
|
||||
description:
|
||||
- Limit results by resource group. Required when filtering by name.
|
||||
name:
|
||||
description:
|
||||
- Only show results for a specific zone.
|
||||
tags:
|
||||
description:
|
||||
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
|
||||
|
||||
extends_documentation_fragment:
|
||||
- azure
|
||||
- azure_tags
|
||||
|
||||
author:
|
||||
- "Obezimnaka Boms @ozboms"
|
||||
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Get facts for one zone
|
||||
azure_rm_dnszone_facts:
|
||||
resource_group: Testing
|
||||
name: foobar22
|
||||
|
||||
- name: Get facts for all zones in a resource group
|
||||
azure_rm_dnszone_facts:
|
||||
resource_group: Testing
|
||||
|
||||
- name: Get facts by tags
|
||||
azure_rm_dnszone_facts:
|
||||
tags:
|
||||
- testing
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
azure_dnszones:
|
||||
description: List of zone dicts.
|
||||
returned: always
|
||||
type: list
|
||||
example: [{
|
||||
"etag": "00000002-0000-0000-0dcb-df5776efd201",
|
||||
"location": "global",
|
||||
"properties": {
|
||||
"maxNumberOfRecordSets": 5000,
|
||||
"numberOfRecordSets": 15
|
||||
},
|
||||
"tags": {}
|
||||
}]
|
||||
'''
|
||||
|
||||
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
|
||||
|
||||
try:
|
||||
from msrestazure.azure_exceptions import CloudError
|
||||
from azure.common import AzureMissingResourceHttpError, AzureHttpError
|
||||
except:
|
||||
# This is handled in azure_rm_common
|
||||
pass
|
||||
|
||||
AZURE_OBJECT_CLASS = 'DnsZone'
|
||||
|
||||
|
||||
class AzureRMDNSZoneFacts(AzureRMModuleBase):
|
||||
|
||||
def __init__(self):
|
||||
|
||||
# define user inputs into argument
|
||||
self.module_arg_spec = dict(
|
||||
name=dict(type='str'),
|
||||
resource_group=dict(type='str'),
|
||||
tags=dict(type='list')
|
||||
)
|
||||
|
||||
# store the results of the module operation
|
||||
self.results = dict(
|
||||
changed=False,
|
||||
ansible_facts=dict(azure_dnszones=[])
|
||||
)
|
||||
|
||||
self.name = None
|
||||
self.resource_group = None
|
||||
self.tags = None
|
||||
|
||||
super(AzureRMDNSZoneFacts, self).__init__(self.module_arg_spec)
|
||||
|
||||
def exec_module(self, **kwargs):
|
||||
|
||||
for key in self.module_arg_spec:
|
||||
setattr(self, key, kwargs[key])
|
||||
|
||||
if self.name and not self.resource_group:
|
||||
self.fail("Parameter error: resource group required when filtering by name.")
|
||||
|
||||
# list the conditions and what to return based on user input
|
||||
|
||||
if self.name is not None:
|
||||
# if there is a name, facts about that specific zone
|
||||
self.results['ansible_facts']['azure_dnszones'] = self.get_item()
|
||||
elif self.resource_group:
|
||||
# all the zones listed in that specific resource group
|
||||
self.results['ansible_facts']['azure_dnszones'] = self.list_resource_group()
|
||||
else:
|
||||
# all the zones in a subscription
|
||||
self.results['ansible_facts']['azure_dnszones'] = self.list_items()
|
||||
|
||||
return self.results
|
||||
|
||||
def get_item(self):
|
||||
self.log('Get properties for {0}'.format(self.name))
|
||||
item = None
|
||||
results = []
|
||||
# get specific zone
|
||||
try:
|
||||
item = self.dns_client.zones.get(self.resource_group, self.name)
|
||||
except CloudError:
|
||||
pass
|
||||
|
||||
# serialize result
|
||||
if item and self.has_tags(item.tags, self.tags):
|
||||
results = [self.serialize_obj(item, AZURE_OBJECT_CLASS)]
|
||||
return results
|
||||
|
||||
def list_resource_group(self):
|
||||
self.log('List items for resource group')
|
||||
try:
|
||||
response = self.dns_client.zones.list_by_resource_group(self.resource_group)
|
||||
except AzureHttpError as exc:
|
||||
self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))
|
||||
|
||||
results = []
|
||||
for item in response:
|
||||
if self.has_tags(item.tags, self.tags):
|
||||
results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
|
||||
return results
|
||||
|
||||
def list_items(self):
|
||||
self.log('List all items')
|
||||
try:
|
||||
response = self.dns_client.zones.list()
|
||||
except AzureHttpError as exc:
|
||||
self.fail("Failed to list all items - {0}".format(str(exc)))
|
||||
|
||||
results = []
|
||||
for item in response:
|
||||
if self.has_tags(item.tags, self.tags):
|
||||
results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
AzureRMDNSZoneFacts()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
3
test/integration/targets/azure_rm_dnszone_facts/aliases
Normal file
3
test/integration/targets/azure_rm_dnszone_facts/aliases
Normal file
|
@ -0,0 +1,3 @@
|
|||
cloud/azure
|
||||
posix/ci/cloud/azure
|
||||
destructive
|
|
@ -0,0 +1,2 @@
|
|||
dependencies:
|
||||
- setup_azure
|
|
@ -0,0 +1,34 @@
|
|||
- name: Create random domain name
|
||||
set_fact:
|
||||
domain_name: "{{ resource_group | hash('md5') | truncate(16, True, '') + (65535 | random | string) }}"
|
||||
|
||||
- name: Create a DNS zone
|
||||
azure_rm_dnszone:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "{{ domain_name }}.com"
|
||||
state: present
|
||||
tags:
|
||||
tag1: tag1
|
||||
register: results
|
||||
|
||||
- name: Assert DNS Zone was created
|
||||
assert:
|
||||
that: results.changed
|
||||
|
||||
- name: Retrieve DNS Zone Facts
|
||||
azure_rm_dnszone_facts:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "{{ domain_name }}.com"
|
||||
register: results
|
||||
|
||||
- name: Assert that facts module returned result
|
||||
assert:
|
||||
that:
|
||||
- not results.changed
|
||||
- results.ansible_facts.azure_dnszones[0].tags.tag1 == 'tag1'
|
||||
|
||||
- name: Delete DNS zone
|
||||
azure_rm_dnszone:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "{{ domain_name }}.com"
|
||||
state: absent
|
Loading…
Reference in a new issue