base.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from cloudbridge.providers.interfaces import CloudProvider
  2. from cloudbridge.providers.interfaces import KeyPair
  3. from cloudbridge.providers.interfaces import SecurityGroup
  4. class BaseCloudProvider(CloudProvider):
  5. def __init__(self, config):
  6. self.config = config
  7. def name(self):
  8. return str(self.__class__.__name__)
  9. def has_service(self, service_type):
  10. """
  11. Checks whether this provider supports a given service.
  12. :type service_type: str or :class:``.CloudProviderServiceType``
  13. :param service_type: Type of service the check support for.
  14. :rtype: bool
  15. :return: ``True`` if the service type is supported.
  16. """
  17. try:
  18. if getattr(self, service_type):
  19. return True
  20. except AttributeError:
  21. pass # Undefined service type
  22. return False
  23. def _get_config_value(self, key, default_value):
  24. if isinstance(self.config, dict):
  25. return self.config.get(key, default_value)
  26. else:
  27. return getattr(self.config, key) if hasattr(self.config, key) and getattr(self.config, key) else default_value
  28. class BaseKeyPair(KeyPair):
  29. def __init__(self, name, material=None):
  30. self.name = name
  31. self.material = material
  32. def __repr__(self):
  33. return "<CBKeyPair: {0}>".format(self.name)
  34. # def name(self):
  35. # """
  36. # Return the name of this key pair.
  37. # :rtype: str
  38. # :return: A name of this ssh key pair
  39. # """
  40. # raise NotImplementedError(
  41. # 'name not implemented by this provider')
  42. class BaseSecurityGroup(SecurityGroup):
  43. def __init__(self, name):
  44. self.name = name
  45. def __repr__(self):
  46. return "<CBSecurityGroup: {0}>".format(self.name)