Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 Praveen Ghuge (@praveenghuge), Karl Dasan (@karldas30)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 DOCUMENTATION = '''
10 ---
11 module: azure_rm_notificationhub_info
12 version_added: "1.7.0"
13 short_description: Get Azure Notification Hub
14 description:
15 - Get facts of Azure Notification Hub
16
17 options:
18 resource_group:
19 description:
20 - The name of the resource group.
21 required: True
22 type: str
23 namespace_name:
24 description:
25 - The name of the namspace.
26 type: str
27 name:
28 description:
29 - The name of the Notification hub.
30 type: str
31
32
33 extends_documentation_fragment:
34 - azure.azcollection.azure
35
36
37 author:
38 - Praveen Ghuge (@praveenghuge)
39 - Karl Dasan (@karldas30)
40 '''
41
42
43 EXAMPLES = '''
44 - name: Get facts of specific notification hub
45 azure.azcollection.azure_rm_notificationhub_info:
46 resource_group: myResourceGroup
47 name: myNotificationHub
48 '''
49
50 RETURN = '''
51 state:
52 description:
53 - Current state of the Notification Hub namesapce or Notification Hub.
54 returned: always
55 type: dict
56 sample: {
57 "additional_properties": {},
58 "critical": false,
59 "data_center": null,
60 "enabled": true,
61 "location": "eastus2",
62 "metric_id": null,
63 "name": "testnaedd3d22d3w",
64 "namespace_type": "NotificationHub",
65 "provisioning_state": "Succeeded",
66 "region": null,
67 "scale_unit": null,
68 "service_bus_endpoint": "https://testnaedd3d22d3w.servicebus.windows.net:443/",
69 "sku": {"name":"Free"},
70 "tags": {
71 "a": "b"
72 },
73 "type": "Microsoft.NotificationHubs/namespaces"
74 }
75 '''
76
77 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
78
79 try:
80 from azure.core.exceptions import ResourceNotFoundError
81 except ImportError:
82 # This is handled in azure_rm_common
83 pass
84
85
86 class AzureNotificationHubInfo(AzureRMModuleBase):
87 def __init__(self):
88
89 self.module_arg_spec = dict(
90 resource_group=dict(
91 type='str',
92 required=True
93 ),
94 namespace_name=dict(
95 type='str'
96 ),
97 name=dict(
98 type='str',
99 )
100 )
101 # store the results of the module operation
102 self.results = dict(
103 changed=False)
104 self.resource_group = None
105 self.namespace_name = None
106 self.name = None
107 self.tags = None
108
109 super(AzureNotificationHubInfo, self).__init__(
110 self.module_arg_spec, supports_check_mode=True, supports_tags=False)
111
112 def exec_module(self, **kwargs):
113
114 for key in self.module_arg_spec:
115 setattr(self, key, kwargs[key])
116
117 if self.name is None and self.namespace_name is None:
118 results = self.list_all_namespace()
119 self.results['namespaces'] = [
120 self.namespace_to_dict(x) for x in results]
121 elif self.name and self.namespace_name:
122 results = self.get_notification_hub()
123 self.results['notificationhub'] = [
124 self.notification_hub_to_dict(x) for x in results]
125 elif self.namespace_name:
126 results = self.get_namespace()
127 self.results['namespace'] = [
128 self.namespace_to_dict(x) for x in results]
129
130 return self.results
131
132 def get_namespace(self):
133 response = None
134 results = []
135 try:
136 response = self.notification_hub_client.namespaces.get(
137 self.resource_group, self.namespace_name)
138 self.log("Response : {0}".format(response))
139
140 except ResourceNotFoundError as e:
141 self.fail('Could not get info for namespace. {0}').format(
142 str(e))
143
144 if response and self.has_tags(response.tags, self.tags):
145 results = [response]
146 return results
147
148 def get_notification_hub(self):
149 response = None
150 results = []
151 try:
152 response = self.notification_hub_client.notification_hubs.get(
153 self.resource_group, self.namespace_name, self.name)
154 self.log("Response : {0}".format(response))
155
156 except ResourceNotFoundError as e:
157 self.fail('Could not get info for notification hub. {0}').format(
158 str(e))
159
160 if response and self.has_tags(response.tags, self.tags):
161 results = [response]
162 return results
163
164 def list_all_namespace(self):
165 self.log('List items for resource group')
166 try:
167 response = self.notification_hub_client.namespaces.list(
168 self.resource_group)
169
170 except Exception as exc:
171 self.fail(
172 "Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))
173
174 results = []
175 for item in response:
176 if self.has_tags(item.tags, self.tags):
177 results.append(item)
178 return results
179
180 def namespace_to_dict(self, item):
181 # turn notification hub object into a dictionary (serialization)
182 namespace = item.as_dict()
183 result = dict(
184 additional_properties=namespace.get(
185 'additional_properties', {}),
186 name=namespace.get('name', None),
187 type=namespace.get('type', None),
188 location=namespace.get(
189 'location', '').replace(' ', '').lower(),
190 sku=namespace.get("sku"),
191 tags=namespace.get('tags', None),
192 provisioning_state=namespace.get(
193 'provisioning_state', None),
194 region=namespace.get('region', None),
195 metric_id=namespace.get('metric_id', None),
196 service_bus_endpoint=namespace.get(
197 'service_bus_endpoint', None),
198 scale_unit=namespace.get('scale_unit', None),
199 enabled=namespace.get('enabled', None),
200 critical=namespace.get('critical', None),
201 data_center=namespace.get('data_center', None),
202 namespace_type=namespace.get('namespace_type', None)
203 )
204 return result
205
206 def notification_hub_to_dict(self, item):
207 # turn notification hub object into a dictionary (serialization)
208 notification_hub = item.as_dict()
209 result = dict(
210 additional_properties=notification_hub.get(
211 'additional_properties', {}),
212 name=notification_hub.get('name', None),
213 type=notification_hub.get('type', None),
214 location=notification_hub.get(
215 'location', '').replace(' ', '').lower(),
216 tags=notification_hub.get('tags', None),
217 name_properties_name=notification_hub.get(
218 'name_properties_name', None),
219 registration_ttl=notification_hub.get('registration_ttl', None),
220 authorization_rules=notification_hub.get(
221 'authorization_rules', None),
222 apns_credential=notification_hub.get(
223 'apns_credential', None),
224 wns_credential=notification_hub.get('wns_credential', None),
225 gcm_credential=notification_hub.get('gcm_credential', None),
226 mpns_credential=notification_hub.get('mpns_credential', None),
227 adm_credential=notification_hub.get('adm_credential', None),
228 baidu_credential=notification_hub.get('baidu_credential', None)
229 )
230 return result
231
232
233 def main():
234 AzureNotificationHubInfo()
235
236
237 if __name__ == '__main__':
238 main()