provider.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. @abstractmethod
  49. def authenticate(self):
  50. """
  51. Checks whether a provider can be successfully authenticated with the
  52. configured settings. Clients are *not* required to call this method
  53. prior to accessing provider services, as most cloud connections are
  54. initialized lazily. The authenticate() method will return True if
  55. cloudbridge can establish a successful connection to the provider.
  56. It will raise an exception with the appropriate error details
  57. otherwise.
  58. Example:
  59. .. code-block:: python
  60. try:
  61. if provider.authenticate():
  62. print("Provider connection successful")
  63. except ProviderConnectionException as e:
  64. print("Could not authenticate with provider: %s" % (e, ))
  65. :rtype: :class:`bool`
  66. :return: ``True`` if authentication is successful.
  67. """
  68. pass
  69. @abstractmethod
  70. def has_service(self, service_type):
  71. """
  72. Checks whether this provider supports a given service.
  73. Example:
  74. .. code-block:: python
  75. if provider.has_service(CloudServiceType.BUCKET):
  76. print("Provider supports object store services")
  77. provider.storage.buckets.list()
  78. :type service_type: :class:`.CloudServiceType`
  79. :param service_type: Type of service to check support for.
  80. :rtype: :class:`bool`
  81. :return: ``True`` if the service type is supported.
  82. """
  83. pass
  84. # @abstractproperty
  85. # def account(self):
  86. # """
  87. # Provides access to all user account related services in this
  88. # provider. This includes listing available tenancies.
  89. #
  90. # :rtype: ``object`` of :class:`.ComputeService`
  91. # :return: a ComputeService object
  92. # """
  93. # pass
  94. @abstractproperty
  95. def compute(self):
  96. """
  97. Provides access to all compute related services in this provider.
  98. Example:
  99. .. code-block:: python
  100. regions = provider.compute.regions.list()
  101. vm_types = provider.compute.vm_types.list()
  102. instances = provider.compute.instances.list()
  103. images = provider.compute.images.list()
  104. # Alternatively
  105. for instance in provider.compute.instances:
  106. print(instance.name)
  107. :rtype: :class:`.ComputeService`
  108. :return: a ComputeService object
  109. """
  110. pass
  111. @abstractproperty
  112. def networking(self):
  113. """
  114. Provide access to all network related services in this provider.
  115. Example:
  116. .. code-block:: python
  117. networks = provider.networking.networks.list()
  118. subnets = provider.networking.subnets.list()
  119. routers = provider.networking.routers.list()
  120. :rtype: :class:`.NetworkingService`
  121. :return: a NetworkingService object
  122. """
  123. @abstractproperty
  124. def security(self):
  125. """
  126. Provides access to key pair management and firewall control
  127. Example:
  128. .. code-block:: python
  129. keypairs = provider.security.keypairs.list()
  130. vm_firewalls = provider.security.vm_firewalls.list()
  131. :rtype: ``object`` of :class:`.SecurityService`
  132. :return: a SecurityService object
  133. """
  134. pass
  135. @abstractproperty
  136. def storage(self):
  137. """
  138. Provides access to storage related services in this provider.
  139. This includes the volume, snapshot and bucket services,
  140. Example:
  141. .. code-block:: python
  142. volumes = provider.storage.volumes.list()
  143. snapshots = provider.storage.snapshots.list()
  144. if provider.has_service(CloudServiceType.BUCKET):
  145. print("Provider supports object store services")
  146. print(provider.storage.buckets.list())
  147. :rtype: :class:`.StorageService`
  148. :return: a StorageService object
  149. """
  150. pass
  151. class TestMockHelperMixin(object):
  152. """
  153. A helper class that providers mock drivers can use to be notified when a
  154. test setup/teardown occurs. This is useful when activating libraries
  155. like HTTPretty which take over socket communications.
  156. """
  157. def setUpMock(self):
  158. """
  159. Called before a test is started.
  160. """
  161. raise NotImplementedError(
  162. 'TestMockHelperMixin.setUpMock not implemented')
  163. def tearDownMock(self):
  164. """
  165. Called before test teardown.
  166. """
  167. raise NotImplementedError(
  168. 'TestMockHelperMixin.tearDownMock not implemented by this'
  169. ' provider')
  170. class ContainerProvider(object):
  171. """
  172. Represents a container instance, such as Docker or LXC
  173. """
  174. __metaclass__ = ABCMeta
  175. @abstractmethod
  176. def create_container(self):
  177. pass
  178. @abstractmethod
  179. def delete_container(self):
  180. pass
  181. class DeploymentProvider(object):
  182. """
  183. Represents a deployment provider, such as Ansible or Shell script provider
  184. """
  185. __metaclass__ = ABCMeta
  186. @abstractmethod
  187. def deploy(self, target):
  188. """
  189. Deploys on given target, where target is an Instance or Container
  190. """
  191. pass