provider.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 has_service(self, service_type):
  48. """
  49. Checks whether this provider supports a given service.
  50. Example:
  51. .. code-block:: python
  52. if provider.has_service(CloudServiceType.OBJECT_STORE):
  53. print("Provider supports object store services")
  54. provider.object_store.list()
  55. :type service_type: :class:`.CloudServiceType`
  56. :param service_type: Type of service to check support for.
  57. :rtype: :class:`bool`
  58. :return: ``True`` if the service type is supported.
  59. """
  60. pass
  61. # @abstractproperty
  62. # def account(self):
  63. # """
  64. # Provides access to all user account related services in this
  65. # provider. This includes listing available tenancies.
  66. #
  67. # :rtype: ``object`` of :class:`.ComputeService`
  68. # :return: a ComputeService object
  69. # """
  70. # pass
  71. @abstractproperty
  72. def compute(self):
  73. """
  74. Provides access to all compute related services in this provider.
  75. Example:
  76. .. code-block:: python
  77. regions = provider.compute.regions.list()
  78. instance_types = provider.compute.instance_types.list()
  79. instances = provider.compute.instances.list()
  80. images = provider.compute.images.list()
  81. # Alternatively
  82. for instance in provider.compute.instances:
  83. print(instance.name)
  84. :rtype: :class:`.ComputeService`
  85. :return: a ComputeService object
  86. """
  87. pass
  88. @abstractproperty
  89. def network(self):
  90. """
  91. Provide access to all network related services in this provider.
  92. Example:
  93. .. code-block:: python
  94. networks = provider.network.list()
  95. network = provider.network.create(name="DevNet")
  96. :rtype: :class:`.NetworkService`
  97. :return: a NetworkService object
  98. """
  99. @abstractproperty
  100. def security(self):
  101. """
  102. Provides access to keypair management and firewall control
  103. Example:
  104. .. code-block:: python
  105. keypairs = provider.security.keypairs.list()
  106. security_groups = provider.security.security_groups.list()
  107. :rtype: ``object`` of :class:`.SecurityService`
  108. :return: a SecurityService object
  109. """
  110. pass
  111. @abstractproperty
  112. def block_store(self):
  113. """
  114. Provides access to the volume and snapshot services in this
  115. provider.
  116. Example:
  117. .. code-block:: python
  118. volumes = provider.block_store.volumes.list()
  119. snapshots = provider.block_store.snapshots.list()
  120. :rtype: :class:`.BlockStoreService`
  121. :return: a BlockStoreService object
  122. """
  123. pass
  124. @abstractproperty
  125. def object_store(self):
  126. """
  127. Provides access to object storage services in this provider.
  128. Example:
  129. .. code-block:: python
  130. if provider.has_service(CloudServiceType.OBJECT_STORE):
  131. print("Provider supports object store services")
  132. print(provider.object_store.list())
  133. :rtype: ``object`` of :class:`.ObjectStoreService`
  134. :return: an ObjectStoreService object
  135. """
  136. pass
  137. class TestMockHelperMixin(object):
  138. """
  139. A helper class that providers mock drivers can use to be notified when a
  140. test setup/teardown occurs. This is useful when activating libraries
  141. like HTTPretty which take over socket communications.
  142. """
  143. def setUpMock(self):
  144. """
  145. Called before a test is started.
  146. """
  147. raise NotImplementedError(
  148. 'TestMockHelperMixin.setUpMock not implemented')
  149. def tearDownMock(self):
  150. """
  151. Called before test teardown.
  152. """
  153. raise NotImplementedError(
  154. 'TestMockHelperMixin.tearDownMock not implemented by this'
  155. ' provider')
  156. class ContainerProvider(object):
  157. """
  158. Represents a container instance, such as Docker or LXC
  159. """
  160. __metaclass__ = ABCMeta
  161. @abstractmethod
  162. def create_container(self):
  163. pass
  164. @abstractmethod
  165. def delete_container(self):
  166. pass
  167. class DeploymentProvider(object):
  168. """
  169. Represents a deployment provider, such as Ansible or Shell script provider
  170. """
  171. __metaclass__ = ABCMeta
  172. @abstractmethod
  173. def deploy(self, target):
  174. """
  175. Deploys on given target, where target is an Instance or Container
  176. """
  177. pass