provider.py 7.2 KB

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