services.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import logging
  2. import uuid
  3. from azure.common import AzureException
  4. from cloudbridge.cloud.base.resources import ClientPagedResultList
  5. from cloudbridge.cloud.base.services import BaseBlockStoreService, \
  6. BaseComputeService, BaseImageService, BaseObjectStoreService, \
  7. BaseSecurityGroupService, BaseSecurityService, \
  8. BaseSnapshotService, BaseVolumeService
  9. from cloudbridge.cloud.interfaces.resources import PlacementZone, \
  10. Snapshot, Volume
  11. from cloudbridge.cloud.providers.azure import helpers as azure_helpers
  12. from msrestazure.azure_exceptions import CloudError
  13. from .resources import AzureBucket, AzureMachineImage, \
  14. AzureSecurityGroup, \
  15. AzureSnapshot, AzureVolume, \
  16. IMAGE_NAME, IMAGE_RESOURCE_ID, \
  17. NETWORK_SECURITY_GROUP_RESOURCE_ID, \
  18. SECURITY_GROUP_NAME, SNAPSHOT_NAME, \
  19. SNAPSHOT_RESOURCE_ID, VOLUME_NAME, VOLUME_RESOURCE_ID
  20. log = logging.getLogger(__name__)
  21. class AzureSecurityService(BaseSecurityService):
  22. def __init__(self, provider):
  23. super(AzureSecurityService, self).__init__(provider)
  24. # Initialize provider services
  25. # self._key_pairs = AzureKeyPairService(provider)
  26. self._security_groups = AzureSecurityGroupService(provider)
  27. @property
  28. def key_pairs(self):
  29. """
  30. Provides access to key pairs for this provider.
  31. :rtype: ``object`` of :class:`.KeyPairService`
  32. :return: a KeyPairService object
  33. """
  34. raise NotImplementedError('AzureSecurityService '
  35. 'not implemented this property')
  36. @property
  37. def security_groups(self):
  38. """
  39. Provides access to security groups for this provider.
  40. :rtype: ``object`` of :class:`.SecurityGroupService`
  41. :return: a SecurityGroupService object
  42. """
  43. return self._security_groups
  44. class AzureSecurityGroupService(BaseSecurityGroupService):
  45. def __init__(self, provider):
  46. super(AzureSecurityGroupService, self).__init__(provider)
  47. def get(self, sg_id):
  48. try:
  49. params = azure_helpers.parse_url(
  50. NETWORK_SECURITY_GROUP_RESOURCE_ID, sg_id)
  51. sgs = self.provider.azure_client.get_security_group(
  52. params.get(SECURITY_GROUP_NAME))
  53. return AzureSecurityGroup(self.provider, sgs)
  54. except CloudError as cloudError:
  55. log.exception(cloudError.message)
  56. return None
  57. def list(self, limit=None, marker=None):
  58. sgs = [AzureSecurityGroup(self.provider, sg)
  59. for sg in self.provider.azure_client.list_security_group()]
  60. return ClientPagedResultList(self.provider, sgs, limit, marker)
  61. def create(self, name, description, network_id):
  62. parameters = {"location": self.provider.region_name,
  63. 'tags': {'Name': name}}
  64. if description:
  65. parameters['tags'].update(Description=description)
  66. sg = self.provider.azure_client.create_security_group(name, parameters)
  67. cb_sg = AzureSecurityGroup(self.provider, sg)
  68. return cb_sg
  69. def find(self, name, limit=None, marker=None):
  70. """
  71. Searches for a security group by a given list of attributes.
  72. """
  73. filters = {'Name': name}
  74. sgs = [AzureSecurityGroup(self.provider, security_group)
  75. for security_group in azure_helpers.filter(
  76. self.provider.azure_client.list_security_group(), filters)]
  77. return ClientPagedResultList(self.provider, sgs,
  78. limit=limit, marker=marker)
  79. def delete(self, group_id):
  80. params = azure_helpers.parse_url(NETWORK_SECURITY_GROUP_RESOURCE_ID,
  81. group_id)
  82. return self.provider.azure_client.delete_security_group(
  83. params.get(SECURITY_GROUP_NAME))
  84. class AzureObjectStoreService(BaseObjectStoreService):
  85. def __init__(self, provider):
  86. super(AzureObjectStoreService, self).__init__(provider)
  87. def get(self, bucket_id):
  88. """
  89. Returns a bucket given its ID. Returns ``None`` if the bucket
  90. does not exist.
  91. """
  92. try:
  93. bucket = self.provider.azure_client.get_container(bucket_id)
  94. return AzureBucket(self.provider, bucket)
  95. except AzureException as error:
  96. log.exception(error)
  97. return None
  98. def find(self, name, limit=None, marker=None):
  99. """
  100. Searches for a bucket by a given list of attributes.
  101. """
  102. buckets = [AzureBucket(self.provider, bucket)
  103. for bucket in
  104. self.provider.azure_client.list_containers(prefix=name)]
  105. return ClientPagedResultList(self.provider, buckets,
  106. limit=limit, marker=marker)
  107. def list(self, limit=None, marker=None):
  108. """
  109. List all containers.
  110. """
  111. buckets = [AzureBucket(self.provider, bucket)
  112. for bucket in self.provider.azure_client.list_containers()]
  113. return ClientPagedResultList(self.provider, buckets,
  114. limit=limit, marker=marker)
  115. def create(self, name, location=None):
  116. """
  117. Create a new bucket.
  118. """
  119. bucket = self.provider.azure_client.create_container(name.lower())
  120. return AzureBucket(self.provider, bucket)
  121. class AzureBlockStoreService(BaseBlockStoreService):
  122. def __init__(self, provider):
  123. super(AzureBlockStoreService, self).__init__(provider)
  124. # Initialize provider services
  125. self._volume_svc = AzureVolumeService(self.provider)
  126. self._snapshot_svc = AzureSnapshotService(self.provider)
  127. @property
  128. def volumes(self):
  129. return self._volume_svc
  130. @property
  131. def snapshots(self):
  132. return self._snapshot_svc
  133. class AzureVolumeService(BaseVolumeService):
  134. def __init__(self, provider):
  135. super(AzureVolumeService, self).__init__(provider)
  136. def get(self, volume_id):
  137. try:
  138. params = azure_helpers.parse_url(VOLUME_RESOURCE_ID, volume_id)
  139. volume = self.provider.azure_client.get_disk(
  140. params.get(VOLUME_NAME))
  141. return AzureVolume(self.provider, volume)
  142. except CloudError as cloudError:
  143. log.exception(cloudError.message)
  144. return None
  145. def find(self, name, limit=None, marker=None):
  146. """
  147. Searches for a volume by a given list of attributes.
  148. """
  149. filters = {'Name': name}
  150. cb_vols = [AzureVolume(self.provider, volume)
  151. for volume in azure_helpers.filter(
  152. self.provider.azure_client.list_disks(), filters)]
  153. return ClientPagedResultList(self.provider, cb_vols,
  154. limit=limit, marker=marker)
  155. def list(self, limit=None, marker=None):
  156. azure_vols = self.provider.azure_client.list_disks()
  157. cb_vols = [AzureVolume(self.provider, vol) for vol in azure_vols]
  158. return ClientPagedResultList(self.provider, cb_vols,
  159. limit=limit, marker=marker)
  160. def create(self, name, size, zone=None, snapshot=None, description=None):
  161. zone_id = zone.id if isinstance(zone, PlacementZone) else zone
  162. snapshot_id = snapshot.id if isinstance(
  163. snapshot, Snapshot) and snapshot else snapshot
  164. disk_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
  165. tags = {'Name': name}
  166. if description:
  167. tags.update(Description=description)
  168. if snapshot_id:
  169. params = {
  170. 'location': zone_id or self.provider.azure_client.region_name,
  171. 'creation_data': {
  172. 'create_option': 'copy',
  173. 'source_uri': snapshot_id
  174. },
  175. 'tags': tags
  176. }
  177. self.provider.azure_client.create_snapshot_disk(disk_name, params)
  178. else:
  179. params = {
  180. 'location': zone_id or self.provider.azure_client.region_name,
  181. 'disk_size_gb': size,
  182. 'creation_data': {
  183. 'create_option': 'empty'
  184. },
  185. 'tags': tags}
  186. self.provider.azure_client.create_empty_disk(disk_name, params)
  187. azure_vol = self.provider.azure_client.get_disk(disk_name)
  188. cb_vol = AzureVolume(self.provider, azure_vol)
  189. return cb_vol
  190. class AzureSnapshotService(BaseSnapshotService):
  191. def __init__(self, provider):
  192. super(AzureSnapshotService, self).__init__(provider)
  193. def get(self, ss_id):
  194. try:
  195. params = azure_helpers.parse_url(SNAPSHOT_RESOURCE_ID, ss_id)
  196. snapshot = self.provider.azure_client. \
  197. get_snapshot(params.get(SNAPSHOT_NAME))
  198. return AzureSnapshot(self.provider, snapshot)
  199. except CloudError as cloudError:
  200. log.exception(cloudError.message)
  201. return None
  202. def find(self, name, limit=None, marker=None):
  203. """
  204. Searches for a snapshot by a given list of attributes.
  205. """
  206. filters = {'Name': name}
  207. cb_snapshots = [AzureSnapshot(self.provider, snapshot)
  208. for snapshot in azure_helpers.filter(
  209. self.provider.azure_client.list_snapshots(), filters)]
  210. return ClientPagedResultList(self.provider, cb_snapshots,
  211. limit=limit, marker=marker)
  212. def list(self, limit=None, marker=None):
  213. """
  214. List all snapshots.
  215. """
  216. snaps = [AzureSnapshot(self.provider, obj)
  217. for obj in
  218. self.provider.
  219. azure_client.list_snapshots()]
  220. return ClientPagedResultList(self.provider, snaps, limit, marker)
  221. def create(self, name, volume, description=None):
  222. volume_id = volume.id if isinstance(volume, Volume) else volume
  223. tags = {'Name': name}
  224. snapshot_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
  225. if description:
  226. tags.update(Description=description)
  227. params = {
  228. 'location': self.provider.azure_client.region_name,
  229. 'creation_data': {
  230. 'create_option': 'Copy',
  231. 'source_uri': volume_id
  232. },
  233. 'tags': tags
  234. }
  235. self.provider.azure_client. \
  236. create_snapshot(snapshot_name, params)
  237. azure_snap = self.provider.azure_client.get_snapshot(snapshot_name)
  238. cb_snap = AzureSnapshot(self.provider, azure_snap)
  239. return cb_snap
  240. class AzureComputeService(BaseComputeService):
  241. def __init__(self, provider):
  242. super(AzureComputeService, self).__init__(provider)
  243. # self._instance_type_svc = AzureInstanceTypesService(self.provider)
  244. # self._instance_svc = AzureInstanceService(self.provider)
  245. # self._region_svc = AzureRegionService(self.provider)
  246. self._images_svc = AzureImageService(self.provider)
  247. @property
  248. def images(self):
  249. return self._images_svc
  250. @property
  251. def instance_types(self):
  252. raise NotImplementedError('AzureComputeService not '
  253. 'implemented this method')
  254. @property
  255. def instances(self):
  256. raise NotImplementedError('AzureComputeService not '
  257. 'implemented this method')
  258. @property
  259. def regions(self):
  260. raise NotImplementedError('AzureComputeService not '
  261. 'implemented this method')
  262. class AzureImageService(BaseImageService):
  263. def __init__(self, provider):
  264. super(AzureImageService, self).__init__(provider)
  265. def get(self, image_id):
  266. try:
  267. params = azure_helpers.parse_url(IMAGE_RESOURCE_ID, image_id)
  268. image = self.provider.azure_client. \
  269. get_image(params.get(IMAGE_NAME))
  270. return AzureMachineImage(self.provider, image)
  271. except CloudError as cloudError:
  272. log.exception(cloudError.message)
  273. return None
  274. def find(self, name, limit=None, marker=None):
  275. raise NotImplementedError('AzureImageService not '
  276. 'implemented this method')
  277. def list(self, limit=None, marker=None):
  278. azure_images = self.provider.azure_client.list_images()
  279. cb_images = [AzureMachineImage(self.provider, img)
  280. for img in azure_images]
  281. return ClientPagedResultList(self.provider, cb_images,
  282. limit=limit, marker=marker)