Skip to content

Commit

Permalink
Ad ex_security_group_ids argument to the create_node method in the EC…
Browse files Browse the repository at this point in the history
… driver in

order to be able to launch nodes with security groups on a VPC.

Closes #373

Signed-off-by: Tomaz Muraus <tomaz@apache.org>
  • Loading branch information
Itxaka Serrano authored and Kami committed Nov 19, 2014
1 parent d84f7f8 commit d7c8671
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Expand Up @@ -27,6 +27,11 @@ Compute
(GITHUB-389)
[Loic Lambiel]

- Add ``ex_security_group_ids`` argument to the create_node method in the
EC2 driver. This way users can launch VPC nodes with security groups.
(GITHUB-373)
[Itxaka Serrano]

Changes with Apache Libcloud 0.16.0
-----------------------------------

Expand Down
18 changes: 18 additions & 0 deletions libcloud/compute/drivers/ec2.py
Expand Up @@ -2138,6 +2138,10 @@ def create_node(self, **kwargs):
assign to the node.
:type ex_security_groups: ``list``
:keyword ex_security_group_ids: A list of ids of security groups to
assign to the node.[for VPC nodes only]
:type ex_security_group_ids: ``list``
:keyword ex_metadata: Key/Value metadata to associate with a node
:type ex_metadata: ``dict``
Expand Down Expand Up @@ -2190,6 +2194,20 @@ def create_node(self, **kwargs):
params['SecurityGroup.%d' % (sig + 1,)] =\
security_groups[sig]

if 'ex_security_group_ids' in kwargs and 'ex_subnet' not in kwargs:
raise ValueError('You can only supply ex_security_group_ids'
' combinated with ex_subnet')

security_group_ids = kwargs.get('ex_security_group_ids', None)

if security_group_ids:
if not isinstance(security_group_ids, (tuple, list)):
security_group_ids = [security_group_ids]

for sig in range(len(security_group_ids)):
params['SecurityGroupId.%d' % (sig + 1,)] =\
security_group_ids[sig]

if 'location' in kwargs:
availability_zone = getattr(kwargs['location'],
'availability_zone', None)
Expand Down
27 changes: 27 additions & 0 deletions libcloud/test/compute/test_ec2.py
Expand Up @@ -34,6 +34,7 @@
from libcloud.compute.drivers.ec2 import IdempotentParamError
from libcloud.compute.drivers.ec2 import REGION_DETAILS
from libcloud.compute.drivers.ec2 import ExEC2AvailabilityZone
from libcloud.compute.drivers.ec2 import EC2NetworkSubnet
from libcloud.compute.base import Node, NodeImage, NodeSize, NodeLocation
from libcloud.compute.base import StorageVolume, VolumeSnapshot
from libcloud.compute.types import KeyPairDoesNotExistError
Expand Down Expand Up @@ -877,6 +878,25 @@ def test_create_node_ex_security_groups(self):
ex_securitygroup=security_groups,
ex_security_groups=security_groups)

def test_create_node_ex_security_group_ids(self):
EC2MockHttp.type = 'ex_security_group_ids'

image = NodeImage(id='ami-be3adfd7',
name=self.image_name,
driver=self.driver)
size = NodeSize('m1.small', 'Small Instance', None, None, None, None,
driver=self.driver)

subnet = EC2NetworkSubnet(12345, "test_subnet", "pending")
security_groups = ['sg-1aa11a1a', 'sg-2bb22b2b']

self.driver.create_node(name='foo', image=image, size=size,
ex_security_group_ids=security_groups,
ex_subnet=subnet)
self.assertRaises(ValueError, self.driver.create_node,
name='foo', image=image, size=size,
ex_security_group_ids=security_groups)

def test_ex_get_metadata_for_node(self):
image = NodeImage(id='ami-be3adfd7',
name=self.image_name,
Expand Down Expand Up @@ -1195,6 +1215,13 @@ def _ex_security_groups_RunInstances(self, method, url, body, headers):
body = self.fixtures.load('run_instances.xml')
return (httplib.OK, body, {}, httplib.responses[httplib.OK])

def _ex_security_group_ids_RunInstances(self, method, url, body, headers):
self.assertUrlContainsQueryParams(url, {'SecurityGroupId.1': 'sg-1aa11a1a'})
self.assertUrlContainsQueryParams(url, {'SecurityGroupId.2': 'sg-2bb22b2b'})

body = self.fixtures.load('run_instances.xml')
return (httplib.OK, body, {}, httplib.responses[httplib.OK])

def _create_ex_blockdevicemappings_RunInstances(self, method, url, body, headers):
expected_params = {
'BlockDeviceMapping.1.DeviceName': '/dev/sda1',
Expand Down

0 comments on commit d7c8671

Please sign in to comment.