| 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_galleryimageversion |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure SIG Image Version instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure SIG Image Version. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
gallery_name: |
| 25 |
description: |
| 26 |
- The name of the Shared Image Gallery in which the Image Definition resides. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
gallery_image_name: |
| 30 |
description: |
| 31 |
- The name of the gallery Image Definition in which the Image Version is to be created. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
name: |
| 35 |
description: |
| 36 |
- The name of the gallery Image Version to be created. |
| 37 |
- Needs to follow semantic version name pattern, The allowed characters are digit and period. |
| 38 |
- Digits must be within the range of a 32-bit integer. For example <MajorVersion>.<MinorVersion>.<Patch>. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
location: |
| 42 |
description: |
| 43 |
- Resource location. |
| 44 |
type: str |
| 45 |
storage_profile: |
| 46 |
description: |
| 47 |
- This is the storage profile of a Gallery Image Version. |
| 48 |
- Required when creating. |
| 49 |
type: dict |
| 50 |
suboptions: |
| 51 |
source_image: |
| 52 |
description: |
| 53 |
- Reference to managed image or gallery image version |
| 54 |
- Could be resource ID to managed image, or dictionary containing I(resource_group) and I(name) |
| 55 |
- Could be resource ID to image version, or dictionary containing I(resource_group),I(gallery_name), I(gallery_image_name) and I(version) |
| 56 |
- Could be resource ID I(virtual_machine_id) of the source virtual machine. |
| 57 |
- Mutual exclusive with os_disk and data_disks |
| 58 |
type: raw |
| 59 |
os_disk: |
| 60 |
description: |
| 61 |
- os disk snapshot |
| 62 |
- Mutual exclusive with source_image |
| 63 |
type: dict |
| 64 |
suboptions: |
| 65 |
source: |
| 66 |
description: |
| 67 |
- Reference to os disk snapshot. |
| 68 |
- Could be resource ID. |
| 69 |
- Could be a dictionary containing I(resource_group) and I(name). |
| 70 |
- Could be a dictionary containing I(resource_group), I(storage_account), and I(uri) |
| 71 |
if the snapshot is stored as a PageBlob in a storage account container. |
| 72 |
type: raw |
| 73 |
host_caching: |
| 74 |
description: |
| 75 |
- host disk caching |
| 76 |
type: str |
| 77 |
default: None |
| 78 |
choices: |
| 79 |
- None |
| 80 |
- ReadOnly |
| 81 |
- ReadWrite |
| 82 |
data_disks: |
| 83 |
description: |
| 84 |
- list of data disk snapshot |
| 85 |
- Mutual exclusive with source_image |
| 86 |
type: list |
| 87 |
elements: raw |
| 88 |
suboptions: |
| 89 |
source: |
| 90 |
description: |
| 91 |
- Reference to data disk snapshot. Could be resource ID or dictionary containing I(resource_group) and I(name) |
| 92 |
type: raw |
| 93 |
lun: |
| 94 |
description: |
| 95 |
- lun of the data disk |
| 96 |
type: int |
| 97 |
host_caching: |
| 98 |
description: |
| 99 |
- host disk caching |
| 100 |
type: str |
| 101 |
default: None |
| 102 |
choices: |
| 103 |
- None |
| 104 |
- ReadOnly |
| 105 |
- ReadWrite |
| 106 |
publishing_profile: |
| 107 |
description: |
| 108 |
- Publishing profile. |
| 109 |
type: dict |
| 110 |
suboptions: |
| 111 |
target_regions: |
| 112 |
description: |
| 113 |
- The target regions where the Image Version is going to be replicated to. |
| 114 |
- This property is updatable. |
| 115 |
type: list |
| 116 |
elements: raw |
| 117 |
suboptions: |
| 118 |
name: |
| 119 |
description: |
| 120 |
- Region name. |
| 121 |
type: str |
| 122 |
required: true |
| 123 |
regional_replica_count: |
| 124 |
description: |
| 125 |
- The number of replicas of the Image Version to be created per region. |
| 126 |
- This property would take effect for a region when regionalReplicaCount is not specified. |
| 127 |
- This property is updatable. |
| 128 |
type: int |
| 129 |
storage_account_type: |
| 130 |
description: |
| 131 |
- Storage account type. |
| 132 |
type: str |
| 133 |
encryption: |
| 134 |
description: |
| 135 |
- Allows users to provide customer managed keys for encrypting the OS and data disks in the gallery artifact. |
| 136 |
type: dict |
| 137 |
suboptions: |
| 138 |
data_disk_images: |
| 139 |
description: |
| 140 |
- A list of encryption specifications for data disk images. |
| 141 |
type: list |
| 142 |
elements: dict |
| 143 |
suboptions: |
| 144 |
disk_encryption_set_id: |
| 145 |
description: |
| 146 |
- A relative URI containing the resource ID of the disk encryption set. |
| 147 |
type: str |
| 148 |
lun: |
| 149 |
description: |
| 150 |
- This property specifies the logical unit number of the data disk. |
| 151 |
- This value is used to identify data disks within the Virtual Machine and |
| 152 |
therefore must be unique for each data disk attached to the Virtual Machine. |
| 153 |
type: int |
| 154 |
os_disk_image: |
| 155 |
description: |
| 156 |
- Contains encryption settings for an OS disk image. |
| 157 |
type: dict |
| 158 |
suboptions: |
| 159 |
disk_encryption_set_id: |
| 160 |
description: |
| 161 |
- A relative URI containing the resource ID of the disk encryption set. |
| 162 |
type: str |
| 163 |
security_profile: |
| 164 |
description: |
| 165 |
- This property specifies the security profile of an OS disk image. |
| 166 |
type: dict |
| 167 |
suboptions: |
| 168 |
confidential_vm_encryption_type: |
| 169 |
description: |
| 170 |
- Confidential VM encryption types. |
| 171 |
type: dict |
| 172 |
suboptions: |
| 173 |
encrypted_vm_guest_state_only_with_pmk: |
| 174 |
description: |
| 175 |
- VM Guest State Only with PMK. |
| 176 |
type: str |
| 177 |
encrypted_with_cmk: |
| 178 |
description: |
| 179 |
- Encrypted with CMK. |
| 180 |
type: str |
| 181 |
encrypted_with_pmk: |
| 182 |
description: |
| 183 |
- Encrypted with PMK. |
| 184 |
type: str |
| 185 |
secure_vm_disk_encryption_set_id: |
| 186 |
description: |
| 187 |
- Secure VM disk encryption set id. |
| 188 |
type: str |
| 189 |
managed_image: |
| 190 |
description: |
| 191 |
- Managed image reference, could be resource ID, or dictionary containing I(resource_group) and I(name) |
| 192 |
- Obsolete since 2.10, use storage_profile instead |
| 193 |
type: raw |
| 194 |
snapshot: |
| 195 |
description: |
| 196 |
- Source snapshot to be used. |
| 197 |
- Obsolete since 2.10, use storage_profile instead |
| 198 |
type: raw |
| 199 |
replica_count: |
| 200 |
description: |
| 201 |
- The number of replicas of the Image Version to be created per region. |
| 202 |
- This property would take effect for a region when regionalReplicaCount is not specified. |
| 203 |
- This property is updatable. |
| 204 |
type: int |
| 205 |
exclude_from_latest: |
| 206 |
description: |
| 207 |
If I(exclude_from_latest=true), Virtual Machines deployed from the latest version of the Image Definition won't use this Image Version. |
| 208 |
type: bool |
| 209 |
end_of_life_date: |
| 210 |
description: |
| 211 |
- The end of life date of the gallery Image Version. |
| 212 |
- This property can be used for decommissioning purposes. |
| 213 |
- This property is updatable. Format should be according to ISO-8601, for instance "2019-06-26". |
| 214 |
type: str |
| 215 |
storage_account_type: |
| 216 |
description: |
| 217 |
- Specifies the storage account type to be used to store the image. |
| 218 |
- This property is not updatable. |
| 219 |
type: str |
| 220 |
choices: |
| 221 |
- Standard_LRS |
| 222 |
- Standard_ZRS |
| 223 |
timeout: |
| 224 |
description: |
| 225 |
- Set the timeout (minute) period for creating an Image Version. |
| 226 |
type: int |
| 227 |
default: 10 |
| 228 |
state: |
| 229 |
description: |
| 230 |
- Assert the state of the GalleryImageVersion. |
| 231 |
- Use C(present) to create or update an GalleryImageVersion and C(absent) to delete it. |
| 232 |
default: present |
| 233 |
choices: |
| 234 |
- absent |
| 235 |
- present |
| 236 |
type: str |
| 237 |
extends_documentation_fragment: |
| 238 |
- azure.azcollection.azure |
| 239 |
- azure.azcollection.azure_tags |
| 240 |
author: |
| 241 |
- Zim Kalinowski (@zikalino) |
| 242 |
|
| 243 |
''' |
| 244 |
|
| 245 |
EXAMPLES = ''' |
| 246 |
- name: Create a gallery image version form a managed image |
| 247 |
azure_rm_galleryimageversion: |
| 248 |
resource_group: myResourceGroup |
| 249 |
gallery_name: myGallery |
| 250 |
gallery_image_name: myGalleryImage |
| 251 |
name: 1.1.0 |
| 252 |
location: East US |
| 253 |
publishing_profile: |
| 254 |
end_of_life_date: "2020-10-01t00:00:00+00:00" |
| 255 |
exclude_from_latest: true |
| 256 |
replica_count: 4 |
| 257 |
storage_account_type: Standard_LRS |
| 258 |
target_regions: |
| 259 |
- name: West US |
| 260 |
regional_replica_count: 1 |
| 261 |
- name: East US |
| 262 |
regional_replica_count: 3 |
| 263 |
storage_account_type: Standard_LRS |
| 264 |
storage_profile: |
| 265 |
source_image: /subscriptions/sub123/resourceGroups/group123/providers/Microsoft.Compute/images/myOsImage |
| 266 |
|
| 267 |
- name: Create a gallery image version from another gallery image version |
| 268 |
azure_rm_galleryimageversion: |
| 269 |
resource_group: myResourceGroup |
| 270 |
gallery_name: myGallery |
| 271 |
gallery_image_name: myGalleryImage |
| 272 |
name: 1.2.0 |
| 273 |
location: East US |
| 274 |
publishing_profile: |
| 275 |
end_of_life_date: "2020-10-01t00:00:00+00:00" |
| 276 |
exclude_from_latest: true |
| 277 |
replica_count: 4 |
| 278 |
storage_account_type: Standard_LRS |
| 279 |
target_regions: |
| 280 |
- name: West US |
| 281 |
regional_replica_count: 1 |
| 282 |
- name: East US |
| 283 |
regional_replica_count: 3 |
| 284 |
storage_account_type: Standard_LRS |
| 285 |
storage_profile: |
| 286 |
source_image: |
| 287 |
version: 1.1.0 |
| 288 |
gallery_name: myGallery2 |
| 289 |
gallery_image_name: myGalleryImage2 |
| 290 |
|
| 291 |
- name: Create gallery image by using one os dist snapshot and zero or many data disk snapshots |
| 292 |
azure_rm_galleryimageversion: |
| 293 |
resource_group: myRsourceGroup |
| 294 |
gallery_name: myGallery |
| 295 |
gallery_image_name: myGalleryImage |
| 296 |
name: 3.4.0 |
| 297 |
location: East US |
| 298 |
publishing_profile: |
| 299 |
end_of_life_date: "2020-10-01t00:00:00+00:00" |
| 300 |
exclude_from_latest: true |
| 301 |
replica_count: 1 |
| 302 |
storage_account_type: Standard_LRS |
| 303 |
target_regions: |
| 304 |
- name: East US |
| 305 |
regional_replica_count: 1 |
| 306 |
storage_account_type: Standard_LRS |
| 307 |
storage_profile: |
| 308 |
os_disk: |
| 309 |
source: "/subscriptions/mySub/resourceGroups/myGroup/providers/Microsoft.Compute/snapshots/os_snapshot_vma" |
| 310 |
data_disks: |
| 311 |
- lun: 0 |
| 312 |
source: |
| 313 |
name: data_snapshot_vma |
| 314 |
- lun: 1 |
| 315 |
source: "/subscriptions/mySub/resourceGroups/myGroup/providers/Microsoft.Compute/snapshots/data_snapshot_vmb" |
| 316 |
|
| 317 |
- name: Create gallery image by using a os disk snapshot stored in Storage Account container |
| 318 |
azure_rm_galleryimageversion: |
| 319 |
resource_group: myResourceGroup |
| 320 |
gallery_name: myGallery |
| 321 |
gallery_image_name: myGalleryImage |
| 322 |
name: 3.4.0 |
| 323 |
location: East US |
| 324 |
publishing_profile: |
| 325 |
end_of_life_date: "2020-10-01t00:00:00+00:00" |
| 326 |
exclude_from_latest: true |
| 327 |
replica_count: 1 |
| 328 |
storage_account_type: Standard_LRS |
| 329 |
target_regions: |
| 330 |
- name: East US |
| 331 |
regional_replica_count: 1 |
| 332 |
storage_account_type: Standard_LRS |
| 333 |
storage_profile: |
| 334 |
os_disk: |
| 335 |
source: |
| 336 |
resource_group: myResourceGroup |
| 337 |
storage_account: myStorageAccount |
| 338 |
uri: "https://myStorageAccount.blob.core.windows.net/myContainer/myImage.vhd" |
| 339 |
''' |
| 340 |
|
| 341 |
RETURN = ''' |
| 342 |
id: |
| 343 |
description: |
| 344 |
- Resource ID. |
| 345 |
returned: always |
| 346 |
type: str |
| 347 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/myGalle |
| 348 |
ry1283/images/myImage/versions/10.1.3" |
| 349 |
''' |
| 350 |
|
| 351 |
try: |
| 352 |
import time |
| 353 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 354 |
from azure.core.polling import LROPoller |
| 355 |
from azure.core.exceptions import ResourceNotFoundError |
| 356 |
except ImportError: |
| 357 |
pass |
| 358 |
|
| 359 |
|
| 360 |
class Actions: |
| 361 |
NoAction, Create, Update, Delete = range(4) |
| 362 |
|
| 363 |
|
| 364 |
class AzureRMGalleryImageVersions(AzureRMModuleBaseExt): |
| 365 |
def __init__(self): |
| 366 |
self.module_arg_spec = dict( |
| 367 |
resource_group=dict( |
| 368 |
type='str', |
| 369 |
required=True |
| 370 |
), |
| 371 |
gallery_name=dict( |
| 372 |
type='str', |
| 373 |
required=True |
| 374 |
), |
| 375 |
gallery_image_name=dict( |
| 376 |
type='str', |
| 377 |
required=True |
| 378 |
), |
| 379 |
name=dict( |
| 380 |
type='str', |
| 381 |
required=True |
| 382 |
), |
| 383 |
tags=dict( |
| 384 |
type='dict', |
| 385 |
), |
| 386 |
location=dict( |
| 387 |
type='str', |
| 388 |
), |
| 389 |
storage_profile=dict( |
| 390 |
type='dict', |
| 391 |
mutually_exclusive=[('source_image', 'os_disk', 'data_disks')], |
| 392 |
options=dict( |
| 393 |
source_image=dict( |
| 394 |
type='raw', |
| 395 |
), |
| 396 |
os_disk=dict( |
| 397 |
type='dict', |
| 398 |
options=dict( |
| 399 |
source=dict( |
| 400 |
type='raw', |
| 401 |
), |
| 402 |
host_caching=dict( |
| 403 |
type='str', |
| 404 |
default="None", |
| 405 |
choices=["ReadOnly", "ReadWrite", "None"] |
| 406 |
) |
| 407 |
) |
| 408 |
), |
| 409 |
data_disks=dict( |
| 410 |
type='list', |
| 411 |
elements='raw', |
| 412 |
options=dict( |
| 413 |
lun=dict( |
| 414 |
type='int' |
| 415 |
), |
| 416 |
source=dict( |
| 417 |
type='raw', |
| 418 |
), |
| 419 |
host_caching=dict( |
| 420 |
type='str', |
| 421 |
default="None", |
| 422 |
choices=["ReadOnly", "ReadWrite", "None"] |
| 423 |
) |
| 424 |
) |
| 425 |
) |
| 426 |
) |
| 427 |
), |
| 428 |
publishing_profile=dict( |
| 429 |
type='dict', |
| 430 |
options=dict( |
| 431 |
target_regions=dict( |
| 432 |
type='list', |
| 433 |
elements='raw', |
| 434 |
options=dict( |
| 435 |
name=dict( |
| 436 |
type='str', |
| 437 |
required=True, |
| 438 |
), |
| 439 |
regional_replica_count=dict( |
| 440 |
type='int', |
| 441 |
), |
| 442 |
storage_account_type=dict( |
| 443 |
type='str', |
| 444 |
), |
| 445 |
encryption=dict( |
| 446 |
type='dict', |
| 447 |
options=dict( |
| 448 |
data_disk_images=dict( |
| 449 |
type='list', |
| 450 |
elements='dict', |
| 451 |
options=dict( |
| 452 |
disk_encryption_set_id=dict( |
| 453 |
type='str', |
| 454 |
), |
| 455 |
lun=dict( |
| 456 |
type='int' |
| 457 |
) |
| 458 |
) |
| 459 |
), |
| 460 |
os_disk_image=dict( |
| 461 |
type='dict', |
| 462 |
options=dict( |
| 463 |
disk_encryption_set_id=dict( |
| 464 |
type='str', |
| 465 |
), |
| 466 |
security_profile=dict( |
| 467 |
type='dict', |
| 468 |
options=dict( |
| 469 |
confidential_vm_encryption_type=dict( |
| 470 |
type='dict', |
| 471 |
options=dict( |
| 472 |
encrypted_vm_guest_state_only_with_pmk=dict( |
| 473 |
type='str', |
| 474 |
), |
| 475 |
encrypted_with_cmk=dict( |
| 476 |
type='str', |
| 477 |
), |
| 478 |
encrypted_with_pmk=dict( |
| 479 |
type='str', |
| 480 |
) |
| 481 |
) |
| 482 |
), |
| 483 |
secure_vm_disk_encryption_set_id=dict( |
| 484 |
type='str', |
| 485 |
) |
| 486 |
) |
| 487 |
) |
| 488 |
) |
| 489 |
) |
| 490 |
) |
| 491 |
) |
| 492 |
) |
| 493 |
), |
| 494 |
managed_image=dict( |
| 495 |
type='raw', |
| 496 |
), |
| 497 |
snapshot=dict( |
| 498 |
type='raw', |
| 499 |
), |
| 500 |
replica_count=dict( |
| 501 |
type='int', |
| 502 |
), |
| 503 |
exclude_from_latest=dict( |
| 504 |
type='bool', |
| 505 |
), |
| 506 |
end_of_life_date=dict( |
| 507 |
type='str', |
| 508 |
), |
| 509 |
storage_account_type=dict( |
| 510 |
type='str', |
| 511 |
choices=['Standard_LRS', |
| 512 |
'Standard_ZRS'] |
| 513 |
) |
| 514 |
) |
| 515 |
), |
| 516 |
timeout=dict( |
| 517 |
type='int', |
| 518 |
default=10 |
| 519 |
), |
| 520 |
state=dict( |
| 521 |
type='str', |
| 522 |
default='present', |
| 523 |
choices=['present', 'absent'] |
| 524 |
) |
| 525 |
) |
| 526 |
|
| 527 |
self.resource_group = None |
| 528 |
self.gallery_name = None |
| 529 |
self.gallery_image_name = None |
| 530 |
self.name = None |
| 531 |
self.gallery_image_version = None |
| 532 |
self.tags = None |
| 533 |
self.state = None |
| 534 |
self.timeout = None |
| 535 |
|
| 536 |
self.body = dict() |
| 537 |
self.results = dict(changed=False) |
| 538 |
self.to_do = Actions.NoAction |
| 539 |
|
| 540 |
super(AzureRMGalleryImageVersions, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 541 |
supports_check_mode=True, |
| 542 |
supports_tags=True) |
| 543 |
|
| 544 |
def exec_module(self, **kwargs): |
| 545 |
for key in list(self.module_arg_spec.keys()): |
| 546 |
if hasattr(self, key): |
| 547 |
setattr(self, key, kwargs[key]) |
| 548 |
if key == 'tags': |
| 549 |
self.body[key] = kwargs[key] |
| 550 |
elif kwargs[key] is not None: |
| 551 |
if key == 'location': |
| 552 |
self.body['location'] = kwargs[key] |
| 553 |
elif key == 'storage_profile': |
| 554 |
self.body['storage_profile'] = {} |
| 555 |
if kwargs[key].get('source_image') is not None: |
| 556 |
self.body['storage_profile']['source'] = {} |
| 557 |
if isinstance(kwargs[key].get('source_image'), str): |
| 558 |
self.body['storage_profile']['source']['id'] = kwargs[key].get('source_image') |
| 559 |
elif isinstance(kwargs[key].get('source_image'), dict): |
| 560 |
if kwargs[key]['source_image'].get('id') is not None: |
| 561 |
self.body['storage_profile']['source']['id'] = kwargs[key]['source_image'].get('id') |
| 562 |
elif kwargs[key]['source_image'].get('virtual_machine_id') is not None: |
| 563 |
self.body['storage_profile']['source']['virtualMachineId'] = kwargs[key]['source_image'].get('virtual_machine_id') |
| 564 |
elif kwargs[key]['source_image'].get('resource_group') is not None and kwargs[key]['source_image'].get('name') is not None: |
| 565 |
self.body['storage_profile']['source']['id'] = ('/subscriptions/' + |
| 566 |
self.subscription_id + |
| 567 |
'/resourceGroups/' + |
| 568 |
kwargs[key]['source_image'].get('resource_group') + |
| 569 |
'/providers/Microsoft.Compute/images/' + |
| 570 |
kwargs[key]['source_image'].get('name')) |
| 571 |
elif (kwargs[key]['source_image'].get('resource_group') is not None and |
| 572 |
kwargs[key]['source_image'].get('gallery_name') is not None and |
| 573 |
kwargs[key]['source_image'].get('gallery_image_name') is not None and kwargs[key]['source_image'].get('version') is not None): |
| 574 |
self.body['storage_profile']['source']['id'] = ('/subscriptions/' + |
| 575 |
self.subscription_id + |
| 576 |
'/resourceGroups/' + |
| 577 |
kwargs[key]['source_image'].get('resource_group') + |
| 578 |
'/providers/Microsoft.Compute/galleries/' + |
| 579 |
kwargs[key]['source_image'].get('gallery_name') + |
| 580 |
'/images/' + |
| 581 |
kwargs[key]['source_image'].get('gallery_image_name') + |
| 582 |
'/versions/' + |
| 583 |
kwargs[key]['source_image'].get('version')) |
| 584 |
else: |
| 585 |
self.fail("The source_image parameters config errors") |
| 586 |
else: |
| 587 |
self.fail("The source_image parameters config errors") |
| 588 |
if kwargs[key].get('os_disk') is not None: |
| 589 |
self.body['storage_profile']['os_disk_image'] = {} |
| 590 |
if kwargs[key]['os_disk'].get('host_caching') is not None: |
| 591 |
self.body['storage_profile']['os_disk_image']['hostCaching'] = kwargs[key]['os_disk'].get('host_caching') |
| 592 |
if kwargs[key]['os_disk'].get('source') is not None: |
| 593 |
self.body['storage_profile']['os_disk_image']['source'] = {} |
| 594 |
if isinstance(kwargs[key]['os_disk']['source'], str): |
| 595 |
self.body['storage_profile']['os_disk_image']['source']['id'] = kwargs[key]['os_disk']['source'] |
| 596 |
elif isinstance(kwargs[key]['os_disk']['source'], dict): |
| 597 |
if kwargs[key]['os_disk']['source'].get('id') is not None: |
| 598 |
self.body['storage_profile']['os_disk_image']['source']['id'] = kwargs[key]['os_disk']['source'].get('id') |
| 599 |
elif kwargs[key]['os_disk']['source'].get('resource_group') is not None and \ |
| 600 |
kwargs[key]['os_disk']['source'].get('name') is not None: |
| 601 |
resource_group = kwargs[key]['os_disk']['source'].get('resource_group') |
| 602 |
self.body['storage_profile']['os_disk_image']['source']['id'] = ('/subscriptions/' + |
| 603 |
self.subscription_id + |
| 604 |
'/resourceGroups/' + |
| 605 |
resource_group + |
| 606 |
'/providers/Microsoft.Compute/snapshots/' + |
| 607 |
kwargs[key]['os_disk']['source'].get('name')) |
| 608 |
elif kwargs[key]['os_disk']['source'].get('uri') is not None and \ |
| 609 |
kwargs[key]['os_disk']['source'].get('resource_group') is not None and \ |
| 610 |
kwargs[key]['os_disk']['source'].get('storage_account') is not None: |
| 611 |
resource_group = kwargs[key]['os_disk']['source'].get('resource_group') |
| 612 |
storage_account = kwargs[key]['os_disk']['source'].get('storage_account') |
| 613 |
self.body['storage_profile']['os_disk_image']['source']['storageAccountId'] = ('/subscriptions/' + |
| 614 |
self.subscription_id + |
| 615 |
'/resourceGroups/' + |
| 616 |
resource_group + |
| 617 |
'/providers/Microsoft.Storage' + |
| 618 |
'/storageAccounts/' + |
| 619 |
storage_account) |
| 620 |
self.body['storage_profile']['os_disk_image']['source']['uri'] = kwargs[key]['os_disk']['source'].get('uri') |
| 621 |
else: |
| 622 |
self.fail("The os_disk.source parameters config errors") |
| 623 |
|
| 624 |
else: |
| 625 |
self.fail("The os_disk.source parameters config errors") |
| 626 |
|
| 627 |
if kwargs[key].get('data_disks') is not None: |
| 628 |
self.body['storage_profile']['data_disk_images'] = [] |
| 629 |
data_disk = {} |
| 630 |
for item in kwargs[key].get('data_disks'): |
| 631 |
if item.get('lun') is not None: |
| 632 |
data_disk['lun'] = item['lun'] |
| 633 |
if item.get('source') is not None: |
| 634 |
data_disk['source'] = {} |
| 635 |
if isinstance(item.get('source'), str): |
| 636 |
data_disk['source']['id'] = item.get('source') |
| 637 |
elif isinstance(item.get('source'), dict): |
| 638 |
if item['source'].get('id') is not None: |
| 639 |
data_disk['source']['id'] = item['source'].get('id') |
| 640 |
elif item['source'].get('resource_group') is not None and item['source'].get('name') is not None: |
| 641 |
data_disk['source']['id'] = ('/subscriptions/' + |
| 642 |
self.subscription_id + |
| 643 |
'/resourceGroups/' + |
| 644 |
item['source'].get('resource_group') + |
| 645 |
'/providers/Microsoft.Compute/snapshots/' + |
| 646 |
item['source'].get('name')) |
| 647 |
else: |
| 648 |
self.fail("The data_disk.source parameters config errors") |
| 649 |
else: |
| 650 |
self.fail("The data_disk.source parameters config errors") |
| 651 |
|
| 652 |
if item.get('host_caching') is not None: |
| 653 |
data_disk['hostCaching'] = item['host_caching'] |
| 654 |
|
| 655 |
self.body['storage_profile']['data_disk_images'].append(data_disk) |
| 656 |
|
| 657 |
elif key == 'publishing_profile': |
| 658 |
self.body['publishing_profile'] = {} |
| 659 |
if kwargs['publishing_profile'].get('target_regions') is not None: |
| 660 |
self.body['publishing_profile']['target_regions'] = [] |
| 661 |
for item in kwargs['publishing_profile']['target_regions']: |
| 662 |
target_regions = {} |
| 663 |
for value in item.keys(): |
| 664 |
if value == 'name': |
| 665 |
target_regions[value] = item[value] |
| 666 |
elif value == 'regional_replica_count': |
| 667 |
target_regions['regional_replica_count'] = item[value] |
| 668 |
elif value == 'storage_account_type': |
| 669 |
target_regions['storage_account_type'] = item[value] |
| 670 |
elif value == 'encryption': |
| 671 |
target_regions['encryption'] = {} |
| 672 |
if item[value].get('data_disk_images') is not None: |
| 673 |
target_regions['encryption']['data_disk_images'] = [] |
| 674 |
for tt in item[value]['data_disk_images']: |
| 675 |
disk_image = {} |
| 676 |
if tt.get('lun') is not None: |
| 677 |
disk_image['lun'] = tt['lun'] |
| 678 |
if tt.get('disk_encryption_set_id') is not None: |
| 679 |
disk_image['disk_encryption_set_id'] = tt['disk_encryption_set_id'] |
| 680 |
target_regions['encryption']['data_disk_images'].append(disk_image) |
| 681 |
|
| 682 |
if item['encryption'].get('os_disk_image') is not None: |
| 683 |
target_regions['encryption']['os_disk_image'] = {} |
| 684 |
if item['encryption']['os_disk_image'].get('disk_encryption_set_id') is not None: |
| 685 |
disk_encryption_set_id = item['encryption']['os_disk_image']['disk_encryption_set_id'] |
| 686 |
target_regions['encryption']['os_disk_image']['disk_encryption_set_id'] = disk_encryption_set_id |
| 687 |
if item['encryption']['os_disk_image'].get('security_profile') is not None: |
| 688 |
target_regions['encryption']['os_disk_image']['security_profile'] = {} |
| 689 |
if item['encryption']['os_disk_image']['security_profile'].get('secure_vm_disk_encryption_set_id') is not None: |
| 690 |
secure_id = item['encryption']['os_disk_image']['security_profile']['secure_vm_disk_encryption_set_id'] |
| 691 |
target_regions['encryption']['os_disk_image']['security_profile']['secure_vm_dis_encryption_set_id'] = secure_id |
| 692 |
|
| 693 |
if item['encryption']['os_disk_image']['security_profile'].get('confidential_vm_encryption_type') is not None: |
| 694 |
target_regions['encryption']['os_disk_image']['security_profile']['confidential_vm_encryption_type'] = {} |
| 695 |
security = item['encryption']['os_disk_image']['security_profile']['confidential_vm_encryption_type'] |
| 696 |
tt = target_regions['encryption']['os_disk_image']['security_profile']['confidential_vm_encryption_type'] |
| 697 |
if security.get('encrypted_vm_guest_state_only_with_pmk') is not None: |
| 698 |
tt['encrypted_vm_guestState_only_with_pmk'] = security.get('encrypted_vm_guest_state_only_with_pmk') |
| 699 |
if security.get('encrypted_with_cmk') is not None: |
| 700 |
tt['encrypted_with_cmk'] = security.get('encrypted_with_cmk') |
| 701 |
if security.get('encrypted_with_pmk') is not None: |
| 702 |
tt['encrypted_with_pmk'] = security.get('encrypted_with_pmk') |
| 703 |
self.body['publishing_profile']['target_regions'].append(target_regions) |
| 704 |
if kwargs[key].get('managed_image') is not None: |
| 705 |
if isinstance(kwargs[key]['managed_image'], str): |
| 706 |
self.body['publishing_profile']['managed_image'] = kwargs[key]['managed_image'] |
| 707 |
elif isinstance(kwargs[key]['managed_image'], dict): |
| 708 |
if kwargs[key]['managed_image'].get('id') is not None: |
| 709 |
self.body['publishing_profile']['managed_image'] = kwargs[key]['managed_image']['id'] |
| 710 |
elif kwargs[key]['managed_image'].get('resource_group') is not None and kwargs[key]['managed_image'].get('name') is not None: |
| 711 |
self.body['publishing_profile']['managed_image'] = ('/subscriptions/' + |
| 712 |
self.subscription_id + |
| 713 |
'/resourceGroups/' + |
| 714 |
kwargs[key]['managed_image'].get('resource_group') + |
| 715 |
'/providers/Microsoft.Compute/images/' + |
| 716 |
kwargs[key]['managed_image'].get('name')) |
| 717 |
else: |
| 718 |
self.fail("The managed_image parameters config errors") |
| 719 |
else: |
| 720 |
self.fail("The managed_image parameters config errors") |
| 721 |
if kwargs[key].get('snapshot') is not None: |
| 722 |
if isinstance(kwargs[key].get('snapshot'), str): |
| 723 |
self.body['publishing_profile']['snapshot'] = kwargs[key].get('snapshot') |
| 724 |
elif isinstance(kwargs[key].get('snapshot'), dict): |
| 725 |
if kwargs[key]['snapshot'].get('id') is not None: |
| 726 |
self.body['publishing_profile']['snapshot'] = kwargs[key]['snapshot'].get('id') |
| 727 |
elif kwargs[key]['snapshot'].get('resource_group') is not None and kwargs[key]['snapshot'].get('name') is not None: |
| 728 |
self.body['publishing_profile']['snapshot'] = ('/subscriptions/' + |
| 729 |
self.subscription_id + |
| 730 |
'/resourceGroups/' + |
| 731 |
kwargs[key]['snapshot'].get('resource_group') + |
| 732 |
'/providers/Microsoft.Compute/snapshots/' + |
| 733 |
kwargs[key]['snapshot'].get('name')) |
| 734 |
else: |
| 735 |
self.fail("The managed_image parameters config errors") |
| 736 |
else: |
| 737 |
self.fail("The managed_image parameters config errors") |
| 738 |
if kwargs[key].get('replica_count') is not None: |
| 739 |
self.body['publishing_profile']['replica_count'] = kwargs[key].get('replica_count') |
| 740 |
if kwargs[key].get('exclude_from_latest') is not None: |
| 741 |
self.body['publishing_profile']['exclude_from_latest'] = kwargs[key].get('exclude_from_latest') |
| 742 |
if kwargs[key].get('end_of_life_date') is not None: |
| 743 |
self.body['publishing_profile']['end_of_life_date'] = kwargs[key].get('end_of_life_date') |
| 744 |
if kwargs[key].get('storage_account_type') is not None: |
| 745 |
self.body['publishing_profile']['storage_account_type'] = kwargs[key].get('storage_account_type') |
| 746 |
|
| 747 |
|
| 748 |
snapshot = self.body.get('publishing_profile', {}).pop('snapshot', None) |
| 749 |
if snapshot is not None: |
| 750 |
self.body.setdefault('storage_profile', {}).setdefault('os_disk_image', {}).setdefault('source', {})['id'] = snapshot |
| 751 |
managed_image = self.body.get('publishing_profile', {}).pop('managed_image', None) |
| 752 |
if managed_image: |
| 753 |
self.body.setdefault('storage_profile', {}).setdefault('source', {})['id'] = managed_image |
| 754 |
|
| 755 |
old_response = None |
| 756 |
response = None |
| 757 |
|
| 758 |
resource_group = self.get_resource_group(self.resource_group) |
| 759 |
|
| 760 |
if 'location' not in self.body: |
| 761 |
self.body['location'] = resource_group.location |
| 762 |
|
| 763 |
old_response = self.get_resource() |
| 764 |
|
| 765 |
if not old_response: |
| 766 |
self.log("GalleryImageVersion instance doesn't exist") |
| 767 |
|
| 768 |
if self.state == 'absent': |
| 769 |
self.log("Old instance didn't exist") |
| 770 |
else: |
| 771 |
self.to_do = Actions.Create |
| 772 |
else: |
| 773 |
self.log('GalleryImageVersion instance already exists') |
| 774 |
|
| 775 |
if self.state == 'absent': |
| 776 |
self.to_do = Actions.Delete |
| 777 |
else: |
| 778 |
update_tags, newtags = self.update_tags(old_response.get('tags', dict())) |
| 779 |
if update_tags: |
| 780 |
self.tags = newtags |
| 781 |
self.body['tags'] = self.tags |
| 782 |
self.to_do = Actions.Update |
| 783 |
if self.body.get('publishing_profile') is not None: |
| 784 |
for key in self.body['publishing_profile'].keys(): |
| 785 |
if key == 'target_regions': |
| 786 |
result = dict(compare=[]) |
| 787 |
modifies = {'/*/name': {'updatable': True, 'comparison': 'location'}} |
| 788 |
if not self.default_compare(modifies, self.body['publishing_profile'][key], old_response['publishing_profile'][key], '', result): |
| 789 |
self.to_do = Actions.Update |
| 790 |
elif key == 'end_of_life_date': |
| 791 |
if self.body['publishing_profile'][key][:10].lower() != old_response['publishing_profile'].get(key)[:10].lower(): |
| 792 |
self.to_do = Actions.Update |
| 793 |
elif self.body['publishing_profile'].get(key) != old_response['publishing_profile'].get(key): |
| 794 |
self.to_do = Actions.Update |
| 795 |
|
| 796 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 797 |
self.log('Need to Create / Update the GalleryImageVersion instance') |
| 798 |
|
| 799 |
if self.check_mode: |
| 800 |
self.results['changed'] = True |
| 801 |
return self.results |
| 802 |
|
| 803 |
response = self.create_update_resource() |
| 804 |
|
| 805 |
self.results['changed'] = True |
| 806 |
self.log('Creation / Update done') |
| 807 |
elif self.to_do == Actions.Delete: |
| 808 |
self.log('GalleryImageVersion instance deleted') |
| 809 |
self.results['changed'] = True |
| 810 |
|
| 811 |
if self.check_mode: |
| 812 |
return self.results |
| 813 |
|
| 814 |
self.delete_resource() |
| 815 |
else: |
| 816 |
self.log('GalleryImageVersion instance unchanged') |
| 817 |
self.results['changed'] = False |
| 818 |
response = old_response |
| 819 |
|
| 820 |
if response: |
| 821 |
self.results["id"] = response["id"] |
| 822 |
|
| 823 |
return self.results |
| 824 |
|
| 825 |
def create_update_resource(self): |
| 826 |
|
| 827 |
|
| 828 |
response = None |
| 829 |
try: |
| 830 |
if self.to_do == Actions.Create: |
| 831 |
response = self.image_version_client.gallery_image_versions.begin_create_or_update(self.resource_group, self.gallery_name, |
| 832 |
self.gallery_image_name, self.name, self.body) |
| 833 |
else: |
| 834 |
response = self.image_version_client.gallery_image_versions.begin_update(self.resource_group, |
| 835 |
self.gallery_name, self.gallery_image_name, self.name, self.body) |
| 836 |
|
| 837 |
except Exception as exc: |
| 838 |
self.log('Error attempting to create the GalleryImageVersion instance.') |
| 839 |
self.fail('Error creating the GalleryImageVersion instance: {0}'.format(str(exc))) |
| 840 |
|
| 841 |
if isinstance(response, LROPoller): |
| 842 |
response = self.get_poller_result(response).as_dict() |
| 843 |
|
| 844 |
i = 0 |
| 845 |
while response['provisioning_state'] == 'Creating': |
| 846 |
time.sleep(60) |
| 847 |
response = self.get_resource() |
| 848 |
i = i + 1 |
| 849 |
if i == self.timeout: |
| 850 |
self.fail("Create or Updating encountered an exception, wait 10 minutes when the status is still 'creating'") |
| 851 |
|
| 852 |
return response |
| 853 |
|
| 854 |
def delete_resource(self): |
| 855 |
|
| 856 |
try: |
| 857 |
response = self.image_version_client.gallery_image_versions.begin_delete(self.resource_group, self.gallery_name, self.gallery_image_name, self.name) |
| 858 |
except Exception as e: |
| 859 |
self.log('Error attempting to delete the GalleryImageVersion instance.') |
| 860 |
self.fail('Error deleting the GalleryImageVersion instance: {0}'.format(str(e))) |
| 861 |
return True |
| 862 |
|
| 863 |
def get_resource(self): |
| 864 |
|
| 865 |
response = None |
| 866 |
try: |
| 867 |
response = self.image_version_client.gallery_image_versions.get(self.resource_group, self.gallery_name, self.gallery_image_name, self.name) |
| 868 |
except ResourceNotFoundError: |
| 869 |
self.log('Could not get the gallery image verison') |
| 870 |
return self.format_item(response) |
| 871 |
|
| 872 |
def format_item(self, item): |
| 873 |
if item is None: |
| 874 |
return None |
| 875 |
else: |
| 876 |
return item.as_dict() |
| 877 |
|
| 878 |
|
| 879 |
def main(): |
| 880 |
AzureRMGalleryImageVersions() |
| 881 |
|
| 882 |
|
| 883 |
if __name__ == '__main__': |
| 884 |
main() |