types.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. """
  21. return self._ec2_instance.tags['Name']
  22. @name.setter
  23. def name(self, value):
  24. """
  25. Set the instance name.
  26. """
  27. self._ec2_instance.add_tag('Name', value)
  28. def public_ips(self):
  29. """
  30. Get all the public IP addresses for this instance.
  31. """
  32. return [self._ec2_instance.ip_address]
  33. def private_ips(self):
  34. """
  35. Get all the private IP addresses for this instance.
  36. """
  37. return [self._ec2_instance.private_ip_address]
  38. def instance_type(self):
  39. """
  40. Get the instance type.
  41. """
  42. return [self._ec2_instance.instance_type]
  43. def reboot(self):
  44. """
  45. Reboot this instance (using the cloud middleware API).
  46. """
  47. self._ec2_instance.reboot()
  48. def terminate(self):
  49. """
  50. Permanently terminate this instance.
  51. """
  52. self._ec2_instance.terminate()
  53. def image_id(self):
  54. """
  55. Get the image ID for this insance.
  56. """
  57. return self._ec2_instance.image_id
  58. def placement_zone(self):
  59. """
  60. Get the placement zone where this instance is running.
  61. """
  62. return self._ec2_instance.placement
  63. def mac_address(self):
  64. """
  65. Get the MAC address for this instance.
  66. """
  67. raise NotImplementedError(
  68. 'mac_address not implemented by this provider')
  69. def security_group_ids(self):
  70. """
  71. Get the security group IDs associated with this instance.
  72. """
  73. return [BaseSecurityGroup(group.name) for group in self._ec2_instance.groups]
  74. def key_pair_name(self):
  75. """
  76. Get the name of the key pair associated with this instance.
  77. """
  78. return BaseKeyPair(self._ec2_instance.key_name)