Skip to content

Commit

Permalink
Merge pull request #670 from AmineChikhaoui/fix-669
Browse files Browse the repository at this point in the history
Fix #669: Use security group id in the api call in case it's deployed in a VPC
  • Loading branch information
rbvermaa committed Jun 20, 2017
2 parents 94450e2 + b4c52c3 commit d4b2fc5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 7 additions & 0 deletions nixops/ec2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ def name_to_security_group(conn, name, vpc_id):

raise Exception("could not resolve security group name '{0}' in VPC '{1}'".format(name, vpc_id))

def id_to_security_group_name(conn, sg_id, vpc_id):
name = None
for sg in conn.get_all_security_groups(filters={'group-id':sg_id, 'vpc-id': vpc_id}):
if sg.id == sg_id:
name = sg.name
return name
raise Exception("could not resolve security group id '{0}' in VPC '{1}'".format(sg_id, vpc_id))
10 changes: 7 additions & 3 deletions nixops/resources/ec2_security_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def create(self, defn, check, allow_reboot, allow_recreate):
self._connect()

try:
grp = self._conn.get_all_security_groups([ defn.security_group_name ])[0]
if self.vpc_id:
grp = self._conn.get_all_security_groups(group_ids=[ self.security_group_id ])[0]
else:
grp = self._conn.get_all_security_groups([ defn.security_group_name ])[0]
self.state = self.UP
self.security_group_id = grp.id
self.security_group_description = grp.description
Expand All @@ -124,13 +127,14 @@ def create(self, defn, check, allow_reboot, allow_recreate):
if grant.cidr_ip:
new_rule.append(grant.cidr_ip)
else:
new_rule.append(grant.groupName)
group = nixops.ec2_utils.id_to_security_group_name(self._conn, grant.groupId, self.vpc_id) if self.vpc_id else grant.groupName
new_rule.append(group)
new_rule.append(grant.owner_id)
rules.append(new_rule)
self.security_group_rules = rules
except boto.exception.EC2ResponseError as e:
if e.error_code == u'InvalidGroup.NotFound':
self.state = self.Missing
self.state = self.MISSING
else:
raise

Expand Down

0 comments on commit d4b2fc5

Please sign in to comment.