provider_development.rst 7.2 KB

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