Merge branch 'LIBCLOUD-467_Add_VPC_Lifecycle_Support' into trunk
Conflicts:
libcloud/compute/drivers/ec2.py
libcloud/test/compute/test_ec2.py
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/e35c9893
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/e35c9893
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/e35c9893
Branch: refs/heads/trunk
Commit: e35c98934dd9fa57bb26921ace35513e1c8e05b0
Parents: b5a105a 7777034
Author: Chris DeRamus <[email protected]>
Authored: Mon Dec 23 23:29:07 2013 -0500
Committer: Chris DeRamus <[email protected]>
Committed: Mon Dec 23 23:29:07 2013 -0500
----------------------------------------------------------------------
libcloud/compute/drivers/ec2.py | 165 ++++++++++++++++++-
.../test/compute/fixtures/ec2/create_vpc.xml | 10 ++
.../test/compute/fixtures/ec2/delete_vpc.xml | 4 +
.../test/compute/fixtures/ec2/describe_vpcs.xml | 28 ++++
libcloud/test/compute/test_ec2.py | 46 ++++++
5 files changed, 251 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/libcloud/blob/e35c9893/libcloud/compute/drivers/ec2.py
----------------------------------------------------------------------
diff --cc libcloud/compute/drivers/ec2.py
index b00f18a,bd25da7..30bd4cd
--- a/libcloud/compute/drivers/ec2.py
+++ b/libcloud/compute/drivers/ec2.py
@@@ -559,24 -503,24 +559,40 @@@ class ExEC2AvailabilityZone(object)
% (self.name, self.zone_state, self.region_name))
+class EC2ReservedNode(Node):
+ """
+ Class which stores information about EC2 reserved instances/nodes
+ Inherits from Node and passes in None for name and private/public IPs
-
- Note: This class is EC2 specific.
+ """
+
+ def __init__(self, id, state, driver, size=None, image=None, extra=None):
+ super(EC2ReservedNode, self).__init__(id=id, name=None, state=state,
+ public_ips=None,
+ private_ips=None,
+ driver=driver, extra=extra)
+
+ def __repr__(self):
+ return (('<EC2ReservedNode: id=%s>') % (self.id))
+
+
+ class EC2Network(object):
+ """
+ Represents information about a VPC (Virtual Private Cloud) network
+
+ Note: This class is EC2 specific.
+ """
+
+ def __init__(self, id, name, cidr_block, extra=None):
+ self.id = id
+ self.name = name
+ self.cidr_block = cidr_block
+ self.extra = extra or {}
+
+ def __repr__(self):
+ return (('<EC2Network: id=%s, name=%s')
+ % (self.id, self.name))
+
+
class BaseEC2NodeDriver(NodeDriver):
"""
Base Amazon EC2 node driver.
@@@ -876,22 -738,73 +892,89 @@@
'description': description,
'state': state})
+ def _to_networks(self, response):
+ return [self._to_network(el) for el in response.findall(
+ fixxpath(xpath='vpcSet/item', namespace=NAMESPACE))
+ ]
+
+ def _to_network(self, element):
+ # Get the network id
+ vpc_id = findtext(element=element,
+ xpath='vpcId',
+ namespace=NAMESPACE)
+
+ # Get our tag items
+ tag_items = findall(element=element,
+ xpath='tagSet/item',
+ namespace=NAMESPACE)
+
+ # Loop through all tag items to build our dictionary
+ tags = {}
+ for tag in tag_items:
+ key = findtext(element=tag,
+ xpath='key',
+ namespace=NAMESPACE)
+
+ value = findtext(element=tag,
+ xpath='value',
+ namespace=NAMESPACE)
+
+ tags[key] = value
+
+ # Set our name if the Name key/value if available
+ # If we don't get anything back then use the vpc_id
+ name = tags.get('Name', vpc_id)
+
+ cidr_block = findtext(element=element,
+ xpath='cidrBlock',
+ namespace=NAMESPACE)
+
+ # Build our extra attributes map
+ extra_attributes_map = {
+ 'state': {
+ 'xpath': 'state',
+ 'type': str
+ },
+ 'dhcp_options_id': {
+ 'xpath': 'dhcpOptionsId',
+ 'type': str
+ },
+ 'instance_tenancy': {
+ 'xpath': 'instanceTenancy',
+ 'type': str
+ },
+ 'is_default': {
+ 'xpath': 'isDefault',
+ 'type': str
+ }
+ }
+
+ # Define and build our extra dictionary
+ extra = {}
+ for attribute, values in extra_attributes_map.items():
+ type_func = values['type']
+ value = findattr(element=element, xpath=values['xpath'],
+ namespace=NAMESPACE)
+ extra[attribute] = type_func(value)
+
+ return EC2Network(vpc_id, name, cidr_block, extra=extra)
+
+ def ex_list_reserved_nodes(self):
+ """
+ List all reserved instances/nodes which can be purchased from Amazon
+ for one or three year terms. Reservations are made at a region level
+ and reduce the hourly charge for instances.
+
+ More information can be found at http://goo.gl/ulXCC7.
+
+ :rtype: ``list`` of :class:`.EC2ReservedNode`
+ """
+ params = {'Action': 'DescribeReservedInstances'}
+
+ response = self.connection.request(self.path, params=params).object
+
+ return self._to_reserved_nodes(response, 'reservedInstancesSet/item')
+
def list_nodes(self, ex_node_ids=None):
"""
List all nodes
http://git-wip-us.apache.org/repos/asf/libcloud/blob/e35c9893/libcloud/test/compute/test_ec2.py
----------------------------------------------------------------------
diff --cc libcloud/test/compute/test_ec2.py
index a7194f3,10412cf..2074a51
--- a/libcloud/test/compute/test_ec2.py
+++ b/libcloud/test/compute/test_ec2.py
@@@ -771,13 -708,39 +771,46 @@@ class EC2Tests(LibcloudTestCase, TestCa
'max-elastic-ips': 5}
self.assertEqual(limits['resource'], expected)
+ def test_ex_create_security_group(self):
+ group = self.driver.ex_create_security_group("WebServers",
+ "Rules to protect web
nodes",
+ "vpc-143cab4")
+
+ self.assertEqual(group["group_id"], "sg-52e2f530")
+
+ def test_ex_list_networks(self):
+ vpcs = self.driver.ex_list_networks()
+
+ self.assertEqual(len(vpcs), 2)
+
+ self.assertEqual('vpc-532335e1', vpcs[0].id)
+ self.assertEqual('vpc-532335e1', vpcs[0].name)
+ self.assertEqual('192.168.51.0/24', vpcs[0].cidr_block)
+ self.assertEqual('available', vpcs[0].extra['state'])
+ self.assertEqual('dopt-7eded312', vpcs[0].extra['dhcp_options_id'])
+
+ self.assertEqual('vpc-62ded30e', vpcs[1].id)
+ self.assertEqual('Test VPC', vpcs[1].name)
+ self.assertEqual('192.168.52.0/24', vpcs[1].cidr_block)
+ self.assertEqual('available', vpcs[1].extra['state'])
+ self.assertEqual('dopt-7eded312', vpcs[1].extra['dhcp_options_id'])
+
+ def test_ex_create_network(self):
+ vpc = self.driver.ex_create_network('192.168.55.0/24',
+ name='Test VPC',
+ instance_tenancy='default')
+
+ self.assertEqual('vpc-ad3527cf', vpc['vpc_id'])
+ self.assertEqual('pending', vpc['state'])
+ self.assertEqual('192.168.55.0/24', vpc['cidr_block'])
+
+ def test_ex_destroy_network(self):
+ vpcs = self.driver.ex_list_networks()
+ vpc = vpcs[0]
+
+ resp = self.driver.ex_destroy_network(vpc.id)
+ self.assertTrue(resp)
+
class EC2USWest1Tests(EC2Tests):
region = 'us-west-1'
@@@ -1057,8 -1012,16 +1090,21 @@@ class EC2MockHttp(MockHttpTestCase)
body = self.fixtures.load('describe_account_attributes.xml')
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
++<<<<<<< HEAD
+ def _CreateSecurityGroup(self, method, url, body, headers):
+ body = self.fixtures.load('create_security_group.xml')
++=======
+ def _DescribeVpcs(self, method, url, body, headers):
+ body = self.fixtures.load('describe_vpcs.xml')
+ return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+ def _CreateVpc(self, method, url, body, headers):
+ body = self.fixtures.load('create_vpc.xml')
+ return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+ def _DeleteVpc(self, method, url, body, headers):
+ body = self.fixtures.load('delete_vpc.xml')
++>>>>>>> LIBCLOUD-467_Add_VPC_Lifecycle_Support
return (httplib.OK, body, {}, httplib.responses[httplib.OK])