99from knack .log import get_logger
1010
1111from azure .cli .core .commands .client_factory import get_mgmt_service_client , get_subscription_id
12- from azure .cli .core .profiles import ResourceType , get_sdk
1312from azure .cli .core .util import should_disable_connection_verify
1413from azure .cli .core .azclierror import ArgumentUsageError , CLIInternalError , InvalidArgumentValueError , ManualInterrupt
15- from ._validators import process_grafana_create_namespace
14+ from azure .mgmt .authorization import AuthorizationManagementClient
15+ from azure .mgmt .authorization .models import RoleAssignmentCreateParameters , PrincipalType
1616
1717from azure .cli .core .aaz import AAZBoolArg , AAZListArg , AAZStrArg
1818from .aaz .latest .grafana ._create import Create as _GrafanaCreate
1919from .aaz .latest .grafana ._delete import Delete as _GrafanaDelete
2020from .aaz .latest .grafana ._update import Update as _GrafanaUpdate
2121
2222from ._client_factory import cf_amg
23- from .utils import get_yes_or_no_option , MGMT_SERVICE_CLIENT_API_VERSION
23+ from .utils import get_yes_or_no_option
2424
2525logger = get_logger (__name__ )
2626
@@ -62,8 +62,6 @@ def pre_operations(self):
6262 if not args .skip_system_assigned_identity :
6363 args .identity = {"type" : "SystemAssigned" }
6464
65- process_grafana_create_namespace (self .ctx , self .ctx .args )
66-
6765 # override the output method to create role assignments after instance creation
6866 def _output (self , * args , ** kwargs ):
6967 from azure .cli .core .commands .arm import resolve_role_id
@@ -86,14 +84,12 @@ def _output(self, *args, **kwargs):
8684 grafana_admin_role_id = resolve_role_id (cli_ctx , "Grafana Admin" , subscription_scope )
8785
8886 for principal_id in principal_ids :
89- principal_types = {"User" , "Group" }
90- _create_role_assignment (cli_ctx , principal_id , principal_types , grafana_admin_role_id ,
87+ _create_role_assignment (cli_ctx , principal_id , grafana_admin_role_id ,
9188 self .ctx .vars .instance .id )
9289
9390 if self .ctx .vars .instance .identity :
9491 monitoring_reader_role_id = resolve_role_id (cli_ctx , "Monitoring Reader" , subscription_scope )
95- principal_types = {"ServicePrincipal" }
96- _create_role_assignment (cli_ctx , self .ctx .vars .instance .identity .principal_id , {"ServicePrincipal" },
92+ _create_role_assignment (cli_ctx , self .ctx .vars .instance .identity .principal_id ,
9793 monitoring_reader_role_id , subscription_scope )
9894
9995 result = self .deserialize_output (self .ctx .vars .instance , client_flatten = True )
@@ -163,23 +159,22 @@ def _get_login_account_principal_id(cli_ctx):
163159 return result [0 ]['id' ]
164160
165161
166- def _create_role_assignment (cli_ctx , principal_id , principal_types , role_definition_id , scope ):
162+ def _create_role_assignment (cli_ctx , principal_id , role_definition_id , scope ):
167163 import time
168164 from azure .core .exceptions import HttpResponseError , ResourceExistsError
169165
170- assignments_client = get_mgmt_service_client (cli_ctx , ResourceType .MGMT_AUTHORIZATION ,
171- api_version = MGMT_SERVICE_CLIENT_API_VERSION ).role_assignments
172- RoleAssignmentCreateParameters = get_sdk (cli_ctx , ResourceType .MGMT_AUTHORIZATION ,
173- 'RoleAssignmentCreateParameters' , mod = 'models' ,
174- operation_group = 'role_assignments' )
175- parameters = RoleAssignmentCreateParameters (role_definition_id = role_definition_id ,
176- principal_id = principal_id , principal_type = principal_types .pop ())
166+ assignments_client = get_mgmt_service_client (cli_ctx , AuthorizationManagementClient ).role_assignments
167+ principal_types = [p .value for p in PrincipalType ]
168+ current_principal_type = principal_types .pop (0 )
177169
178170 logger .info ("Creating an assignment with a role '%s' on the scope of '%s'" , role_definition_id , scope )
179171 retry_times = 36
180172 assignment_name = _gen_guid ()
181173 for retry_time in range (0 , retry_times ):
182174 try :
175+ parameters = RoleAssignmentCreateParameters (role_definition_id = role_definition_id ,
176+ principal_id = principal_id ,
177+ principal_type = current_principal_type )
183178 assignments_client .create (scope = scope , role_assignment_name = assignment_name ,
184179 parameters = parameters )
185180 break
@@ -188,9 +183,11 @@ def _create_role_assignment(cli_ctx, principal_id, principal_types, role_definit
188183 break
189184 except HttpResponseError as ex :
190185 if 'UnmatchedPrincipalType' in ex .message : # try each principal_type until we get the right one
191- parameters = RoleAssignmentCreateParameters (role_definition_id = role_definition_id ,
192- principal_id = principal_id ,
193- principal_type = principal_types .pop ())
186+ logger .debug ("Principal type '%s' is not matched" , current_principal_type )
187+ try :
188+ current_principal_type = principal_types .pop (0 )
189+ except :
190+ raise CLIInternalError ("Failed to create a role assignment. No matching principal types found." )
194191 continue
195192 if 'role assignment already exists' in ex .message : # Exception from Track-1 SDK
196193 logger .info ('Role assignment already exists' )
@@ -204,8 +201,7 @@ def _create_role_assignment(cli_ctx, principal_id, principal_types, role_definit
204201
205202
206203def _delete_role_assignment (cli_ctx , principal_id , role_definition_id = None , scope = None ):
207- assignments_client = get_mgmt_service_client (cli_ctx , ResourceType .MGMT_AUTHORIZATION ,
208- api_version = MGMT_SERVICE_CLIENT_API_VERSION ).role_assignments
204+ assignments_client = get_mgmt_service_client (cli_ctx , AuthorizationManagementClient ).role_assignments
209205 f = f"principalId eq '{ principal_id } '"
210206
211207 if role_definition_id and scope :
0 commit comments