99
1010from azure .cli .testsdk import ScenarioTest , ResourceGroupPreparer
1111from .utils import ApicServicePreparer
12- from .constants import TEST_REGION
12+ from .constants import TEST_REGION , USERASSIGNED_IDENTITY
13+
14+ # if USERASSIGNED_IDENTITY is set, enable_system_assigned_identity is False, otherwise use system assigned identity
15+ enable_system_assigned_identity = False if USERASSIGNED_IDENTITY else True
1316
1417class ServiceCommandsTests (ScenarioTest ):
1518
@@ -111,7 +114,7 @@ def test_delete_service(self):
111114 self .cmd ('az apic show -g {rg} -n {s}' , expect_failure = True )
112115
113116 @ResourceGroupPreparer (name_prefix = "clirg" , location = TEST_REGION , random_name_length = 32 )
114- @ApicServicePreparer (enable_system_assigned_identity = True )
117+ @ApicServicePreparer (enable_system_assigned_identity = enable_system_assigned_identity )
115118 def test_import_from_apim (self ):
116119 self .kwargs .update ({
117120 'apim_name' : self .create_random_name (prefix = 'cli' , length = 24 )
@@ -127,7 +130,7 @@ def test_import_from_apim(self):
127130
128131
129132 @ResourceGroupPreparer (name_prefix = "clirg" , location = TEST_REGION , random_name_length = 32 )
130- @ApicServicePreparer (enable_system_assigned_identity = True )
133+ @ApicServicePreparer (enable_system_assigned_identity = enable_system_assigned_identity )
131134 def test_import_from_apim_for_one_api (self ):
132135 self .kwargs .update ({
133136 'apim_name' : self .create_random_name (prefix = 'cli' , length = 24 )
@@ -146,7 +149,7 @@ def test_import_from_apim_for_one_api(self):
146149 ])
147150
148151 @ResourceGroupPreparer (name_prefix = "clirg" , location = TEST_REGION , random_name_length = 32 )
149- @ApicServicePreparer (enable_system_assigned_identity = True )
152+ @ApicServicePreparer (enable_system_assigned_identity = enable_system_assigned_identity )
150153 def test_import_from_apim_for_multiple_apis (self ):
151154 self .kwargs .update ({
152155 'apim_name' : self .create_random_name (prefix = 'cli' , length = 24 )
@@ -195,7 +198,7 @@ def test_examples_delete_service(self):
195198 self .cmd ('az apic show -g {rg} -n {s}' , expect_failure = True )
196199
197200 @ResourceGroupPreparer (name_prefix = "clirg" , location = TEST_REGION , random_name_length = 32 )
198- @ApicServicePreparer (enable_system_assigned_identity = True )
201+ @ApicServicePreparer (enable_system_assigned_identity = enable_system_assigned_identity )
199202 def test_examples_import_all_apis_from_apim (self ):
200203 self .kwargs .update ({
201204 'apim_name' : self .create_random_name (prefix = 'cli' , length = 24 )
@@ -204,7 +207,7 @@ def test_examples_import_all_apis_from_apim(self):
204207 self .cmd ('az apic import-from-apim -g {rg} --service-name {s} --apim-name {apim_name} --apim-apis *' )
205208
206209 @ResourceGroupPreparer (name_prefix = "clirg" , location = TEST_REGION , random_name_length = 32 )
207- @ApicServicePreparer (enable_system_assigned_identity = True )
210+ @ApicServicePreparer (enable_system_assigned_identity = enable_system_assigned_identity )
208211 def test_examples_import_selected_apis_from_apim (self ):
209212 self .kwargs .update ({
210213 'apim_name' : self .create_random_name (prefix = 'cli' , length = 24 )
@@ -240,7 +243,7 @@ def _prepare_apim(self):
240243 apic_service = self .cmd ('az apic show -g {rg} -n {s}' ).get_output_in_json ()
241244 self .kwargs .update ({
242245 'identity_id' : apic_service ['identity' ]['principalId' ]
243- })
246+ }) if enable_system_assigned_identity else None
244247 # Create APIM service
245248 apim_service = self .
cmd (
'az apim create -g {rg} --name {apim_name} --publisher-name test --publisher-email [email protected] --sku-name Consumption' ).
get_output_in_json ()
246249 # Add echo api
@@ -251,7 +254,13 @@ def _prepare_apim(self):
251254 self .cmd ('az apim api operation create -g {rg} --service-name {apim_name} --api-id foo --url-template "/foo" --method "GET" --display-name "GetOperation"' )
252255 apim_id = apim_service ['id' ]
253256 self .kwargs .update ({
254- 'apim_id' : apim_id
257+ 'apim_id' : apim_id ,
258+ 'usi_id' : USERASSIGNED_IDENTITY
255259 })
256- # Grant system assigned identity of API Center access to APIM
257- self .cmd ('az role assignment create --role "API Management Service Reader Role" --assignee-object-id {identity_id} --assignee-principal-type ServicePrincipal --scope {apim_id}' )
260+
261+ if enable_system_assigned_identity :
262+ # Grant system assigned identity of API Center access to APIM
263+ self .cmd ('az role assignment create --role "API Management Service Reader Role" --assignee-object-id {identity_id} --assignee-principal-type ServicePrincipal --scope {apim_id}' )
264+ else :
265+ # add user-assigned identity to api center service:
266+ self .cmd ('az apic update --name {s} -g {rg} --identity {{type:UserAssigned,user-assigned-identities:{usi_id}}}' )
0 commit comments