2
0

provider.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. """
  2. Specification for a provider interface
  3. """
  4. from abc import ABCMeta
  5. from abc import abstractmethod
  6. from abc import abstractproperty
  7. class CloudProvider(object):
  8. """
  9. Base interface for a cloud provider
  10. """
  11. __metaclass__ = ABCMeta
  12. @abstractmethod
  13. def __init__(self, config):
  14. """
  15. Create a new provider instance given a dictionary of
  16. configuration attributes.
  17. :type config: :class:`dict`
  18. :param config: A dictionary object containing provider initialization
  19. values. Alternatively, this can be an iterable of
  20. key/value pairs (as tuples or other iterables of length
  21. two). See specific provider implementation for the
  22. required fields.
  23. :rtype: :class:`.CloudProvider`
  24. :return: a concrete provider instance
  25. """
  26. pass
  27. @abstractproperty
  28. def config(self):
  29. """
  30. Returns the config object associated with this provider. This object
  31. is a subclass of :class:`dict` and will contain the properties
  32. provided at initialization time, grouped under `cloud_properties` and
  33. `credentials` keys. In addition, it also contains extra provider-wide
  34. properties such as the default result limit for `list()` queries.
  35. Example:
  36. .. code-block:: python
  37. config = { 'aws_access_key' : '<my_key>' }
  38. provider = factory.create_provider(ProviderList.AWS, config)
  39. print(provider.config['credentials'].get('aws_access_key'))
  40. print(provider.config.default_result_limit))
  41. # change provider result limit
  42. provider.config.default_result_limit = 100
  43. :rtype: :class:`.Configuration`
  44. :return: An object of class Configuration, which contains the values
  45. used to initialize the provider, as well as other global
  46. configuration properties.
  47. """
  48. pass
  49. @abstractproperty
  50. def middleware(self):
  51. """
  52. Returns the middleware manager associated with this provider. The
  53. middleware manager can be used to add or remove middleware from
  54. cloudbridge. Refer to pyeventsystem documentation for more information
  55. on how the middleware manager works.
  56. :rtype: :class:`.MiddlewareManager`
  57. :return: An object of class MiddlewareManager, which can be used to
  58. add or remove middleware from cloudbridge.
  59. """
  60. pass
  61. @abstractmethod
  62. def authenticate(self):
  63. """
  64. Checks whether a provider can be successfully authenticated with the
  65. configured settings. Clients are *not* required to call this method
  66. prior to accessing provider services, as most cloud connections are
  67. initialized lazily. The authenticate() method will return True if
  68. cloudbridge can establish a successful connection to the provider.
  69. It will raise an exception with the appropriate error details
  70. otherwise.
  71. Example:
  72. .. code-block:: python
  73. try:
  74. if provider.authenticate():
  75. print("Provider connection successful")
  76. except ProviderConnectionException as e:
  77. print("Could not authenticate with provider: %s" % (e, ))
  78. :rtype: :class:`bool`
  79. :return: ``True`` if authentication is successful.
  80. """
  81. pass
  82. @abstractmethod
  83. def has_service(self, service_type):
  84. """
  85. Checks whether this provider supports a given service.
  86. Example:
  87. .. code-block:: python
  88. if provider.has_service(CloudServiceType.BUCKET):
  89. print("Provider supports object store services")
  90. provider.storage.buckets.list()
  91. :type service_type: :class:`.CloudServiceType`
  92. :param service_type: Type of service to check support for.
  93. :rtype: :class:`bool`
  94. :return: ``True`` if the service type is supported.
  95. """
  96. pass
  97. # @abstractproperty
  98. # def account(self):
  99. # """
  100. # Provides access to all user account related services in this
  101. # provider. This includes listing available tenancies.
  102. #
  103. # :rtype: ``object`` of :class:`.ComputeService`
  104. # :return: a ComputeService object
  105. # """
  106. # pass
  107. @abstractproperty
  108. def compute(self):
  109. """
  110. Provides access to all compute related services in this provider.
  111. Example:
  112. .. code-block:: python
  113. regions = provider.compute.regions.list()
  114. vm_types = provider.compute.vm_types.list()
  115. instances = provider.compute.instances.list()
  116. images = provider.compute.images.list()
  117. # Alternatively
  118. for instance in provider.compute.instances:
  119. print(instance.name)
  120. :rtype: :class:`.ComputeService`
  121. :return: a ComputeService object
  122. """
  123. pass
  124. @abstractproperty
  125. def networking(self):
  126. """
  127. Provide access to all network related services in this provider.
  128. Example:
  129. .. code-block:: python
  130. networks = provider.networking.networks.list()
  131. subnets = provider.networking.subnets.list()
  132. routers = provider.networking.routers.list()
  133. :rtype: :class:`.NetworkingService`
  134. :return: a NetworkingService object
  135. """
  136. @abstractproperty
  137. def security(self):
  138. """
  139. Provides access to key pair management and firewall control
  140. Example:
  141. .. code-block:: python
  142. keypairs = provider.security.keypairs.list()
  143. vm_firewalls = provider.security.vm_firewalls.list()
  144. :rtype: ``object`` of :class:`.SecurityService`
  145. :return: a SecurityService object
  146. """
  147. pass
  148. @abstractproperty
  149. def storage(self):
  150. """
  151. Provides access to storage related services in this provider.
  152. This includes the volume, snapshot and bucket services,
  153. Example:
  154. .. code-block:: python
  155. volumes = provider.storage.volumes.list()
  156. snapshots = provider.storage.snapshots.list()
  157. if provider.has_service(CloudServiceType.BUCKET):
  158. print("Provider supports object store services")
  159. print(provider.storage.buckets.list())
  160. :rtype: :class:`.StorageService`
  161. :return: a StorageService object
  162. """
  163. pass
  164. class TestMockHelperMixin(object):
  165. """
  166. A helper class that providers mock drivers can use to be notified when a
  167. test setup/teardown occurs. This is useful when activating libraries
  168. like HTTPretty which take over socket communications.
  169. """
  170. def setUpMock(self):
  171. """
  172. Called before a test is started.
  173. """
  174. raise NotImplementedError(
  175. 'TestMockHelperMixin.setUpMock not implemented')
  176. def tearDownMock(self):
  177. """
  178. Called before test teardown.
  179. """
  180. raise NotImplementedError(
  181. 'TestMockHelperMixin.tearDownMock not implemented by this'
  182. ' provider')
  183. class ContainerProvider(object):
  184. """
  185. Represents a container instance, such as Docker or LXC
  186. """
  187. __metaclass__ = ABCMeta
  188. @abstractmethod
  189. def create_container(self):
  190. pass
  191. @abstractmethod
  192. def delete_container(self):
  193. pass
  194. class DeploymentProvider(object):
  195. """
  196. Represents a deployment provider, such as Ansible or Shell script provider
  197. """
  198. __metaclass__ = ABCMeta
  199. @abstractmethod
  200. def deploy(self, target):
  201. """
  202. Deploys on given target, where target is an Instance or Container
  203. """
  204. pass