Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_cosmosdbaccount_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_cosmosdbaccount_info
14 version_added: "0.1.2"
15 short_description: Get Azure Cosmos DB Account facts
16 description:
17 - Get facts of Azure Cosmos DB Account.
18
19 options:
20 resource_group:
21 description:
22 - Name of an Azure resource group.
23 type: str
24 name:
25 description:
26 - Cosmos DB database account 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 retrieve_keys:
34 description:
35 - Retrieve keys and connection strings.
36 type: str
37 choices:
38 - all
39 - readonly
40 retrieve_connection_strings:
41 description:
42 - Retrieve connection strings.
43 type: bool
44
45 extends_documentation_fragment:
46 - azure.azcollection.azure
47
48 author:
49 - Zim Kalinowski (@zikalino)
50
51 '''
52
53 EXAMPLES = '''
54 - name: Get instance of Database Account
55 azure_rm_cosmosdbaccount_info:
56 resource_group: myResourceGroup
57 name: testaccount
58
59 - name: List instances of Database Account
60 azure_rm_cosmosdbaccount_info:
61 resource_group: myResourceGroup
62 tags:
63 - key
64 - key:value
65 '''
66
67 RETURN = '''
68 accounts:
69 description: A list of dictionaries containing facts for Database Account.
70 returned: always
71 type: complex
72 contains:
73 id:
74 description:
75 - The unique resource identifier of the database account.
76 returned: always
77 type: str
78 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DocumentDB/databaseAccount
79 s/testaccount"
80 resource_group:
81 description:
82 - Name of an Azure resource group.
83 returned: always
84 type: str
85 sample: myResourceGroup
86 name:
87 description:
88 - The name of the database account.
89 returned: always
90 type: str
91 sample: testaccount
92 location:
93 description:
94 - The location of the resource group to which the resource belongs.
95 returned: always
96 type: str
97 sample: westus
98 kind:
99 description:
100 - Indicates the type of database account.
101 returned: always
102 type: str
103 sample: global_document_db
104 consistency_policy:
105 description:
106 - Consistency policy.
107 returned: always
108 type: complex
109 contains:
110 default_consistency_level:
111 description:
112 - Default consistency level.
113 returned: always
114 type: str
115 sample: session
116 max_interval_in_seconds:
117 description:
118 - Maximum interval in seconds.
119 returned: always
120 type: int
121 sample: 5
122 max_staleness_prefix:
123 description:
124 - Maximum staleness prefix.
125 returned: always
126 type: int
127 sample: 100
128 failover_policies:
129 description:
130 - The list of new failover policies for the failover priority change.
131 returned: always
132 type: complex
133 contains:
134 name:
135 description:
136 - Location name.
137 returned: always
138 type: str
139 sample: eastus
140 failover_priority:
141 description:
142 - Failover priority.
143 returned: always
144 type: int
145 sample: 0
146 id:
147 description:
148 - Read location ID.
149 returned: always
150 type: str
151 sample: testaccount-eastus
152 read_locations:
153 description:
154 - Read locations.
155 returned: always
156 type: complex
157 contains:
158 name:
159 description:
160 - Location name.
161 returned: always
162 type: str
163 sample: eastus
164 failover_priority:
165 description:
166 - Failover priority.
167 returned: always
168 type: int
169 sample: 0
170 id:
171 description:
172 - Read location ID.
173 returned: always
174 type: str
175 sample: testaccount-eastus
176 document_endpoint:
177 description:
178 - Document endpoint.
179 returned: always
180 type: str
181 sample: https://testaccount-eastus.documents.azure.com:443/
182 provisioning_state:
183 description:
184 - Provisioning state.
185 returned: always
186 type: str
187 sample: Succeeded
188 write_locations:
189 description:
190 - Write locations.
191 returned: always
192 type: complex
193 contains:
194 name:
195 description:
196 - Location name.
197 returned: always
198 type: str
199 sample: eastus
200 failover_priority:
201 description:
202 - Failover priority.
203 returned: always
204 type: int
205 sample: 0
206 id:
207 description:
208 - Read location ID.
209 returned: always
210 type: str
211 sample: testaccount-eastus
212 document_endpoint:
213 description:
214 - Document endpoint.
215 returned: always
216 type: str
217 sample: https://testaccount-eastus.documents.azure.com:443/
218 provisioning_state:
219 description:
220 - Provisioning state.
221 returned: always
222 type: str
223 sample: Succeeded
224 database_account_offer_type:
225 description:
226 - Offer type.
227 returned: always
228 type: str
229 sample: Standard
230 enable_free_tier:
231 description:
232 - If enabled the account is free-tier.
233 returned: always
234 type: bool
235 sample: true
236 version_added: "1.10.0"
237 ip_range_filter:
238 description:
239 - (deprecated) Enabled IP range filter.
240 - This value has been deprecated, and will be removed in a later version. Use c(ip_rules) instead.
241 returned: always
242 type: str
243 sample: 10.10.10.10
244 ip_rules:
245 description:
246 - The IP addresses or IP address ranges in CIDR form included as the allowed list of client IPs.
247 returned: always
248 type: list
249 sample: ["10.10.10.10", "20.20.20.20/28"]
250 version_added: "1.10.0"
251 is_virtual_network_filter_enabled:
252 description:
253 - Enable virtual network filter.
254 returned: always
255 type: bool
256 sample: true
257 enable_automatic_failover:
258 description:
259 - Enable automatic failover.
260 returned: always
261 type: bool
262 sample: true
263 enable_cassandra:
264 description:
265 - Enable Cassandra.
266 returned: always
267 type: bool
268 sample: true
269 enable_table:
270 description:
271 - Enable Table.
272 returned: always
273 type: bool
274 sample: true
275 enable_gremlin:
276 description:
277 - Enable Gremlin.
278 returned: always
279 type: bool
280 sample: true
281 mongo_version:
282 description:
283 - Server version for the MongoDB account.
284 - Only used for c(kind) = i(mongo_db); otherwise value is null/none.
285 returned: always
286 type: str
287 sample: "4.0"
288 version_added: "1.10.0"
289 public_network_access:
290 description:
291 - If public network access is allowed to the server.
292 returned: always
293 type: str
294 sample: Enabled
295 version_added: "1.10.0"
296 virtual_network_rules:
297 description:
298 - List of Virtual Network ACL rules configured for the Cosmos DB account.
299 type: list
300 contains:
301 subnet:
302 description:
303 - Resource id of a subnet.
304 type: str
305 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNet
306 works/testvnet/subnets/testsubnet1"
307 ignore_missing_v_net_service_endpoint:
308 description:
309 - Create Cosmos DB account without existing virtual network service endpoint.
310 type: bool
311 enable_multiple_write_locations:
312 description:
313 - Enable multiple write locations.
314 returned: always
315 type: bool
316 sample: true
317 document_endpoint:
318 description:
319 - Document endpoint.
320 returned: always
321 type: str
322 sample: https://testaccount.documents.azure.com:443/
323 provisioning_state:
324 description:
325 - Provisioning state of Cosmos DB.
326 returned: always
327 type: str
328 sample: Succeeded
329 primary_master_key:
330 description:
331 - Primary master key.
332 returned: when requested
333 type: str
334 sample: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
335 secondary_master_key:
336 description:
337 - Primary master key.
338 returned: when requested
339 type: str
340 sample: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
341 primary_readonly_master_key:
342 description:
343 - Primary master key.
344 returned: when requested
345 type: str
346 sample: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
347 secondary_readonly_master_key:
348 description:
349 - Primary master key.
350 returned: when requested
351 type: str
352 sample: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
353 connection_strings:
354 description:
355 - List of connection strings.
356 type: list
357 returned: when requested
358 contains:
359 connection_string:
360 description:
361 - Description of connection string.
362 type: str
363 returned: always
364 sample: Primary SQL Connection String
365 description:
366 description:
367 - Connection string.
368 type: str
369 returned: always
370 sample: "AccountEndpoint=https://testaccount.documents.azure.com:443/;AccountKey=xxxxx"
371 tags:
372 description:
373 - Tags assigned to the resource. Dictionary of "string":"string" pairs.
374 returned: always
375 type: dict
376 sample: { "tag1":"abc" }
377 '''
378
379 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
380 from ansible.module_utils.common.dict_transformations import _camel_to_snake
381
382 try:
383 from azure.core.exceptions import ResourceNotFoundError
384 from azure.mgmt.cosmosdb import CosmosDBManagementClient
385 except ImportError:
386 # This is handled in azure_rm_common
387 pass
388
389
390 class AzureRMCosmosDBAccountInfo(AzureRMModuleBase):
391 def __init__(self):
392 # define user inputs into argument
393 self.module_arg_spec = dict(
394 resource_group=dict(
395 type='str'
396 ),
397 name=dict(
398 type='str'
399 ),
400 tags=dict(
401 type='list',
402 elements='str'
403 ),
404 retrieve_keys=dict(
405 type='str',
406 choices=['all', 'readonly']
407 ),
408 retrieve_connection_strings=dict(
409 type='bool'
410 )
411 )
412 # store the results of the module operation
413 self.results = dict(
414 changed=False
415 )
416 self.mgmt_client = None
417 self.resource_group = None
418 self.name = None
419 self.tags = None
420 self.retrieve_keys = None
421 self.retrieve_connection_strings = None
422
423 super(AzureRMCosmosDBAccountInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
424
425 def exec_module(self, **kwargs):
426
427 for key in self.module_arg_spec:
428 setattr(self, key, kwargs[key])
429 self.mgmt_client = self.get_mgmt_svc_client(CosmosDBManagementClient,
430 base_url=self._cloud_environment.endpoints.resource_manager)
431
432 if self.name is not None:
433 self.results['accounts'] = self.get()
434 elif self.resource_group is not None:
435 self.results['accounts'] = self.list_by_resource_group()
436 else:
437 self.results['accounts'] = self.list_all()
438 return self.results
439
440 def get(self):
441 response = None
442 results = []
443 try:
444 response = self.mgmt_client.database_accounts.get(resource_group_name=self.resource_group,
445 account_name=self.name)
446 self.log("Response : {0}".format(response))
447 except ResourceNotFoundError as e:
448 self.log('Could not get facts for Database Account.')
449
450 if response and self.has_tags(response.tags, self.tags):
451 results.append(self.format_response(response))
452
453 return results
454
455 def list_by_resource_group(self):
456 response = None
457 results = []
458 try:
459 response = self.mgmt_client.database_accounts.list_by_resource_group(resource_group_name=self.resource_group)
460 self.log("Response : {0}".format(response))
461 except Exception as e:
462 self.log('Could not get facts for Database Account.')
463
464 if response is not None:
465 for item in response:
466 if self.has_tags(item.tags, self.tags):
467 results.append(self.format_response(item))
468
469 return results
470
471 def list_all(self):
472 response = None
473 results = []
474 try:
475 response = self.mgmt_client.database_accounts.list()
476 self.log("Response : {0}".format(response))
477 except Exception as e:
478 self.log('Could not get facts for Database Account.')
479
480 if response is not None:
481 for item in response:
482 if self.has_tags(item.tags, self.tags):
483 results.append(self.format_response(item))
484
485 return results
486
487 def format_response(self, item):
488 d = item.as_dict()
489 d = {
490 'id': d.get('id'),
491 'resource_group': self.parse_resource_to_dict(d.get('id')).get('resource_group'),
492 'name': d.get('name', None),
493 'location': d.get('location', '').replace(' ', '').lower(),
494 'kind': _camel_to_snake(d.get('kind', None)),
495 'consistency_policy': {'default_consistency_level': _camel_to_snake(d['consistency_policy']['default_consistency_level']),
496 'max_interval_in_seconds': d['consistency_policy']['max_interval_in_seconds'],
497 'max_staleness_prefix': d['consistency_policy']['max_staleness_prefix']},
498 'failover_policies': [{'name': fp['location_name'].replace(' ', '').lower(),
499 'failover_priority': fp['failover_priority'],
500 'id': fp['id']} for fp in d['failover_policies']],
501 'read_locations': [{'name': rl['location_name'].replace(' ', '').lower(),
502 'failover_priority': rl['failover_priority'],
503 'id': rl['id'],
504 'document_endpoint': rl['document_endpoint'],
505 'provisioning_state': rl['provisioning_state']} for rl in d['read_locations']],
506 'write_locations': [{'name': wl['location_name'].replace(' ', '').lower(),
507 'failover_priority': wl['failover_priority'],
508 'id': wl['id'],
509 'document_endpoint': wl['document_endpoint'],
510 'provisioning_state': wl['provisioning_state']} for wl in d['write_locations']],
511 'database_account_offer_type': d.get('database_account_offer_type'),
512 'enable_free_tier': d.get('enable_free_tier'),
513 'ip_rules': [ip['ip_address_or_range'] for ip in d.get('ip_rules', [])],
514 'ip_range_filter': ",".join([ip['ip_address_or_range'] for ip in d.get('ip_rules', [])]),
515 'is_virtual_network_filter_enabled': d.get('is_virtual_network_filter_enabled'),
516 'enable_automatic_failover': d.get('enable_automatic_failover'),
517 'enable_cassandra': 'EnableCassandra' in d.get('capabilities', []),
518 'enable_table': 'EnableTable' in d.get('capabilities', []),
519 'enable_gremlin': 'EnableGremlin' in d.get('capabilities', []),
520 'mongo_version': d.get('api_properties', {}).get('server_version'),
521 'public_network_access': d.get('public_network_access'),
522 'virtual_network_rules': d.get('virtual_network_rules'),
523 'enable_multiple_write_locations': d.get('enable_multiple_write_locations'),
524 'document_endpoint': d.get('document_endpoint'),
525 'provisioning_state': d.get('provisioning_state'),
526 'tags': d.get('tags', None),
527 'identity': d.get('identity', None),
528 'default_identity': d.get('default_identity', None)
529 }
530
531 if self.retrieve_keys == 'all':
532 keys = self.mgmt_client.database_accounts.list_keys(resource_group_name=self.resource_group,
533 account_name=self.name)
534 d['primary_master_key'] = keys.primary_master_key
535 d['secondary_master_key'] = keys.secondary_master_key
536 d['primary_readonly_master_key'] = keys.primary_readonly_master_key
537 d['secondary_readonly_master_key'] = keys.secondary_readonly_master_key
538 elif self.retrieve_keys == 'readonly':
539 keys = self.mgmt_client.database_accounts.get_read_only_keys(resource_group_name=self.resource_group,
540 account_name=self.name)
541 d['primary_readonly_master_key'] = keys.primary_readonly_master_key
542 d['secondary_readonly_master_key'] = keys.secondary_readonly_master_key
543 if self.retrieve_connection_strings:
544 connection_strings = self.mgmt_client.database_accounts.list_connection_strings(resource_group_name=self.resource_group,
545 account_name=self.name)
546 d['connection_strings'] = connection_strings.as_dict()
547 return d
548
549
550 def main():
551 AzureRMCosmosDBAccountInfo()
552
553
554 if __name__ == '__main__':
555 main()