1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Added an elb_region parameter for interacting with the ec2 elastic load balancer to work with regions other than us-east-1

This commit is contained in:
Ian O Connell 2013-07-19 18:13:46 -07:00
parent 162530dd74
commit 1264a5b47d

View file

@ -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()