Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Yuwei Zhou, <[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_loganalyticsworkspace_info
14 version_added: "0.1.2"
15 short_description: Get facts of Azure Log Analytics workspaces
16 description:
17 - Get, query Azure Log Analytics workspaces.
18 options:
19 resource_group:
20 description:
21 - Name of resource group.
22 type: str
23 name:
24 description:
25 - Name of the workspace.
26 - The resource group must be configured when name exists.
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 show_intelligence_packs:
34 description:
35 - Show the intelligence packs for a workspace.
36 - Note this will cost one more network overhead for each workspace, expected slow response.
37 type: bool
38 show_management_groups:
39 description:
40 - Show the management groups for a workspace.
41 - Note this will cost one more network overhead for each workspace, expected slow response.
42 type: bool
43 show_shared_keys:
44 description:
45 - Show the shared keys for a workspace.
46 - Note this will cost one more network overhead for each workspace, expected slow response.
47 type: bool
48 show_usages:
49 description:
50 - Show the list of usages for a workspace.
51 - Note this will cost one more network overhead for each workspace, expected slow response.
52 type: bool
53 extends_documentation_fragment:
54 - azure.azcollection.azure
55
56 author:
57 - Yuwei Zhou (@yuwzho)
58
59 '''
60
61 EXAMPLES = '''
62 - name: Query a workspace
63 azure_rm_loganalyticsworkspace_info:
64 resource_group: myResourceGroup
65 name: myLogAnalyticsWorkspace
66 show_intelligence_packs: true
67 show_management_groups: true
68 show_shared_keys: true
69 show_usages: true
70 '''
71
72 RETURN = '''
73 id:
74 description:
75 - Workspace resource path.
76 type: str
77 returned: success
78 example: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.OperationalInsights/workspaces/m
79 yLogAnalyticsWorkspace"
80 location:
81 description:
82 - Resource location.
83 type: str
84 returned: success
85 example: "eastus"
86 sku:
87 description:
88 - The SKU of the workspace.
89 type: str
90 returned: success
91 example: "per_gb2018"
92 retention_in_days:
93 description:
94 - The workspace data retention in days.
95 - -1 means Unlimited retention for I(sku=unlimited).
96 - 730 days is the maximum allowed for all other SKUs.
97 type: int
98 returned: success
99 example: 40
100 intelligence_packs:
101 description:
102 - Lists all the intelligence packs possible and whether they are enabled or disabled for a given workspace.
103 type: list
104 returned: success
105 example: [ {'name': 'CapacityPerformance', 'enabled': true} ]
106 management_groups:
107 description:
108 - Management groups connected to the workspace.
109 type: dict
110 returned: success
111 example: {'value': []}
112 shared_keys:
113 description:
114 - Shared keys for the workspace.
115 type: dict
116 returned: success
117 example: {
118 'primarySharedKey': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
119 'secondarySharedKey': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
120 }
121 usages:
122 description:
123 - Usage metrics for the workspace.
124 type: dict
125 returned: success
126 example: {
127 'value': [
128 {
129 'name': {
130 'value': 'DataAnalyzed',
131 'localizedValue': 'Data Analyzed'
132 },
133 'unit': 'Bytes',
134 'currentValue': 0,
135 'limit': 524288000,
136 'nextResetTime': '2017-10-03T00:00:00Z',
137 'quotaPeriod': 'P1D'
138 }
139 ]
140 }
141 ''' # NOQA
142
143 from ansible.module_utils.common.dict_transformations import _camel_to_snake
144
145 try:
146 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
147 from azure.core.exceptions import ResourceNotFoundError
148 from azure.mgmt.core.tools import parse_resource_id
149 except ImportError:
150 # This is handled in azure_rm_common
151 pass
152
153
154 class AzureRMLogAnalyticsWorkspaceInfo(AzureRMModuleBase):
155
156 def __init__(self):
157
158 self.module_arg_spec = dict(
159 resource_group=dict(type='str'),
160 name=dict(type='str'),
161 tags=dict(type='list', elements='str'),
162 show_shared_keys=dict(type='bool'),
163 show_intelligence_packs=dict(type='bool'),
164 show_usages=dict(type='bool'),
165 show_management_groups=dict(type='bool')
166 )
167
168 self.results = dict(
169 changed=False,
170 workspaces=[]
171 )
172
173 self.resource_group = None
174 self.name = None
175 self.tags = None
176 self.show_intelligence_packs = None
177 self.show_shared_keys = None
178 self.show_usages = None
179 self.show_management_groups = None
180
181 super(AzureRMLogAnalyticsWorkspaceInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
182
183 def exec_module(self, **kwargs):
184
185 for key in list(self.module_arg_spec.keys()):
186 setattr(self, key, kwargs[key])
187
188 if self.name:
189 if self.resource_group:
190 item = self.get_workspace()
191 response = [item] if item else []
192 else:
193 self.fail('The resource_group must be configured when name exists')
194 elif self.resource_group:
195 response = self.list_by_resource_group()
196 else:
197 response = self.list()
198
199 self.results['workspaces'] = [self.to_dict(x) for x in response if self.has_tags(x.tags, self.tags)]
200 return self.results
201
202 def get_workspace(self):
203 try:
204 return self.log_analytics_client.workspaces.get(self.resource_group, self.name)
205 except ResourceNotFoundError:
206 pass
207 return None
208
209 def list_by_resource_group(self):
210 try:
211 return self.log_analytics_client.workspaces.list_by_resource_group(self.resource_group)
212 except Exception as ec:
213 pass
214 return []
215
216 def list(self):
217 try:
218 return self.log_analytics_client.workspaces.list()
219 except Exception:
220 pass
221 return []
222
223 def list_intelligence_packs(self, resource_group, name):
224 try:
225 response = self.log_analytics_client.intelligence_packs.list(resource_group, name)
226 return [x.as_dict() for x in response]
227 except Exception as exc:
228 self.fail('Error when listing intelligence packs {0}'.format(exc.message or str(exc)))
229
230 def list_management_groups(self, resource_group, name):
231 result = []
232 try:
233 response = self.log_analytics_client.management_groups.list(resource_group, name)
234 while True:
235 result.append(response.next().as_dict())
236 except StopIteration:
237 pass
238 except Exception as exc:
239 self.fail('Error when listing management groups {0}'.format(exc.message or str(exc)))
240 return result
241
242 def list_usages(self, resource_group, name):
243 result = []
244 try:
245 response = self.log_analytics_client.usages.list(resource_group, name)
246 while True:
247 result.append(response.next().as_dict())
248 except StopIteration:
249 pass
250 except Exception as exc:
251 self.fail('Error when listing usages {0}'.format(exc.message or str(exc)))
252 return result
253
254 def get_shared_keys(self, resource_group, name):
255 try:
256 return self.log_analytics_client.shared_keys.get_shared_keys(resource_group, name).as_dict()
257 except Exception as exc:
258 self.fail('Error when getting shared key {0}'.format(exc.message or str(exc)))
259
260 def to_dict(self, workspace):
261 result = workspace.as_dict()
262 result['sku'] = _camel_to_snake(workspace.sku.name)
263 result['resource_group'] = parse_resource_id(result['id'])['resource_group']
264 if self.show_intelligence_packs:
265 result['intelligence_packs'] = self.list_intelligence_packs(result['resource_group'], result['name'])
266 if self.show_management_groups:
267 result['management_groups'] = self.list_management_groups(result['resource_group'], result['name'])
268 if self.show_shared_keys:
269 result['shared_keys'] = self.get_shared_keys(result['resource_group'], result['name'])
270 if self.show_usages:
271 result['usages'] = self.list_usages(result['resource_group'], result['name'])
272 return result
273
274
275 def main():
276 AzureRMLogAnalyticsWorkspaceInfo()
277
278
279 if __name__ == '__main__':
280 main()