Make ec2_elb_lb respect VPCs when resolving groups

AWS security groups are unique by name only by VPC (Restated, the VPC
and group name form a unique key).

When attaching security groups to an ELB, the ec2_elb_lb module would
erroneously find security groups of the same name in other VPCs thus
causing an error stating as such.

To eliminate the error, we check that we are attaching subnets (implying
that we are in a VPC), grab the vpc_id of the 0th subnet, and filtering
the list of security groups on this VPC.  In other cases, no such filter
is applied (filters=None).
This commit is contained in:
Shaun Brady 2016-07-28 13:44:41 -04:00 committed by Matt Clay
parent 3b32b60338
commit 7b8d625546

View file

@ -389,6 +389,7 @@ try:
import boto import boto
import boto.ec2.elb import boto.ec2.elb
import boto.ec2.elb.attributes import boto.ec2.elb.attributes
import boto.vpc
from boto.ec2.elb.healthcheck import HealthCheck from boto.ec2.elb.healthcheck import HealthCheck
from boto.ec2.tag import Tag from boto.ec2.tag import Tag
from boto.regioninfo import RegionInfo from boto.regioninfo import RegionInfo
@ -417,6 +418,12 @@ def _throttleable_operation(max_retries):
return _do_op return _do_op
return _operation_wrapper return _operation_wrapper
def _get_vpc_connection(module, region, aws_connect_params):
try:
return connect_to_aws(boto.vpc, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
module.fail_json(msg=str(e))
_THROTTLING_RETRIES = 5 _THROTTLING_RETRIES = 5
@ -1288,7 +1295,13 @@ def main():
security_group_ids = [] security_group_ids = []
try: try:
ec2 = ec2_connect(module) ec2 = ec2_connect(module)
grp_details = ec2.get_all_security_groups() if subnets: # We have at least one subnet, ergo this is a VPC
vpc_conn = _get_vpc_connection(module=module, region=region, aws_connect_params=aws_connect_params)
vpc_id = vpc_conn.get_all_subnets([subnets[0]])[0].vpc_id
filters = {'vpc_id': vpc_id}
else:
filters = None
grp_details = ec2.get_all_security_groups(filters=filters)
for group_name in security_group_names: for group_name in security_group_names:
if isinstance(group_name, basestring): if isinstance(group_name, basestring):