mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Use common code for ec2_elb and ec2_elb_lb
Uses the new get_aws_connection_info and connect_to_aws common methods to reuse code Now complains if region is not set in one of the three possible methods Also moved over to common documentation code so this is actually based on #6913
This commit is contained in:
parent
509561f658
commit
e7b275483c
2 changed files with 24 additions and 67 deletions
|
@ -25,7 +25,6 @@ description:
|
||||||
if state=absent is passed as an argument.
|
if state=absent is passed as an argument.
|
||||||
- Will be marked changed when called only if there are ELBs found to operate on.
|
- Will be marked changed when called only if there are ELBs found to operate on.
|
||||||
version_added: "1.2"
|
version_added: "1.2"
|
||||||
requirements: [ "boto" ]
|
|
||||||
author: John Jarvis
|
author: John Jarvis
|
||||||
options:
|
options:
|
||||||
state:
|
state:
|
||||||
|
@ -33,29 +32,15 @@ options:
|
||||||
- register or deregister the instance
|
- register or deregister the instance
|
||||||
required: true
|
required: true
|
||||||
choices: ['present', 'absent']
|
choices: ['present', 'absent']
|
||||||
|
|
||||||
instance_id:
|
instance_id:
|
||||||
description:
|
description:
|
||||||
- EC2 Instance ID
|
- EC2 Instance ID
|
||||||
required: true
|
required: true
|
||||||
|
|
||||||
ec2_elbs:
|
ec2_elbs:
|
||||||
description:
|
description:
|
||||||
- List of ELB names, required for registration. The ec2_elbs fact should be used if there was a previous de-register.
|
- List of ELB names, required for registration. The ec2_elbs fact should be used if there was a previous de-register.
|
||||||
required: false
|
required: false
|
||||||
default: None
|
default: None
|
||||||
aws_secret_key:
|
|
||||||
description:
|
|
||||||
- AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used.
|
|
||||||
required: false
|
|
||||||
default: None
|
|
||||||
aliases: ['ec2_secret_key', 'secret_key' ]
|
|
||||||
aws_access_key:
|
|
||||||
description:
|
|
||||||
- AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used.
|
|
||||||
required: false
|
|
||||||
default: None
|
|
||||||
aliases: ['ec2_access_key', 'access_key' ]
|
|
||||||
region:
|
region:
|
||||||
description:
|
description:
|
||||||
- The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
|
- The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
|
||||||
|
@ -88,7 +73,7 @@ options:
|
||||||
required: false
|
required: false
|
||||||
default: 0
|
default: 0
|
||||||
version_added: "1.6"
|
version_added: "1.6"
|
||||||
|
extends_documentation_fragment: aws
|
||||||
"""
|
"""
|
||||||
|
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
|
@ -130,12 +115,11 @@ class ElbManager:
|
||||||
"""Handles EC2 instance ELB registration and de-registration"""
|
"""Handles EC2 instance ELB registration and de-registration"""
|
||||||
|
|
||||||
def __init__(self, module, instance_id=None, ec2_elbs=None,
|
def __init__(self, module, instance_id=None, ec2_elbs=None,
|
||||||
aws_access_key=None, aws_secret_key=None, region=None):
|
region=None, **aws_connect_params):
|
||||||
self.aws_access_key = aws_access_key
|
|
||||||
self.aws_secret_key = aws_secret_key
|
|
||||||
self.module = module
|
self.module = module
|
||||||
self.instance_id = instance_id
|
self.instance_id = instance_id
|
||||||
self.region = region
|
self.region = region
|
||||||
|
self.aws_connect_params = aws_connect_params
|
||||||
self.lbs = self._get_instance_lbs(ec2_elbs)
|
self.lbs = self._get_instance_lbs(ec2_elbs)
|
||||||
self.changed = False
|
self.changed = False
|
||||||
|
|
||||||
|
@ -270,9 +254,8 @@ class ElbManager:
|
||||||
are attached to self.instance_id"""
|
are attached to self.instance_id"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
endpoint="elasticloadbalancing.%s.amazonaws.com" % self.region
|
elb = connect_to_aws(boto.ec2.elb, self.region,
|
||||||
connect_region = RegionInfo(name=self.region, endpoint=endpoint)
|
**self.aws_connect_params)
|
||||||
elb = boto.ec2.elb.ELBConnection(self.aws_access_key, self.aws_secret_key, region=connect_region)
|
|
||||||
except boto.exception.NoAuthHandlerFound, e:
|
except boto.exception.NoAuthHandlerFound, e:
|
||||||
self.module.fail_json(msg=str(e))
|
self.module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
@ -291,12 +274,11 @@ class ElbManager:
|
||||||
def _get_instance(self):
|
def _get_instance(self):
|
||||||
"""Returns a boto.ec2.InstanceObject for self.instance_id"""
|
"""Returns a boto.ec2.InstanceObject for self.instance_id"""
|
||||||
try:
|
try:
|
||||||
endpoint = "ec2.%s.amazonaws.com" % self.region
|
ec2 = connect_to_aws(boto.ec2, self.region,
|
||||||
connect_region = RegionInfo(name=self.region, endpoint=endpoint)
|
**self.aws_connect_params)
|
||||||
ec2_conn = boto.ec2.EC2Connection(self.aws_access_key, self.aws_secret_key, region=connect_region)
|
|
||||||
except boto.exception.NoAuthHandlerFound, e:
|
except boto.exception.NoAuthHandlerFound, e:
|
||||||
self.module.fail_json(msg=str(e))
|
self.module.fail_json(msg=str(e))
|
||||||
return ec2_conn.get_only_instances(instance_ids=[self.instance_id])[0]
|
return ec2.get_only_instances(instance_ids=[self.instance_id])[0]
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -315,12 +297,12 @@ def main():
|
||||||
argument_spec=argument_spec,
|
argument_spec=argument_spec,
|
||||||
)
|
)
|
||||||
|
|
||||||
# def get_ec2_creds(module):
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
||||||
# return ec2_url, ec2_access_key, ec2_secret_key, region
|
|
||||||
ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module)
|
if not region:
|
||||||
|
module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file")
|
||||||
|
|
||||||
ec2_elbs = module.params['ec2_elbs']
|
ec2_elbs = module.params['ec2_elbs']
|
||||||
region = module.params['region']
|
|
||||||
wait = module.params['wait']
|
wait = module.params['wait']
|
||||||
enable_availability_zone = module.params['enable_availability_zone']
|
enable_availability_zone = module.params['enable_availability_zone']
|
||||||
timeout = module.params['wait_timeout']
|
timeout = module.params['wait_timeout']
|
||||||
|
@ -329,8 +311,8 @@ def main():
|
||||||
module.fail_json(msg="ELBs are required for registration")
|
module.fail_json(msg="ELBs are required for registration")
|
||||||
|
|
||||||
instance_id = module.params['instance_id']
|
instance_id = module.params['instance_id']
|
||||||
elb_man = ElbManager(module, instance_id, ec2_elbs, aws_access_key,
|
elb_man = ElbManager(module, instance_id, ec2_elbs,
|
||||||
aws_secret_key, region=region)
|
region=region, **aws_connect_params)
|
||||||
|
|
||||||
if ec2_elbs is not None:
|
if ec2_elbs is not None:
|
||||||
for elb in ec2_elbs:
|
for elb in ec2_elbs:
|
||||||
|
|
|
@ -22,7 +22,6 @@ short_description: Creates or destroys Amazon ELB.
|
||||||
- Returns information about the load balancer.
|
- Returns information about the load balancer.
|
||||||
- Will be marked changed when called only if state is changed.
|
- Will be marked changed when called only if state is changed.
|
||||||
version_added: "1.5"
|
version_added: "1.5"
|
||||||
requirements: [ "boto" ]
|
|
||||||
author: Jim Dalton
|
author: Jim Dalton
|
||||||
options:
|
options:
|
||||||
state:
|
state:
|
||||||
|
@ -62,32 +61,12 @@ options:
|
||||||
- An associative array of health check configuration settigs (see example)
|
- An associative array of health check configuration settigs (see example)
|
||||||
require: false
|
require: false
|
||||||
default: None
|
default: None
|
||||||
aws_secret_key:
|
|
||||||
description:
|
|
||||||
- AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used.
|
|
||||||
required: false
|
|
||||||
default: None
|
|
||||||
aliases: ['ec2_secret_key', 'secret_key']
|
|
||||||
aws_access_key:
|
|
||||||
description:
|
|
||||||
- AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used.
|
|
||||||
required: false
|
|
||||||
default: None
|
|
||||||
aliases: ['ec2_access_key', 'access_key']
|
|
||||||
region:
|
region:
|
||||||
description:
|
description:
|
||||||
- The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
|
- The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
|
||||||
required: false
|
required: false
|
||||||
aliases: ['aws_region', 'ec2_region']
|
aliases: ['aws_region', 'ec2_region']
|
||||||
validate_certs:
|
extends_documentation_fragment: aws
|
||||||
description:
|
|
||||||
- When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.
|
|
||||||
required: false
|
|
||||||
default: "yes"
|
|
||||||
choices: ["yes", "no"]
|
|
||||||
aliases: []
|
|
||||||
version_added: "1.5"
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
|
@ -190,7 +169,7 @@ class ElbManager(object):
|
||||||
|
|
||||||
def __init__(self, module, name, listeners=None, purge_listeners=None,
|
def __init__(self, module, name, listeners=None, purge_listeners=None,
|
||||||
zones=None, purge_zones=None, security_group_ids=None, health_check=None,
|
zones=None, purge_zones=None, security_group_ids=None, health_check=None,
|
||||||
aws_access_key=None, aws_secret_key=None, region=None):
|
region=None, **aws_connect_params):
|
||||||
self.module = module
|
self.module = module
|
||||||
self.name = name
|
self.name = name
|
||||||
self.listeners = listeners
|
self.listeners = listeners
|
||||||
|
@ -200,8 +179,7 @@ class ElbManager(object):
|
||||||
self.security_group_ids = security_group_ids
|
self.security_group_ids = security_group_ids
|
||||||
self.health_check = health_check
|
self.health_check = health_check
|
||||||
|
|
||||||
self.aws_access_key = aws_access_key
|
self.aws_connect_params = aws_connect_params
|
||||||
self.aws_secret_key = aws_secret_key
|
|
||||||
self.region = region
|
self.region = region
|
||||||
|
|
||||||
self.changed = False
|
self.changed = False
|
||||||
|
@ -271,11 +249,8 @@ class ElbManager(object):
|
||||||
|
|
||||||
def _get_elb_connection(self):
|
def _get_elb_connection(self):
|
||||||
try:
|
try:
|
||||||
endpoint = "elasticloadbalancing.%s.amazonaws.com" % self.region
|
return connect_to_aws(boto.ec2.elb, self.region,
|
||||||
connect_region = RegionInfo(name=self.region, endpoint=endpoint)
|
**self.aws_connect_params)
|
||||||
return boto.ec2.elb.ELBConnection(self.aws_access_key,
|
|
||||||
self.aws_secret_key,
|
|
||||||
region=connect_region)
|
|
||||||
except boto.exception.NoAuthHandlerFound, e:
|
except boto.exception.NoAuthHandlerFound, e:
|
||||||
self.module.fail_json(msg=str(e))
|
self.module.fail_json(msg=str(e))
|
||||||
|
|
||||||
|
@ -479,9 +454,9 @@ def main():
|
||||||
argument_spec=argument_spec,
|
argument_spec=argument_spec,
|
||||||
)
|
)
|
||||||
|
|
||||||
# def get_ec2_creds(module):
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
||||||
# return ec2_url, ec2_access_key, ec2_secret_key, region
|
if not region:
|
||||||
ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module)
|
module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file")
|
||||||
|
|
||||||
name = module.params['name']
|
name = module.params['name']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
@ -499,8 +474,8 @@ def main():
|
||||||
module.fail_json(msg="At least one availability zone is required for ELB creation")
|
module.fail_json(msg="At least one availability zone is required for ELB creation")
|
||||||
|
|
||||||
elb_man = ElbManager(module, name, listeners, purge_listeners, zones,
|
elb_man = ElbManager(module, name, listeners, purge_listeners, zones,
|
||||||
purge_zones, security_group_ids, health_check, aws_access_key,
|
purge_zones, security_group_ids, health_check,
|
||||||
aws_secret_key, region=region)
|
region=region, **aws_connect_params)
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
elb_man.ensure_ok()
|
elb_man.ensure_ok()
|
||||||
|
|
Loading…
Reference in a new issue