Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure container registry tags #830

Merged
merged 31 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
40ee355
update to latest containerregistry sdk
l3ender Apr 13, 2022
206ea80
add support for retrieving container registry tags
l3ender Apr 16, 2022
6ee59d9
Merge remote-tracking branch 'upstream/dev' into acr-tags
l3ender Apr 16, 2022
ec58e33
formatting cleanup
l3ender Apr 17, 2022
b192319
module for deleting container tags
l3ender Apr 17, 2022
eda2fa6
cleanup/formatting
l3ender Apr 17, 2022
d5610bc
Merge branch 'dev' into acr-tags
l3ender Apr 18, 2022
c34f279
bump sanity requirements to latest
l3ender Apr 18, 2022
47fdab1
add initial containerregistry tag tests
l3ender Apr 18, 2022
0d89aca
initial test coverage for acr tags
l3ender Apr 19, 2022
40c7d31
Merge branch 'dev' into acr-tags
l3ender Apr 20, 2022
544c8a1
correct delete registry method and idempotency
l3ender Apr 20, 2022
b56ece7
add support for tag import to acr
l3ender Apr 20, 2022
a3f1c9a
formatting
l3ender Apr 20, 2022
d5cc330
clean up examples
l3ender Apr 20, 2022
87f4f94
Apply suggestions from code review
l3ender Apr 21, 2022
b842303
Merge remote-tracking branch 'upstream/dev' into acr-tags
l3ender Apr 21, 2022
c6e3da2
rename module to match collection standards
l3ender Apr 21, 2022
2b8a728
add 'returned' property to module return doc
l3ender Apr 21, 2022
e245f5b
correct sanity errors
l3ender Apr 21, 2022
24482ec
update module test name
l3ender Apr 22, 2022
d7ce174
Merge remote-tracking branch 'upstream/dev' into acr-tags
l3ender Apr 25, 2022
81e4296
ensure all containerregistry modules use track2
l3ender Apr 25, 2022
668411c
Apply suggestions from code review
l3ender Apr 26, 2022
cba6733
update formatting after review suggestions
l3ender Apr 26, 2022
ad539f4
use valid latest api version
l3ender Apr 26, 2022
103ce7a
correct not found scenario and add test
l3ender Apr 26, 2022
309cfd5
Merge remote-tracking branch 'upstream/dev' into acr-tags
l3ender May 14, 2022
2125b2f
Merge remote-tracking branch 'upstream/dev' into acr-tags
l3ender May 24, 2022
2796b68
provide required params for replication mgmt
l3ender May 24, 2022
999c853
Merge remote-tracking branch 'upstream/dev' into acr-tags
l3ender Jun 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,7 @@ def containerregistry_client(self):
if not self._containerregistry_client:
self._containerregistry_client = self.get_mgmt_svc_client(ContainerRegistryManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager,
is_track2=True,
api_version='2017-10-01')
l3ender marked this conversation as resolved.
Show resolved Hide resolved

return self._containerregistry_client
Expand Down
56 changes: 25 additions & 31 deletions plugins/modules/azure_rm_containerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,13 @@
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

try:
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import ResourceNotFoundError
from azure.mgmt.containerregistry.models import (
Registry,
RegistryUpdateParameters,
StorageAccountProperties,
Sku,
SkuName,
SkuTier,
ProvisioningState,
PasswordName,
WebhookCreateParameters,
WebhookUpdateParameters,
WebhookAction,
WebhookStatus
RegistryNameCheckRequest,
)
from azure.mgmt.containerregistry import ContainerRegistryManagementClient
except ImportError as exc:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -262,10 +253,9 @@ def exec_module(self, **kwargs):
if not self.location:
self.location = resource_group.location

# Check if the container registry instance already present in the RG
if self.state == 'present':
response = self.get_containerregistry()
response = self.get_containerregistry()

if self.state == 'present':
if not response:
to_do = Actions.Create
else:
Expand All @@ -292,7 +282,8 @@ def exec_module(self, **kwargs):
self.results['changed'] = False

self.log("Container registry instance created or updated")
elif self.state == 'absent':
elif self.state == 'absent' and response:
self.results['changed'] = True
if self.check_mode:
return self.results
self.delete_containerregistry()
Expand All @@ -311,9 +302,13 @@ def create_update_containerregistry(self, to_do):
try:
if to_do != Actions.NoAction:
if to_do == Actions.Create:
name_status = self.containerregistry_client.registries.check_name_availability(self.name)
name_status = self.containerregistry_client.registries.check_name_availability(
registry_name_check_request=RegistryNameCheckRequest(
name=self.name,
)
)
if name_status.name_available:
poller = self.containerregistry_client.registries.create(
poller = self.containerregistry_client.registries.begin_create(
resource_group_name=self.resource_group,
registry_name=self.name,
registry=Registry(
Expand All @@ -328,9 +323,9 @@ def create_update_containerregistry(self, to_do):
else:
raise Exception("Invalid registry name. reason: " + name_status.reason + " message: " + name_status.message)
else:
registry = self.containerregistry_client.registries.get(self.resource_group, self.name)
registry = self.containerregistry_client.registries.get(resource_group_name=self.resource_group, registry_name=self.name)
if registry is not None:
poller = self.containerregistry_client.registries.update(
poller = self.containerregistry_client.registries.begin_update(
resource_group_name=self.resource_group,
registry_name=self.name,
registry_update_parameters=RegistryUpdateParameters(
Expand All @@ -345,14 +340,14 @@ def create_update_containerregistry(self, to_do):
raise Exception("Update registry failed as registry '" + self.name + "' doesn't exist.")
response = self.get_poller_result(poller)
if self.admin_user_enabled:
credentials = self.containerregistry_client.registries.list_credentials(self.resource_group, self.name)
credentials = self.containerregistry_client.registries.list_credentials(resource_group_name=self.resource_group, registry_name=self.name)
else:
self.log('Cannot perform credential operations as admin user is disabled')
credentials = None
else:
response = None
credentials = None
except (CloudError, Exception) as exc:
except (Exception) as exc:
self.log('Error attempting to create / update the container registry instance.')
self.fail("Error creating / updating the container registry instance: {0}".format(str(exc)))
return create_containerregistry_dict(response, credentials)
Expand All @@ -365,8 +360,10 @@ def delete_containerregistry(self):
'''
self.log("Deleting the container registry instance {0}".format(self.name))
try:
self.containerregistry_client.registries.delete(self.resource_group, self.name).wait()
except CloudError as e:
poller = self.containerregistry_client.registries.begin_delete(resource_group_name=self.resource_group, registry_name=self.name)
response = self.get_poller_result(poller)
self.log("Delete container registry response: {0}".format(response))
except Exception as e:
self.log('Error attempting to delete the container registry instance.')
self.fail("Error deleting the container registry instance: {0}".format(str(e)))

Expand All @@ -381,20 +378,17 @@ def get_containerregistry(self):
self.log("Checking if the container registry instance {0} is present".format(self.name))
found = False
try:
response = self.containerregistry_client.registries.get(self.resource_group, self.name)
response = self.containerregistry_client.registries.get(resource_group_name=self.resource_group, registry_name=self.name)
found = True
self.log("Response : {0}".format(response))
self.log("Container registry instance : {0} found".format(response.name))
except CloudError as e:
if e.error.error == 'ResourceNotFound':
self.log('Did not find the container registry instance: {0}'.format(str(e)))
else:
self.fail('Error while trying to get container registry instance: {0}'.format(str(e)))
except ResourceNotFoundError as e:
self.log('Did not find the container registry instance: {0}'.format(str(e)))
response = None
if found is True and self.admin_user_enabled is True:
try:
credentials = self.containerregistry_client.registries.list_credentials(self.resource_group, self.name)
except CloudError as e:
credentials = self.containerregistry_client.registries.list_credentials(resource_group_name=self.resource_group, registry_name=self.name)
except Exception as e:
self.fail('List registry credentials failed: {0}'.format(str(e)))
credentials = None
elif found is True and self.admin_user_enabled is False:
Expand Down
23 changes: 7 additions & 16 deletions plugins/modules/azure_rm_containerregistry_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@

from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

try:
from msrestazure.azure_exceptions import CloudError
from msrestazure.azure_operation import AzureOperationPoller
from azure.mgmt.containerregistry import ContainerRegistryManagementClient
from msrest.serialization import Model
except ImportError:
# This is handled in azure_rm_common
pass


class AzureRMContainerRegistryInfo(AzureRMModuleBase):
def __init__(self):
Expand Down Expand Up @@ -207,8 +198,8 @@ def get(self):
response = self.containerregistry_client.registries.get(resource_group_name=self.resource_group,
registry_name=self.name)
self.log("Response : {0}".format(response))
except CloudError as e:
self.log('Could not get facts for Registries.')
except Exception as e:
self.log("Could not get facts for Registries: {0}".format(str(e)))

if response is not None:
if self.has_tags(response.tags, self.tags):
Expand All @@ -222,8 +213,8 @@ def list_all(self):
try:
response = self.containerregistry_client.registries.list()
self.log("Response : {0}".format(response))
except CloudError as e:
self.fail('Could not get facts for Registries.')
except Exception as e:
self.fail("Could not get facts for Registries: {0}".format(str(e)))

if response is not None:
for item in response:
Expand All @@ -237,8 +228,8 @@ def list_by_resource_group(self):
try:
response = self.containerregistry_client.registries.list_by_resource_group(resource_group_name=self.resource_group)
self.log("Response : {0}".format(response))
except CloudError as e:
self.fail('Could not get facts for Registries.')
except Exception as e:
self.fail("Could not get facts for Registries: {0}".format(str(e)))

if response is not None:
for item in response:
Expand All @@ -254,7 +245,7 @@ def format_item(self, item):
admin_user_enabled = d['admin_user_enabled']

if self.retrieve_credentials and admin_user_enabled:
credentials = self.containerregistry_client.registries.list_credentials(resource_group, name).as_dict()
credentials = self.containerregistry_client.registries.list_credentials(resource_group_name=resource_group, registry_name=name).as_dict()
for index in range(len(credentials['passwords'])):
password = credentials['passwords'][index]
if password['name'] == 'password':
Expand Down
Loading