types.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. DataTypes used by this provider
  3. """
  4. from cloudbridge.providers.base import BaseKeyPair
  5. from cloudbridge.providers.base import BaseSecurityGroup
  6. from cloudbridge.providers.interfaces import Instance
  7. class EC2Instance(Instance):
  8. def __init__(self, provider, ec2_instance):
  9. self.provider = provider
  10. self._ec2_instance = ec2_instance
  11. def instance_id(self):
  12. """
  13. Get the instance identifier.
  14. """
  15. return self._ec2_instance.id
  16. @property
  17. def name(self):
  18. """
  19. Get the instance name.
  20. .. note:: an instance must have a (case sensitive) tag ``Name``
  21. """
  22. return self._ec2_instance.tags.get('Name')
  23. @name.setter
  24. def name(self, value):
  25. """
  26. Set the instance name.
  27. """
  28. self._ec2_instance.add_tag('Name', value)
  29. def public_ips(self):
  30. """
  31. Get all the public IP addresses for this instance.
  32. """
  33. return [self._ec2_instance.ip_address]
  34. def private_ips(self):
  35. """
  36. Get all the private IP addresses for this instance.
  37. """
  38. return [self._ec2_instance.private_ip_address]
  39. def instance_type(self):
  40. """
  41. Get the instance type.
  42. """
  43. return [self._ec2_instance.instance_type]
  44. def reboot(self):
  45. """
  46. Reboot this instance (using the cloud middleware API).
  47. """
  48. self._ec2_instance.reboot()
  49. def terminate(self):
  50. """
  51. Permanently terminate this instance.
  52. """
  53. self._ec2_instance.terminate()
  54. def image_id(self):
  55. """
  56. Get the image ID for this insance.
  57. """
  58. return self._ec2_instance.image_id
  59. def placement_zone(self):
  60. """
  61. Get the placement zone where this instance is running.
  62. """
  63. return self._ec2_instance.placement
  64. def mac_address(self):
  65. """
  66. Get the MAC address for this instance.
  67. """
  68. raise NotImplementedError(
  69. 'mac_address not implemented by this provider')
  70. def security_group_ids(self):
  71. """
  72. Get the security group IDs associated with this instance.
  73. """
  74. return [BaseSecurityGroup(group.name) for group in self._ec2_instance.groups]
  75. def key_pair_name(self):
  76. """
  77. Get the name of the key pair associated with this instance.
  78. """
  79. return BaseKeyPair(self._ec2_instance.key_name)