Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_privatednszone.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
16
17 version_added: "0.0.1"
18
19 short_description: Manage Azure private DNS zones
20
21 description:
22 - Creates and deletes Azure private DNS zones.
23
24 options:
25 resource_group:
26 description:
27 - Name of resource group.
28 type: str
29 required: true
30 name:
31 description:
32 - Name of the private DNS zone.
33 type: str
34 required: true
35 state:
36 description:
37 - Assert the state of the zone. Use C(present) to create or update and C(absent) to delete.
38 default: present
39 type: str
40 choices:
41 - absent
42 - present
43
44 extends_documentation_fragment:
45 - azure.azcollection.azure
46 - azure.azcollection.azure_tags
47
48 author:
49 - Jose Angel Munoz (@imjoseangel)
50 '''
51
52 EXAMPLES = '''
53
54 - name: Create a private DNS zone
55 azure_rm_privatednszone:
56 resource_group: myResourceGroup
57 name: example.com
58
59 - name: Delete a private DNS zone
60 azure_rm_privatednszone:
61 resource_group: myResourceGroup
62 name: example.com
63 state: absent
64 '''
65
66 RETURN = '''
67 state:
68 description:
69 - Current state of the zone.
70 returned: always
71 type: dict
72 sample: {
73 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup",
74 "location": "global",
75 "name": "Testing",
76 "number_of_record_sets": 2,
77 "number_of_virtual_network_links": 0,
78 "number_of_virtual_network_links_with_registration": 0
79 }
80
81 '''
82
83 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
84
85 try:
86 from azure.core.exceptions import ResourceNotFoundError
87 from azure.core.polling import LROPoller
88 except ImportError:
89 # This is handled in azure_rm_common
90 pass
91
92
93 class AzureRMPrivateDNSZone(AzureRMModuleBase):
94 def __init__(self):
95
96 # define user inputs from playbook
97 self.module_arg_spec = dict(resource_group=dict(type='str',
98 required=True),
99 name=dict(type='str', required=True),
100 state=dict(choices=['present', 'absent'],
101 default='present',
102 type='str'))
103
104 # store the results of the module operation
105 self.results = dict(changed=False, state=dict())
106
107 self.resource_group = None
108 self.name = None
109 self.state = None
110 self.tags = None
111
112 super(AzureRMPrivateDNSZone, self).__init__(self.module_arg_spec,
113 supports_check_mode=True,
114 supports_tags=True)
115
116 def exec_module(self, **kwargs):
117
118 # create a new zone variable in case the 'try' doesn't find a zone
119 zone = None
120 for key in list(self.module_arg_spec.keys()) + ['tags']:
121 setattr(self, key, kwargs[key])
122
123 self.results['check_mode'] = self.check_mode
124
125 # retrieve resource group to make sure it exists
126 self.get_resource_group(self.resource_group)
127
128 changed = False
129 results = dict()
130
131 try:
132 self.log('Fetching private DNS zone {0}'.format(self.name))
133 zone = self.private_dns_client.private_zones.get(
134 self.resource_group, self.name)
135
136 # serialize object into a dictionary
137 results = zone_to_dict(zone)
138
139 # don't change anything if creating an existing zone, but change if deleting it
140 if self.state == 'present':
141 changed = False
142
143 update_tags, results['tags'] = self.update_tags(
144 results['tags'])
145 if update_tags:
146 changed = True
147 elif self.state == 'absent':
148 changed = True
149
150 except ResourceNotFoundError:
151 # the zone does not exist so create it
152 if self.state == 'present':
153 changed = True
154 else:
155 # you can't delete what is not there
156 changed = False
157
158 self.results['changed'] = changed
159 self.results['state'] = results
160
161 # return the results if your only gathering information
162 if self.check_mode:
163 return self.results
164
165 if changed:
166 if self.state == 'present':
167 zone = self.private_dns_models.PrivateZone(tags=self.tags,
168 location='global')
169 self.results['state'] = self.create_or_update_zone(zone)
170 elif self.state == 'absent':
171 # delete zone
172 self.delete_zone()
173 # the delete does not actually return anything. if no exception, then we'll assume
174 # it worked.
175 self.results['state']['status'] = 'Deleted'
176
177 return self.results
178
179 def create_or_update_zone(self, zone):
180 try:
181 # create or update the new Zone object we created
182 new_zone = self.private_dns_client.private_zones.begin_create_or_update(
183 self.resource_group, self.name, zone)
184
185 if isinstance(new_zone, LROPoller):
186 new_zone = self.get_poller_result(new_zone)
187
188 except Exception as exc:
189 self.fail("Error creating or updating zone {0} - {1}".format(
190 self.name, exc.message or str(exc)))
191 return zone_to_dict(new_zone)
192
193 def delete_zone(self):
194 try:
195 # delete the Zone
196 poller = self.private_dns_client.private_zones.begin_delete(
197 self.resource_group, self.name)
198 result = self.get_poller_result(poller)
199 except Exception as exc:
200 self.fail("Error deleting zone {0} - {1}".format(
201 self.name, exc.message or str(exc)))
202 return result
203
204
205 def zone_to_dict(zone):
206 # turn Zone object into a dictionary (serialization)
207 result = dict(
208 id=zone.id,
209 name=zone.name,
210 number_of_record_sets=zone.number_of_record_sets,
211 number_of_virtual_network_links=zone.number_of_virtual_network_links,
212 number_of_virtual_network_links_with_registration=zone.
213 number_of_virtual_network_links_with_registration,
214 tags=zone.tags)
215 return result
216
217
218 def main():
219 AzureRMPrivateDNSZone()
220
221
222 if __name__ == '__main__':
223 main()