provider_development.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. Provider Development Walkthrough
  2. ================================
  3. This guide will walk you through the basic process of developing a new provider
  4. for CloudBridge.
  5. 1. We start off by creating a new folder for the provider within the
  6. ``cloudbridge/cloud/providers`` folder. In this case: ``gce``. Further, install
  7. the native cloud provider Python library, here
  8. ``pip install google-api-python-client==1.4.2`` and a couple of its requirements
  9. ``oauth2client==1.5.2`` and ``pycrypto==2.6.1``.
  10. 2. Add a ``provider.py`` file. This file will contain the main implementation
  11. of the cloud provider and will be the entry point that CloudBridge uses for all
  12. provider related services. You will need to subclass ``BaseCloudProvider`` and
  13. add a class variable named ``PROVIDER_ID``.
  14. .. code-block:: python
  15. from cloudbridge.cloud.base import BaseCloudProvider
  16. class GCECloudProvider(BaseCloudProvider):
  17. PROVIDER_ID = 'gce'
  18. def __init__(self, config):
  19. super(GCECloudProvider, self).__init__(config)
  20. 3. Add an ``__init__.py`` to the ``cloudbridge/cloud/providers/gce`` folder
  21. and export the provider.
  22. .. code-block:: python
  23. from .provider import GCECloudProvider # noqa
  24. .. tip ::
  25. You can view the code so far here: `commit 1`_
  26. 4. Next, we need to register the provider with the factory.
  27. This only requires that you register the provider's ID in the ``ProviderList``.
  28. Add GCE to the ``ProviderList`` class in ``cloudbridge/cloud/factory.py``.
  29. 5. Run the test suite. We will get the tests passing on py27 first.
  30. .. code-block:: bash
  31. export CB_TEST_PROVIDER=gce
  32. tox -e py27
  33. You should see the tests fail with the following message:
  34. .. code-block:: bash
  35. TypeError: Can't instantiate abstract class GCECloudProvider with abstract
  36. methods storage, compute, security, network
  37. 6. Therefore, our next step is to implement these methods. We can start off by
  38. implementing these methods in ``provider.py`` and raising a
  39. ``NotImplementedError``.
  40. .. code-block:: python
  41. @property
  42. def compute(self):
  43. raise NotImplementedError(
  44. "GCECloudProvider does not implement this service")
  45. @property
  46. def network(self):
  47. raise NotImplementedError(
  48. "GCECloudProvider does not implement this service")
  49. @property
  50. def security(self):
  51. raise NotImplementedError(
  52. "GCECloudProvider does not implement this service")
  53. @property
  54. def storage(self):
  55. raise NotImplementedError(
  56. "GCECloudProvider does not implement this service")
  57. Running the tests now will complain as much. We will next implement each
  58. Service in turn.
  59. 7. We will start with the compute service. Add a ``services.py`` file.
  60. .. code-block:: python
  61. from cloudbridge.cloud.base.services import BaseSecurityService
  62. class GCESecurityService(BaseSecurityService):
  63. def __init__(self, provider):
  64. super(GCESecurityService, self).__init__(provider)
  65. 8. We can now return this new service from the security property in
  66. ``provider.py`` as follows:
  67. .. code-block:: python
  68. def __init__(self, config):
  69. super(GCECloudProvider, self).__init__(config)
  70. self._security = GCESecurityService(self)
  71. @property
  72. def security(self):
  73. return self._security
  74. .. tip ::
  75. You can view the code so far here: `commit 2`_
  76. 9. Run the tests, and the following message will cause all security service
  77. tests to fail:
  78. .. code-block:: bash
  79. TypeError: Can't instantiate abstract class GCESecurityService with abstract
  80. methods key_pairs, security_groups
  81. The Abstract Base Classes are doing their job and flagging all methods that
  82. need to be implemented.
  83. 10. Since the security service simply provides organisational structure, and is
  84. a container for the ``key_pairs`` and ``security_groups`` services, we must
  85. next implement these services.
  86. .. code-block:: python
  87. from cloudbridge.cloud.base.services import BaseKeyPairService
  88. from cloudbridge.cloud.base.services import BaseSecurityGroupService
  89. from cloudbridge.cloud.base.services import BaseSecurityService
  90. class GCESecurityService(BaseSecurityService):
  91. def __init__(self, provider):
  92. super(GCESecurityService, self).__init__(provider)
  93. # Initialize provider services
  94. self._key_pairs = GCEKeyPairService(provider)
  95. self._security_groups = GCESecurityGroupService(provider)
  96. @property
  97. def key_pairs(self):
  98. return self._key_pairs
  99. @property
  100. def security_groups(self):
  101. return self._security_groups
  102. class GCEKeyPairService(BaseKeyPairService):
  103. def __init__(self, provider):
  104. super(GCEKeyPairService, self).__init__(provider)
  105. class GCESecurityGroupService(BaseSecurityGroupService):
  106. def __init__(self, provider):
  107. super(GCESecurityGroupService, self).__init__(provider)
  108. .. tip ::
  109. You can view the code so far here: `commit 3`_
  110. Once again, running the tests will complain of missing methods:
  111. .. code-block:: bash
  112. TypeError: Can't instantiate abstract class GCEKeyPairService with abstract
  113. methods create, find, get, list
  114. 11. Keep implementing the methods till the security service works, and the
  115. tests pass.
  116. .. note ::
  117. We start off by implementing the list keypairs method. Therefore, to obtain
  118. the keypair, we need to have a connection to the cloud provider. For this,
  119. we need to install the Google sdk, and thereafter, to obtain the desired
  120. connection via the sdk. While the design and structure of that connection
  121. is up to the implementor, a general design we have followed is to have the
  122. cloud connection globally available within the provider.
  123. To add the sdk, we edit CloudBridge's main ``setup.py`` and list the
  124. dependencies.
  125. .. code-block:: python
  126. gce_reqs = ['google-api-python-client==1.4.2']
  127. full_reqs = base_reqs + aws_reqs + openstack_reqs + gce_reqs
  128. We will also register the provider in ``cloudbridge/cloud/factory.py``'s
  129. provider list.
  130. .. code-block:: python
  131. class ProviderList(object):
  132. AWS = 'aws'
  133. OPENSTACK = 'openstack'
  134. ...
  135. GCE = 'gce'
  136. .. tip ::
  137. You can view the code so far here: `commit 4`_
  138. 12. Thereafter, we create the actual connection through the sdk. In the case of
  139. GCE, we need a Compute API client object. We will make this connection
  140. available as a public property named ``gce_compute`` in the provider. We will
  141. then lazily initialize this connection.
  142. A full implementation of the KeyPair service can now be made in a provider
  143. specific manner.
  144. .. tip ::
  145. You can view the code so far here: `commit 5`_
  146. .. _commit 1: https://github.com/CloudVE/cloudbridge/commit/54c67e93a3cd9d51e7d2b1195ebf4e257d165297
  147. .. _commit 2: https://github.com/CloudVE/cloudbridge/commit/82c0244aa4229ae0aecfe40d769eb93b06470dc7
  148. .. _commit 3: https://github.com/CloudVE/cloudbridge/commit/e90a7f6885814a3477cd0b38398d62af64f91093
  149. .. _commit 4: https://github.com/CloudVE/cloudbridge/commit/2d5c14166a538d320e54eed5bc3fa04997828715
  150. .. _commit 5: https://github.com/CloudVE/cloudbridge/commit/98c9cf578b672867ee503027295f9d901411e496