From 8fc80f296e46200e70280d01f8c02359f625d76a Mon Sep 17 00:00:00 2001 From: Ian O Connell Date: Fri, 19 Jul 2013 18:13:46 -0700 Subject: [PATCH] Added an elb_region parameter for interacting with the ec2 elastic load balancer to work with regions other than us-east-1 --- cloud/ec2_elb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/cloud/ec2_elb b/cloud/ec2_elb index ca776c4edf9..0437fd29e1d 100644 --- a/cloud/ec2_elb +++ b/cloud/ec2_elb @@ -53,6 +53,11 @@ options: - AWS Access API key required: false default: None + elb_region: + description: + - AWS Region the load balancer is in + required: false + default: None """ @@ -83,6 +88,7 @@ import os try: import boto + from boto.ec2.elb import regions as elb_regions except ImportError: print "failed=True msg='boto required for this module'" sys.exit(1) @@ -92,12 +98,23 @@ class ElbManager: """Handles EC2 instance ELB registration and de-registration""" def __init__(self, module, instance_id=None, ec2_elbs=None, - ec2_access_key=None, ec2_secret_key=None): + ec2_access_key=None, ec2_secret_key=None, elb_region_name=None): self.ec2_access_key = ec2_access_key self.ec2_secret_key = ec2_secret_key self.module = module self.instance_id = instance_id + self.elb_region = None + + if elb_region_name is not None: + for region in elb_regions(): + if region.name == elb_region_name: + self.elb_region = region + + if self.elb_region is None: + self.module.fail_json(msg=str("Invalid region")) + self.lbs = self._get_instance_lbs(ec2_elbs) + # if there are no ELBs to operate on # there will be no changes made if len(self.lbs) > 0: @@ -140,7 +157,7 @@ class ElbManager: are attached to self.instance_id""" try: - elb = boto.connect_elb(self.ec2_access_key, self.ec2_secret_key) + elb = boto.connect_elb(self.ec2_access_key, self.ec2_secret_key, region=self.elb_region) except boto.exception.NoAuthHandlerFound, e: self.module.fail_json(msg=str(e)) elbs = elb.get_all_load_balancers() @@ -165,13 +182,15 @@ def main(): instance_id={'required': True}, ec2_elbs={'default': None, 'required': False}, ec2_secret_key={'default': None, 'aliases': ['EC2_SECRET_KEY']}, - ec2_access_key={'default': None, 'aliases': ['EC2_ACCESS_KEY']} + ec2_access_key={'default': None, 'aliases': ['EC2_ACCESS_KEY']}, + elb_region={'required': False, 'default': None} ) ) ec2_secret_key = module.params['ec2_secret_key'] ec2_access_key = module.params['ec2_access_key'] ec2_elbs = module.params['ec2_elbs'] + elb_region = module.params['elb_region'] if module.params['state'] == 'present' and 'ec2_elbs' not in module.params: module.fail_json(msg="ELBs are required for registration") @@ -183,7 +202,7 @@ def main(): instance_id = module.params['instance_id'] elb_man = ElbManager(module, instance_id, ec2_elbs, ec2_access_key, - ec2_secret_key) + ec2_secret_key, elb_region) if module.params['state'] == 'present': elb_man.register()