@@ -558,11 +558,15 @@ def get_azure_vm_create_json(self, group_name, vm_name, nics, radl,
558558 # azr://Canonical/UbuntuServer/16.04.0-LTS/latest
559559 # azr://MicrosoftWindowsServerEssentials/WindowsServerEssentials/WindowsServerEssentials/latest
560560 image_values = (url [1 ] + url [2 ]).split ("/" )
561- if len (image_values ) not in [3 , 4 ]:
561+ if len (image_values ) not in [3 , 4 , 5 ]:
562562 raise Exception ("The Azure image has to have the format: azr://publisher/offer/sku/version"
563+ " or azr://region/publisher/offer/sku/version"
563564 " or azr://[snapshots|disk]/rgname/diskname" )
564565
565566 location = self .DEFAULT_LOCATION
567+ if len (image_values ) == 5 :
568+ location = image_values [0 ]
569+ image_values = image_values [1 :]
566570 if system .getValue ('availability_zone' ):
567571 location = system .getValue ('availability_zone' )
568572
@@ -837,12 +841,6 @@ def create_vms(self, rg_name, inf, radl, requested_radl, num_vm, location,
837841 return vms
838842
839843 def launch (self , inf , radl , requested_radl , num_vm , auth_data ):
840- location = self .DEFAULT_LOCATION
841- if radl .systems [0 ].getValue ('availability_zone' ):
842- location = radl .systems [0 ].getValue ('availability_zone' )
843- else :
844- radl .systems [0 ].setValue ('availability_zone' , location )
845-
846844 credentials , subscription_id = self .get_credentials (auth_data )
847845 compute_client = ComputeManagementClient (credentials , subscription_id )
848846
@@ -852,12 +850,22 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
852850 # azr://Canonical/UbuntuServer/16.04.0-LTS/latest
853851 # azr://MicrosoftWindowsServerEssentials/WindowsServerEssentials/WindowsServerEssentials/latest
854852 image_values = (url [1 ] + url [2 ]).split ("/" )
855- if len (image_values ) not in [3 , 4 ]:
853+ if len (image_values ) not in [3 , 4 , 5 ]:
856854 raise Exception ("The Azure image has to have the format: azr://publisher/offer/sku/version"
855+ " or azr://region/publisher/offer/sku/version"
857856 " or azr://[snapshots|disk|image]/rgname/diskname" )
858857 if len (image_values ) == 3 and image_values [0 ] not in ["snapshot" , "disk" ]:
859858 raise Exception ("Incorrect image url: it must be snapshot or disk." )
860859
860+ location = self .DEFAULT_LOCATION
861+ if len (image_values ) == 5 :
862+ location = image_values [0 ]
863+ image_values = image_values [1 :]
864+ if radl .systems [0 ].getValue ('availability_zone' ):
865+ location = radl .systems [0 ].getValue ('availability_zone' )
866+ else :
867+ radl .systems [0 ].setValue ('availability_zone' , location )
868+
861869 if len (image_values ) == 4 :
862870 offers = compute_client .virtual_machine_images .list (location ,
863871 image_values [0 ],
@@ -1274,3 +1282,44 @@ def list_images(self, auth_data, filters=None):
12741282 res .append ({"uri" : "azr://%s/%s/%s/latest" % (pub , offer , sku ),
12751283 "name" : name })
12761284 return self ._filter_images (res , filters )
1285+
1286+ def get_quotas (self , auth_data , region = None ):
1287+ credentials , subscription_id = self .get_credentials (auth_data )
1288+ compute_client = ComputeManagementClient (credentials , subscription_id )
1289+ location = self .DEFAULT_LOCATION
1290+ try :
1291+ # Get the region from the auth data
1292+ location = auth_data .getAuthInfo (self .type )[0 ].get ('region' , location )
1293+ except Exception :
1294+ pass
1295+ if region :
1296+ location = region
1297+
1298+ # Initialize default values
1299+ quotas = {}
1300+
1301+ try :
1302+ usage_list = compute_client .usage .list (location )
1303+ for usage in usage_list :
1304+ name = usage .name .localized_value .lower ()
1305+ if name == "total regional vcpus" :
1306+ quotas ["cores" ] = {}
1307+ quotas ["cores" ]["used" ] = usage .current_value
1308+ quotas ["cores" ]["limit" ] = usage .limit
1309+ elif name == "virtual machines" :
1310+ quotas ["instances" ] = {}
1311+ quotas ["instances" ]["used" ] = usage .current_value
1312+ quotas ["instances" ]["limit" ] = usage .limit
1313+ elif "storage" in name and "disks" in name :
1314+ quotas ["volumes" ] = {}
1315+ quotas ["volumes" ]["used" ] = usage .current_value
1316+ quotas ["volumes" ]["limit" ] = usage .limit
1317+ elif "family vcpus" in name :
1318+ fam = usage .name .localized_value [:- 13 ].strip ().replace (" " , "_" )
1319+ quotas [fam ] = {"cores" : {}}
1320+ quotas [fam ]["cores" ]["used" ] = usage .current_value
1321+ quotas [fam ]["cores" ]["limit" ] = usage .limit
1322+ return quotas
1323+ except Exception :
1324+ self .log_exception ("Error retrieving Azure quotas" )
1325+ return {}
0 commit comments