use utils.boto in boto_elb

This commit is contained in:
Matthew Williams 2015-04-13 21:33:14 +00:00
parent 89d6bbcf10
commit 5f03b0da97

View file

@ -33,6 +33,9 @@ Connection module for Amazon ELB
:depends: boto >= 2.33.0
'''
# keep lint from choking on _get_conn and _cache_id
#pylint disable=F821
from __future__ import absolute_import
# Import Python libs
@ -64,8 +67,10 @@ try:
except ImportError:
HAS_BOTO = False
# Import Salt libs
from salt.ext.six import string_types
import salt.utils.odict as odict
from salt.exceptions import CommandExecutionError
def __virtual__():
@ -74,6 +79,7 @@ def __virtual__():
'''
if not HAS_BOTO:
return False
__utils__['boto.assign_funcs'](__name__, 'elb', module='ec2.elb')
return True
@ -85,9 +91,8 @@ def exists(name, region=None, key=None, keyid=None, profile=None):
salt myminion boto_elb.exists myelb region=us-east-1
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try:
elb = conn.get_all_load_balancers(load_balancer_names=[name])
if elb:
@ -109,8 +114,9 @@ def get_elb_config(name, region=None, key=None, keyid=None, profile=None):
salt myminion boto_elb.exists myelb region=us-east-1
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
except CommandExecutionError:
return []
try:
lb = conn.get_all_load_balancers(load_balancer_names=[name])
@ -150,9 +156,8 @@ def create(name, availability_zones, listeners=None, subnets=None,
salt myminion boto_elb.create myelb '["us-east-1a", "us-east-1e"]' listeners='[["HTTPS", "HTTP", 443, 80, "arn:aws:iam::1111111:server-certificate/mycert"]]' region=us-east-1
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if __salt__['boto_elb.exists'](name, region, key, keyid, profile):
return True
if isinstance(availability_zones, string_types):
@ -198,9 +203,8 @@ def delete(name, region=None, key=None, keyid=None, profile=None):
salt myminion boto_elb.delete myelb region=us-east-1
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not __salt__['boto_elb.exists'](name, region, key, keyid, profile):
return True
try:
@ -224,9 +228,8 @@ def create_listeners(name, listeners=None, region=None, key=None, keyid=None,
salt myminion boto_elb.create_listeners myelb listeners='[["HTTPS", "HTTP", 443, 80, "arn:aws:iam::11 11111:server-certificate/mycert"]]'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(listeners, string_types):
listeners = json.loads(listeners)
# Combining listeners and complex_listeners together makes our lives
@ -264,9 +267,8 @@ def delete_listeners(name, ports, region=None, key=None, keyid=None,
salt myminion boto_elb.delete_listeners myelb '[80,443]'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(ports, string_types):
ports = json.loads(ports)
try:
@ -290,9 +292,8 @@ def apply_security_groups(name, security_groups, region=None, key=None,
salt myminion boto_elb.apply_security_groups myelb '["mysecgroup1"]'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(security_groups, string_types):
security_groups = json.loads(security_groups)
try:
@ -317,9 +318,8 @@ def enable_availability_zones(name, availability_zones, region=None, key=None,
salt myminion boto_elb.enable_availability_zones myelb '["us-east-1a"]'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(availability_zones, string_types):
availability_zones = json.loads(availability_zones)
try:
@ -343,9 +343,8 @@ def disable_availability_zones(name, availability_zones, region=None, key=None,
salt myminion boto_elb.disable_availability_zones myelb '["us-east-1a"]'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(availability_zones, string_types):
availability_zones = json.loads(availability_zones)
try:
@ -369,9 +368,8 @@ def attach_subnets(name, subnets, region=None, key=None, keyid=None,
salt myminion boto_elb.attach_subnets myelb '["mysubnet"]'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(subnets, string_types):
subnets = json.loads(subnets)
try:
@ -395,9 +393,8 @@ def detach_subnets(name, subnets, region=None, key=None, keyid=None,
salt myminion boto_elb.detach_subnets myelb '["mysubnet"]'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if isinstance(subnets, string_types):
subnets = json.loads(subnets)
try:
@ -420,8 +417,9 @@ def get_attributes(name, region=None, key=None, keyid=None, profile=None):
salt myminion boto_elb.get_attributes myelb
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
except CommandExecutionError:
return {}
try:
lbattrs = conn.get_all_lb_attributes(name)
@ -458,9 +456,8 @@ def set_attributes(name, attributes, region=None, key=None, keyid=None,
salt myminion boto_elb.set_attributes myelb '{"access_log": {"enabled": "true", "s3_bucket_name": "mybucket", "s3_bucket_prefix": "mylogs/", "emit_interval": "5"}}' region=us-east-1
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
al = attributes.get('access_log', {})
czlb = attributes.get('cross_zone_load_balancing', {})
cd = attributes.get('connection_draining', {})
@ -528,8 +525,9 @@ def get_health_check(name, region=None, key=None, keyid=None, profile=None):
salt myminion boto_elb.get_health_check myelb
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
except CommandExecutionError:
return {}
try:
lb = conn.get_all_load_balancers(load_balancer_names=[name])
@ -557,9 +555,8 @@ def set_health_check(name, health_check, region=None, key=None, keyid=None,
salt myminion boto_elb.set_health_check myelb '{"target": "HTTP:80/"}'
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
hc = HealthCheck(**health_check)
try:
conn.configure_health_check(name, hc)
@ -591,9 +588,8 @@ def register_instances(name, instances, region=None, key=None, keyid=None,
# variable throughout the register_instances method
if isinstance(instances, str) or isinstance(instances, six.text_type):
instances = [instances]
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try:
registered_instances = conn.register_instances(name, instances)
except boto.exception.BotoServerError as e:
@ -634,9 +630,8 @@ def deregister_instances(name, instances, region=None, key=None, keyid=None,
# variable throughout the deregister_instances method
if isinstance(instances, str) or isinstance(instances, six.text_type):
instances = [instances]
conn = _get_conn(region, key, keyid, profile)
if not conn:
return False
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
try:
registered_instances = conn.deregister_instances(name, instances)
except boto.exception.BotoServerError as e:
@ -675,9 +670,11 @@ def get_instance_health(name, region=None, key=None, keyid=None, profile=None, i
salt myminion boto_elb.get_instance_health myelb
salt myminion boto_elb.get_instance_health myelb region=us-east-1 instances="[instance_id,instance_id]"
'''
conn = _get_conn(region, key, keyid, profile)
if not conn:
try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
except CommandExecutionError:
return []
try:
instance_states = conn.describe_instance_health(name, instances)
ret = []
@ -691,37 +688,3 @@ def get_instance_health(name, region=None, key=None, keyid=None, profile=None, i
except boto.exception.BotoServerError as e:
log.debug(e)
return []
def _get_conn(region, key, keyid, profile):
'''
Get a boto connection to ELB.
'''
if profile:
if isinstance(profile, string_types):
_profile = __salt__['config.option'](profile)
elif isinstance(profile, dict):
_profile = profile
key = _profile.get('key', None)
keyid = _profile.get('keyid', None)
region = _profile.get('region', None)
if not region and __salt__['config.option']('elb.region'):
region = __salt__['config.option']('elb.region')
if not region:
region = 'us-east-1'
if not key and __salt__['config.option']('elb.key'):
key = __salt__['config.option']('elb.key')
if not keyid and __salt__['config.option']('elb.keyid'):
keyid = __salt__['config.option']('elb.keyid')
try:
conn = boto.ec2.elb.connect_to_region(region, aws_access_key_id=keyid,
aws_secret_access_key=key)
except boto.exception.NoAuthHandlerFound:
log.error('No authentication credentials found when attempting to'
' make boto elb connection.')
return None
return conn