| 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_cosmosdbaccount |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Database Account instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure Database Account. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- Name of an Azure resource group. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Cosmos DB database account name. |
| 28 |
required: True |
| 29 |
type: str |
| 30 |
location: |
| 31 |
description: |
| 32 |
- The location of the resource group to which the resource belongs. |
| 33 |
- Required when I(state=present). |
| 34 |
type: str |
| 35 |
kind: |
| 36 |
description: |
| 37 |
- Indicates the type of database account. This can only be set at database account creation. |
| 38 |
type: str |
| 39 |
choices: |
| 40 |
- 'global_document_db' |
| 41 |
- 'mongo_db' |
| 42 |
- 'parse' |
| 43 |
consistency_policy: |
| 44 |
description: |
| 45 |
- The consistency policy for the Cosmos DB account. |
| 46 |
type: dict |
| 47 |
suboptions: |
| 48 |
default_consistency_level: |
| 49 |
description: |
| 50 |
- The default consistency level and configuration settings of the Cosmos DB account. |
| 51 |
- Required when I(state=present). |
| 52 |
type: str |
| 53 |
choices: |
| 54 |
- 'eventual' |
| 55 |
- 'session' |
| 56 |
- 'bounded_staleness' |
| 57 |
- 'strong' |
| 58 |
- 'consistent_prefix' |
| 59 |
max_staleness_prefix: |
| 60 |
description: |
| 61 |
- When used with the Bounded Staleness consistency level, this value represents the number of stale requests tolerated. |
| 62 |
- Accepted range for this value is 1 - 2,147,483,647. Required when I(default_consistency_policy=bounded_staleness). |
| 63 |
type: int |
| 64 |
max_interval_in_seconds: |
| 65 |
description: |
| 66 |
- When used with the Bounded Staleness consistency level, this value represents the time amount of staleness (in seconds) tolerated. |
| 67 |
- Accepted range for this value is 5 - 86400. Required when I(default_consistency_policy=bounded_staleness). |
| 68 |
type: int |
| 69 |
geo_rep_locations: |
| 70 |
description: |
| 71 |
- An array that contains the georeplication locations enabled for the Cosmos DB account. |
| 72 |
- Required when I(state=present). |
| 73 |
type: list |
| 74 |
elements: dict |
| 75 |
suboptions: |
| 76 |
name: |
| 77 |
description: |
| 78 |
- The name of the region. |
| 79 |
type: str |
| 80 |
required: true |
| 81 |
failover_priority: |
| 82 |
description: |
| 83 |
- The failover priority of the region. A failover priority of 0 indicates a write region. |
| 84 |
- The maximum value for a failover priority = (total number of regions - 1). |
| 85 |
- Failover priority values must be unique for each of the regions in which the database account exists. |
| 86 |
type: int |
| 87 |
required: true |
| 88 |
database_account_offer_type: |
| 89 |
description: |
| 90 |
- Database account offer type, for example I(Standard) |
| 91 |
- Required when I(state=present). |
| 92 |
type: str |
| 93 |
enable_free_tier: |
| 94 |
description: |
| 95 |
- If enabled the account is free-tier. |
| 96 |
type: bool |
| 97 |
default: false |
| 98 |
version_added: "1.10.0" |
| 99 |
ip_range_filter: |
| 100 |
description: |
| 101 |
- (deprecated) Cosmos DB Firewall support. This value specifies the set of IP addresses or IP address ranges. |
| 102 |
- In CIDR form to be included as the allowed list of client IPs for a given database account. |
| 103 |
- IP addresses/ranges must be comma separated and must not contain any spaces. |
| 104 |
- This value has been deprecated, and will be removed in a later version. Use I(ip_rules) instead. |
| 105 |
type: str |
| 106 |
ip_rules: |
| 107 |
description: |
| 108 |
- The IP addresses or IP address ranges in CIDR form to be included as the allowed list of client IPs. |
| 109 |
type: list |
| 110 |
elements: str |
| 111 |
version_added: "1.10.0" |
| 112 |
is_virtual_network_filter_enabled: |
| 113 |
description: |
| 114 |
- Flag to indicate whether to enable/disable Virtual Network ACL rules. |
| 115 |
type: bool |
| 116 |
enable_automatic_failover: |
| 117 |
description: |
| 118 |
- Enables automatic failover of the write region in the rare event that the region is unavailable due to an outage. |
| 119 |
- Automatic failover will result in a new write region for the account and is chosen based on the failover priorities configured for the account. |
| 120 |
type: bool |
| 121 |
enable_cassandra: |
| 122 |
description: |
| 123 |
- Enable Cassandra. |
| 124 |
type: bool |
| 125 |
enable_table: |
| 126 |
description: |
| 127 |
- Enable Table. |
| 128 |
type: bool |
| 129 |
enable_gremlin: |
| 130 |
description: |
| 131 |
- Enable Gremlin. |
| 132 |
type: bool |
| 133 |
mongo_version: |
| 134 |
description: |
| 135 |
- Server version for the MongoDB account, such as c(3.2) or c(4.0). |
| 136 |
- Only used when c(kind) = i(mongo_db). |
| 137 |
type: str |
| 138 |
version_added: "1.10.0" |
| 139 |
public_network_access: |
| 140 |
description: |
| 141 |
- Enables or disables public network access to server. |
| 142 |
type: str |
| 143 |
default: Enabled |
| 144 |
choices: |
| 145 |
- Enabled |
| 146 |
- Disabled |
| 147 |
version_added: "1.10.0" |
| 148 |
virtual_network_rules: |
| 149 |
description: |
| 150 |
- List of Virtual Network ACL rules configured for the Cosmos DB account. |
| 151 |
type: list |
| 152 |
elements: dict |
| 153 |
suboptions: |
| 154 |
subnet: |
| 155 |
description: |
| 156 |
- It can be a string containing resource id of a subnet. |
| 157 |
- It can be a dictionary containing 'resource_group', 'virtual_network_name' and 'subnet_name' |
| 158 |
type: raw |
| 159 |
required: true |
| 160 |
ignore_missing_v_net_service_endpoint: |
| 161 |
description: |
| 162 |
- Create Cosmos DB account without existing virtual network service endpoint. |
| 163 |
type: bool |
| 164 |
enable_multiple_write_locations: |
| 165 |
description: |
| 166 |
- Enables the account to write in multiple locations |
| 167 |
type: bool |
| 168 |
default_identity: |
| 169 |
description: |
| 170 |
- The primary identity to access key vault in CMK related features. |
| 171 |
e.g. 'FirstPartyIdentity', 'SystemAssignedIdentity' and more. |
| 172 |
User-assigned identities are specified in format |
| 173 |
'UserAssignedIdentity=<resource ID of the user-assigned identity>'. |
| 174 |
type: str |
| 175 |
state: |
| 176 |
description: |
| 177 |
- Assert the state of the Database Account. |
| 178 |
- Use C(present) to create or update an Database Account and C(absent) to delete it. |
| 179 |
default: present |
| 180 |
type: str |
| 181 |
choices: |
| 182 |
- absent |
| 183 |
- present |
| 184 |
|
| 185 |
extends_documentation_fragment: |
| 186 |
- azure.azcollection.azure |
| 187 |
- azure.azcollection.azure_tags |
| 188 |
- azure.azcollection.azure_identity_multiple |
| 189 |
|
| 190 |
author: |
| 191 |
- Zim Kalinowski (@zikalino) |
| 192 |
|
| 193 |
''' |
| 194 |
|
| 195 |
EXAMPLES = ''' |
| 196 |
- name: Create Cosmos DB Account - min |
| 197 |
azure_rm_cosmosdbaccount: |
| 198 |
resource_group: myResourceGroup |
| 199 |
name: myDatabaseAccount |
| 200 |
location: westus |
| 201 |
geo_rep_locations: |
| 202 |
- name: southcentralus |
| 203 |
failover_priority: 0 |
| 204 |
database_account_offer_type: Standard |
| 205 |
|
| 206 |
- name: Create Cosmos DB Account - max |
| 207 |
azure_rm_cosmosdbaccount: |
| 208 |
resource_group: myResourceGroup |
| 209 |
name: myDatabaseAccount |
| 210 |
location: westus |
| 211 |
kind: mongo_db |
| 212 |
geo_rep_locations: |
| 213 |
- name: southcentralus |
| 214 |
failover_priority: 0 |
| 215 |
database_account_offer_type: Standard |
| 216 |
ip_rules: |
| 217 |
- 10.10.10.10 |
| 218 |
enable_multiple_write_locations: true |
| 219 |
virtual_network_rules: |
| 220 |
- subnet: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVi |
| 221 |
rtualNetwork/subnets/mySubnet" |
| 222 |
consistency_policy: |
| 223 |
default_consistency_level: bounded_staleness |
| 224 |
max_staleness_prefix: 10 |
| 225 |
max_interval_in_seconds: 1000 |
| 226 |
''' |
| 227 |
|
| 228 |
RETURN = ''' |
| 229 |
id: |
| 230 |
description: |
| 231 |
- The unique resource identifier of the database account. |
| 232 |
returned: always |
| 233 |
type: str |
| 234 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DocumentDB/databaseAccounts/myData |
| 235 |
baseAccount" |
| 236 |
''' |
| 237 |
|
| 238 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 239 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel |
| 240 |
|
| 241 |
try: |
| 242 |
from azure.core.polling import LROPoller |
| 243 |
from azure.core.exceptions import ResourceNotFoundError |
| 244 |
from azure.mgmt.cosmosdb import CosmosDBManagementClient |
| 245 |
from azure.mgmt.cosmosdb.models import ManagedServiceIdentity, ManagedServiceIdentityUserAssignedIdentity |
| 246 |
except ImportError: |
| 247 |
|
| 248 |
pass |
| 249 |
|
| 250 |
|
| 251 |
class Actions: |
| 252 |
NoAction, Create, Update, Delete = range(4) |
| 253 |
|
| 254 |
|
| 255 |
class AzureRMCosmosDBAccount(AzureRMModuleBaseExt): |
| 256 |
"""Configuration class for an Azure RM Database Account resource""" |
| 257 |
|
| 258 |
def __init__(self): |
| 259 |
self.module_arg_spec = dict( |
| 260 |
resource_group=dict( |
| 261 |
type='str', |
| 262 |
required=True |
| 263 |
), |
| 264 |
name=dict( |
| 265 |
type='str', |
| 266 |
required=True |
| 267 |
), |
| 268 |
location=dict( |
| 269 |
type='str' |
| 270 |
), |
| 271 |
kind=dict( |
| 272 |
type='str', |
| 273 |
choices=['global_document_db', |
| 274 |
'mongo_db', |
| 275 |
'parse'] |
| 276 |
), |
| 277 |
consistency_policy=dict( |
| 278 |
type='dict', |
| 279 |
options=dict( |
| 280 |
default_consistency_level=dict( |
| 281 |
type='str', |
| 282 |
choices=['eventual', |
| 283 |
'session', |
| 284 |
'bounded_staleness', |
| 285 |
'strong', |
| 286 |
'consistent_prefix'] |
| 287 |
), |
| 288 |
max_staleness_prefix=dict( |
| 289 |
type='int' |
| 290 |
), |
| 291 |
max_interval_in_seconds=dict( |
| 292 |
type='int' |
| 293 |
) |
| 294 |
) |
| 295 |
), |
| 296 |
geo_rep_locations=dict( |
| 297 |
type='list', |
| 298 |
elements='dict', |
| 299 |
options=dict( |
| 300 |
name=dict( |
| 301 |
type='str', |
| 302 |
required=True |
| 303 |
), |
| 304 |
failover_priority=dict( |
| 305 |
type='int', |
| 306 |
required=True |
| 307 |
) |
| 308 |
) |
| 309 |
), |
| 310 |
database_account_offer_type=dict( |
| 311 |
type='str' |
| 312 |
), |
| 313 |
enable_free_tier=dict( |
| 314 |
type='bool', |
| 315 |
default=False, |
| 316 |
), |
| 317 |
ip_range_filter=dict( |
| 318 |
type='str' |
| 319 |
), |
| 320 |
ip_rules=dict( |
| 321 |
type='list', |
| 322 |
elements='str', |
| 323 |
), |
| 324 |
is_virtual_network_filter_enabled=dict( |
| 325 |
type='bool' |
| 326 |
), |
| 327 |
enable_automatic_failover=dict( |
| 328 |
type='bool' |
| 329 |
), |
| 330 |
enable_cassandra=dict( |
| 331 |
type='bool' |
| 332 |
), |
| 333 |
enable_table=dict( |
| 334 |
type='bool' |
| 335 |
), |
| 336 |
enable_gremlin=dict( |
| 337 |
type='bool' |
| 338 |
), |
| 339 |
mongo_version=dict( |
| 340 |
type='str' |
| 341 |
), |
| 342 |
public_network_access=dict( |
| 343 |
type='str', |
| 344 |
default='Enabled', |
| 345 |
choices=['Enabled', 'Disabled'] |
| 346 |
), |
| 347 |
virtual_network_rules=dict( |
| 348 |
type='list', |
| 349 |
elements='dict', |
| 350 |
options=dict( |
| 351 |
subnet=dict( |
| 352 |
type='raw', |
| 353 |
required=True |
| 354 |
), |
| 355 |
ignore_missing_v_net_service_endpoint=dict( |
| 356 |
type='bool' |
| 357 |
) |
| 358 |
) |
| 359 |
), |
| 360 |
enable_multiple_write_locations=dict( |
| 361 |
type='bool' |
| 362 |
), |
| 363 |
identity=dict( |
| 364 |
type="dict", |
| 365 |
options=self.managed_identity_multiple_spec |
| 366 |
), |
| 367 |
default_identity=dict( |
| 368 |
type='str' |
| 369 |
), |
| 370 |
state=dict( |
| 371 |
type='str', |
| 372 |
default='present', |
| 373 |
choices=['present', 'absent'] |
| 374 |
) |
| 375 |
) |
| 376 |
|
| 377 |
self.resource_group = None |
| 378 |
self.name = None |
| 379 |
self.parameters = dict() |
| 380 |
|
| 381 |
self.results = dict(changed=False) |
| 382 |
self.mgmt_client = None |
| 383 |
self.state = None |
| 384 |
self.to_do = Actions.NoAction |
| 385 |
self.identity = None |
| 386 |
self._managed_identity = None |
| 387 |
|
| 388 |
super(AzureRMCosmosDBAccount, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 389 |
supports_check_mode=True, |
| 390 |
supports_tags=True) |
| 391 |
|
| 392 |
@property |
| 393 |
def managed_identity(self): |
| 394 |
if not self._managed_identity: |
| 395 |
self._managed_identity = { |
| 396 |
"identity": ManagedServiceIdentity, |
| 397 |
"user_assigned": ManagedServiceIdentityUserAssignedIdentity, |
| 398 |
} |
| 399 |
return self._managed_identity |
| 400 |
|
| 401 |
def exec_module(self, **kwargs): |
| 402 |
"""Main module execution method""" |
| 403 |
|
| 404 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 405 |
if hasattr(self, key): |
| 406 |
setattr(self, key, kwargs[key]) |
| 407 |
elif kwargs[key] is not None: |
| 408 |
self.parameters[key] = kwargs[key] |
| 409 |
|
| 410 |
kind = self.parameters.get('kind') |
| 411 |
if kind == 'global_document_db': |
| 412 |
self.parameters['kind'] = 'GlobalDocumentDB' |
| 413 |
elif kind == 'mongo_db': |
| 414 |
self.parameters['kind'] = 'MongoDB' |
| 415 |
elif kind == 'parse': |
| 416 |
self.parameters['kind'] = 'Parse' |
| 417 |
|
| 418 |
ip_range_filter = self.parameters.pop('ip_range_filter', None) |
| 419 |
ip_rules = self.parameters.pop('ip_rules', []) |
| 420 |
if ip_range_filter: |
| 421 |
self.parameters['ip_rules'] = [{"ip_address_or_range": ip} for ip in ip_range_filter.split(",")] |
| 422 |
if ip_rules: |
| 423 |
|
| 424 |
self.parameters['ip_rules'] = [{"ip_address_or_range": ip} for ip in ip_rules] |
| 425 |
|
| 426 |
dict_camelize(self.parameters, ['consistency_policy', 'default_consistency_level'], True) |
| 427 |
dict_rename(self.parameters, ['geo_rep_locations', 'name'], 'location_name') |
| 428 |
dict_rename(self.parameters, ['geo_rep_locations'], 'locations') |
| 429 |
self.parameters['capabilities'] = [] |
| 430 |
if self.parameters.pop('enable_cassandra', False): |
| 431 |
self.parameters['capabilities'].append({'name': 'EnableCassandra'}) |
| 432 |
if self.parameters.pop('enable_table', False): |
| 433 |
self.parameters['capabilities'].append({'name': 'EnableTable'}) |
| 434 |
if self.parameters.pop('enable_gremlin', False): |
| 435 |
self.parameters['capabilities'].append({'name': 'EnableGremlin'}) |
| 436 |
|
| 437 |
mongo_version = self.parameters.pop('mongo_version', None) |
| 438 |
if kind == 'mongo_db' and mongo_version is not None: |
| 439 |
self.parameters['api_properties'] = dict() |
| 440 |
self.parameters['api_properties']['server_version'] = mongo_version |
| 441 |
|
| 442 |
for rule in self.parameters.get('virtual_network_rules', []): |
| 443 |
subnet = rule.pop('subnet') |
| 444 |
if isinstance(subnet, dict): |
| 445 |
virtual_network_name = subnet.get('virtual_network_name') |
| 446 |
subnet_name = subnet.get('subnet_name') |
| 447 |
resource_group_name = subnet.get('resource_group', self.resource_group) |
| 448 |
template = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/virtualNetworks/{2}/subnets/{3}" |
| 449 |
subnet = template.format(self.subscription_id, resource_group_name, virtual_network_name, subnet_name) |
| 450 |
rule['id'] = subnet |
| 451 |
|
| 452 |
response = None |
| 453 |
|
| 454 |
self.mgmt_client = self.get_mgmt_svc_client(CosmosDBManagementClient, |
| 455 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 456 |
|
| 457 |
resource_group = self.get_resource_group(self.resource_group) |
| 458 |
|
| 459 |
if "location" not in self.parameters: |
| 460 |
self.parameters["location"] = resource_group.location |
| 461 |
|
| 462 |
old_response = self.get_databaseaccount() |
| 463 |
|
| 464 |
if self.identity: |
| 465 |
curr_identity = old_response['identity'] if old_response else None |
| 466 |
update_identity, new_identity = self.update_managed_identity(curr_identity=curr_identity, new_identity=self.identity, patch_support=True) |
| 467 |
if update_identity: |
| 468 |
self.parameters['identity'] = new_identity |
| 469 |
|
| 470 |
if not old_response: |
| 471 |
self.log("Database Account instance doesn't exist") |
| 472 |
if self.state == 'absent': |
| 473 |
self.log("Old instance didn't exist") |
| 474 |
else: |
| 475 |
self.to_do = Actions.Create |
| 476 |
else: |
| 477 |
self.log("Database Account instance already exists") |
| 478 |
if self.state == 'absent': |
| 479 |
self.to_do = Actions.Delete |
| 480 |
elif self.state == 'present': |
| 481 |
old_response['locations'] = old_response['failover_policies'] |
| 482 |
if not default_compare(self.parameters, old_response, '', self.results): |
| 483 |
self.to_do = Actions.Update |
| 484 |
|
| 485 |
if self.to_do == Actions.Create: |
| 486 |
self.log("Need to Create the Database Account instance") |
| 487 |
|
| 488 |
if self.check_mode: |
| 489 |
self.results['changed'] = True |
| 490 |
return self.results |
| 491 |
|
| 492 |
response = self.create_databaseaccount() |
| 493 |
|
| 494 |
self.results['changed'] = True |
| 495 |
self.log("Creation done") |
| 496 |
elif self.to_do == Actions.Update: |
| 497 |
self.log("Need to Update the Database Account instance") |
| 498 |
|
| 499 |
if self.check_mode: |
| 500 |
self.results['changed'] = True |
| 501 |
return self.results |
| 502 |
|
| 503 |
response = self.update_databaseaccount() |
| 504 |
|
| 505 |
self.results['changed'] = True |
| 506 |
self.log("Update done") |
| 507 |
elif self.to_do == Actions.Delete: |
| 508 |
self.log("Database Account instance deleted") |
| 509 |
self.results['changed'] = True |
| 510 |
|
| 511 |
if self.check_mode: |
| 512 |
return self.results |
| 513 |
|
| 514 |
self.delete_databaseaccount() |
| 515 |
else: |
| 516 |
self.log("Database Account instance unchanged") |
| 517 |
self.results['changed'] = False |
| 518 |
response = old_response |
| 519 |
|
| 520 |
if self.state == 'present': |
| 521 |
self.results.update({'id': response.get('id', None)}) |
| 522 |
return self.results |
| 523 |
|
| 524 |
def create_databaseaccount(self): |
| 525 |
''' |
| 526 |
Creates Database Account with the specified configuration. |
| 527 |
|
| 528 |
:return: deserialized Database Account instance state dictionary |
| 529 |
''' |
| 530 |
self.log("Creating the Database Account instance {0}".format(self.name)) |
| 531 |
|
| 532 |
try: |
| 533 |
response = self.mgmt_client.database_accounts.begin_create_or_update(resource_group_name=self.resource_group, |
| 534 |
account_name=self.name, |
| 535 |
create_update_parameters=self.parameters) |
| 536 |
if isinstance(response, LROPoller): |
| 537 |
response = self.get_poller_result(response) |
| 538 |
|
| 539 |
except Exception as exc: |
| 540 |
self.log('Error attempting to create the Database Account instance.') |
| 541 |
self.fail("Error creating the Database Account instance: {0}".format(str(exc))) |
| 542 |
return response.as_dict() |
| 543 |
|
| 544 |
def update_databaseaccount(self): |
| 545 |
''' |
| 546 |
Updates Database Account with the specified configuration. |
| 547 |
|
| 548 |
:return: deserialized Database Account instance state dictionary |
| 549 |
''' |
| 550 |
self.log("Updating the Database Account instance {0}".format(self.name)) |
| 551 |
|
| 552 |
try: |
| 553 |
response = self.mgmt_client.database_accounts.begin_update(resource_group_name=self.resource_group, |
| 554 |
account_name=self.name, |
| 555 |
update_parameters=self.parameters) |
| 556 |
if isinstance(response, LROPoller): |
| 557 |
response = self.get_poller_result(response) |
| 558 |
|
| 559 |
except Exception as exc: |
| 560 |
self.log('Error attempting to update the Database Account instance.') |
| 561 |
self.fail("Error updating the Database Account instance: {0}".format(str(exc))) |
| 562 |
return response.as_dict() |
| 563 |
|
| 564 |
def delete_databaseaccount(self): |
| 565 |
''' |
| 566 |
Deletes specified Database Account instance in the specified subscription and resource group. |
| 567 |
|
| 568 |
:return: True |
| 569 |
''' |
| 570 |
self.log("Deleting the Database Account instance {0}".format(self.name)) |
| 571 |
try: |
| 572 |
response = self.mgmt_client.database_accounts.begin_delete(resource_group_name=self.resource_group, |
| 573 |
account_name=self.name) |
| 574 |
|
| 575 |
if isinstance(response, LROPoller): |
| 576 |
response = self.get_poller_result(response) |
| 577 |
except Exception as e: |
| 578 |
self.log('Error attempting to delete the Database Account instance.') |
| 579 |
self.fail("Error deleting the Database Account instance: {0}".format(str(e))) |
| 580 |
|
| 581 |
return True |
| 582 |
|
| 583 |
def get_databaseaccount(self): |
| 584 |
''' |
| 585 |
Gets the properties of the specified Database Account. |
| 586 |
|
| 587 |
:return: deserialized Database Account instance state dictionary |
| 588 |
''' |
| 589 |
self.log("Checking if the Database Account instance {0} is present".format(self.name)) |
| 590 |
found = False |
| 591 |
try: |
| 592 |
response = self.mgmt_client.database_accounts.get(resource_group_name=self.resource_group, |
| 593 |
account_name=self.name) |
| 594 |
if not response: |
| 595 |
return False |
| 596 |
|
| 597 |
found = True |
| 598 |
self.log("Response : {0}".format(response)) |
| 599 |
self.log("Database Account instance : {0} found".format(response.name)) |
| 600 |
except ResourceNotFoundError as e: |
| 601 |
self.log('Did not find the Database Account instance.') |
| 602 |
if found is True: |
| 603 |
return response.as_dict() |
| 604 |
|
| 605 |
return False |
| 606 |
|
| 607 |
|
| 608 |
def default_compare(new, old, path, result): |
| 609 |
''' |
| 610 |
:return: false if differences are found between old and new. |
| 611 |
''' |
| 612 |
if new is None: |
| 613 |
return True |
| 614 |
elif isinstance(new, dict): |
| 615 |
if not isinstance(old, dict): |
| 616 |
result['compare'] = 'changed [' + path + '] old dict is null' |
| 617 |
return False |
| 618 |
for k in new.keys(): |
| 619 |
if not default_compare(new.get(k), old.get(k, None), path + '/' + k, result): |
| 620 |
return False |
| 621 |
return True |
| 622 |
elif isinstance(new, list): |
| 623 |
if not isinstance(old, list) or len(new) != len(old): |
| 624 |
result['compare'] = 'changed [' + path + '] length is different or null' |
| 625 |
return False |
| 626 |
elif len(old) == 0: |
| 627 |
return True |
| 628 |
elif isinstance(old[0], dict): |
| 629 |
key = None |
| 630 |
if 'id' in old[0] and 'id' in new[0]: |
| 631 |
key = 'id' |
| 632 |
elif 'name' in old[0] and 'name' in new[0]: |
| 633 |
key = 'name' |
| 634 |
else: |
| 635 |
key = list(old[0])[0] |
| 636 |
new = sorted(new, key=lambda x: x.get(key, '')) |
| 637 |
old = sorted(old, key=lambda x: x.get(key, '')) |
| 638 |
else: |
| 639 |
new = sorted(new) |
| 640 |
old = sorted(old) |
| 641 |
for i in range(len(new)): |
| 642 |
if not default_compare(new[i], old[i], path + '/*', result): |
| 643 |
return False |
| 644 |
return True |
| 645 |
else: |
| 646 |
if path == '/location' or path.endswith('location_name'): |
| 647 |
new = new.replace(' ', '').lower() |
| 648 |
old = new.replace(' ', '').lower() |
| 649 |
if isinstance(old, str) and isinstance(new, str): |
| 650 |
new = new.lower() |
| 651 |
old = old.lower() |
| 652 |
if new == old: |
| 653 |
return True |
| 654 |
else: |
| 655 |
result['compare'] = 'changed [' + path + '] ' + str(new) + ' != ' + str(old) |
| 656 |
return False |
| 657 |
|
| 658 |
|
| 659 |
def dict_camelize(d, path, camelize_first): |
| 660 |
if isinstance(d, list): |
| 661 |
for i in range(len(d)): |
| 662 |
dict_camelize(d[i], path, camelize_first) |
| 663 |
elif isinstance(d, dict): |
| 664 |
if len(path) == 1: |
| 665 |
old_value = d.get(path[0], None) |
| 666 |
if old_value is not None: |
| 667 |
d[path[0]] = _snake_to_camel(old_value, camelize_first) |
| 668 |
else: |
| 669 |
sd = d.get(path[0], None) |
| 670 |
if sd is not None: |
| 671 |
dict_camelize(sd, path[1:], camelize_first) |
| 672 |
|
| 673 |
|
| 674 |
def dict_upper(d, path): |
| 675 |
if isinstance(d, list): |
| 676 |
for i in range(len(d)): |
| 677 |
dict_upper(d[i], path) |
| 678 |
elif isinstance(d, dict): |
| 679 |
if len(path) == 1: |
| 680 |
old_value = d.get(path[0], None) |
| 681 |
if old_value is not None: |
| 682 |
d[path[0]] = old_value.upper() |
| 683 |
else: |
| 684 |
sd = d.get(path[0], None) |
| 685 |
if sd is not None: |
| 686 |
dict_upper(sd, path[1:]) |
| 687 |
|
| 688 |
|
| 689 |
def dict_rename(d, path, new_name): |
| 690 |
if isinstance(d, list): |
| 691 |
for i in range(len(d)): |
| 692 |
dict_rename(d[i], path, new_name) |
| 693 |
elif isinstance(d, dict): |
| 694 |
if len(path) == 1: |
| 695 |
old_value = d.pop(path[0], None) |
| 696 |
if old_value is not None: |
| 697 |
d[new_name] = old_value |
| 698 |
else: |
| 699 |
sd = d.get(path[0], None) |
| 700 |
if sd is not None: |
| 701 |
dict_rename(sd, path[1:], new_name) |
| 702 |
|
| 703 |
|
| 704 |
def dict_expand(d, path, outer_dict_name): |
| 705 |
if isinstance(d, list): |
| 706 |
for i in range(len(d)): |
| 707 |
dict_expand(d[i], path, outer_dict_name) |
| 708 |
elif isinstance(d, dict): |
| 709 |
if len(path) == 1: |
| 710 |
old_value = d.pop(path[0], None) |
| 711 |
if old_value is not None: |
| 712 |
d[outer_dict_name] = d.get(outer_dict_name, {}) |
| 713 |
d[outer_dict_name] = old_value |
| 714 |
else: |
| 715 |
sd = d.get(path[0], None) |
| 716 |
if sd is not None: |
| 717 |
dict_expand(sd, path[1:], outer_dict_name) |
| 718 |
|
| 719 |
|
| 720 |
def main(): |
| 721 |
"""Main execution""" |
| 722 |
AzureRMCosmosDBAccount() |
| 723 |
|
| 724 |
|
| 725 |
if __name__ == '__main__': |
| 726 |
main() |