Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_privatednszone_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (c) 2020 Jose Angel Munoz, <[email protected]>
5 #
6 #
7 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
8 #
9
10 from __future__ import absolute_import, division, print_function
11 __metaclass__ = type
12
13 DOCUMENTATION = '''
14 ---
15 module: azure_rm_privatednszone_info
16
17 version_added: "0.0.1"
18
19 short_description: Get private DNS zone facts
20
21 description:
22 - Get facts for a specific private DNS zone or all private DNS zones within a resource group.
23
24 options:
25 resource_group:
26 description:
27 - Limit results by resource group. Required when filtering by name.
28 type: str
29 name:
30 description:
31 - Only show results for a specific zone.
32 type: str
33 tags:
34 description:
35 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
36 type: list
37 elements: str
38
39 extends_documentation_fragment:
40 - azure.azcollection.azure
41
42 author:
43 - Jose Angel Munoz (@imjoseangel)
44
45 '''
46
47 EXAMPLES = '''
48 - name: Get facts for one zone
49 azure_rm_privatednszone_info:
50 resource_group: myResourceGroup
51 name: foobar22
52
53 - name: Get facts for all zones in a resource group
54 azure_rm_privatednszone_info:
55 resource_group: myResourceGroup
56
57 - name: Get facts for privatednszone with tags
58 azure_rm_privatednszone_info:
59 tags:
60 - testing
61 - foo:bar
62 '''
63
64 RETURN = '''
65 azure_privatednszones:
66 description:
67 - List of private zone dicts.
68 returned: always
69 type: list
70 example: [{
71 "etag": "00000002-0000-0000-0dcb-df5776efd201",
72 "location": "global",
73 "properties": {
74 "maxNumberOfRecordSets": 5000,
75 "number_of_virtual_network_links": 0,
76 "number_of_virtual_network_links_with_registration": 0
77 },
78 "tags": {}
79 }]
80 privatednszones:
81 description:
82 - List of private zone dicts, which share the same layout as azure_rm_privatednszone module parameter.
83 returned: always
84 type: list
85 contains:
86 id:
87 description:
88 - id of the private DNS Zone.
89 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privatednszones/azure.com"
90 type: str
91 name:
92 description:
93 - name of the private DNS zone.
94 sample: azure.com
95 type: str
96 number_of_record_sets:
97 description:
98 - The current number of record sets in this private DNS zone.
99 type: int
100 sample: 2
101 number_of_virtual_network_links:
102 description:
103 - The current number of network links in this private DNS zone.
104 type: int
105 sample: 0
106 number_of_virtual_network_links_with_registration:
107 description:
108 - The current number of network links with registration in this private DNS zone.
109 type: int
110 sample: 0
111 '''
112
113 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
114
115 try:
116 from azure.core.exceptions import ResourceNotFoundError
117 except Exception:
118 # This is handled in azure_rm_common
119 pass
120
121 AZURE_OBJECT_CLASS = 'PrivateDnsZone'
122
123
124 class AzurePrivateRMDNSZoneInfo(AzureRMModuleBase):
125 def __init__(self):
126
127 # define user inputs into argument
128 self.module_arg_spec = dict(name=dict(type='str'),
129 resource_group=dict(type='str'),
130 tags=dict(type='list', elements='str'))
131
132 # store the results of the module operation
133 self.results = dict(changed=False,
134 ansible_info=dict(azure_privatednszones=[]))
135
136 self.name = None
137 self.resource_group = None
138 self.tags = None
139
140 super(AzurePrivateRMDNSZoneInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
141
142 def exec_module(self, **kwargs):
143
144 for key in self.module_arg_spec:
145 setattr(self, key, kwargs[key])
146
147 if self.name and not self.resource_group:
148 self.fail(
149 "Parameter error: resource group required when filtering by name."
150 )
151
152 results = []
153 # list the conditions and what to return based on user input
154 if self.name is not None:
155 # if there is a name, facts about that specific zone
156 results = self.get_item()
157 elif self.resource_group:
158 # all the zones listed in that specific resource group
159 results = self.list_resource_group()
160 else:
161 # all the zones in a subscription
162 results = self.list_items()
163
164 self.results['ansible_info'][
165 'azure_privatednszones'] = self.serialize_items(results)
166 self.results['privatednszones'] = self.curated_items(results)
167
168 return self.results
169
170 def get_item(self):
171 self.log('Get properties for {0}'.format(self.name))
172 item = None
173 results = []
174 # get specific zone
175 try:
176 item = self.private_dns_client.private_zones.get(
177 self.resource_group, self.name)
178 except ResourceNotFoundError:
179 pass
180
181 # serialize result
182 if item and self.has_tags(item.tags, self.tags):
183 results = [item]
184 return results
185
186 def list_resource_group(self):
187 self.log('List items for resource group')
188 try:
189 response = self.private_dns_client.private_zones.list_by_resource_group(
190 self.resource_group)
191 except Exception as exc:
192 self.fail("Failed to list for resource group {0} - {1}".format(
193 self.resource_group, str(exc)))
194
195 results = []
196 for item in response:
197 if self.has_tags(item.tags, self.tags):
198 results.append(item)
199 return results
200
201 def list_items(self):
202 self.log('List all items')
203 try:
204 response = self.private_dns_client.private_zones.list()
205 except Exception as exc:
206 self.fail("Failed to list all items - {0}".format(str(exc)))
207
208 results = []
209 for item in response:
210 if self.has_tags(item.tags, self.tags):
211 results.append(item)
212 return results
213
214 def serialize_items(self, raws):
215 return [self.serialize_obj(item, AZURE_OBJECT_CLASS)
216 for item in raws] if raws else []
217
218 def curated_items(self, raws):
219 return [self.zone_to_dict(item) for item in raws] if raws else []
220
221 def zone_to_dict(self, zone):
222 return dict(id=zone.id,
223 name=zone.name,
224 number_of_record_sets=zone.number_of_record_sets,
225 number_of_virtual_network_links=zone.
226 number_of_virtual_network_links,
227 number_of_virtual_network_links_with_registration=zone.
228 number_of_virtual_network_links_with_registration,
229 tags=zone.tags)
230
231
232 def main():
233 AzurePrivateRMDNSZoneInfo()
234
235
236 if __name__ == '__main__':
237 main()