| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- import logging
- import uuid
- from azure.common import AzureException
- from cloudbridge.cloud.base.resources import ClientPagedResultList
- from cloudbridge.cloud.base.services import BaseBlockStoreService, \
- BaseComputeService, BaseImageService, BaseObjectStoreService, \
- BaseSecurityGroupService, BaseSecurityService, \
- BaseSnapshotService, BaseVolumeService
- from cloudbridge.cloud.interfaces.resources import PlacementZone, \
- Snapshot, Volume
- from cloudbridge.cloud.providers.azure import helpers as azure_helpers
- from msrestazure.azure_exceptions import CloudError
- from .resources import AzureBucket, AzureMachineImage, \
- AzureSecurityGroup, \
- AzureSnapshot, AzureVolume, \
- IMAGE_NAME, IMAGE_RESOURCE_ID, \
- NETWORK_SECURITY_GROUP_RESOURCE_ID, \
- SECURITY_GROUP_NAME, SNAPSHOT_NAME, \
- SNAPSHOT_RESOURCE_ID, VOLUME_NAME, VOLUME_RESOURCE_ID
- log = logging.getLogger(__name__)
- class AzureSecurityService(BaseSecurityService):
- def __init__(self, provider):
- super(AzureSecurityService, self).__init__(provider)
- # Initialize provider services
- # self._key_pairs = AzureKeyPairService(provider)
- self._security_groups = AzureSecurityGroupService(provider)
- @property
- def key_pairs(self):
- """
- Provides access to key pairs for this provider.
- :rtype: ``object`` of :class:`.KeyPairService`
- :return: a KeyPairService object
- """
- raise NotImplementedError('AzureSecurityService '
- 'not implemented this property')
- @property
- def security_groups(self):
- """
- Provides access to security groups for this provider.
- :rtype: ``object`` of :class:`.SecurityGroupService`
- :return: a SecurityGroupService object
- """
- return self._security_groups
- class AzureSecurityGroupService(BaseSecurityGroupService):
- def __init__(self, provider):
- super(AzureSecurityGroupService, self).__init__(provider)
- def get(self, sg_id):
- try:
- params = azure_helpers.parse_url(
- NETWORK_SECURITY_GROUP_RESOURCE_ID, sg_id)
- sgs = self.provider.azure_client.get_security_group(
- params.get(SECURITY_GROUP_NAME))
- return AzureSecurityGroup(self.provider, sgs)
- except CloudError as cloudError:
- log.exception(cloudError.message)
- return None
- def list(self, limit=None, marker=None):
- sgs = [AzureSecurityGroup(self.provider, sg)
- for sg in self.provider.azure_client.list_security_group()]
- return ClientPagedResultList(self.provider, sgs, limit, marker)
- def create(self, name, description, network_id):
- parameters = {"location": self.provider.region_name,
- 'tags': {'Name': name}}
- if description:
- parameters['tags'].update(Description=description)
- sg = self.provider.azure_client.create_security_group(name, parameters)
- cb_sg = AzureSecurityGroup(self.provider, sg)
- return cb_sg
- def find(self, name, limit=None, marker=None):
- """
- Searches for a security group by a given list of attributes.
- """
- filters = {'Name': name}
- sgs = [AzureSecurityGroup(self.provider, security_group)
- for security_group in azure_helpers.filter(
- self.provider.azure_client.list_security_group(), filters)]
- return ClientPagedResultList(self.provider, sgs,
- limit=limit, marker=marker)
- def delete(self, group_id):
- params = azure_helpers.parse_url(NETWORK_SECURITY_GROUP_RESOURCE_ID,
- group_id)
- return self.provider.azure_client.delete_security_group(
- params.get(SECURITY_GROUP_NAME))
- class AzureObjectStoreService(BaseObjectStoreService):
- def __init__(self, provider):
- super(AzureObjectStoreService, self).__init__(provider)
- def get(self, bucket_id):
- """
- Returns a bucket given its ID. Returns ``None`` if the bucket
- does not exist.
- """
- try:
- bucket = self.provider.azure_client.get_container(bucket_id)
- return AzureBucket(self.provider, bucket)
- except AzureException as error:
- log.exception(error)
- return None
- def find(self, name, limit=None, marker=None):
- """
- Searches for a bucket by a given list of attributes.
- """
- buckets = [AzureBucket(self.provider, bucket)
- for bucket in
- self.provider.azure_client.list_containers(prefix=name)]
- return ClientPagedResultList(self.provider, buckets,
- limit=limit, marker=marker)
- def list(self, limit=None, marker=None):
- """
- List all containers.
- """
- buckets = [AzureBucket(self.provider, bucket)
- for bucket in self.provider.azure_client.list_containers()]
- return ClientPagedResultList(self.provider, buckets,
- limit=limit, marker=marker)
- def create(self, name, location=None):
- """
- Create a new bucket.
- """
- bucket = self.provider.azure_client.create_container(name.lower())
- return AzureBucket(self.provider, bucket)
- class AzureBlockStoreService(BaseBlockStoreService):
- def __init__(self, provider):
- super(AzureBlockStoreService, self).__init__(provider)
- # Initialize provider services
- self._volume_svc = AzureVolumeService(self.provider)
- self._snapshot_svc = AzureSnapshotService(self.provider)
- @property
- def volumes(self):
- return self._volume_svc
- @property
- def snapshots(self):
- return self._snapshot_svc
- class AzureVolumeService(BaseVolumeService):
- def __init__(self, provider):
- super(AzureVolumeService, self).__init__(provider)
- def get(self, volume_id):
- try:
- params = azure_helpers.parse_url(VOLUME_RESOURCE_ID, volume_id)
- volume = self.provider.azure_client.get_disk(
- params.get(VOLUME_NAME))
- return AzureVolume(self.provider, volume)
- except CloudError as cloudError:
- log.exception(cloudError.message)
- return None
- def find(self, name, limit=None, marker=None):
- """
- Searches for a volume by a given list of attributes.
- """
- filters = {'Name': name}
- cb_vols = [AzureVolume(self.provider, volume)
- for volume in azure_helpers.filter(
- self.provider.azure_client.list_disks(), filters)]
- return ClientPagedResultList(self.provider, cb_vols,
- limit=limit, marker=marker)
- def list(self, limit=None, marker=None):
- azure_vols = self.provider.azure_client.list_disks()
- cb_vols = [AzureVolume(self.provider, vol) for vol in azure_vols]
- return ClientPagedResultList(self.provider, cb_vols,
- limit=limit, marker=marker)
- def create(self, name, size, zone=None, snapshot=None, description=None):
- zone_id = zone.id if isinstance(zone, PlacementZone) else zone
- snapshot_id = snapshot.id if isinstance(
- snapshot, Snapshot) and snapshot else snapshot
- disk_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
- tags = {'Name': name}
- if description:
- tags.update(Description=description)
- if snapshot_id:
- params = {
- 'location': zone_id or self.provider.azure_client.region_name,
- 'creation_data': {
- 'create_option': 'copy',
- 'source_uri': snapshot_id
- },
- 'tags': tags
- }
- self.provider.azure_client.create_snapshot_disk(disk_name, params)
- else:
- params = {
- 'location': zone_id or self.provider.azure_client.region_name,
- 'disk_size_gb': size,
- 'creation_data': {
- 'create_option': 'empty'
- },
- 'tags': tags}
- self.provider.azure_client.create_empty_disk(disk_name, params)
- azure_vol = self.provider.azure_client.get_disk(disk_name)
- cb_vol = AzureVolume(self.provider, azure_vol)
- return cb_vol
- class AzureSnapshotService(BaseSnapshotService):
- def __init__(self, provider):
- super(AzureSnapshotService, self).__init__(provider)
- def get(self, ss_id):
- try:
- params = azure_helpers.parse_url(SNAPSHOT_RESOURCE_ID, ss_id)
- snapshot = self.provider.azure_client. \
- get_snapshot(params.get(SNAPSHOT_NAME))
- return AzureSnapshot(self.provider, snapshot)
- except CloudError as cloudError:
- log.exception(cloudError.message)
- return None
- def find(self, name, limit=None, marker=None):
- """
- Searches for a snapshot by a given list of attributes.
- """
- filters = {'Name': name}
- cb_snapshots = [AzureSnapshot(self.provider, snapshot)
- for snapshot in azure_helpers.filter(
- self.provider.azure_client.list_snapshots(), filters)]
- return ClientPagedResultList(self.provider, cb_snapshots,
- limit=limit, marker=marker)
- def list(self, limit=None, marker=None):
- """
- List all snapshots.
- """
- snaps = [AzureSnapshot(self.provider, obj)
- for obj in
- self.provider.
- azure_client.list_snapshots()]
- return ClientPagedResultList(self.provider, snaps, limit, marker)
- def create(self, name, volume, description=None):
- volume_id = volume.id if isinstance(volume, Volume) else volume
- tags = {'Name': name}
- snapshot_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
- if description:
- tags.update(Description=description)
- params = {
- 'location': self.provider.azure_client.region_name,
- 'creation_data': {
- 'create_option': 'Copy',
- 'source_uri': volume_id
- },
- 'tags': tags
- }
- self.provider.azure_client. \
- create_snapshot(snapshot_name, params)
- azure_snap = self.provider.azure_client.get_snapshot(snapshot_name)
- cb_snap = AzureSnapshot(self.provider, azure_snap)
- return cb_snap
- class AzureComputeService(BaseComputeService):
- def __init__(self, provider):
- super(AzureComputeService, self).__init__(provider)
- # self._instance_type_svc = AzureInstanceTypesService(self.provider)
- # self._instance_svc = AzureInstanceService(self.provider)
- # self._region_svc = AzureRegionService(self.provider)
- self._images_svc = AzureImageService(self.provider)
- @property
- def images(self):
- return self._images_svc
- @property
- def instance_types(self):
- raise NotImplementedError('AzureComputeService not '
- 'implemented this method')
- @property
- def instances(self):
- raise NotImplementedError('AzureComputeService not '
- 'implemented this method')
- @property
- def regions(self):
- raise NotImplementedError('AzureComputeService not '
- 'implemented this method')
- class AzureImageService(BaseImageService):
- def __init__(self, provider):
- super(AzureImageService, self).__init__(provider)
- def get(self, image_id):
- try:
- params = azure_helpers.parse_url(IMAGE_RESOURCE_ID, image_id)
- image = self.provider.azure_client. \
- get_image(params.get(IMAGE_NAME))
- return AzureMachineImage(self.provider, image)
- except CloudError as cloudError:
- log.exception(cloudError.message)
- return None
- def find(self, name, limit=None, marker=None):
- raise NotImplementedError('AzureImageService not '
- 'implemented this method')
- def list(self, limit=None, marker=None):
- azure_images = self.provider.azure_client.list_images()
- cb_images = [AzureMachineImage(self.provider, img)
- for img in azure_images]
- return ClientPagedResultList(self.provider, cb_images,
- limit=limit, marker=marker)
|