From ed2cd40a5b6bf3be31de822c0090457b76bc2221 Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Wed, 29 Aug 2018 13:52:18 +0800 Subject: [PATCH] adding new clients to common (#44804) * adding new clients to common * container registry and instance clients to the common * fix problem --- lib/ansible/module_utils/azure_rm_common.py | 24 +++++++++++++++ .../cloud/azure/azure_rm_containerinstance.py | 15 ++++------ .../cloud/azure/azure_rm_containerregistry.py | 29 +++++-------------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/lib/ansible/module_utils/azure_rm_common.py b/lib/ansible/module_utils/azure_rm_common.py index 0e0d6605daf..7bc1fba7905 100644 --- a/lib/ansible/module_utils/azure_rm_common.py +++ b/lib/ansible/module_utils/azure_rm_common.py @@ -153,6 +153,8 @@ try: from adal.authentication_context import AuthenticationContext from azure.mgmt.rdbms.postgresql import PostgreSQLManagementClient from azure.mgmt.rdbms.mysql import MySQLManagementClient + from azure.mgmt.containerregistry import ContainerRegistryManagementClient + from azure.mgmt.containerinstance import ContainerInstanceManagementClient except ImportError as exc: HAS_AZURE_EXC = exc HAS_AZURE = False @@ -276,6 +278,8 @@ class AzureRMModuleBase(object): self._containerservice_client = None self._mysql_client = None self._postgresql_client = None + self._containerregistry_client = None + self._containerinstance_client = None self._adfs_authority_url = None self._resource = None @@ -1075,3 +1079,23 @@ class AzureRMModuleBase(object): self._mysql_client = self.get_mgmt_svc_client(MySQLManagementClient, base_url=self._cloud_environment.endpoints.resource_manager) return self._mysql_client + + @property + def containerregistry_client(self): + self.log('Getting container registry mgmt client') + if not self._containerregistry_client: + self._containerregistry_client = self.get_mgmt_svc_client(ContainerRegistryManagementClient, + base_url=self._cloud_environment.endpoints.resource_manager, + api_version='2017-10-01') + + return self._containerregistry_client + + @property + def containerinstance_client(self): + self.log('Getting container instance mgmt client') + if not self._containerinstance_client: + self._containerinstance_client = self.get_mgmt_svc_client(ContainerInstanceManagementClient, + base_url=self._cloud_environment.endpoints.resource_manager, + api_version='2018-06-01') + + return self._containerinstance_client diff --git a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py index bfeecbe89eb..2ab58cb0b5d 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py @@ -257,7 +257,6 @@ class AzureRMContainerInstance(AzureRMModuleBase): self.containers = None self.results = dict(changed=False, state=dict()) - self.client = None self.cgmodels = None super(AzureRMContainerInstance, self).__init__(derived_arg_spec=self.module_arg_spec, @@ -274,10 +273,8 @@ class AzureRMContainerInstance(AzureRMModuleBase): response = None results = dict() - self.client = self.get_mgmt_svc_client(ContainerInstanceManagementClient) - # since this client hasn't been upgraded to expose models directly off the OperationClass, fish them out - self.cgmodels = self.client.container_groups.models + self.cgmodels = self.containerinstance_client.container_groups.models resource_group = self.get_resource_group(self.resource_group) @@ -380,9 +377,9 @@ class AzureRMContainerInstance(AzureRMModuleBase): os_type=self.os_type, volumes=None) - response = self.client.container_groups.create_or_update(resource_group_name=self.resource_group, - container_group_name=self.name, - container_group=parameters) + response = self.containerinstance_client.container_groups.create_or_update(resource_group_name=self.resource_group, + container_group_name=self.name, + container_group=parameters) if isinstance(response, AzureOperationPoller): response = self.get_poller_result(response) @@ -396,7 +393,7 @@ class AzureRMContainerInstance(AzureRMModuleBase): :return: True ''' self.log("Deleting the container instance {0}".format(self.name)) - response = self.client.container_groups.delete(resource_group_name=self.resource_group, container_group_name=self.name) + response = self.containerinstance_client.container_groups.delete(resource_group_name=self.resource_group, container_group_name=self.name) return True def get_containerinstance(self): @@ -408,7 +405,7 @@ class AzureRMContainerInstance(AzureRMModuleBase): self.log("Checking if the container instance {0} is present".format(self.name)) found = False try: - response = self.client.container_groups.get(resource_group_name=self.resource_group, container_group_name=self.name) + response = self.containerinstance_client.container_groups.get(resource_group_name=self.resource_group, container_group_name=self.name) found = True self.log("Response : {0}".format(response)) self.log("Container instance : {0} found".format(response.name)) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_containerregistry.py b/lib/ansible/modules/cloud/azure/azure_rm_containerregistry.py index 298d5daf306..2f15e5dc2f2 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_containerregistry.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_containerregistry.py @@ -245,7 +245,6 @@ class AzureRMContainerRegistry(AzureRMModuleBase): self.state = None self.sku = None self.tags = None - self._containerregistry_mgmt_client = None self.results = dict(changed=False, state=dict()) @@ -315,9 +314,9 @@ class AzureRMContainerRegistry(AzureRMModuleBase): try: if to_do != Actions.NoAction: if to_do == Actions.Create: - name_status = self.containerregistry_mgmt_client.registries.check_name_availability(self.name) + name_status = self.containerregistry_client.registries.check_name_availability(self.name) if name_status.name_available: - poller = self.containerregistry_mgmt_client.registries.create( + poller = self.containerregistry_client.registries.create( resource_group_name=self.resource_group, registry_name=self.name, registry=Registry( @@ -332,9 +331,9 @@ class AzureRMContainerRegistry(AzureRMModuleBase): else: raise Exception("Invalid registry name. reason: " + name_status.reason + " message: " + name_status.message) else: - registry = self.containerregistry_mgmt_client.registries.get(self.resource_group, self.name) + registry = self.containerregistry_client.registries.get(self.resource_group, self.name) if registry is not None: - poller = self.containerregistry_mgmt_client.registries.update( + poller = self.containerregistry_client.registries.update( resource_group_name=self.resource_group, registry_name=self.name, registry_update_parameters=RegistryUpdateParameters( @@ -349,7 +348,7 @@ class AzureRMContainerRegistry(AzureRMModuleBase): 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_mgmt_client.registries.list_credentials(self.resource_group, self.name) + credentials = self.containerregistry_client.registries.list_credentials(self.resource_group, self.name) else: self.log('Cannot perform credential operations as admin user is disabled') credentials = None @@ -369,7 +368,7 @@ class AzureRMContainerRegistry(AzureRMModuleBase): ''' self.log("Deleting the container registry instance {0}".format(self.name)) try: - self.containerregistry_mgmt_client.registries.delete(self.resource_group, self.name).wait() + self.containerregistry_client.registries.delete(self.resource_group, self.name).wait() except CloudError as e: self.log('Error attempting to delete the container registry instance.') self.fail("Error deleting the container registry instance: {0}".format(str(e))) @@ -385,7 +384,7 @@ class AzureRMContainerRegistry(AzureRMModuleBase): self.log("Checking if the container registry instance {0} is present".format(self.name)) found = False try: - response = self.containerregistry_mgmt_client.registries.get(self.resource_group, self.name) + response = self.containerregistry_client.registries.get(self.resource_group, self.name) found = True self.log("Response : {0}".format(response)) self.log("Container registry instance : {0} found".format(response.name)) @@ -397,7 +396,7 @@ class AzureRMContainerRegistry(AzureRMModuleBase): response = None if found is True and self.admin_user_enabled is True: try: - credentials = self.containerregistry_mgmt_client.registries.list_credentials(self.resource_group, self.name) + credentials = self.containerregistry_client.registries.list_credentials(self.resource_group, self.name) except CloudError as e: self.fail('List registry credentials failed: {0}'.format(str(e))) credentials = None @@ -407,18 +406,6 @@ class AzureRMContainerRegistry(AzureRMModuleBase): return None return create_containerregistry_dict(response, credentials) - @property - def containerregistry_mgmt_client(self): - self.log('Getting container registry mgmt client') - if not self._containerregistry_mgmt_client: - self._containerregistry_mgmt_client = self.get_mgmt_svc_client( - ContainerRegistryManagementClient, - base_url=self._cloud_environment.endpoints.resource_manager, - api_version='2017-10-01' - ) - - return self._containerregistry_mgmt_client - def main(): """Main execution"""