| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_containerregistry |
| 13 |
version_added: "0.1.2" |
| 14 |
short_description: Manage an Azure Container Registry |
| 15 |
description: |
| 16 |
- Create, update and delete an Azure Container Registry. |
| 17 |
|
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of a resource group where the Container Registry exists or will be created. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the Container Registry. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Assert the state of the container registry. Use C(present) to create or update an container registry and C(absent) to delete it. |
| 32 |
default: present |
| 33 |
type: str |
| 34 |
choices: |
| 35 |
- absent |
| 36 |
- present |
| 37 |
location: |
| 38 |
description: |
| 39 |
- Valid azure location. Defaults to location of the resource group. |
| 40 |
type: str |
| 41 |
admin_user_enabled: |
| 42 |
description: |
| 43 |
- If enabled, you can use the registry name as username and admin user access key as password to docker login to your container registry. |
| 44 |
type: bool |
| 45 |
default: no |
| 46 |
sku: |
| 47 |
description: |
| 48 |
- Specifies the SKU to use. Currently can be either C(Basic), C(Standard) or C(Premium). |
| 49 |
default: Standard |
| 50 |
type: str |
| 51 |
choices: |
| 52 |
- Basic |
| 53 |
- Standard |
| 54 |
- Premium |
| 55 |
|
| 56 |
extends_documentation_fragment: |
| 57 |
- azure.azcollection.azure |
| 58 |
- azure.azcollection.azure_tags |
| 59 |
- azure.azcollection.azure_identity_multiple_user |
| 60 |
|
| 61 |
author: |
| 62 |
- Yawei Wang (@yaweiw) |
| 63 |
|
| 64 |
''' |
| 65 |
|
| 66 |
EXAMPLES = ''' |
| 67 |
- name: Create an azure container registry |
| 68 |
azure_rm_containerregistry: |
| 69 |
name: myRegistry |
| 70 |
location: eastus |
| 71 |
resource_group: myResourceGroup |
| 72 |
admin_user_enabled: true |
| 73 |
sku: Premium |
| 74 |
tags: |
| 75 |
Release: beta1 |
| 76 |
Environment: Production |
| 77 |
|
| 78 |
- name: Remove an azure container registry |
| 79 |
azure_rm_containerregistry: |
| 80 |
name: myRegistry |
| 81 |
resource_group: myResourceGroup |
| 82 |
state: absent |
| 83 |
''' |
| 84 |
RETURN = ''' |
| 85 |
id: |
| 86 |
description: |
| 87 |
- Resource ID. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registries/myRegistry |
| 91 |
name: |
| 92 |
description: |
| 93 |
- Registry name. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: myregistry |
| 97 |
location: |
| 98 |
description: |
| 99 |
- Resource location. |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
sample: westus |
| 103 |
admin_user_enabled: |
| 104 |
description: |
| 105 |
- Is admin user enabled. |
| 106 |
returned: always |
| 107 |
type: bool |
| 108 |
sample: true |
| 109 |
sku: |
| 110 |
description: |
| 111 |
- The SKU name of the container registry. |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
sample: Standard |
| 115 |
provisioning_state: |
| 116 |
description: |
| 117 |
- Provisioning state. |
| 118 |
returned: always |
| 119 |
type: str |
| 120 |
sample: Succeeded |
| 121 |
login_server: |
| 122 |
description: |
| 123 |
- Registry login server. |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: myregistry.azurecr.io |
| 127 |
credentials: |
| 128 |
description: |
| 129 |
- Passwords defined for the registry. |
| 130 |
returned: always |
| 131 |
type: complex |
| 132 |
contains: |
| 133 |
password: |
| 134 |
description: |
| 135 |
- password value. |
| 136 |
returned: when registry exists and C(admin_user_enabled) is set |
| 137 |
type: str |
| 138 |
sample: pass1value |
| 139 |
password2: |
| 140 |
description: |
| 141 |
- password2 value. |
| 142 |
returned: when registry exists and C(admin_user_enabled) is set |
| 143 |
type: str |
| 144 |
sample: pass2value |
| 145 |
tags: |
| 146 |
description: |
| 147 |
- Tags assigned to the resource. Dictionary of string:string pairs. |
| 148 |
returned: always |
| 149 |
type: dict |
| 150 |
''' |
| 151 |
|
| 152 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 153 |
|
| 154 |
try: |
| 155 |
from azure.core.exceptions import ResourceNotFoundError |
| 156 |
from azure.mgmt.containerregistry.models import ( |
| 157 |
Registry, |
| 158 |
RegistryUpdateParameters, |
| 159 |
Sku, |
| 160 |
RegistryNameCheckRequest, |
| 161 |
IdentityProperties, |
| 162 |
UserIdentityProperties, |
| 163 |
) |
| 164 |
except ImportError as exc: |
| 165 |
|
| 166 |
pass |
| 167 |
|
| 168 |
|
| 169 |
def create_containerregistry_dict(registry, credentials): |
| 170 |
''' |
| 171 |
Helper method to deserialize a ContainerRegistry to a dict |
| 172 |
:param: registry: return container registry object from Azure rest API call |
| 173 |
:param: credentials: return credential objects from Azure rest API call |
| 174 |
:return: dict of return container registry and it's credentials |
| 175 |
''' |
| 176 |
results = dict( |
| 177 |
id=registry.id if registry is not None else "", |
| 178 |
name=registry.name if registry is not None else "", |
| 179 |
location=registry.location if registry is not None else "", |
| 180 |
admin_user_enabled=registry.admin_user_enabled if registry is not None else "", |
| 181 |
sku=registry.sku.name if registry is not None else "", |
| 182 |
provisioning_state=registry.provisioning_state if registry is not None else "", |
| 183 |
login_server=registry.login_server if registry is not None else "", |
| 184 |
credentials=dict(), |
| 185 |
identity=registry.identity.as_dict() if registry is not None and registry.identity else None, |
| 186 |
tags=registry.tags if registry is not None else "" |
| 187 |
) |
| 188 |
if credentials: |
| 189 |
results['credentials'] = dict( |
| 190 |
password=credentials.passwords[0].value, |
| 191 |
password2=credentials.passwords[1].value |
| 192 |
) |
| 193 |
|
| 194 |
return results |
| 195 |
|
| 196 |
|
| 197 |
class Actions: |
| 198 |
NoAction, Create, Update = range(3) |
| 199 |
|
| 200 |
|
| 201 |
class AzureRMContainerRegistry(AzureRMModuleBaseExt): |
| 202 |
"""Configuration class for an Azure RM container registry resource""" |
| 203 |
|
| 204 |
def __init__(self): |
| 205 |
self.module_arg_spec = dict( |
| 206 |
resource_group=dict( |
| 207 |
type='str', |
| 208 |
required=True |
| 209 |
), |
| 210 |
name=dict( |
| 211 |
type='str', |
| 212 |
required=True |
| 213 |
), |
| 214 |
state=dict( |
| 215 |
type='str', |
| 216 |
default='present', |
| 217 |
choices=['present', 'absent'] |
| 218 |
), |
| 219 |
location=dict( |
| 220 |
type='str' |
| 221 |
), |
| 222 |
admin_user_enabled=dict( |
| 223 |
type='bool', |
| 224 |
default=False |
| 225 |
), |
| 226 |
identity=dict( |
| 227 |
type='dict', |
| 228 |
options=self.managed_identity_multiple_user_assigned_spec |
| 229 |
), |
| 230 |
sku=dict( |
| 231 |
type='str', |
| 232 |
default='Standard', |
| 233 |
choices=['Basic', 'Standard', 'Premium'] |
| 234 |
) |
| 235 |
) |
| 236 |
|
| 237 |
self.resource_group = None |
| 238 |
self.name = None |
| 239 |
self.location = None |
| 240 |
self.state = None |
| 241 |
self.sku = None |
| 242 |
self.tags = None |
| 243 |
self.identity = None |
| 244 |
|
| 245 |
self._managed_identity = None |
| 246 |
|
| 247 |
self.results = dict(changed=False, state=dict()) |
| 248 |
|
| 249 |
super(AzureRMContainerRegistry, self).__init__( |
| 250 |
derived_arg_spec=self.module_arg_spec, |
| 251 |
supports_check_mode=True, |
| 252 |
supports_tags=True) |
| 253 |
|
| 254 |
@property |
| 255 |
def managed_identity(self): |
| 256 |
if not self._managed_identity: |
| 257 |
self._managed_identity = {"identity": IdentityProperties, |
| 258 |
"user_assigned": UserIdentityProperties |
| 259 |
} |
| 260 |
return self._managed_identity |
| 261 |
|
| 262 |
def exec_module(self, **kwargs): |
| 263 |
"""Main module execution method""" |
| 264 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 265 |
setattr(self, key, kwargs[key]) |
| 266 |
|
| 267 |
resource_group = None |
| 268 |
response = None |
| 269 |
to_do = Actions.NoAction |
| 270 |
|
| 271 |
resource_group = self.get_resource_group(self.resource_group) |
| 272 |
if not self.location: |
| 273 |
self.location = resource_group.location |
| 274 |
|
| 275 |
response = self.get_containerregistry() |
| 276 |
|
| 277 |
if self.state == 'present': |
| 278 |
if not response: |
| 279 |
if self.identity: |
| 280 |
update_identity, self.identity = self.update_managed_identity(new_identity=self.identity) |
| 281 |
to_do = Actions.Create |
| 282 |
else: |
| 283 |
self.log('Results : {0}'.format(response)) |
| 284 |
self.results.update(response) |
| 285 |
if response['provisioning_state'] == "Succeeded": |
| 286 |
if self.identity: |
| 287 |
update_identity, self.identity = self.update_managed_identity(new_identity=self.identity, curr_identity=response.get('identity', None)) |
| 288 |
to_do = Actions.NoAction |
| 289 |
if (self.location is not None) and self.location != response['location']: |
| 290 |
to_do = Actions.Update |
| 291 |
elif (self.sku is not None) and self.sku != response['sku']: |
| 292 |
to_do = Actions.Update |
| 293 |
elif (self.identity is not None) and update_identity: |
| 294 |
to_do = Actions.Update |
| 295 |
else: |
| 296 |
to_do = Actions.NoAction |
| 297 |
|
| 298 |
self.log("Create / Update the container registry instance") |
| 299 |
if self.check_mode: |
| 300 |
return self.results |
| 301 |
|
| 302 |
self.results.update(self.create_update_containerregistry(to_do)) |
| 303 |
if to_do != Actions.NoAction: |
| 304 |
self.results['changed'] = True |
| 305 |
else: |
| 306 |
self.results.update(response) |
| 307 |
self.results['changed'] = False |
| 308 |
|
| 309 |
self.log("Container registry instance created or updated") |
| 310 |
elif self.state == 'absent' and response: |
| 311 |
self.results['changed'] = True |
| 312 |
if self.check_mode: |
| 313 |
return self.results |
| 314 |
self.delete_containerregistry() |
| 315 |
self.log("Container registry instance deleted") |
| 316 |
|
| 317 |
return self.results |
| 318 |
|
| 319 |
def create_update_containerregistry(self, to_do): |
| 320 |
''' |
| 321 |
Creates or updates a container registry. |
| 322 |
|
| 323 |
:return: deserialized container registry instance state dictionary |
| 324 |
''' |
| 325 |
self.log("Creating / Updating the container registry instance {0}".format(self.name)) |
| 326 |
|
| 327 |
try: |
| 328 |
if to_do != Actions.NoAction: |
| 329 |
if to_do == Actions.Create: |
| 330 |
name_status = self.containerregistry_client.registries.check_name_availability( |
| 331 |
registry_name_check_request=RegistryNameCheckRequest( |
| 332 |
name=self.name, |
| 333 |
) |
| 334 |
) |
| 335 |
if name_status.name_available: |
| 336 |
poller = self.containerregistry_client.registries.begin_create( |
| 337 |
resource_group_name=self.resource_group, |
| 338 |
registry_name=self.name, |
| 339 |
registry=Registry( |
| 340 |
location=self.location, |
| 341 |
sku=Sku( |
| 342 |
name=self.sku |
| 343 |
), |
| 344 |
tags=self.tags, |
| 345 |
admin_user_enabled=self.admin_user_enabled, |
| 346 |
identity=self.identity |
| 347 |
) |
| 348 |
) |
| 349 |
else: |
| 350 |
raise Exception("Invalid registry name. reason: " + name_status.reason + " message: " + name_status.message) |
| 351 |
else: |
| 352 |
registry = self.containerregistry_client.registries.get(resource_group_name=self.resource_group, registry_name=self.name) |
| 353 |
if registry is not None: |
| 354 |
poller = self.containerregistry_client.registries.begin_update( |
| 355 |
resource_group_name=self.resource_group, |
| 356 |
registry_name=self.name, |
| 357 |
registry_update_parameters=RegistryUpdateParameters( |
| 358 |
sku=Sku( |
| 359 |
name=self.sku |
| 360 |
), |
| 361 |
tags=self.tags, |
| 362 |
admin_user_enabled=self.admin_user_enabled, |
| 363 |
identity=self.identity |
| 364 |
) |
| 365 |
) |
| 366 |
else: |
| 367 |
raise Exception("Update registry failed as registry '" + self.name + "' doesn't exist.") |
| 368 |
response = self.get_poller_result(poller) |
| 369 |
if self.admin_user_enabled: |
| 370 |
credentials = self.containerregistry_client.registries.list_credentials(resource_group_name=self.resource_group, registry_name=self.name) |
| 371 |
else: |
| 372 |
self.log('Cannot perform credential operations as admin user is disabled') |
| 373 |
credentials = None |
| 374 |
else: |
| 375 |
response = None |
| 376 |
credentials = None |
| 377 |
except (Exception) as exc: |
| 378 |
self.log('Error attempting to create / update the container registry instance.') |
| 379 |
self.fail("Error creating / updating the container registry instance: {0}".format(str(exc))) |
| 380 |
return create_containerregistry_dict(response, credentials) |
| 381 |
|
| 382 |
def delete_containerregistry(self): |
| 383 |
''' |
| 384 |
Deletes the specified container registry in the specified subscription and resource group. |
| 385 |
|
| 386 |
:return: True |
| 387 |
''' |
| 388 |
self.log("Deleting the container registry instance {0}".format(self.name)) |
| 389 |
try: |
| 390 |
poller = self.containerregistry_client.registries.begin_delete(resource_group_name=self.resource_group, registry_name=self.name) |
| 391 |
response = self.get_poller_result(poller) |
| 392 |
self.log("Delete container registry response: {0}".format(response)) |
| 393 |
except Exception as e: |
| 394 |
self.log('Error attempting to delete the container registry instance.') |
| 395 |
self.fail("Error deleting the container registry instance: {0}".format(str(e))) |
| 396 |
|
| 397 |
return True |
| 398 |
|
| 399 |
def get_containerregistry(self): |
| 400 |
''' |
| 401 |
Gets the properties of the specified container registry. |
| 402 |
|
| 403 |
:return: deserialized container registry state dictionary |
| 404 |
''' |
| 405 |
self.log("Checking if the container registry instance {0} is present".format(self.name)) |
| 406 |
found = False |
| 407 |
try: |
| 408 |
response = self.containerregistry_client.registries.get(resource_group_name=self.resource_group, registry_name=self.name) |
| 409 |
found = True |
| 410 |
self.log("Response : {0}".format(response)) |
| 411 |
self.log("Container registry instance : {0} found".format(response.name)) |
| 412 |
except ResourceNotFoundError as e: |
| 413 |
self.log('Did not find the container registry instance: {0}'.format(str(e))) |
| 414 |
response = None |
| 415 |
if found is True and self.admin_user_enabled is True: |
| 416 |
try: |
| 417 |
credentials = self.containerregistry_client.registries.list_credentials(resource_group_name=self.resource_group, registry_name=self.name) |
| 418 |
except Exception as e: |
| 419 |
self.fail('List registry credentials failed: {0}'.format(str(e))) |
| 420 |
credentials = None |
| 421 |
elif found is True and self.admin_user_enabled is False: |
| 422 |
credentials = None |
| 423 |
else: |
| 424 |
return None |
| 425 |
return create_containerregistry_dict(response, credentials) |
| 426 |
|
| 427 |
|
| 428 |
def main(): |
| 429 |
"""Main execution""" |
| 430 |
AzureRMContainerRegistry() |
| 431 |
|
| 432 |
|
| 433 |
if __name__ == '__main__': |
| 434 |
main() |