provider.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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, grouped under `cloud_properties` and
  31. `credentials` keys. In addition, it also contains extra provider-wide
  32. properties such as the default result limit for `list()` 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['credentials'].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.BUCKET):
  74. print("Provider supports object store services")
  75. provider.storage.buckets.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. vm_types = provider.compute.vm_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 networking(self):
  111. """
  112. Provide access to all network related services in this provider.
  113. Example:
  114. .. code-block:: python
  115. networks = provider.networking.networks.list()
  116. subnets = provider.networking.subnets.list()
  117. routers = provider.networking.routers.list()
  118. :rtype: :class:`.NetworkingService`
  119. :return: a NetworkingService object
  120. """
  121. @abstractproperty
  122. def security(self):
  123. """
  124. Provides access to key pair management and firewall control
  125. Example:
  126. .. code-block:: python
  127. keypairs = provider.security.keypairs.list()
  128. vm_firewalls = provider.security.vm_firewalls.list()
  129. :rtype: ``object`` of :class:`.SecurityService`
  130. :return: a SecurityService object
  131. """
  132. pass
  133. @abstractproperty
  134. def storage(self):
  135. """
  136. Provides access to storage related services in this provider.
  137. This includes the volume, snapshot and bucket services,
  138. Example:
  139. .. code-block:: python
  140. volumes = provider.storage.volumes.list()
  141. snapshots = provider.storage.snapshots.list()
  142. if provider.has_service(CloudServiceType.BUCKET):
  143. print("Provider supports object store services")
  144. print(provider.storage.buckets.list())
  145. :rtype: :class:`.StorageService`
  146. :return: a StorageService object
  147. """
  148. pass
  149. class TestMockHelperMixin(object):
  150. """
  151. A helper class that providers mock drivers can use to be notified when a
  152. test setup/teardown occurs. This is useful when activating libraries
  153. like HTTPretty which take over socket communications.
  154. """
  155. def setUpMock(self):
  156. """
  157. Called before a test is started.
  158. """
  159. raise NotImplementedError(
  160. 'TestMockHelperMixin.setUpMock not implemented')
  161. def tearDownMock(self):
  162. """
  163. Called before test teardown.
  164. """
  165. raise NotImplementedError(
  166. 'TestMockHelperMixin.tearDownMock not implemented by this'
  167. ' provider')
  168. class ContainerProvider(object):
  169. """
  170. Represents a container instance, such as Docker or LXC
  171. """
  172. __metaclass__ = ABCMeta
  173. @abstractmethod
  174. def create_container(self):
  175. pass
  176. @abstractmethod
  177. def delete_container(self):
  178. pass
  179. class DeploymentProvider(object):
  180. """
  181. Represents a deployment provider, such as Ansible or Shell script provider
  182. """
  183. __metaclass__ = ABCMeta
  184. @abstractmethod
  185. def deploy(self, target):
  186. """
  187. Deploys on given target, where target is an Instance or Container
  188. """
  189. pass