@@ -56,12 +56,11 @@ def create_resource_group(credentials, subscription_id, **kwargs):
5656 resource_group_name = kwargs .get ("resource_group" , DefaultSettings .resource_group ),
5757 parameters = {
5858 'location' : kwargs .get ("region" , DefaultSettings .region ),
59- }
60- )
59+ })
6160 except CloudError as e :
6261 if i == 2 :
63- raise AccountSetupError (
64- "Unable to create resource group in region {}" . format ( kwargs .get ("region" , DefaultSettings .region )))
62+ raise AccountSetupError ("Unable to create resource group in region {}" . format (
63+ kwargs .get ("region" , DefaultSettings .region )))
6564 print (e .message )
6665 print ("Please try again." )
6766 kwargs ["resource_group" ] = prompt_with_default ("Azure Region" , DefaultSettings .region )
@@ -82,15 +81,10 @@ def create_storage_account(credentials, subscription_id, **kwargs):
8281 resource_group_name = kwargs .get ("resource_group" , DefaultSettings .resource_group ),
8382 account_name = kwargs .get ("storage_account" , DefaultSettings .storage_account ),
8483 parameters = StorageAccountCreateParameters (
85- sku = Sku (SkuName .standard_lrs ),
86- kind = Kind .storage ,
87- location = kwargs .get ('region' , DefaultSettings .region )
88- )
89- )
84+ sku = Sku (SkuName .standard_lrs ), kind = Kind .storage , location = kwargs .get ('region' , DefaultSettings .region )))
9085 return storage_account .result ().id
9186
9287
93-
9488def create_batch_account (credentials , subscription_id , ** kwargs ):
9589 """
9690 Create a Batch account
@@ -108,10 +102,7 @@ def create_batch_account(credentials, subscription_id, **kwargs):
108102 parameters = BatchAccountCreateParameters (
109103 location = kwargs .get ('region' , DefaultSettings .region ),
110104 auto_storage = AutoStorageBaseProperties (
111- storage_account_id = kwargs .get ('storage_account_id' , DefaultSettings .region )
112- )
113- )
114- )
105+ storage_account_id = kwargs .get ('storage_account_id' , DefaultSettings .region ))))
115106 return batch_account .result ().id
116107
117108
@@ -151,19 +142,13 @@ def create_vnet(credentials, subscription_id, **kwargs):
151142 resource_group_name = resource_group_name ,
152143 virtual_network_name = kwargs .get ("virtual_network_name" , DefaultSettings .virtual_network_name ),
153144 parameters = VirtualNetwork (
154- location = kwargs .get ("region" , DefaultSettings .region ),
155- address_space = AddressSpace (["10.0.0.0/24" ])
156- )
157- )
145+ location = kwargs .get ("region" , DefaultSettings .region ), address_space = AddressSpace (["10.0.0.0/24" ])))
158146 virtual_network = virtual_network .result ()
159147 subnet = network_client .subnets .create_or_update (
160148 resource_group_name = resource_group_name ,
161149 virtual_network_name = virtual_network_name ,
162150 subnet_name = subnet_name ,
163- subnet_parameters = Subnet (
164- address_prefix = '10.0.0.0/24'
165- )
166- )
151+ subnet_parameters = Subnet (address_prefix = '10.0.0.0/24' ))
167152 return subnet .result ().id
168153
169154
@@ -175,10 +160,7 @@ def create_aad_user(credentials, tenant_id, **kwargs):
175160 :param **application_name: str
176161 """
177162 graph_rbac_client = GraphRbacManagementClient (
178- credentials ,
179- tenant_id ,
180- base_url = AZURE_PUBLIC_CLOUD .endpoints .active_directory_graph_resource_id
181- )
163+ credentials , tenant_id , base_url = AZURE_PUBLIC_CLOUD .endpoints .active_directory_graph_resource_id )
182164 application_credential = uuid .uuid4 ()
183165 try :
184166 display_name = kwargs .get ("application_name" , DefaultSettings .application_name )
@@ -192,42 +174,32 @@ def create_aad_user(credentials, tenant_id, **kwargs):
192174 start_date = datetime (2000 , 1 , 1 , 0 , 0 , 0 , 0 , tzinfo = timezone .utc ),
193175 end_date = datetime (2299 , 12 , 31 , 0 , 0 , 0 , 0 , tzinfo = timezone .utc ),
194176 value = application_credential ,
195- key_id = uuid .uuid4 ()
196- )
197- ]
198- )
199- )
177+ key_id = uuid .uuid4 ())
178+ ]))
200179 service_principal = graph_rbac_client .service_principals .create (
201- ServicePrincipalCreateParameters (
202- app_id = application .app_id ,
203- account_enabled = True
204- )
205- )
180+ ServicePrincipalCreateParameters (app_id = application .app_id , account_enabled = True ))
206181 except GraphErrorException as e :
207182 if e .inner_exception .code == "Request_BadRequest" :
208- application = next (graph_rbac_client .applications .list (
209- filter = "identifierUris/any(c:c eq 'http://{}.com')" .format (display_name )))
183+ application = next (
184+ graph_rbac_client .applications .list (
185+ filter = "identifierUris/any(c:c eq 'http://{}.com')" .format (display_name )))
210186
211187 confirmation_prompt = "Previously created application with name {} found. " \
212188 "Would you like to use it? (y/n): " .format (application .display_name )
213189 prompt_for_confirmation (confirmation_prompt , e , ValueError ("Response not recognized. Please try again." ))
214- password_credentials = list (graph_rbac_client .applications .list_password_credentials (application_object_id = application .object_id ))
190+ password_credentials = list (
191+ graph_rbac_client .applications .list_password_credentials (application_object_id = application .object_id ))
215192 password_credentials .append (
216193 PasswordCredential (
217194 start_date = datetime (2000 , 1 , 1 , 0 , 0 , 0 , 0 , tzinfo = timezone .utc ),
218195 end_date = datetime (2299 , 12 , 31 , 0 , 0 , 0 , 0 , tzinfo = timezone .utc ),
219196 value = application_credential ,
220- key_id = uuid .uuid4 ()
221- )
222- )
197+ key_id = uuid .uuid4 ()))
223198 graph_rbac_client .applications .patch (
224199 application_object_id = application .object_id ,
225- parameters = ApplicationUpdateParameters (
226- password_credentials = password_credentials
227- )
228- )
229- service_principal = next (graph_rbac_client .service_principals .list (
230- filter = "appId eq '{}'" .format (application .app_id )))
200+ parameters = ApplicationUpdateParameters (password_credentials = password_credentials ))
201+ service_principal = next (
202+ graph_rbac_client .service_principals .list (filter = "appId eq '{}'" .format (application .app_id )))
231203 else :
232204 raise e
233205
@@ -244,21 +216,15 @@ def create_role_assignment(credentials, subscription_id, scope, principal_id):
244216 """
245217 authorization_client = AuthorizationManagementClient (credentials , subscription_id )
246218 role_name = 'Contributor'
247- roles = list (authorization_client .role_definitions .list (
248- scope ,
249- filter = "roleName eq '{}'" .format (role_name )
250- ))
219+ roles = list (authorization_client .role_definitions .list (scope , filter = "roleName eq '{}'" .format (role_name )))
251220 contributor_role = roles [0 ]
252221 for i in range (10 ):
253222 try :
254- authorization_client .role_assignments .create (
255- scope ,
256- uuid .uuid4 (),
257- {
258- 'role_definition_id' : contributor_role .id ,
259- 'principal_id' : principal_id
260- }
261- )
223+ authorization_client .role_assignments .create (scope , uuid .uuid4 (),
224+ {
225+ 'role_definition_id' : contributor_role .id ,
226+ 'principal_id' : principal_id
227+ })
262228 break
263229 except CloudError as e :
264230 # ignore error if service principal has not yet been created
@@ -321,15 +287,15 @@ def prompt_tenant_selection(tenant_ids):
321287 raise AccountSetupError ("Tenant selection not recognized after 3 attempts." )
322288
323289
324-
325290class Spinner :
326291 busy = False
327292 delay = 0.1
328293
329294 @staticmethod
330295 def spinning_cursor ():
331296 while 1 :
332- for cursor in '|/-\\ ' : yield cursor
297+ for cursor in '|/-\\ ' :
298+ yield cursor
333299
334300 def __init__ (self , delay = None ):
335301 self .spinner_generator = self .spinning_cursor ()
@@ -358,7 +324,6 @@ def stop(self):
358324 time .sleep (self .delay )
359325
360326
361-
362327if __name__ == "__main__" :
363328 print ("\n Getting credentials." )
364329 # get credentials and tenant_id
@@ -374,15 +339,22 @@ def stop(self):
374339 "Default values are provided in the brackets. " \
375340 "Hit enter to use default." )
376341 kwargs = {
377- "region" : prompt_with_default ("Azure Region" , DefaultSettings .region ),
378- "resource_group" : prompt_with_default ("Resource Group Name" , DefaultSettings .resource_group ),
379- "storage_account" : prompt_with_default ("Storage Account Name" , DefaultSettings .storage_account ),
380- "batch_account" : prompt_with_default ("Batch Account Name" , DefaultSettings .batch_account ),
381- # "virtual_network_name": prompt_with_default("Virtual Network Name", DefaultSettings.virtual_network_name),
382- # "subnet_name": prompt_with_default("Subnet Name", DefaultSettings.subnet_name),
383- "application_name" : prompt_with_default ("Active Directory Application Name" , DefaultSettings .application_name ),
384- "application_credential_name" : prompt_with_default ("Active Directory Application Credential Name" , DefaultSettings .resource_group ),
385- "service_principal" : prompt_with_default ("Service Principal Name" , DefaultSettings .service_principal )
342+ "region" :
343+ prompt_with_default ("Azure Region" , DefaultSettings .region ),
344+ "resource_group" :
345+ prompt_with_default ("Resource Group Name" , DefaultSettings .resource_group ),
346+ "storage_account" :
347+ prompt_with_default ("Storage Account Name" , DefaultSettings .storage_account ),
348+ "batch_account" :
349+ prompt_with_default ("Batch Account Name" , DefaultSettings .batch_account ),
350+ # "virtual_network_name": prompt_with_default("Virtual Network Name", DefaultSettings.virtual_network_name),
351+ # "subnet_name": prompt_with_default("Subnet Name", DefaultSettings.subnet_name),
352+ "application_name" :
353+ prompt_with_default ("Active Directory Application Name" , DefaultSettings .application_name ),
354+ "application_credential_name" :
355+ prompt_with_default ("Active Directory Application Credential Name" , DefaultSettings .resource_group ),
356+ "service_principal" :
357+ prompt_with_default ("Service Principal Name" , DefaultSettings .service_principal )
386358 }
387359 print ("Creating the Azure resources." )
388360
@@ -410,9 +382,9 @@ def stop(self):
410382 with Spinner ():
411383 profile = credentials .get_cli_profile ()
412384 aad_cred , subscription_id , tenant_id = profile .get_login_credentials (
413- resource = AZURE_PUBLIC_CLOUD .endpoints .active_directory_graph_resource_id
414- )
415- application_id , service_principal_object_id , application_credential = create_aad_user ( aad_cred , tenant_id , ** kwargs )
385+ resource = AZURE_PUBLIC_CLOUD .endpoints .active_directory_graph_resource_id )
386+ application_id , service_principal_object_id , application_credential = create_aad_user (
387+ aad_cred , tenant_id , ** kwargs )
416388
417389 print ("Created Azure Active Directory service principal." )
418390
@@ -425,10 +397,9 @@ def stop(self):
425397 "tenant_id" : tenant_id ,
426398 "client_id" : application_id ,
427399 "credential" : application_credential ,
428- # "subnet_id": subnet_id,
400+ # "subnet_id": subnet_id,
429401 "batch_account_resource_id" : batch_account_id ,
430402 "storage_account_resource_id" : storage_account_id
431- }
432- )
403+ })
433404
434405 print ("\n # Copy the following into your .aztk/secrets.yaml file\n {}" .format (secrets ))
0 commit comments