Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2022 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
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_sqlmanagedinstance
14 version_added: '1.14.0'
15 short_description: Manage SQL managed instances
16 description:
17 - Create, update, or delete SQL managed instances.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 required: True
25 name:
26 description:
27 - The name of the sql managed instance.
28 type: str
29 required: True
30 location:
31 description:
32 - The location of the sql managed instance.
33 type: str
34 sku:
35 description:
36 - An ARM Resource SKU.
37 type: dict
38 suboptions:
39 name:
40 description:
41 - The name of the SKU, typically, a letter add Number code.
42 type: str
43 tier:
44 description:
45 - The tier or edition of the particular SKU.
46 type: str
47 size:
48 description:
49 - Size of the particular SKU.
50 type: str
51 family:
52 description:
53 - If the service has different generations of hardware, for the same SKU, then that can be captured here.
54 type: str
55 capacity:
56 description:
57 - The capacity of the managed instance in integer number of vcores.
58 type: str
59 administrators:
60 description:
61 - The Azure Active Directory administrator of the server.
62 type: str
63 managed_instance_create_mode:
64 description:
65 - Specifies the mode of database creation.
66 type: str
67 administrator_login:
68 description:
69 - Administrator username for the managed instance.
70 - Can only be specified when the managed instance is being created (and is required for creation).
71 type: str
72 administrator_login_password:
73 description:
74 - The administrator login password (required for managed instance creation).
75 type: str
76 subnet_id:
77 description:
78 - Subnet resource ID for the managed instance.
79 type: str
80 license_type:
81 description:
82 - The license type.
83 - Possible values are C(LicenseIncluded) and C(BasePrice).
84 - Discounted AHB price for bringing your own SQL licenses.
85 - Regular price inclusive of a new SQL license.
86 type: str
87 choices:
88 - LicenseIncluded
89 - BasePrice
90 v_cores:
91 description:
92 - The number of vCores.
93 type: int
94 choices:
95 - 8
96 - 16
97 - 24
98 - 32
99 - 40
100 - 64
101 - 80
102 storage_size_in_gb:
103 description:
104 - Storage size in GB.
105 - Minimum value is C(32). Maximum value is C(8192).
106 - Increments of 32 GB allowed only.
107 type: int
108 collation:
109 description:
110 - Collation of the managed instance.
111 type: str
112 dns_zone:
113 description:
114 - The Dns Zone that the managed instance is in.
115 type: str
116 dns_zone_partner:
117 description:
118 - The resource ID of another managed instance whose DNS zone this managed instance will share after creation.
119 type: str
120 public_data_endpoint_enabled:
121 description:
122 - Whether or not the public data endpoint is enabled.
123 type: bool
124 source_managed_instance_id:
125 description:
126 - The resource identifier of the source managed instance associated with create operation of this instance.
127 type: str
128 restore_point_in_time:
129 description:
130 - Specifies the point in time (ISO8601 format) of the source database that will be restored to create the new database.
131 type: str
132 proxy_override:
133 description:
134 - Connection type used for connecting to the instance.
135 type: str
136 choices:
137 - Proxy
138 - Redirect
139 - Default
140 timezone_id:
141 description:
142 - ID of the timezone.
143 - Allowed values are timezones supported by Windows.
144 - Windows keeps details on supported timezones.
145 type: str
146 instance_pool_id:
147 description:
148 - The ID of the instance pool this managed server belongs to.
149 type: str
150 private_endpoint_connections:
151 description:
152 - List of private endpoint connections on a managed instance.
153 type: list
154 elements: str
155 maintenance_configuration_id:
156 description:
157 - Specifies maintenance configuration ID to apply to this managed instance.
158 type: str
159 minimal_tls_version:
160 description:
161 - Minimal TLS version. Allowed values C(None), C(1.0), C(1.1), C(1.2).
162 type: str
163 choices:
164 - 'None'
165 - '1.0'
166 - '1.1'
167 - '1.2'
168 storage_account_type:
169 description:
170 - The storage account type used to store backups for this instance.
171 type: str
172 zone_redundant:
173 description:
174 - Whether or not the multi-az is enabled.
175 type: bool
176 primary_user_assigned_identity_id:
177 description:
178 - The resource id of a user assigned identity to be used by default.
179 type: str
180 key_id:
181 description:
182 - A CMK URI of the key to use for encryption.
183 type: str
184 state:
185 description:
186 - State of the sql managed instance.
187 - Use C(present) to create or update a automation runbook and use C(absent) to delete.
188 type: str
189 default: present
190 choices:
191 - present
192 - absent
193
194 extends_documentation_fragment:
195 - azure.azcollection.azure
196 - azure.azcollection.azure_tags
197 - azure.azcollection.azure_identity_multiple
198
199 author:
200 - xuzhang3 (@xuzhang3)
201 - Fred Sun (@Fred-sun)
202 '''
203
204 EXAMPLES = '''
205 - name: Create sql managed instance
206 azure_rm_sqlmanagedinstance:
207 resource_group: "{{ resource_group }}"
208 name: testmanagedinstance
209 subnet_id: subnet_id
210 sku:
211 name: GP_Gen5
212 tier: GeneralPurpose
213 family: Gen5
214 capacity: 5
215 identity:
216 type: SystemAssigned
217 administrator_login: azureuser
218 administrator_login_password: "{{ password }}"
219 storage_size_in_gb: 256
220 v_cores: 8
221
222 - name: Delete sql managed instance
223 azure_rm_sqlmanagedinstance:
224 resource_group: "{{ resource_group }}"
225 name: testmanagedinstance
226 state: absent
227 '''
228
229 RETURN = '''
230 sql_managed_instance:
231 description:
232 - A list of dictionaries containing facts for SQL Managed Instance.
233 returned: always
234 type: complex
235 contains:
236 id:
237 description:
238 - Resource ID.
239 returned: always
240 type: str
241 sample: "/subscription/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Sql/managedInstances/fredsqlinstance"
242 name:
243 description:
244 - SQL managed instance name.
245 returned: always
246 type: str
247 sample: testmanagedinstance
248 location:
249 description:
250 - Resource location.
251 returned: always
252 type: str
253 sample: eastus
254 tags:
255 description:
256 - Resource tags.
257 returned: always
258 type: dict
259 sample: { 'taga':'aaa', 'tagb':'bbb' }
260 identity:
261 description:
262 - Azure Active Directory identity configuration for a resource.
263 returned: always
264 type: complex
265 contains:
266 principal_id:
267 description:
268 - The Azure Active Directory principal ID.
269 type: str
270 returned: always
271 sample: 895c-xxx-xxxbe
272 tenant_id:
273 description:
274 - The Azure Active Directory tenant ID.
275 type: str
276 returned: always
277 sample: 72fxxxxx-xxxx-xxxx-xxxx-xxxxxx11db47
278 type:
279 description:
280 - The identity type.
281 type: str
282 returned: always
283 sample: SystemAssigned
284 user_assigned_identities:
285 description:
286 - The resource ids of the user assigned identities to use.
287 type: str
288 returned: always
289 sample: null
290 sku:
291 description:
292 - An ARM Resource SKU.
293 returned: always
294 type: complex
295 contains:
296 name:
297 description:
298 - The name of the SKU.
299 returned: always
300 type: str
301 sample: BC_Gen4_2
302 tier:
303 description:
304 - The SKU tier.
305 returned: always
306 type: str
307 sample: BusinessCritical
308 capacity:
309 description:
310 - The SKU capacity.
311 returned: always
312 type: int
313 sample: 2
314 family:
315 description:
316 - If the service has different generations of hardware, for the same SKU, then that can be captured here.
317 type: str
318 returned: always
319 sample: Gen5
320 size:
321 description:
322 - Size of the particular SKU.
323 type: str
324 returned: always
325 sample: null
326 collation:
327 description:
328 - The collation of the SQL managed instance.
329 returned: always
330 type: str
331 sample: SQL_Latin1_General_CP1_CI_AS
332 administrator_login:
333 description:
334 - Administrator username for the managed instance.
335 type: str
336 returned: always
337 sample: azureuser
338 administrators:
339 description:
340 - The Azure Active Directory administrator of the server.
341 type: str
342 returned: always
343 sample: null
344 dns_zone:
345 description:
346 -The Dns Zone that the managed instance is in.
347 type: str
348 returned: always
349 sample: 8a23abba54cd
350 dns_zone_partner:
351 description:
352 - The resource ID of another managed instance whose DNS zone this managed instance will share after creation.
353 type: str
354 returned: always
355 sample: null
356 fully_qualified_domain_name:
357 description:
358 - The fully qualified domain name of the managed instance.
359 type: str
360 returned: always
361 sample: fredsqlinstance.8a23abba54cd.database.windows.net
362 instance_pool_id:
363 description:
364 - The ID of the instance pool this managed server belongs to.
365 type: str
366 returned: always
367 sample: null
368 key_id:
369 description:
370 - A CMK URI of the key to use for encryption.
371 type: str
372 returned: always
373 sample: null
374 license_type:
375 description:
376 - The license type.
377 type: str
378 returned: always
379 sample: LicenseIncluded
380 maintenance_configuration_id:
381 description:
382 - Specifies maintenance configuration ID to apply to this managed instance.
383 type: str
384 returned: always
385 sample: /subscriptions/xxx-xxxx/providers/Microsoft.Maintenance/publicMaintenanceConfigurations/SQL_Default
386 managed_instance_create_mode:
387 description:
388 - Specifies the mode of database creation.
389 type: str
390 returned: always
391 sample: null
392 minimal_tls_version:
393 description:
394 - Minimal TLS version. Allowed values 'None', '1.0', '1.1', '1.2'.
395 type: str
396 returned: always
397 sample: 1.2
398 primary_user_assigned_identity_id:
399 description:
400 - The resource id of a user assigned identity to be used by default.
401 type: str
402 returned: always
403 sample: null
404 private_endpoint_connections:
405 description:
406 - List of private endpoint connections on a managed instance.
407 type: list
408 returned: always
409 sample: []
410 provisioning_state:
411 description:
412 - The Status of the SQL managed instance.
413 type: str
414 returned: always
415 sample: Successed
416 proxy_override:
417 description:
418 - Connection type used for connecting to the instance.
419 type: str
420 returned: always
421 sample: Proxy
422 public_data_endpoint_enabled:
423 description:
424 - Whether or not the public data endpoint is enabled.
425 type: bool
426 returned: always
427 sample: false
428 restore_point_in_time:
429 description:
430 - Specifies the point in time (ISO8601 format) of the source database that will be restored to create the new database.
431 type: str
432 returned: always
433 sample: null
434 source_managed_instance_id:
435 description:
436 - The resource identifier of the source managed instance associated with create operation of this instance.
437 type: str
438 returned: always
439 sample: null
440 state:
441 description:
442 - The state of the managed instance.
443 type: str
444 returned: always
445 sample: Ready
446 storage_account_type:
447 description:
448 - The storage account type used to store backups for this instance.
449 type: str
450 returned: always
451 sample: GRS
452 storage_size_in_gb:
453 description:
454 - Storage size in GB. Minimum value 32. Maximum value 8192.
455 type: int
456 returned: always
457 sample: 256
458 subnet_id:
459 description:
460 - Subnet resource ID for the managed instance.
461 type: str
462 returned: always
463 sample: /subscriptions/xxx-xxxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/vnet-smi/subnets/sqi_sub
464 timezone_id:
465 description:
466 - Id of the timezone. Allowed values are timezones supported by Windows.
467 type: str
468 returned: always
469 sample: UTC
470 type:
471 description:
472 - The SQL managed instance type.
473 type: str
474 returned: always
475 sample: "Microsoft.Sql/managedInstances"
476 v_cores:
477 description:
478 - The number of vCores. Allowed values 8, 16, 24, 32, 40, 64, 80.
479 type: int
480 returned: always
481 sample: 8
482 zone_redundant:
483 description:
484 - Whether or not the multi-az is enabled.
485 type: bool
486 returned: always
487 sample: false
488 '''
489
490 # from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
491 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
492
493 try:
494 from azure.core.exceptions import ResourceNotFoundError
495 from azure.mgmt.sql.models import (ResourceIdentity, UserIdentity)
496 from azure.core.polling import LROPoller
497 except ImportError:
498 pass
499
500
501 sku_spec = dict(
502 name=dict(type='str'),
503 tier=dict(type='str'),
504 size=dict(type='str'),
505 family=dict(type='str'),
506 capacity=dict(type='str')
507 )
508
509
510 user_assigned_identities_spec = dict(
511 id=dict(
512 type='list',
513 default=[],
514 elements='str'
515 )
516 )
517
518
519 # class AzureRMSqlManagedInstance(AzureRMModuleBase):
520 class AzureRMSqlManagedInstance(AzureRMModuleBaseExt):
521 def __init__(self):
522 # define user inputs into argument
523 self.module_arg_spec = dict(
524 resource_group=dict(
525 type='str',
526 required=True
527 ),
528 name=dict(
529 type='str',
530 required=True
531 ),
532 location=dict(
533 type='str'
534 ),
535 subnet_id=dict(
536 type='str'
537 ),
538 identity=dict(
539 type='dict',
540 options=self.managed_identity_multiple_spec
541 ),
542 sku=dict(
543 type='dict',
544 options=sku_spec
545 ),
546 managed_instance_create_mode=dict(
547 type='str'
548 ),
549 administrator_login=dict(
550 type='str',
551 ),
552 administrator_login_password=dict(
553 type='str',
554 no_log=True,
555 ),
556 license_type=dict(
557 type='str',
558 choices=['LicenseIncluded', 'BasePrice']
559 ),
560 v_cores=dict(
561 type='int',
562 choices=[8, 16, 24, 32, 40, 64, 80]
563 ),
564 storage_size_in_gb=dict(
565 type='int'
566 ),
567 collation=dict(
568 type='str'
569 ),
570 dns_zone=dict(
571 type='str'
572 ),
573 dns_zone_partner=dict(
574 type='str'
575 ),
576 public_data_endpoint_enabled=dict(
577 type='bool'
578 ),
579 source_managed_instance_id=dict(
580 type='str'
581 ),
582 restore_point_in_time=dict(
583 type='str'
584 ),
585 proxy_override=dict(
586 type='str',
587 choices=['Proxy', 'Redirect', 'Default']
588 ),
589 timezone_id=dict(
590 type='str'
591 ),
592 instance_pool_id=dict(
593 type='str'
594 ),
595 maintenance_configuration_id=dict(
596 type='str'
597 ),
598 private_endpoint_connections=dict(
599 type='list',
600 elements='str'
601 ),
602 minimal_tls_version=dict(
603 type='str',
604 choices=['None', '1.0', '1.1', '1.2']
605 ),
606 storage_account_type=dict(
607 type='str'
608 ),
609 zone_redundant=dict(
610 type='bool'
611 ),
612 primary_user_assigned_identity_id=dict(
613 type='str'
614 ),
615 key_id=dict(
616 type='str'
617 ),
618 administrators=dict(
619 type='str'
620 ),
621 state=dict(
622 type='str',
623 choices=['present', 'absent'],
624 default='present'
625 )
626 )
627 # store the results of the module operation
628 self.results = dict(changed=False)
629 self.resource_group = None
630 self.name = None
631 self.location = None
632 self.state = None
633 self.identity = None
634 self.body = dict()
635 self._managed_identity = None
636
637 super(AzureRMSqlManagedInstance, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=True)
638
639 @property
640 def managed_identity(self):
641 if not self._managed_identity:
642 self._managed_identity = {"identity": ResourceIdentity,
643 "user_assigned": UserIdentity
644 }
645 return self._managed_identity
646
647 def exec_module(self, **kwargs):
648
649 for key in list(self.module_arg_spec) + ['tags']:
650 if hasattr(self, key):
651 setattr(self, key, kwargs[key])
652 elif kwargs[key] is not None:
653 self.body[key] = kwargs[key]
654
655 self.inflate_parameters(self.module_arg_spec, self.body, 0)
656
657 if not self.location:
658 resource_group = self.get_resource_group(self.resource_group)
659 self.location = resource_group.location
660 self.body['location'] = self.location
661
662 sql_managed_instance = self.get()
663
664 update_identity, identity = self.update_managed_identity(new_identity=self.identity or {},
665 curr_identity=sql_managed_instance and
666 sql_managed_instance.get('identity', {}) or {})
667 if update_identity:
668 self.body["identity"] = identity.as_dict()
669
670 changed = False
671 if self.state == 'present':
672 if sql_managed_instance:
673 modifiers = {}
674 self.create_compare_modifiers(self.module_arg_spec, '', modifiers)
675 self.results['modifiers'] = modifiers
676 self.results['compare'] = []
677 if not self.default_compare(modifiers, self.body, sql_managed_instance, '', self.results):
678 changed = True
679
680 if changed or update_identity:
681 if not self.check_mode:
682 # sql_managed_instance = self.update_sql_managed_instance(self.body)
683 sql_managed_instance = self.create_or_update(self.body)
684 else:
685 changed = True
686 if not self.check_mode:
687 sql_managed_instance = self.create_or_update(self.body)
688
689 else:
690 changed = True
691 if not self.check_mode:
692 sql_managed_instance = self.delete_sql_managed_instance()
693
694 self.results['changed'] = changed
695 self.results['state'] = sql_managed_instance
696 return self.results
697
698 def get(self):
699 try:
700 response = self.sql_client.managed_instances.get(self.resource_group, self.name)
701 return self.to_dict(response)
702 except ResourceNotFoundError:
703 pass
704
705 def update_sql_managed_instance(self, parameters):
706 try:
707 response = self.sql_client.managed_instances.begin_update(resource_group_name=self.resource_group,
708 managed_instance_name=self.name,
709 parameters=parameters)
710 if isinstance(response, LROPoller):
711 response = self.get_poller_result(response)
712 return self.to_dict(response)
713 except Exception as exc:
714 self.fail('Error when updating SQL managed instance {0}: {1}'.format(self.name, exc.message))
715
716 def create_or_update(self, parameters):
717 try:
718 response = self.sql_client.managed_instances.begin_create_or_update(resource_group_name=self.resource_group,
719 managed_instance_name=self.name,
720 parameters=parameters)
721 if isinstance(response, LROPoller):
722 response = self.get_poller_result(response)
723 return self.to_dict(response)
724 except Exception as exc:
725 self.fail('Error when creating SQL managed instance {0}: {1}'.format(self.name, exc))
726
727 def delete_sql_managed_instance(self):
728 try:
729 response = self.sql_client.managed_instances.begin_delete(self.resource_group, self.name)
730 if isinstance(response, LROPoller):
731 self.get_poller_result(response)
732
733 except Exception as exc:
734 self.fail('Error when deleting SQL managed instance {0}: {1}'.format(self.name, exc))
735
736 def to_dict(self, item):
737 if not item:
738 return None
739 d = item.as_dict()
740 d = {
741 'resource_group': self.resource_group,
742 'id': d.get('id', None),
743 'name': d.get('name', None),
744 'location': d.get('location', None),
745 'type': d.get('type', None),
746 'tags': d.get('tags', None),
747 'identity': {
748 'user_assigned_identities': d.get('identity', {}).get('user_assigned_identities', None),
749 'principal_id': d.get('identity', {}).get('principal_id', None),
750 'type': d.get('identity', {}).get('type', None),
751 'tenant_id': d.get('identity', {}).get('tenant_id', None)
752 },
753 'sku': {
754 'name': d.get('sku', {}).get('name', None),
755 'size': d.get('sku', {}).get('size', None),
756 'family': d.get('sku', {}).get('family', None),
757 'tier': d.get('sku', {}).get('tier', None),
758 'capacity': d.get('sku', {}).get('capacity', None)
759 },
760 'provisioning_state': d.get('provisioning_state', None),
761 'managed_instance_create_mode': d.get('managed_instance_create_mode', None),
762 'fully_qualified_domain_name': d.get('fully_qualified_domain_name', None),
763 'administrator_login': d.get('administrator_login', None),
764 'subnet_id': d.get('subnet_id', None),
765 'state': d.get('state', None),
766 'license_type': d.get('license_type', None),
767 'v_cores': d.get('v_cores', None),
768 'storage_size_in_gb': d.get('storage_size_in_gb', None),
769 'collation': d.get('collation', None),
770 'dns_zone': d.get('dns_zone', None),
771 'dns_zone_partner': d.get('dns_zone_partner', None),
772 'public_data_endpoint_enabled': d.get('public_data_endpoint_enabled', None),
773 'source_managed_instance_id': d.get('source_managed_instance_id', None),
774 'restore_point_in_time': d.get('restore_point_in_time', None),
775 'proxy_override': d.get('proxy_override', None),
776 'timezone_id': d.get('timezone_id', None),
777 'instance_pool_id': d.get('instance_pool_id', None),
778 'maintenance_configuration_id': d.get('maintenance_configuration_id', None),
779 'private_endpoint_connections': d.get('private_endpoint_connections', None),
780 'minimal_tls_version': d.get('minimal_tls_version', None),
781 'storage_account_type': d.get('storage_account_type', None),
782 'zone_redundant': d.get('zone_redundant', None),
783 'primary_user_assigned_identity_id': d.get('primary_user_assigned_identity_id', None),
784 'key_id': d.get('key_id', None),
785 'administrators': d.get('administrators', None)
786 }
787 return d
788
789
790 def main():
791 AzureRMSqlManagedInstance()
792
793
794 if __name__ == '__main__':
795 main()