| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_sqlelasticpool |
| 14 |
version_added: "1.14.0" |
| 15 |
short_description: Manage SQL Elastic Pool instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of SQL Elastic Pool. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group that contains the resource. |
| 23 |
- You can obtain this value from the Azure Resource Manager API or the portal. |
| 24 |
required: True |
| 25 |
type: str |
| 26 |
server_name: |
| 27 |
description: |
| 28 |
- The name of the server. |
| 29 |
required: True |
| 30 |
type: str |
| 31 |
name: |
| 32 |
description: |
| 33 |
- The name of the elastic pool to be operated on (updated or created). |
| 34 |
required: True |
| 35 |
type: str |
| 36 |
location: |
| 37 |
description: |
| 38 |
- Resource location. If not set, location from the resource group will be used as default. |
| 39 |
type: str |
| 40 |
sku: |
| 41 |
description: |
| 42 |
- The sku of the elastic pool. The Elastic PoolEditions enumeration contains all the valid sku. |
| 43 |
type: dict |
| 44 |
suboptions: |
| 45 |
name: |
| 46 |
description: |
| 47 |
- Name of the elastic pool SKU, typically, a letter + Number code, e.g. P3 |
| 48 |
required: True |
| 49 |
type: str |
| 50 |
tier: |
| 51 |
description: |
| 52 |
- The tier or edition of the particular SKU, e.g. Basic, Premium |
| 53 |
type: str |
| 54 |
capacity: |
| 55 |
description: |
| 56 |
- Capacity of the particular SKU. |
| 57 |
type: int |
| 58 |
size: |
| 59 |
description: |
| 60 |
- Size of the particular SKU |
| 61 |
type: str |
| 62 |
family: |
| 63 |
description: |
| 64 |
- If the service has different generations of hardware, for the same SKU, then that can be used here |
| 65 |
type: str |
| 66 |
max_size_bytes: |
| 67 |
description: |
| 68 |
- The max size of the elasticpool expressed in bytes. |
| 69 |
- If not I(create_mode=default), this value is ignored. |
| 70 |
- To see possible values, query the capabilities API (/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationID}/capabilities). |
| 71 |
referred to by operationId:'Capabilities_ListByLocation'. |
| 72 |
type: str |
| 73 |
zone_redundant: |
| 74 |
description: |
| 75 |
- Is this elasticpool is zone redundant? It means the replicas of this elasticpool will be spread across multiple availability zones. |
| 76 |
type: bool |
| 77 |
default: False |
| 78 |
per_elasticpool_settings: |
| 79 |
description: |
| 80 |
- The per database settings for the elastic pool. |
| 81 |
type: dict |
| 82 |
suboptions: |
| 83 |
min_capacity: |
| 84 |
description: |
| 85 |
- The minimum capacity all databases are guaranteed. |
| 86 |
type: float |
| 87 |
max_capacity: |
| 88 |
description: |
| 89 |
- The maximum capacity all databases are guaranteed. |
| 90 |
type: float |
| 91 |
license_type: |
| 92 |
description: |
| 93 |
- The license type to apply for this elastic pool. |
| 94 |
type: str |
| 95 |
default: LicenseIncluded |
| 96 |
choices: |
| 97 |
- LicenseIncluded |
| 98 |
- BasePrice |
| 99 |
maintenance_configuration_id: |
| 100 |
description: |
| 101 |
- Maintenance configuration id assigned to the elastic pool. |
| 102 |
type: str |
| 103 |
state: |
| 104 |
description: |
| 105 |
- Assert the state of the SQL Elastic Pool. Use C(present) to create or update an SQL Elastic Pool and C(absent) to delete it. |
| 106 |
default: present |
| 107 |
type: str |
| 108 |
choices: |
| 109 |
- absent |
| 110 |
- present |
| 111 |
|
| 112 |
extends_documentation_fragment: |
| 113 |
- azure.azcollection.azure |
| 114 |
- azure.azcollection.azure_tags |
| 115 |
|
| 116 |
author: |
| 117 |
- xuzhang3 (@xuzhang3) |
| 118 |
- Fred Sun (@Fred-sun) |
| 119 |
''' |
| 120 |
|
| 121 |
EXAMPLES = ''' |
| 122 |
- name: Create (or update) SQL Elastic Pool |
| 123 |
azure_rm_elastic_pool: |
| 124 |
resource_group: myResourceGroup |
| 125 |
server_name: sqlcrudtest-5961 |
| 126 |
name: testEP |
| 127 |
zone_redundant: false |
| 128 |
sku: |
| 129 |
name: GP_Gen5 |
| 130 |
family: Gen5 |
| 131 |
capacity: 3 |
| 132 |
tags: |
| 133 |
key1: value1 |
| 134 |
|
| 135 |
- name: Delete SQL Elastic Pool |
| 136 |
azure_rm_elastic_pool: |
| 137 |
resource_group: myResourceGroup |
| 138 |
server_name: sqlcrudtest-5961 |
| 139 |
name: testEP |
| 140 |
state: absent |
| 141 |
''' |
| 142 |
|
| 143 |
RETURN = ''' |
| 144 |
elastic_pool: |
| 145 |
description: |
| 146 |
- A list of dictionaries containing facts for SQL Elastic Pool. |
| 147 |
returned: always |
| 148 |
type: complex |
| 149 |
contains: |
| 150 |
id: |
| 151 |
description: |
| 152 |
- Resource ID. |
| 153 |
returned: always |
| 154 |
type: str |
| 155 |
sample: /subscriptions/xxx-xxx/resourceGroups/testrg/providers/Microsoft.Sql/servers/sqlsrvfredsqldb/elasticPools/fedelastic01 |
| 156 |
name: |
| 157 |
description: |
| 158 |
- Elastic Pool name. |
| 159 |
returned: always |
| 160 |
type: str |
| 161 |
sample: testEP |
| 162 |
location: |
| 163 |
description: |
| 164 |
- Resource location. |
| 165 |
returned: always |
| 166 |
type: str |
| 167 |
sample: eastus |
| 168 |
tags: |
| 169 |
description: |
| 170 |
- Resource tags. |
| 171 |
returned: always |
| 172 |
type: dict |
| 173 |
sample: { 'taga':'aaa', 'tagb':'bbb' } |
| 174 |
sku: |
| 175 |
description: |
| 176 |
- The name and tier of the SKU. |
| 177 |
returned: always |
| 178 |
type: complex |
| 179 |
contains: |
| 180 |
name: |
| 181 |
description: |
| 182 |
- The name of the SKU. |
| 183 |
returned: always |
| 184 |
type: str |
| 185 |
sample: GP_Gen5 |
| 186 |
tier: |
| 187 |
description: |
| 188 |
- The SKU tier. |
| 189 |
returned: always |
| 190 |
type: str |
| 191 |
sample: GeneralPurpose |
| 192 |
capacity: |
| 193 |
description: |
| 194 |
- The SKU capacity. |
| 195 |
returned: always |
| 196 |
type: int |
| 197 |
sample: 2 |
| 198 |
family: |
| 199 |
description: |
| 200 |
- If the service has different generations of hardware, for the same SKU, then that can be captured here. |
| 201 |
type: str |
| 202 |
returned: always |
| 203 |
sample: Gen5 |
| 204 |
size: |
| 205 |
description: |
| 206 |
- Size of the particular SKU. |
| 207 |
type: str |
| 208 |
returned: always |
| 209 |
sample: null |
| 210 |
zone_redundant: |
| 211 |
description: |
| 212 |
- Whether or not this database is zone redundant, which means the replicas of this database will be spread across multiple availability zones. |
| 213 |
returned: always |
| 214 |
type: bool |
| 215 |
sample: true |
| 216 |
license_type: |
| 217 |
description: |
| 218 |
- The license type to apply for this elastic pool. |
| 219 |
type: str |
| 220 |
returned: always |
| 221 |
sample: LicenseIncluded |
| 222 |
maintenance_configuration_id: |
| 223 |
description: |
| 224 |
- Maintenance configuration id assigned to the elastic pool. |
| 225 |
type: str |
| 226 |
returned: always |
| 227 |
sample: /subscriptions/xxx-xxx/providers/Microsoft.Maintenance/publicMaintenanceConfigurations/SQL_Default |
| 228 |
max_size_bytes: |
| 229 |
description: |
| 230 |
- The storage limit for the database elastic pool in bytes. |
| 231 |
type: str |
| 232 |
returned: always |
| 233 |
sample: 34359738368 |
| 234 |
per_database_settings: |
| 235 |
description: |
| 236 |
- The per database settings for the elastic pool. |
| 237 |
type: complex |
| 238 |
returned: always |
| 239 |
contains: |
| 240 |
min_capacity: |
| 241 |
description: |
| 242 |
- The minimum capacity all databases are guaranteed |
| 243 |
type: float |
| 244 |
returned: always |
| 245 |
sample: 0.0 |
| 246 |
max_capacity: |
| 247 |
description: |
| 248 |
- The maximum capacity any one database can consume. |
| 249 |
type: float |
| 250 |
returned: always |
| 251 |
sample: 2.0 |
| 252 |
''' |
| 253 |
|
| 254 |
import time |
| 255 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 256 |
|
| 257 |
try: |
| 258 |
from azure.core.exceptions import ResourceNotFoundError |
| 259 |
from azure.core.polling import LROPoller |
| 260 |
except ImportError: |
| 261 |
|
| 262 |
pass |
| 263 |
|
| 264 |
sku_spec = dict( |
| 265 |
name=dict(type='str', required=True), |
| 266 |
tier=dict(type='str'), |
| 267 |
size=dict(type='str'), |
| 268 |
family=dict(type='str'), |
| 269 |
capacity=dict(type='int') |
| 270 |
) |
| 271 |
|
| 272 |
|
| 273 |
per_elasticpool_settings_spec = dict( |
| 274 |
min_capacity=dict(type='float'), |
| 275 |
max_capacity=dict(type='float') |
| 276 |
) |
| 277 |
|
| 278 |
|
| 279 |
class Actions: |
| 280 |
NoAction, Create, Update, Delete = range(4) |
| 281 |
|
| 282 |
|
| 283 |
class AzureRMSqlElasticPool(AzureRMModuleBase): |
| 284 |
"""Configuration class for an Azure RM SQL Elastic Pool resource""" |
| 285 |
|
| 286 |
def __init__(self): |
| 287 |
self.module_arg_spec = dict( |
| 288 |
resource_group=dict( |
| 289 |
type='str', |
| 290 |
required=True |
| 291 |
), |
| 292 |
server_name=dict( |
| 293 |
type='str', |
| 294 |
required=True |
| 295 |
), |
| 296 |
name=dict( |
| 297 |
type='str', |
| 298 |
required=True |
| 299 |
), |
| 300 |
location=dict( |
| 301 |
type='str' |
| 302 |
), |
| 303 |
sku=dict( |
| 304 |
type='dict', |
| 305 |
options=sku_spec |
| 306 |
), |
| 307 |
max_size_bytes=dict( |
| 308 |
type='str' |
| 309 |
), |
| 310 |
zone_redundant=dict( |
| 311 |
type='bool', |
| 312 |
default=False |
| 313 |
), |
| 314 |
per_elasticpool_settings=dict( |
| 315 |
type='dict', |
| 316 |
options=per_elasticpool_settings_spec |
| 317 |
), |
| 318 |
maintenance_configuration_id=dict( |
| 319 |
type='str' |
| 320 |
), |
| 321 |
license_type=dict( |
| 322 |
type='str', |
| 323 |
default="LicenseIncluded", |
| 324 |
choices=["LicenseIncluded", "BasePrice"] |
| 325 |
), |
| 326 |
state=dict( |
| 327 |
type='str', |
| 328 |
default='present', |
| 329 |
choices=['present', 'absent'] |
| 330 |
) |
| 331 |
) |
| 332 |
|
| 333 |
self.resource_group = None |
| 334 |
self.server_name = None |
| 335 |
self.name = None |
| 336 |
self.location = None |
| 337 |
self.sku = None |
| 338 |
self.max_size_bytes = None |
| 339 |
self.zone_redundant = None |
| 340 |
self.per_elasticpool_settings = None |
| 341 |
self.maintenance_configuration_id = None |
| 342 |
self.body = dict() |
| 343 |
|
| 344 |
self.results = dict(changed=False) |
| 345 |
self.to_do = Actions.NoAction |
| 346 |
|
| 347 |
super(AzureRMSqlElasticPool, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 348 |
supports_check_mode=True, |
| 349 |
supports_tags=True) |
| 350 |
|
| 351 |
def exec_module(self, **kwargs): |
| 352 |
"""Main module execution method""" |
| 353 |
|
| 354 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 355 |
setattr(self, key, kwargs[key]) |
| 356 |
if key not in ['resource_group', 'server_name', 'name', 'state']: |
| 357 |
self.body[key] = kwargs[key] |
| 358 |
|
| 359 |
resource_group = self.get_resource_group(self.resource_group) |
| 360 |
if not self.location: |
| 361 |
|
| 362 |
self.location = resource_group.location |
| 363 |
|
| 364 |
old_response = None |
| 365 |
response = None |
| 366 |
|
| 367 |
old_response = self.get_elastic_pool() |
| 368 |
|
| 369 |
if not old_response: |
| 370 |
self.log("SQL Elastic Pool instance doesn't exist") |
| 371 |
if self.state == 'absent': |
| 372 |
self.log("Old instance didn't exist") |
| 373 |
else: |
| 374 |
self.to_do = Actions.Create |
| 375 |
else: |
| 376 |
self.log("SQL Elastic Pool instance already exists") |
| 377 |
if self.state == 'absent': |
| 378 |
self.to_do = Actions.Delete |
| 379 |
elif self.state == 'present': |
| 380 |
self.log( |
| 381 |
"Need to check if SQL Elastic Pool instance has to be deleted or may be updated") |
| 382 |
if self.per_elasticpool_settings is not None and (self.body['per_elasticpool_settings'] != old_response['per_elasticpool_settings']): |
| 383 |
self.to_do = Actions.Update |
| 384 |
if self.maintenance_configuration_id and (self.body['maintenance_configuration_id'] != old_response['maintenance_configuration_id']): |
| 385 |
self.to_do = Actions.Update |
| 386 |
if self.license_type is not None and (self.body['license_type'] != old_response['license_type']): |
| 387 |
self.to_do = Actions.Update |
| 388 |
if self.zone_redundant is not None and (bool(self.body['zone_redundant']) != bool(old_response['zone_redundant'])): |
| 389 |
self.to_do = Actions.Update |
| 390 |
if self.max_size_bytes is not None and (self.body['max_size_bytes'] != old_response['max_size_bytes']): |
| 391 |
self.to_do = Actions.Update |
| 392 |
if self.sku is not None: |
| 393 |
for key in self.sku.keys(): |
| 394 |
if self.sku[key] is not None and (self.sku[key] != old_response['sku'][key]): |
| 395 |
self.to_do = Actions.Update |
| 396 |
else: |
| 397 |
self.sku[key] = old_response['sku'][key] |
| 398 |
update_tags, newtags = self.update_tags(old_response.get('tags', dict())) |
| 399 |
if update_tags: |
| 400 |
self.body['tags'] = newtags |
| 401 |
self.to_do = Actions.Update |
| 402 |
|
| 403 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 404 |
self.log("Need to Create / Update the SQL Elastic Pool instance") |
| 405 |
|
| 406 |
self.results['changed'] = True |
| 407 |
|
| 408 |
if self.check_mode: |
| 409 |
return self.results |
| 410 |
|
| 411 |
if self.to_do == Actions.Create: |
| 412 |
self.body['location'] = self.location |
| 413 |
response = self.create_elastic_pool(self.body) |
| 414 |
else: |
| 415 |
response = self.update_elastic_pool(self.body) |
| 416 |
|
| 417 |
elif self.to_do == Actions.Delete: |
| 418 |
self.log("SQL Elastic Pool instance deleted") |
| 419 |
self.results['changed'] = True |
| 420 |
|
| 421 |
if self.check_mode: |
| 422 |
return self.results |
| 423 |
|
| 424 |
self.delete_elastic_pool() |
| 425 |
|
| 426 |
|
| 427 |
while self.get_elastic_pool(): |
| 428 |
time.sleep(20) |
| 429 |
else: |
| 430 |
self.log("SQL Elastic Pool instance unchanged") |
| 431 |
self.results['changed'] = False |
| 432 |
response = old_response |
| 433 |
|
| 434 |
self.results['elastic_pool'] = response |
| 435 |
|
| 436 |
return self.results |
| 437 |
|
| 438 |
def update_elastic_pool(self, parameters): |
| 439 |
''' |
| 440 |
Creates or updates SQL Elastic Pool with the specified configuration. |
| 441 |
|
| 442 |
:return: deserialized SQL Elastic Pool instance state dictionary |
| 443 |
''' |
| 444 |
self.log( |
| 445 |
"Creating / Updating the SQL Elastic Pool instance {0}".format(self.name)) |
| 446 |
|
| 447 |
try: |
| 448 |
response = self.sql_client.elastic_pools.begin_update(resource_group_name=self.resource_group, |
| 449 |
server_name=self.server_name, |
| 450 |
elastic_pool_name=self.name, |
| 451 |
parameters=parameters) |
| 452 |
if isinstance(response, LROPoller): |
| 453 |
response = self.get_poller_result(response) |
| 454 |
except Exception as exc: |
| 455 |
self.log('Error attempting to create the SQL Elastic Pool instance.') |
| 456 |
self.fail( |
| 457 |
"Error creating the SQL Elastic Pool instance: {0}".format(str(exc))) |
| 458 |
return self.format_item(response) |
| 459 |
|
| 460 |
def create_elastic_pool(self, parameters): |
| 461 |
''' |
| 462 |
Creates or updates SQL Elastic Pool with the specified configuration. |
| 463 |
|
| 464 |
:return: deserialized SQL Elastic Pool instance state dictionary |
| 465 |
''' |
| 466 |
self.log( |
| 467 |
"Creating / Updating the SQL Elastic Pool instance {0}".format(self.name)) |
| 468 |
|
| 469 |
try: |
| 470 |
response = self.sql_client.elastic_pools.begin_create_or_update(resource_group_name=self.resource_group, |
| 471 |
server_name=self.server_name, |
| 472 |
elastic_pool_name=self.name, |
| 473 |
parameters=parameters) |
| 474 |
if isinstance(response, LROPoller): |
| 475 |
response = self.get_poller_result(response) |
| 476 |
except Exception as exc: |
| 477 |
self.log('Error attempting to create the SQL Elastic Pool instance.') |
| 478 |
self.fail( |
| 479 |
"Error creating the SQL Elastic Pool instance: {0}".format(str(exc))) |
| 480 |
return self.format_item(response) |
| 481 |
|
| 482 |
def delete_elastic_pool(self): |
| 483 |
''' |
| 484 |
Deletes specified SQL Elastic Pool instance in the specified subscription and resource group. |
| 485 |
|
| 486 |
:return: True |
| 487 |
''' |
| 488 |
self.log("Deleting the SQL Elastic Pool instance {0}".format(self.name)) |
| 489 |
try: |
| 490 |
response = self.sql_client.elastic_pools.begin_delete(resource_group_name=self.resource_group, |
| 491 |
server_name=self.server_name, |
| 492 |
elastic_pool_name=self.name) |
| 493 |
if isinstance(response, LROPoller): |
| 494 |
response = self.get_poller_result(response) |
| 495 |
except Exception as e: |
| 496 |
self.log('Error attempting to delete the SQL Elastic Pool instance.') |
| 497 |
self.fail( |
| 498 |
"Error deleting the SQL Elastic Pool instance: {0}".format(str(e))) |
| 499 |
|
| 500 |
return True |
| 501 |
|
| 502 |
def get_elastic_pool(self): |
| 503 |
''' |
| 504 |
Gets the properties of the specified SQL Elastic Pool. |
| 505 |
|
| 506 |
:return: deserialized SQL Elastic Pool instance state dictionary |
| 507 |
''' |
| 508 |
found = False |
| 509 |
try: |
| 510 |
response = self.sql_client.elastic_pools.get(resource_group_name=self.resource_group, |
| 511 |
server_name=self.server_name, |
| 512 |
elastic_pool_name=self.name) |
| 513 |
found = True |
| 514 |
except ResourceNotFoundError: |
| 515 |
self.log('Did not find the SQL Elastic Pool instance.') |
| 516 |
if found is True: |
| 517 |
return self.format_item(response) |
| 518 |
|
| 519 |
return False |
| 520 |
|
| 521 |
def format_item(self, item): |
| 522 |
if not item: |
| 523 |
return None |
| 524 |
|
| 525 |
d = dict( |
| 526 |
resource_group=self.resource_group, |
| 527 |
id=item.id, |
| 528 |
name=item.name, |
| 529 |
location=item.location, |
| 530 |
tags=item.tags, |
| 531 |
max_size_bytes=item.max_size_bytes, |
| 532 |
zone_redundant=item.zone_redundant, |
| 533 |
license_type=item.license_type, |
| 534 |
maintenance_configuration_id=item.maintenance_configuration_id, |
| 535 |
per_database_settings=dict(), |
| 536 |
sku=dict() |
| 537 |
) |
| 538 |
|
| 539 |
if item.sku is not None: |
| 540 |
d['sku']['name'] = item.sku.name |
| 541 |
d['sku']['tier'] = item.sku.tier |
| 542 |
d['sku']['size'] = item.sku.size |
| 543 |
d['sku']['family'] = item.sku.family |
| 544 |
d['sku']['capacity'] = item.sku.capacity |
| 545 |
if item.per_database_settings is not None: |
| 546 |
d['per_database_settings']['min_capacity'] = item.per_database_settings.min_capacity |
| 547 |
d['per_database_settings']['max_capacity'] = item.per_database_settings.max_capacity |
| 548 |
|
| 549 |
return d |
| 550 |
|
| 551 |
|
| 552 |
def main(): |
| 553 |
"""Main execution""" |
| 554 |
AzureRMSqlElasticPool() |
| 555 |
|
| 556 |
|
| 557 |
if __name__ == '__main__': |
| 558 |
main() |