Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2021 Praveen Ghuge(@praveenghuge) Karl Dasan(@karldas30) Saurabh Malpani (@saurabh3796)
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_eventhub_info
14 version_added: "1.6.0"
15 short_description: Get Azure Event Hub
16 description:
17 - Get facts of Azure Event Hub.
18 options:
19 resource_group:
20 description:
21 - The name of the resource group.
22 required: True
23 type: str
24 namespace_name:
25 description:
26 - The name of the namspace.
27 required: False
28 type: str
29 name:
30 description:
31 - The name of the Event hub.
32 type: str
33 extends_documentation_fragment:
34 - azure.azcollection.azure
35 author:
36 - Saurabh Malpani (@saurabh3796)
37 '''
38
39
40 EXAMPLES = '''
41 - name: Get facts of specific Event hub
42 azure.azcollection.azure_rm_eventhub_info:
43 resource_group: myResourceGroup
44 name: myEventHub
45 '''
46
47 RETURN = '''
48 state:
49 description:
50 - Current state of the Event Hub namesapce or Event Hub.
51 returned: always
52 type: dict
53 sample: {
54 "additional_properties": {},
55 "created_at": "2021-04-19T12:49:46.597Z",
56 "critical": null,
57 "data_center": null,
58 "status": "Active",
59 "location": "eastus",
60 "metric_id": "149f0952-6f3d-48ba-9e98-57011575cbbd:eventhubtestns1753",
61 "name": "eventhubtestns1753",
62 "namespace_type": null,
63 "provisioning_state": "Succeeded",
64 "region": null,
65 "scale_unit": null,
66 "service_bus_endpoint": "https://eventhubtestns1753.servicebus.windows.net:443/",
67 "sku": "Basic",
68 "tags": {},
69 "type": "Microsoft.EventHub/Namespaces",
70 "updated_at": "2021-04-19T12:54:33.397Z"
71 }
72 '''
73
74 try:
75 from azure.core.exceptions import ResourceNotFoundError
76 except ImportError:
77 # This is handled in azure_rm_common
78 pass
79 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
80
81
82 class AzureRMEventHubInfo(AzureRMModuleBase):
83 def __init__(self):
84
85 self.module_arg_spec = dict(
86 resource_group=dict(
87 type='str',
88 required=True
89 ),
90 namespace_name=dict(
91 type='str',
92 required=False
93 ),
94 name=dict(
95 type='str',
96 required=False
97 )
98 )
99
100 # store the results of the module operation
101 self.results = dict(
102 changed=False)
103 self.resource_group = None
104 self.namespace_name = None
105 self.name = None
106 self.tags = None
107
108 super(AzureRMEventHubInfo, self).__init__(
109 self.module_arg_spec, supports_check_mode=True, supports_tags=False)
110
111 def exec_module(self, **kwargs):
112
113 for key in self.module_arg_spec:
114 setattr(self, key, kwargs[key])
115
116 if self.name and self.namespace_name and self.resource_group:
117 results = self.get_event_hub()
118 self.results['eventhub'] = [
119 self.event_hub_to_dict(x) for x in results]
120 elif self.namespace_name:
121 results = self.get_namespace()
122 self.results['namespace'] = [
123 self.namespace_to_dict(x) for x in results]
124 elif self.name is None and self.namespace_name is None:
125 results = self.list_all_namespace()
126 self.results['namespaces'] = [
127 self.namespace_to_dict(x) for x in results]
128 return self.results
129
130 def get_namespace(self):
131 '''
132 Get the namespace using resource group and namespace name
133 '''
134 response = None
135 results = []
136 try:
137 response = self.event_hub_client.namespaces.get(
138 self.resource_group, self.namespace_name)
139 self.log("Response : {0}".format(response))
140
141 except ResourceNotFoundError as e:
142 self.fail('Could not get info for namespace. {0}').format(
143 str(e))
144
145 if response:
146 results = [response]
147 return results
148
149 def get_event_hub(self):
150 '''
151 get event hub using resource_group, namespace_name and name.
152 '''
153 response = None
154 results = []
155
156 try:
157 response = self.event_hub_client.event_hubs.get(
158 self.resource_group, self.namespace_name, self.name)
159
160 except ResourceNotFoundError as e:
161 self.fail('Could not get info for event hub. {0}').format(
162 str(e))
163
164 if response:
165 results = [response]
166 return results
167
168 def list_all_namespace(self):
169 '''
170 List all namespaces in particular resource_group
171 '''
172 self.log('List items for resource group')
173 try:
174 response = self.event_hub_client.namespaces.list_by_resource_group(
175 self.resource_group)
176
177 except Exception as exc:
178 self.fail(
179 "Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))
180
181 results = []
182 for item in response:
183 results.append(item)
184 return results
185
186 def namespace_to_dict(self, item):
187 # turn event hub object into a dictionary (serialization)
188 namespace = item.as_dict()
189 result = dict(
190 additional_properties=namespace.get(
191 'additional_properties', {}),
192 name=namespace.get('name', None),
193 type=namespace.get('type', None),
194 location=namespace.get(
195 'location', '').replace(' ', '').lower(),
196 sku=namespace.get("sku").get("name"),
197 tags=namespace.get('tags', None),
198 provisioning_state=namespace.get(
199 'provisioning_state', None),
200 region=namespace.get('region', None),
201 metric_id=namespace.get('metric_id', None),
202 service_bus_endpoint=namespace.get(
203 'service_bus_endpoint', None),
204 scale_unit=namespace.get('scale_unit', None),
205 enabled=namespace.get('enabled', None),
206 critical=namespace.get('critical', None),
207 data_center=namespace.get('data_center', None),
208 namespace_type=namespace.get('namespace_type', None),
209 updated_at=namespace.get('updated_at', None),
210 created_at=namespace.get('created_at', None),
211 is_auto_inflate_enabled=namespace.get(
212 'is_auto_inflate_enabled', None),
213 maximum_throughput_units=namespace.get(
214 'maximum_throughput_units', None),
215 identity=namespace.get('identity', None)
216 )
217 return result
218
219 def event_hub_to_dict(self, item):
220 # turn event hub object into a dictionary (serialization)
221 event_hub = item.as_dict()
222 result = dict()
223 if item.additional_properties:
224 result['additional_properties'] = item.additional_properties
225 result['name'] = event_hub.get('name', None)
226 result['partition_ids'] = event_hub.get('partition_ids', None)
227 result['created_at'] = event_hub.get('created_at', None)
228 result['updated_at'] = event_hub.get('updated_at', None)
229 result['message_retention_in_days'] = event_hub.get(
230 'message_retention_in_days', None)
231 result['partition_count'] = event_hub.get('partition_count', None)
232 result['status'] = event_hub.get('status', None)
233 result['tags'] = event_hub.get('tags', None)
234 result['identity'] = event_hub.get('identity', None)
235 return result
236
237
238 def main():
239 AzureRMEventHubInfo()
240
241
242 if __name__ == '__main__':
243 main()