services.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. """
  2. Base implementation for services available through a provider
  3. """
  4. import logging
  5. import cloudbridge.cloud.base.helpers as cb_helpers
  6. from cloudbridge.cloud.interfaces.resources import Router
  7. from cloudbridge.cloud.interfaces.services import BucketService
  8. from cloudbridge.cloud.interfaces.services import CloudService
  9. from cloudbridge.cloud.interfaces.services import ComputeService
  10. from cloudbridge.cloud.interfaces.services import ImageService
  11. from cloudbridge.cloud.interfaces.services import InstanceService
  12. from cloudbridge.cloud.interfaces.services import KeyPairService
  13. from cloudbridge.cloud.interfaces.services import NetworkService
  14. from cloudbridge.cloud.interfaces.services import NetworkingService
  15. from cloudbridge.cloud.interfaces.services import RegionService
  16. from cloudbridge.cloud.interfaces.services import RouterService
  17. from cloudbridge.cloud.interfaces.services import SecurityService
  18. from cloudbridge.cloud.interfaces.services import SnapshotService
  19. from cloudbridge.cloud.interfaces.services import StorageService
  20. from cloudbridge.cloud.interfaces.services import SubnetService
  21. from cloudbridge.cloud.interfaces.services import VMFirewallService
  22. from cloudbridge.cloud.interfaces.services import VMTypeService
  23. from cloudbridge.cloud.interfaces.services import VolumeService
  24. from .resources import BasePageableObjectMixin
  25. from .resources import ClientPagedResultList
  26. log = logging.getLogger(__name__)
  27. class BaseCloudService(CloudService):
  28. def __init__(self, provider):
  29. self._provider = provider
  30. @property
  31. def provider(self):
  32. return self._provider
  33. class BaseComputeService(ComputeService, BaseCloudService):
  34. def __init__(self, provider):
  35. super(BaseComputeService, self).__init__(provider)
  36. class BaseVolumeService(
  37. BasePageableObjectMixin, VolumeService, BaseCloudService):
  38. def __init__(self, provider):
  39. super(BaseVolumeService, self).__init__(provider)
  40. class BaseSnapshotService(
  41. BasePageableObjectMixin, SnapshotService, BaseCloudService):
  42. def __init__(self, provider):
  43. super(BaseSnapshotService, self).__init__(provider)
  44. class BaseStorageService(StorageService, BaseCloudService):
  45. def __init__(self, provider):
  46. super(BaseStorageService, self).__init__(provider)
  47. class BaseImageService(
  48. BasePageableObjectMixin, ImageService, BaseCloudService):
  49. def __init__(self, provider):
  50. super(BaseImageService, self).__init__(provider)
  51. class BaseBucketService(
  52. BasePageableObjectMixin, BucketService, BaseCloudService):
  53. def __init__(self, provider):
  54. super(BaseBucketService, self).__init__(provider)
  55. class BaseSecurityService(SecurityService, BaseCloudService):
  56. def __init__(self, provider):
  57. super(BaseSecurityService, self).__init__(provider)
  58. class BaseKeyPairService(
  59. BasePageableObjectMixin, KeyPairService, BaseCloudService):
  60. def __init__(self, provider):
  61. super(BaseKeyPairService, self).__init__(provider)
  62. def delete(self, key_pair_id):
  63. """
  64. Delete an existing key pair.
  65. :type key_pair_id: str
  66. :param key_pair_id: The id of the key pair to be deleted.
  67. :rtype: ``bool``
  68. :return: ``True`` if the key does not exist. Note that this implies
  69. that the key may not have been deleted by this method but
  70. instead has not existed in the first place.
  71. """
  72. log.info("Deleting the existing key pair %s", key_pair_id)
  73. kp = self.get(key_pair_id)
  74. if kp:
  75. kp.delete()
  76. return True
  77. class BaseVMFirewallService(
  78. BasePageableObjectMixin, VMFirewallService, BaseCloudService):
  79. def __init__(self, provider):
  80. super(BaseVMFirewallService, self).__init__(provider)
  81. class BaseVMTypeService(
  82. BasePageableObjectMixin, VMTypeService, BaseCloudService):
  83. def __init__(self, provider):
  84. super(BaseVMTypeService, self).__init__(provider)
  85. def get(self, vm_type_id):
  86. vm_type = (t for t in self if t.id == vm_type_id)
  87. return next(vm_type, None)
  88. def find(self, **kwargs):
  89. obj_list = self
  90. filters = ['name']
  91. matches = cb_helpers.generic_find(filters, kwargs, obj_list)
  92. return ClientPagedResultList(self._provider, list(matches))
  93. class BaseInstanceService(
  94. BasePageableObjectMixin, InstanceService, BaseCloudService):
  95. def __init__(self, provider):
  96. super(BaseInstanceService, self).__init__(provider)
  97. class BaseRegionService(
  98. BasePageableObjectMixin, RegionService, BaseCloudService):
  99. def __init__(self, provider):
  100. super(BaseRegionService, self).__init__(provider)
  101. def find(self, **kwargs):
  102. obj_list = self
  103. filters = ['name']
  104. matches = cb_helpers.generic_find(filters, kwargs, obj_list)
  105. return ClientPagedResultList(self._provider, list(matches))
  106. class BaseNetworkingService(NetworkingService, BaseCloudService):
  107. def __init__(self, provider):
  108. super(BaseNetworkingService, self).__init__(provider)
  109. class BaseNetworkService(
  110. BasePageableObjectMixin, NetworkService, BaseCloudService):
  111. def __init__(self, provider):
  112. super(BaseNetworkService, self).__init__(provider)
  113. @property
  114. def subnets(self):
  115. return [subnet for subnet in self.provider.subnets
  116. if subnet.network_id == self.id]
  117. def delete(self, network_id):
  118. network = self.get(network_id)
  119. if network:
  120. log.info("Deleting network %s", network_id)
  121. network.delete()
  122. class BaseSubnetService(
  123. BasePageableObjectMixin, SubnetService, BaseCloudService):
  124. def __init__(self, provider):
  125. super(BaseSubnetService, self).__init__(provider)
  126. def find(self, **kwargs):
  127. obj_list = self
  128. filters = ['name']
  129. matches = cb_helpers.generic_find(filters, kwargs, obj_list)
  130. return ClientPagedResultList(self._provider, list(matches))
  131. class BaseRouterService(
  132. BasePageableObjectMixin, RouterService, BaseCloudService):
  133. def __init__(self, provider):
  134. super(BaseRouterService, self).__init__(provider)
  135. def delete(self, router):
  136. if isinstance(router, Router):
  137. log.info("Router %s successful deleted.", router)
  138. router.delete()
  139. else:
  140. log.info("Getting router %s", router)
  141. router = self.get(router)
  142. if router:
  143. log.info("Router %s successful deleted.", router)
  144. router.delete()