Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2018 Hai Cao, <[email protected]>, Yunge Zhu <[email protected]>
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
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_trafficmanagerendpoint_info
14
15 version_added: "0.1.2"
16
17 short_description: Get Azure Traffic Manager endpoint facts
18
19 description:
20 - Get facts for a specific Traffic Manager endpoints or all endpoints in a Traffic Manager profile.
21
22 options:
23 name:
24 description:
25 - Limit results to a specific Traffic Manager endpoint.
26 type: str
27 resource_group:
28 description:
29 - The resource group to search for the desired Traffic Manager profile.
30 required: True
31 type: str
32 profile_name:
33 description:
34 - Name of Traffic Manager Profile.
35 required: True
36 type: str
37 type:
38 description:
39 - Type of endpoint.
40 type: str
41 choices:
42 - azure_endpoints
43 - external_endpoints
44 - nested_endpoints
45
46 extends_documentation_fragment:
47 - azure.azcollection.azure
48
49 author:
50 - Hai Cao (@caohai)
51 - Yunge Zhu (@yungezz)
52 '''
53
54 EXAMPLES = '''
55 - name: Get endpoints facts of a Traffic Manager profile
56 azure_rm_trafficmanagerendpoint_info:
57 resource_group: myResourceGroup
58 profile_name: Testing
59
60 - name: Get specific endpoint of a Traffic Manager profile
61 azure_rm_trafficmanager_info:
62 resource_group: myResourceGroup
63 profile_name: Testing
64 name: test_external_endpoint
65 '''
66
67 RETURN = '''
68 endpoints:
69 description:
70 - List of Traffic Manager endpoints.
71 returned: always
72 type: complex
73 contains:
74 resource_group:
75 description:
76 - Name of a resource group.
77 returned: always
78 type: str
79 sample: myResourceGroup
80 name:
81 description:
82 - Name of the Traffic Manager endpoint.
83 returned: always
84 type: str
85 sample: testendpoint
86 type:
87 description:
88 - The type of the endpoint.
89 returned: always
90 type: str
91 sample: external_endpoints
92 target_resource_id:
93 description:
94 - The Azure Resource URI of the of the endpoint.
95 returned: always
96 type: str
97 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ClassicCompute/domainNames/vscjavaci
98 target:
99 description:
100 - The fully-qualified DNS name of the endpoint.
101 returned: always
102 type: str
103 sample: 8.8.8.8
104 enabled:
105 description:
106 - The status of the endpoint.
107 returned: always
108 type: str
109 sample: Enabled
110 weight:
111 description:
112 - The weight of this endpoint when using the 'Weighted' traffic routing method.
113 returned: always
114 type: int
115 sample: 10
116 priority:
117 description:
118 - The priority of this endpoint when using the 'Priority' traffic routing method.
119 returned: always
120 type: str
121 sample: 3
122 location:
123 description:
124 - The location of the external or nested endpoints when using the 'Performance' traffic routing method.
125 returned: always
126 type: str
127 sample: East US
128 min_child_endpoints:
129 description:
130 - The minimum number of endpoints that must be available in the child profile to make the parent profile available.
131 returned: always
132 type: int
133 sample: 3
134 geo_mapping:
135 description:
136 - The list of countries/regions mapped to this endpoint when using the 'Geographic' traffic routing method.
137 returned: always
138 type: list
139 sample: [
140 "GEO-NA",
141 "GEO-AS"
142 ]
143 '''
144
145 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
146 from ansible.module_utils.common.dict_transformations import (
147 _snake_to_camel, _camel_to_snake
148 )
149
150 try:
151 from azure.core.exceptions import ResourceNotFoundError
152 except Exception:
153 # handled in azure_rm_common
154 pass
155
156 AZURE_OBJECT_CLASS = 'TrafficManagerEndpoints'
157
158
159 def serialize_endpoint(endpoint, resource_group):
160 result = dict(
161 id=endpoint.id,
162 name=endpoint.name,
163 target_resource_id=endpoint.target_resource_id,
164 target=endpoint.target,
165 enabled=True,
166 weight=endpoint.weight,
167 priority=endpoint.priority,
168 location=endpoint.endpoint_location,
169 min_child_endpoints=endpoint.min_child_endpoints,
170 geo_mapping=endpoint.geo_mapping,
171 monitor_status=endpoint.endpoint_monitor_status,
172 resource_group=resource_group
173 )
174
175 if endpoint.endpoint_status and endpoint.endpoint_status == 'Disabled':
176 result['enabled'] = False
177
178 if endpoint.type:
179 result['type'] = _camel_to_snake(endpoint.type.split("/")[-1])
180
181 return result
182
183
184 class AzureRMTrafficManagerEndpointInfo(AzureRMModuleBase):
185 """Utility class to get Azure Traffic Manager Endpoint facts"""
186
187 def __init__(self):
188
189 self.module_args = dict(
190 profile_name=dict(
191 type='str',
192 required=True),
193 resource_group=dict(
194 type='str',
195 required=True),
196 name=dict(type='str'),
197 type=dict(
198 type='str',
199 choices=[
200 'azure_endpoints',
201 'external_endpoints',
202 'nested_endpoints'
203 ])
204 )
205
206 self.results = dict(
207 changed=False,
208 endpoints=[]
209 )
210
211 self.profile_name = None
212 self.name = None
213 self.resource_group = None
214 self.type = None
215
216 super(AzureRMTrafficManagerEndpointInfo, self).__init__(
217 derived_arg_spec=self.module_args,
218 supports_check_mode=True,
219 supports_tags=False,
220 facts_module=True
221 )
222
223 def exec_module(self, **kwargs):
224
225 for key in self.module_args:
226 setattr(self, key, kwargs[key])
227
228 if self.type:
229 self.type = _snake_to_camel(self.type)
230
231 if self.name and not self.resource_group:
232 self.fail("Parameter error: resource group required when filtering by name.")
233
234 if self.name:
235 self.results['endpoints'] = self.get_item()
236 elif self.type:
237 self.results['endpoints'] = self.list_by_type()
238 else:
239 self.results['endpoints'] = self.list_by_profile()
240
241 return self.results
242
243 def get_item(self):
244 """Get a single Azure Traffic Manager endpoint"""
245
246 self.log('Get properties for {0}'.format(self.name))
247
248 item = None
249 result = []
250
251 try:
252 item = self.traffic_manager_management_client.endpoints.get(
253 self.resource_group, self.profile_name, self.type, self.name)
254 except ResourceNotFoundError:
255 pass
256
257 if item:
258 if (self.type and self.type == item.type) or self.type is None:
259 result = [self.serialize_tm(item)]
260
261 return result
262
263 def list_by_profile(self):
264 """Get all Azure Traffic Manager endpoints of a profile"""
265
266 self.log('List all endpoints belongs to a Traffic Manager profile')
267
268 try:
269 response = self.traffic_manager_management_client.profiles.get(self.resource_group, self.profile_name)
270 except Exception as exc:
271 self.fail('Failed to list all items - {0}'.format(str(exc)))
272
273 results = []
274 if response and response.endpoints:
275 for endpoint in response.endpoints:
276 results.append(serialize_endpoint(endpoint, self.resource_group))
277
278 return results
279
280 def list_by_type(self):
281 """Get all Azure Traffic Managers endpoints of a profile by type"""
282 self.log('List all Traffic Manager endpoints of a profile by type')
283 try:
284 response = self.traffic_manager_management_client.profiles.get(self.resource_group, self.profile_name)
285 except Exception as exc:
286 self.fail('Failed to list all items - {0}'.format(str(exc)))
287
288 results = []
289 for item in response:
290 if item.endpoints:
291 for endpoint in item.endpoints:
292 if endpoint.type == self.type:
293 results.append(serialize_endpoint(endpoint, self.resource_group))
294 return results
295
296
297 def main():
298 """Main module execution code path"""
299
300 AzureRMTrafficManagerEndpointInfo()
301
302
303 if __name__ == '__main__':
304 main()