services.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import logging
  2. from cloudbridge.cloud.base.resources import ClientPagedResultList
  3. from cloudbridge.cloud.base.services import BaseObjectStoreService, BaseSecurityGroupService, BaseSecurityService
  4. from .resources import AzureBucket, AzureSecurityGroup
  5. log = logging.getLogger(__name__)
  6. class AzureSecurityService(BaseSecurityService):
  7. def __init__(self, provider):
  8. super(AzureSecurityService, self).__init__(provider)
  9. # Initialize provider services
  10. # self._key_pairs = AzureKeyPairService(provider)
  11. self._security_groups = AzureSecurityGroupService(provider)
  12. @property
  13. def key_pairs(self):
  14. """
  15. Provides access to key pairs for this provider.
  16. :rtype: ``object`` of :class:`.KeyPairService`
  17. :return: a KeyPairService object
  18. """
  19. return None
  20. @property
  21. def security_groups(self):
  22. """
  23. Provides access to security groups for this provider.
  24. :rtype: ``object`` of :class:`.SecurityGroupService`
  25. :return: a SecurityGroupService object
  26. """
  27. return self._security_groups
  28. class AzureSecurityGroupService(BaseSecurityGroupService):
  29. def __init__(self, provider):
  30. super(AzureSecurityGroupService, self).__init__(provider)
  31. def get(self, sg_id):
  32. for item in self.provider.azure_client.list_security_group():
  33. if item.id == sg_id:
  34. return AzureSecurityGroup(self.provider, item)
  35. return None
  36. def list(self, limit=None, marker=None):
  37. nsg_list = self.provider.azure_client.list_security_group()
  38. network_security_group = [AzureSecurityGroup(self.provider, sg)
  39. for sg in nsg_list]
  40. return ClientPagedResultList(self.provider, network_security_group, limit, marker)
  41. # network_id is similar to resource group in azure
  42. def create(self, name, description, network_id):
  43. parameters = {"location": self.provider.region_name}
  44. result = self.provider.azure_client.create_security_group(name, parameters)
  45. return AzureSecurityGroup(self.provider, result)
  46. def find(self, name, limit=None, marker=None):
  47. raise NotImplementedError(
  48. "AzureSecurityGroupService does not implement this method")
  49. def delete(self, group_id):
  50. for item in self.provider.azure_client.list_security_group():
  51. if item.id == group_id:
  52. sg_name = item.name
  53. self.provider.azure_client.delete_security_group(sg_name)
  54. return True
  55. return False
  56. class AzureObjectStoreService(BaseObjectStoreService):
  57. def __init__(self, provider):
  58. super(AzureObjectStoreService, self).__init__(provider)
  59. def get(self, bucket_id):
  60. log.info("Azure Object Store Service get API with bucket id - " + str(bucket_id))
  61. object_store = self.provider.azure_client.get_container(bucket_id)
  62. if object_store:
  63. return AzureBucket(self.provider, object_store)
  64. return None
  65. def find(self, name, limit=None, marker=None):
  66. object_store = self.provider.azure_client.get_container(name)
  67. object_stores = []
  68. if object_store:
  69. object_stores.append(AzureBucket(self.provider, object_store))
  70. return ClientPagedResultList(self.provider, object_stores,
  71. limit=limit, marker=marker)
  72. def list(self, limit=None, marker=None):
  73. raise NotImplementedError(
  74. "AzureObjectStoreService does not implement this method")
  75. def create(self, name, location=None):
  76. self.provider.azure_client.create_container(name)