Răsfoiți Sursa

Updated exceptions to match latest azure libs

Nuwan Goonasekera 4 ani în urmă
părinte
comite
2e79be7f6a

+ 38 - 45
cloudbridge/providers/azure/azure_client.py

@@ -309,51 +309,44 @@ class AzureClient(object):
             try:
                 self._storage_account = \
                     self.get_storage_account(self.storage_account)
-            except HttpResponseError as cloud_error:
-                if isinstance(cloud_error, ResourceNotFoundError):
-                    storage_account_params = {
-                        'sku': {
-                            'name': 'Standard_LRS'
-                        },
-                        'kind': 'storage',
-                        'location': self.region_name,
-                    }
-                    try:
-                        self._storage_account = \
-                            self.create_storage_account(self.storage_account,
-                                                        storage_account_params)
-                    except HttpResponseError as cloud_error2:  # pragma: no cover
-                        if isinstance(cloud_error2, ClientAuthenticationError):
-                            mess = 'The following error was returned by ' \
-                                   'Azure:\n%s\n\nThis is likely because the' \
-                                   ' Role associated with the provided ' \
-                                   'credentials does not allow for Storage ' \
-                                   'Account creation.\nA Storage Account is ' \
-                                   'necessary in order to perform the ' \
-                                   'desired operation. You must either ' \
-                                   'provide an existing Storage Account name' \
-                                   ' as part of the configuration, or ' \
-                                   'elevate the associated Role.\nFor more ' \
-                                   'information on roles, see: https://docs.' \
-                                   'microsoft.com/en-us/azure/role-based-' \
-                                   'access-control/overview\n' % cloud_error2
-                            raise ProviderConnectionException(mess)
-
-                        elif isinstance(cloud_error2, ResourceExistsError):
-                            mess = 'The following error was ' \
-                                   'returned by Azure:\n%s\n\n' \
-                                   'Note that Storage Account names must be ' \
-                                   'unique across Azure (not just in your ' \
-                                   'subscription).\nFor more information ' \
-                                   'see https://docs.microsoft.com/en-us/' \
-                                   'azure/azure-resource-manager/resource-' \
-                                   'manager-storage-account-name-errors\n' \
-                                   % cloud_error2
-                            raise InvalidLabelException(mess)
-                        else:
-                            raise cloud_error2
-                else:
-                    raise cloud_error
+            except ResourceNotFoundError:
+                storage_account_params = {
+                    'sku': {
+                        'name': 'Standard_LRS'
+                    },
+                    'kind': 'storage',
+                    'location': self.region_name,
+                }
+                try:
+                    self._storage_account = \
+                        self.create_storage_account(self.storage_account,
+                                                    storage_account_params)
+                except ClientAuthenticationError as auth_err:
+                    mess = 'The following error was returned by ' \
+                           'Azure:\n%s\n\nThis is likely because the' \
+                           ' Role associated with the provided ' \
+                           'credentials does not allow for Storage ' \
+                           'Account creation.\nA Storage Account is ' \
+                           'necessary in order to perform the ' \
+                           'desired operation. You must either ' \
+                           'provide an existing Storage Account name' \
+                           ' as part of the configuration, or ' \
+                           'elevate the associated Role.\nFor more ' \
+                           'information on roles, see: https://docs.' \
+                           'microsoft.com/en-us/azure/role-based-' \
+                           'access-control/overview\n' % auth_err
+                    raise ProviderConnectionException(mess)
+                except ResourceExistsError as exists_err:
+                    mess = 'The following error was ' \
+                           'returned by Azure:\n%s\n\n' \
+                           'Note that Storage Account names must be ' \
+                           'unique across Azure (not just in your ' \
+                           'subscription).\nFor more information ' \
+                           'see https://docs.microsoft.com/en-us/' \
+                           'azure/azure-resource-manager/resource-' \
+                           'manager-storage-account-name-errors\n' \
+                               % exists_err
+                    raise InvalidLabelException(mess)
 
     def list_locations(self):
         return self.subscription_client.subscriptions. \

+ 14 - 15
cloudbridge/providers/azure/services.py

@@ -25,7 +25,9 @@ from cloudbridge.interfaces.exceptions import (DuplicateResourceException,
 from cloudbridge.interfaces.resources import (MachineImage, Network, Snapshot,
                                               TrafficDirection, VMFirewall,
                                               VMType, Volume)
-from msrestazure.azure_exceptions import CloudError
+from azure.core.exceptions import (ClientAuthenticationError,
+                                   HttpResponseError, ResourceExistsError,
+                                   ResourceNotFoundError)
 
 from azure.common import AzureException
 from azure.mgmt.compute.models import DiskCreateOption
@@ -72,7 +74,7 @@ class AzureVMFirewallService(BaseVMFirewallService):
         try:
             fws = self.provider.azure_client.get_vm_firewall(vm_firewall_id)
             return AzureVMFirewall(self.provider, fws)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -342,7 +344,7 @@ class AzureVolumeService(BaseVolumeService):
         try:
             volume = self.provider.azure_client.get_disk(volume_id)
             return AzureVolume(self.provider, volume)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -434,7 +436,7 @@ class AzureSnapshotService(BaseSnapshotService):
         try:
             snapshot = self.provider.azure_client.get_snapshot(snapshot_id)
             return AzureSnapshot(self.provider, snapshot)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -619,7 +621,7 @@ class AzureImageService(BaseImageService):
         try:
             image = self.provider.azure_client.get_image(image_id)
             return AzureMachineImage(self.provider, image)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -944,7 +946,7 @@ class AzureInstanceService(BaseInstanceService):
         try:
             vm = self.provider.azure_client.get_vm(instance_id)
             return AzureInstance(self.provider, vm)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -1091,7 +1093,7 @@ class AzureNetworkService(BaseNetworkService):
         try:
             network = self.provider.azure_client.get_network(network_id)
             return AzureNetwork(self.provider, network)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -1148,11 +1150,8 @@ class AzureSubnetService(BaseSubnetService):
                     result_list.extend(self.provider.azure_client.list_subnets(
                         net.id
                     ))
-                except CloudError as cloud_error:
-                    if "NotFound" in cloud_error.error.error:
-                        log.exception(cloud_error)
-                    else:
-                        raise cloud_error
+                except ResourceNotFoundError as not_found_error:
+                    log.exception(not_found_error)
         subnets = [AzureSubnet(self.provider, subnet)
                    for subnet in result_list]
 
@@ -1171,7 +1170,7 @@ class AzureSubnetService(BaseSubnetService):
             azure_subnet = self.provider.azure_client.get_subnet(subnet_id)
             return AzureSubnet(self.provider,
                                azure_subnet) if azure_subnet else None
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -1243,7 +1242,7 @@ class AzureRouterService(BaseRouterService):
         try:
             route = self.provider.azure_client.get_route_table(router_id)
             return AzureRouter(self.provider, route)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None
@@ -1335,7 +1334,7 @@ class AzureFloatingIPService(BaseFloatingIPService):
     def get(self, gateway, fip_id):
         try:
             az_ip = self.provider.azure_client.get_floating_ip(fip_id)
-        except (CloudError, InvalidValueException) as cloud_error:
+        except (ResourceNotFoundError, InvalidValueException) as cloud_error:
             # Azure raises the cloud error if the resource not available
             log.exception(cloud_error)
             return None

+ 2 - 2
docs/topics/design_decisions.rst

@@ -55,7 +55,7 @@ Resource identification, naming, and labeling
   do (at least for some resources, such as vmfirewalls within a private
   network). Overall, consistency was challenging to achieve with resource
   naming. Therefore, it was decided that CloudBridge would continue to support
-  resource renaming to the best extent possible and balance between the
+  resource renaming to the best extent possible and strike a balance between the
   use of the resource name property and resource tags. However, because of the
   inconsistency in rename functionality across providers, using the rename
   capabilities within CloudBridge would lead to cloud-dependent code (Related to
@@ -129,7 +129,7 @@ Make providers single zone
   such as the one detailed above. Ultimately, it led to an impasse with GCP,
   which tended to require the zone for almost every operation and some of our
   methods were not geared to do so. Therefore, by making the provider zone
-  specific, we have removed a considerable amount of complexity from both the
+  specific, we have removed a considerable amount of complexity from the
   code, with no significant impact on usability, since operations generally
   tend to be confined to the same zone. Multi-zone operations now require
   multiple cloud provider instances.

+ 1 - 1
tox.ini

@@ -24,7 +24,7 @@ setenv =
 passenv =
     PYTHONUNBUFFERED
     aws: CB_IMAGE_AWS CB_INSTANCE_TYPE_AWS CB_PLACEMENT_AWS AWS_ACCESS_KEY AWS_SECRET_KEY
-    azure: AZURE_SUBSCRIPTION_ID AZURE_CLIENT_ID AZURE_SECRET AZURE_TENANT AZURE_REGION_NAME AZURE_RESOURCE_GROUP AZURE_STORAGE_ACCOUNT AZURE_VM_DEFAULT_USER_NAME AZURE_PUBLIC_KEY_STORAGE_TABLE_NAME
+    azure: CB_IMAGE_AZURE AZURE_SUBSCRIPTION_ID AZURE_CLIENT_ID AZURE_SECRET AZURE_TENANT AZURE_REGION_NAME AZURE_RESOURCE_GROUP AZURE_STORAGE_ACCOUNT AZURE_VM_DEFAULT_USER_NAME AZURE_PUBLIC_KEY_STORAGE_TABLE_NAME
     gcp: CB_IMAGE_GCP CB_INSTANCE_TYPE_GCP CB_PLACEMENT_GCP GCP_DEFAULT_REGION GCP_DEFAULT_ZONE GCP_PROJECT_NAME GCP_SERVICE_CREDS_FILE GCP_SERVICE_CREDS_DICT
     openstack:  CB_IMAGE_OS CB_INSTANCE_TYPE_OS CB_PLACEMENT_OS OS_AUTH_URL OS_PASSWORD OS_PROJECT_NAME OS_TENANT_NAME OS_USERNAME OS_REGION_NAME OS_USER_DOMAIN_NAME OS_PROJECT_DOMAIN_NAME NOVA_SERVICE_NAME
     mock: CB_IMAGE_AWS CB_INSTANCE_TYPE_AWS CB_PLACEMENT_AWS AWS_ACCESS_KEY AWS_SECRET_KEY