Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2021 Aparna Patil(@techcon65)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_privatednszonelink_info
13
14 version_added: "1.6.0"
15
16 short_description: Get Virtual Network link facts for private DNS zone
17
18 description:
19 - Get a specified virtual network link or all virtual network links facts for a Private DNS zone.
20
21 options:
22 resource_group:
23 description:
24 - Name of resource group.
25 required: true
26 type: str
27 name:
28 description:
29 - The name of the virtual network link.
30 type: str
31 zone_name:
32 description:
33 - The name of the Private DNS zone.
34 required: true
35 type: str
36 tags:
37 description:
38 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
39 type: list
40 elements: str
41
42 extends_documentation_fragment:
43 - azure.azcollection.azure
44
45 author:
46 - Aparna Patil (@techcon65)
47
48 '''
49
50 EXAMPLES = '''
51 - name: Get facts for one virtual network link in private DNS zone
52 azure_rm_privatednszonelink_info:
53 resource_group: myResourceGroup
54 name: vnetlink1
55 zone_name: privatezone.com
56
57 - name: Get facts for all virtual network links in private DNS zone
58 azure_rm_privatednszonelink_info:
59 resource_group: myResourceGroup
60 zone_name: privatezone.com
61 '''
62
63 RETURN = '''
64 virtualnetworklinks:
65 description:
66 - Gets a list of virtual network links dict in a Private DNS zone.
67 returned: always
68 type: list
69 elements: dict
70 sample: [
71 {
72 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/
73 Microsoft.Network/privateDnsZones/privatezone.com/virtualNetworkLinks/vnetlink1",
74 "name": "vnetlink1",
75 "provisioning_state": "Succeeded",
76 "registration_enabled": true,
77 "tags": {
78 "key1": "value1"
79 },
80 "virtual_network": {
81 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/
82 providers/Microsoft.Network/virtualNetworks/MyAzureVNet"
83 },
84 "virtual_network_link_state": "Completed",
85 'resolution_policy': 'null'
86 }
87 ]
88 '''
89
90 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
91
92 try:
93 from azure.core.exceptions import ResourceNotFoundError
94 except Exception:
95 # This is handled in azure_rm_common
96 pass
97
98 AZURE_OBJECT_CLASS = 'VirtualNetworkLink'
99
100
101 class AzureRMVirtualNetworkLinkInfo(AzureRMModuleBase):
102
103 def __init__(self):
104
105 # define user inputs into argument
106 self.module_arg_spec = dict(
107 name=dict(type='str'),
108 resource_group=dict(type='str', required=True),
109 zone_name=dict(type='str', required=True),
110 tags=dict(type='list', elements='str')
111 )
112
113 # store the results of the module operation
114 self.results = dict(
115 changed=False
116 )
117
118 self.name = None
119 self.resource_group = None
120 self.zone_name = None
121 self.tags = None
122 self.log_path = None
123 self.log_mode = None
124
125 super(AzureRMVirtualNetworkLinkInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
126
127 def exec_module(self, **kwargs):
128
129 for key in self.module_arg_spec:
130 setattr(self, key, kwargs[key])
131
132 results = []
133 # list the conditions and results to return based on user input
134 if self.name is not None:
135 # if there is a link name provided, return facts about that specific virtual network link
136 results = self.get_item()
137 else:
138 # all the virtual network links in specified private DNS zone
139 results = self.list_items()
140
141 self.results['virtualnetworklinks'] = self.curated_items(results)
142
143 return self.results
144
145 def get_item(self):
146 self.log('Get properties for {0}'.format(self.name))
147 item = None
148 results = []
149 # get specific virtual network link
150 try:
151 item = self.private_dns_client.virtual_network_links.get(self.resource_group,
152 self.zone_name,
153 self.name)
154 except ResourceNotFoundError:
155 pass
156
157 # serialize result
158 if item and self.has_tags(item.tags, self.tags):
159 results = [item]
160 return results
161
162 def list_items(self):
163 self.log('List all virtual network links for private DNS zone - {0}'.format(self.zone_name))
164 try:
165 response = self.private_dns_client.virtual_network_links.list(self.resource_group, self.zone_name)
166 except Exception as exc:
167 self.fail("Failed to list all items - {0}".format(str(exc)))
168
169 results = []
170 for item in response:
171 if self.has_tags(item.tags, self.tags):
172 results.append(item)
173 return results
174
175 def curated_items(self, raws):
176 return [self.vnetlink_to_dict(item) for item in raws] if raws else []
177
178 def vnetlink_to_dict(self, link):
179 result = dict(
180 id=link.id,
181 name=link.name,
182 virtual_network=dict(id=link.virtual_network.id),
183 registration_enabled=link.registration_enabled,
184 tags=link.tags,
185 virtual_network_link_state=link.virtual_network_link_state,
186 provisioning_state=link.provisioning_state,
187 resolution_policy=link.resolution_policy
188 )
189 return result
190
191
192 def main():
193 AzureRMVirtualNetworkLinkInfo()
194
195
196 if __name__ == '__main__':
197 main()