From 132a13ccfda2661277191dcafa0fdcd9f24681f8 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Thu, 18 Aug 2022 17:02:01 +0800 Subject: [PATCH 1/2] Upgrade azure-mgmt-authorizaiton to 2.0.0 --- plugins/module_utils/azure_rm_common.py | 7 ++++--- plugins/modules/azure_rm_roleassignment.py | 16 +++++++--------- plugins/modules/azure_rm_roleassignment_info.py | 11 +++++------ plugins/modules/azure_rm_roledefinition.py | 10 +++++----- plugins/modules/azure_rm_roledefinition_info.py | 8 ++++---- requirements-azure.txt | 2 +- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/plugins/module_utils/azure_rm_common.py b/plugins/module_utils/azure_rm_common.py index d2e914330..4b034838e 100644 --- a/plugins/module_utils/azure_rm_common.py +++ b/plugins/module_utils/azure_rm_common.py @@ -88,7 +88,7 @@ def default_api_version(self): # For now, we have to copy from azure-cli AZURE_API_PROFILES = { 'latest': { - 'AuthorizationManagementClient': '2018-09-01-preview', + 'AuthorizationManagementClient': '2020-04-01-preview', 'ContainerInstanceManagementClient': '2018-02-01-preview', 'ComputeManagementClient': dict( default_api_version='2018-10-01', @@ -1012,12 +1012,13 @@ def authorization_client(self): if not self._authorization_client: self._authorization_client = self.get_mgmt_svc_client(AuthorizationManagementClient, base_url=self._cloud_environment.endpoints.resource_manager, - api_version='2018-09-01-preview') + is_track2=True, + api_version='2020-04-01-preview') return self._authorization_client @property def authorization_models(self): - return AuthorizationManagementClient.models('2018-09-01-preview') + return AuthorizationManagementClient.models('2020-04-01-preview') @property def subscription_client(self): diff --git a/plugins/modules/azure_rm_roleassignment.py b/plugins/modules/azure_rm_roleassignment.py index b7f4949e8..36a294917 100644 --- a/plugins/modules/azure_rm_roleassignment.py +++ b/plugins/modules/azure_rm_roleassignment.py @@ -143,11 +143,9 @@ sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ''' -import uuid -from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase - try: - from msrestazure.azure_exceptions import CloudError + import uuid + from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase except ImportError: # This is handled in azure_rm_common @@ -266,7 +264,7 @@ def create_roleassignment(self): role_assignment_name=self.name, parameters=parameters) - except CloudError as exc: + except Exception as exc: self.log('Error attempting to create role assignment.') self.fail("Error creating role assignment: {0}".format(str(exc))) return self.roleassignment_to_dict(response) @@ -280,7 +278,7 @@ def delete_roleassignment(self, assignment_id): self.log("Deleting the role assignment {0}".format(self.name)) try: response = self.authorization_client.role_assignments.delete_by_id(role_id=assignment_id) - except CloudError as e: + except Exception as e: self.log('Error attempting to delete the role assignment.') self.fail("Error deleting the role assignment: {0}".format(str(e))) @@ -307,7 +305,7 @@ def get_roleassignment(self): != self.role_definition_id.split('/')[-1].lower()): self.fail('State Mismatch Error: The assignment ID exists, but does not match the provided role.') - except CloudError as ex: + except Exception as ex: self.log("Didn't find role assignments id {0}".format(self.id)) elif self.name and self.scope: @@ -321,7 +319,7 @@ def get_roleassignment(self): != self.role_definition_id.split('/')[-1].lower()): self.fail('State Mismatch Error: The assignment name exists, but does not match the provided role.') - except CloudError as ex: + except Exception as ex: self.log("Didn't find role assignment by name {0} at scope {1}".format(self.name, self.scope)) else: @@ -337,7 +335,7 @@ def get_roleassignment(self): self.fail('If id or name are not supplied, then assignee_object_id and role_definition_id are required.') if response: role_assignment = response[0] - except CloudError as ex: + except Exception as ex: self.log("Didn't find role assignments for subscription {0}".format(self.subscription_id)) return role_assignment diff --git a/plugins/modules/azure_rm_roleassignment_info.py b/plugins/modules/azure_rm_roleassignment_info.py index 65a76686b..b7ddf3d1f 100644 --- a/plugins/modules/azure_rm_roleassignment_info.py +++ b/plugins/modules/azure_rm_roleassignment_info.py @@ -138,12 +138,11 @@ ''' try: - from msrestazure.azure_exceptions import CloudError + from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase except ImportError: # This is handled in azure_rm_common pass -from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase class AzureRMRoleAssignmentInfo(AzureRMModuleBase): @@ -216,7 +215,7 @@ def get_by_id(self): response = [self.roleassignment_to_dict(a) for a in response] results = response - except CloudError as ex: + except Exception as ex: self.log("Didn't find role assignments id {0}".format(self.scope)) return results @@ -242,7 +241,7 @@ def get_by_name(self): results = response - except CloudError as ex: + except Exception as ex: self.log("Didn't find role assignment {0} in scope {1}".format(self.name, self.scope)) return results @@ -275,7 +274,7 @@ def list_assignments(self, filter=None): results = response - except CloudError as ex: + except Exception as ex: self.log("Didn't find role assignments in subscription {0}.".format(self.subscription_id)) return results @@ -310,7 +309,7 @@ def list_by_scope(self): results = response - except CloudError as ex: + except Exception as ex: self.log("Didn't find role assignments at scope {0}".format(self.scope)) return results diff --git a/plugins/modules/azure_rm_roledefinition.py b/plugins/modules/azure_rm_roledefinition.py index 6b157149b..d1cc4c2a5 100644 --- a/plugins/modules/azure_rm_roledefinition.py +++ b/plugins/modules/azure_rm_roledefinition.py @@ -108,9 +108,8 @@ try: from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase - from msrestazure.azure_exceptions import CloudError from msrestazure.azure_operation import AzureOperationPoller - from msrest.polling import LROPoller + from azure.core.polling import LROPoller from msrest.serialization import Model from azure.mgmt.authorization import AuthorizationManagementClient from azure.mgmt.authorization.model import (RoleDefinition, Permission) @@ -228,6 +227,7 @@ def exec_module(self, **kwargs): # get management client self._client = self.get_mgmt_svc_client(AuthorizationManagementClient, base_url=self._cloud_environment.endpoints.resource_manager, + is_track2=True, api_version="2018-01-01-preview") self.scope = self.build_scope() @@ -344,7 +344,7 @@ def create_update_roledefinition(self): if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller): response = self.get_poller_result(response) - except CloudError as exc: + except Exception as exc: self.log('Error attempting to create role definition.') self.fail("Error creating role definition: {0}".format(str(exc))) return roledefinition_to_dict(response) @@ -362,7 +362,7 @@ def delete_roledefinition(self, role_definition_id): role_definition_id=role_definition_id) if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller): response = self.get_poller_result(response) - except CloudError as e: + except Exception as e: self.log('Error attempting to delete the role definition.') self.fail("Error deleting the role definition: {0}".format(str(e))) @@ -394,7 +394,7 @@ def get_roledefinition(self): if len(roles) > 1: self.fail("Found multiple role definitions: {0}".format(roles)) - except CloudError as ex: + except Exception as ex: self.log("Didn't find role definition {0}".format(self.name)) return False diff --git a/plugins/modules/azure_rm_roledefinition_info.py b/plugins/modules/azure_rm_roledefinition_info.py index 3ff02faee..3899ce404 100644 --- a/plugins/modules/azure_rm_roledefinition_info.py +++ b/plugins/modules/azure_rm_roledefinition_info.py @@ -124,7 +124,6 @@ try: from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase - from msrestazure.azure_exceptions import CloudError from msrest.serialization import Model from azure.mgmt.authorization import AuthorizationManagementClient @@ -198,6 +197,7 @@ def exec_module(self, **kwargs): # get management client self._client = self.get_mgmt_svc_client(AuthorizationManagementClient, base_url=self._cloud_environment.endpoints.resource_manager, + is_track2=True, api_version="2018-01-01-preview") if self.id: @@ -242,7 +242,7 @@ def list(self): if len(roles) > 0: return [roledefinition_to_dict(r) for r in roles] - except CloudError as ex: + except Exception as ex: self.log("Didn't find role definition in scope {0}".format(self.scope)) return response @@ -267,7 +267,7 @@ def get_by_id(self): else: return [response] - except CloudError as ex: + except Exception as ex: self.log("Didn't find role definition by id {0}".format(self.id)) return [] @@ -297,7 +297,7 @@ def get_by_role_name(self): if len(roles) > 1: self.fail("Found multiple Role Definitions with name: {0}".format(self.role_name)) - except CloudError as ex: + except Exception as ex: self.log("Didn't find Role Definition by name {0}".format(self.role_name)) return [] diff --git a/requirements-azure.txt b/requirements-azure.txt index 61d314d64..1babc2fb1 100644 --- a/requirements-azure.txt +++ b/requirements-azure.txt @@ -5,7 +5,7 @@ azure-cli-core==2.34.0 azure-common==1.1.11 azure-identity==1.7.0 azure-mgmt-apimanagement==0.2.0 -azure-mgmt-authorization==0.51.1 +azure-mgmt-authorization==2.0.0 azure-mgmt-batch==5.0.1 azure-mgmt-cdn==3.0.0 azure-mgmt-compute==26.1.0 From cd6e03a0b85a06fcd350e93a3a6a922799e88501 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Thu, 18 Aug 2022 17:21:30 +0800 Subject: [PATCH 2/2] remove blank line --- plugins/modules/azure_rm_roleassignment_info.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/azure_rm_roleassignment_info.py b/plugins/modules/azure_rm_roleassignment_info.py index b7ddf3d1f..4fa338a81 100644 --- a/plugins/modules/azure_rm_roleassignment_info.py +++ b/plugins/modules/azure_rm_roleassignment_info.py @@ -144,7 +144,6 @@ pass - class AzureRMRoleAssignmentInfo(AzureRMModuleBase): def __init__(self):