Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Zim Kalinowski, (@zikalino)
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_hdinsightcluster_info
14 version_added: "0.1.2"
15 short_description: Get Azure HDInsight Cluster facts
16 description:
17 - Get facts of Azure HDInsight Cluster.
18
19 options:
20 resource_group:
21 description:
22 - Name of an Azure resource group.
23 type: str
24 name:
25 description:
26 - HDInsight cluster name.
27 type: str
28 tags:
29 description:
30 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
31 type: list
32 elements: str
33
34 extends_documentation_fragment:
35 - azure.azcollection.azure
36
37 author:
38 - Zim Kalinowski (@zikalino)
39
40 '''
41
42 EXAMPLES = '''
43 - name: Get instance of HDInsight Cluster
44 azure_rm_hdinsightcluster_info:
45 resource_group: myResourceGroup
46 name: myCluster
47
48 - name: List instances of HDInsight Cluster
49 azure_rm_hdinsightcluster_info:
50 resource_group: myResourceGroup
51 tags:
52 - key:value
53 '''
54
55 RETURN = '''
56 clusters:
57 description:
58 - A list of dictionaries containing facts for HDInsight Cluster.
59 returned: always
60 type: complex
61 contains:
62 id:
63 description:
64 - The unique resource identifier of the HDInsight Cluster.
65 returned: always
66 type: str
67 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.HDInsight/clusters/myCluster"
68 resource_group:
69 description:
70 - Name of an Azure resource group.
71 returned: always
72 type: str
73 sample: myResourceGroup
74 name:
75 description:
76 - The name of the HDInsight Cluster.
77 returned: always
78 type: str
79 sample: testaccount
80 location:
81 description:
82 - The location of the resource group to which the resource belongs.
83 returned: always
84 type: str
85 sample: westus
86 cluster_version:
87 description:
88 - The version of the cluster.
89 returned: always
90 type: str
91 sample: 3.6.1000.67
92 os_type:
93 description:
94 - The type of operating system.
95 returned: always
96 type: str
97 sample: linux
98 tier:
99 description:
100 - The cluster tier.
101 returned: always
102 type: str
103 sample: standard
104 cluster_definition:
105 description:
106 - The cluster definition.
107 type: complex
108 contains:
109 kind:
110 description:
111 - The type of cluster.
112 returned: always
113 type: str
114 sample: spark
115 compute_profile_roles:
116 description:
117 - The list of roles in the cluster.
118 type: complex
119 contains:
120 name:
121 description:
122 - The name of the role.
123 returned: always
124 type: str
125 sample: headnode
126 target_instance_count:
127 description:
128 - The instance count of the cluster.
129 returned: always
130 type: int
131 sample: 2
132 vm_size:
133 description:
134 - The size of the VM.
135 returned: always
136 type: str
137 sample: Standard_D3
138 linux_profile:
139 description:
140 - The Linux OS profile.
141 type: dict
142 contains:
143 username:
144 description:
145 - User name.
146 returned: always
147 type: str
148 sample: myuser
149 connectivity_endpoints:
150 description:
151 - Cluster's connectivity endpoints.
152 type: complex
153 contains:
154 location:
155 description:
156 - Endpoint location.
157 returned: always
158 type: str
159 sample: myCluster-ssh.azurehdinsight.net
160 name:
161 description:
162 - Endpoint name.
163 returned: always
164 type: str
165 sample: SSH
166 port:
167 description:
168 - Endpoint port.
169 returned: always
170 type: int
171 sample: 22
172 protocol:
173 description:
174 - Endpoint protocol.
175 returned: always
176 type: str
177 sample: TCP
178 tags:
179 description:
180 - The tags of the resource.
181 returned: always
182 type: dict
183 sample: {}
184 '''
185
186 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
187
188 try:
189 from azure.core.exceptions import ResourceNotFoundError
190 from azure.mgmt.hdinsight import HDInsightManagementClient
191 except ImportError:
192 # This is handled in azure_rm_common
193 pass
194
195
196 class AzureRMHDInsightclusterInfo(AzureRMModuleBase):
197 def __init__(self):
198 # define user inputs into argument
199 self.module_arg_spec = dict(
200 resource_group=dict(
201 type='str'
202 ),
203 name=dict(
204 type='str'
205 ),
206 tags=dict(
207 type='list',
208 elements='str'
209 )
210 )
211 # store the results of the module operation
212 self.results = dict(
213 changed=False
214 )
215 self.mgmt_client = None
216 self.resource_group = None
217 self.name = None
218 self.tags = None
219
220 super(AzureRMHDInsightclusterInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
221
222 def exec_module(self, **kwargs):
223
224 for key in self.module_arg_spec:
225 setattr(self, key, kwargs[key])
226 self.mgmt_client = self.get_mgmt_svc_client(HDInsightManagementClient,
227 base_url=self._cloud_environment.endpoints.resource_manager)
228
229 if self.name is not None:
230 self.results['clusters'] = self.get()
231 elif self.resource_group is not None:
232 self.results['clusters'] = self.list_by_resource_group()
233 else:
234 self.results['clusters'] = self.list_all()
235 return self.results
236
237 def get(self):
238 response = None
239 results = []
240 try:
241 response = self.mgmt_client.clusters.get(resource_group_name=self.resource_group,
242 cluster_name=self.name)
243 self.log("Response : {0}".format(response))
244 except ResourceNotFoundError as e:
245 self.log('Could not get facts for HDInsight Cluster.')
246
247 if response and self.has_tags(response.tags, self.tags):
248 results.append(self.format_response(response))
249
250 return results
251
252 def list_by_resource_group(self):
253 response = None
254 results = []
255 try:
256 response = self.mgmt_client.clusters.list_by_resource_group(resource_group_name=self.resource_group)
257 self.log("Response : {0}".format(response))
258 except Exception as e:
259 self.log('Could not get facts for HDInsight Cluster.')
260
261 if response is not None:
262 for item in response:
263 if self.has_tags(item.tags, self.tags):
264 results.append(self.format_response(item))
265
266 return results
267
268 def list_all(self):
269 response = None
270 results = []
271 try:
272 response = self.mgmt_client.clusters.list()
273 self.log("Response : {0}".format(response))
274 except Exception as e:
275 self.log('Could not get facts for HDInsight Cluster.')
276
277 if response is not None:
278 for item in response:
279 if self.has_tags(item.tags, self.tags):
280 results.append(self.format_response(item))
281
282 return results
283
284 def format_response(self, item):
285 d = item.as_dict()
286 d = {
287 'id': d.get('id'),
288 'resource_group': self.parse_resource_to_dict(d.get('id')).get('resource_group'),
289 'name': d.get('name', None),
290 'location': d.get('location', '').replace(' ', '').lower(),
291
292 'cluster_version': d.get('properties', {}).get('cluster_version'),
293 'os_type': d.get('properties', {}).get('os_type'),
294 'tier': d.get('properties', {}).get('tier'),
295 'cluster_definition': {
296 'kind': d.get('properties', {}).get('cluster_definition', {}).get('kind')
297 },
298 'identity': d.get('identity'),
299 'cluster_state': d.get('properties', {}).get('cluster_state'),
300 'compute_profile_roles': [{
301 'name': item.get('name'),
302 'target_instance_count': item.get('target_instance_count'),
303 'vm_size': item.get('hardware_profile', {}).get('vm_size'),
304 'linux_profile': {
305 'username': item.get('os_profile', {}).get('linux_operating_system_profile', {}).get('username')
306 }
307 } for item in d.get('properties', []).get('compute_profile', {}).get('roles', [])],
308 'connectivity_endpoints': d.get('properties', {}).get('connectivity_endpoints'),
309 'tags': d.get('tags', None)
310 }
311
312 return d
313
314
315 def main():
316 AzureRMHDInsightclusterInfo()
317
318
319 if __name__ == '__main__':
320 main()