use utils.boto in boto_route53 and boto_secgroup

This commit is contained in:
Matthew Williams 2015-04-13 21:48:01 +00:00
parent bf7bee7624
commit f0b89ea6f1
2 changed files with 32 additions and 103 deletions

View File

@ -33,6 +33,9 @@ Connection module for Amazon Route53
:depends: boto :depends: boto
''' '''
# keep lint from choking on _get_conn and _cache_id
#pylint disable=F821
from __future__ import absolute_import from __future__ import absolute_import
# Import Python libs # Import Python libs
@ -43,14 +46,15 @@ log = logging.getLogger(__name__)
# Import third party libs # Import third party libs
try: try:
# pylint: disable=import-error
import boto import boto
import boto.route53 import boto.route53
# pylint: enable=import-error
logging.getLogger('boto').setLevel(logging.CRITICAL) logging.getLogger('boto').setLevel(logging.CRITICAL)
HAS_BOTO = True HAS_BOTO = True
except ImportError: except ImportError:
HAS_BOTO = False HAS_BOTO = False
from salt.ext.six import string_types
import salt.utils.odict as odict import salt.utils.odict as odict
@ -60,6 +64,7 @@ def __virtual__():
''' '''
if not HAS_BOTO: if not HAS_BOTO:
return False return False
__utils__['boto.assign_funcs'](__name__, 'route53')
return True return True
@ -72,9 +77,8 @@ def get_record(name, zone, record_type, fetch_all=False, region=None, key=None,
salt myminion boto_route53.get_record test.example.org example.org A salt myminion boto_route53.get_record test.example.org example.org A
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return None
_zone = conn.get_zone(zone) _zone = conn.get_zone(zone)
if not _zone: if not _zone:
msg = 'Failed to retrieve zone {0}'.format(zone) msg = 'Failed to retrieve zone {0}'.format(zone)
@ -120,9 +124,8 @@ def add_record(name, value, zone, record_type, identifier=None, ttl=None,
salt myminion boto_route53.add_record test.example.org 1.1.1.1 example.org A salt myminion boto_route53.add_record test.example.org 1.1.1.1 example.org A
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
_zone = conn.get_zone(zone) _zone = conn.get_zone(zone)
if not _zone: if not _zone:
msg = 'Failed to retrieve zone {0}'.format(zone) msg = 'Failed to retrieve zone {0}'.format(zone)
@ -155,9 +158,8 @@ def update_record(name, value, zone, record_type, identifier=None, ttl=None,
salt myminion boto_route53.modify_record test.example.org 1.1.1.1 example.org A salt myminion boto_route53.modify_record test.example.org 1.1.1.1 example.org A
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
_zone = conn.get_zone(zone) _zone = conn.get_zone(zone)
if not _zone: if not _zone:
msg = 'Failed to retrieve zone {0}'.format(zone) msg = 'Failed to retrieve zone {0}'.format(zone)
@ -190,9 +192,8 @@ def delete_record(name, zone, record_type, identifier=None, all_records=False,
salt myminion boto_route53.delete_record test.example.org example.org A salt myminion boto_route53.delete_record test.example.org example.org A
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
_zone = conn.get_zone(zone) _zone = conn.get_zone(zone)
if not _zone: if not _zone:
msg = 'Failed to retrieve zone {0}'.format(zone) msg = 'Failed to retrieve zone {0}'.format(zone)
@ -229,37 +230,3 @@ def _wait_for_sync(status, conn, wait_for_sync):
time.sleep(10) time.sleep(10)
log.error('Timed out waiting for Route53 status update.') log.error('Timed out waiting for Route53 status update.')
return False return False
def _get_conn(region, key, keyid, profile):
'''
Get a boto connection to Route53.
'''
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']('route53.region'):
region = __salt__['config.option']('route53.region')
if not region:
region = 'us-east-1'
if not key and __salt__['config.option']('route53.key'):
key = __salt__['config.option']('route53.key')
if not keyid and __salt__['config.option']('route53.keyid'):
keyid = __salt__['config.option']('route53.keyid')
try:
conn = boto.route53.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 route53 connection.')
return None
return conn

View File

@ -33,6 +33,9 @@ Connection module for Amazon Security Groups
:depends: boto :depends: boto
''' '''
# keep lint from choking on _get_conn and _cache_id
#pylint disable=F821
from __future__ import absolute_import from __future__ import absolute_import
# Import Python libs # Import Python libs
@ -54,7 +57,6 @@ try:
except ImportError: except ImportError:
HAS_BOTO = False HAS_BOTO = False
from salt.ext.six import string_types
import salt.utils.odict as odict import salt.utils.odict as odict
@ -74,6 +76,7 @@ def __virtual__():
elif _LooseVersion(boto.__version__) < _LooseVersion(required_boto_version): elif _LooseVersion(boto.__version__) < _LooseVersion(required_boto_version):
return False return False
else: else:
__utils__['boto.assign_funcs'](__name__, 'ec2')
return True return True
@ -86,9 +89,8 @@ def exists(name=None, region=None, key=None, keyid=None, profile=None,
salt myminion boto_secgroup.exists mysecgroup salt myminion boto_secgroup.exists mysecgroup
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
group = _get_group(conn, name, vpc_id, group_id, region) group = _get_group(conn, name, vpc_id, group_id, region)
if group: if group:
return True return True
@ -173,9 +175,8 @@ def get_group_id(name, vpc_id=None, region=None, key=None, keyid=None, profile=N
salt myminion boto_secgroup.get_group_id mysecgroup salt myminion boto_secgroup.get_group_id mysecgroup
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
group = _get_group(conn, name, vpc_id, region) group = _get_group(conn, name, vpc_id, region)
if group: if group:
return group.id return group.id
@ -221,9 +222,8 @@ def get_config(name=None, group_id=None, region=None, key=None, keyid=None,
salt myminion boto_secgroup.get_config mysecgroup salt myminion boto_secgroup.get_config mysecgroup
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return None
sg = _get_group(conn, name, vpc_id, group_id, region) sg = _get_group(conn, name, vpc_id, group_id, region)
if sg: if sg:
ret = odict.OrderedDict() ret = odict.OrderedDict()
@ -281,9 +281,8 @@ def create(name, description, vpc_id=None, region=None, key=None, keyid=None,
salt myminion boto_secgroup.create mysecgroup 'My Security Group' salt myminion boto_secgroup.create mysecgroup 'My Security Group'
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
created = conn.create_security_group(name, description, vpc_id) created = conn.create_security_group(name, description, vpc_id)
if created: if created:
log.info('Created security group {0}.'.format(name)) log.info('Created security group {0}.'.format(name))
@ -303,9 +302,8 @@ def delete(name=None, group_id=None, region=None, key=None, keyid=None,
salt myminion boto_secgroup.delete mysecgroup salt myminion boto_secgroup.delete mysecgroup
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
group = _get_group(conn, name, vpc_id, group_id, region) group = _get_group(conn, name, vpc_id, group_id, region)
if group: if group:
deleted = conn.delete_security_group(group_id=group.id) deleted = conn.delete_security_group(group_id=group.id)
@ -334,9 +332,8 @@ def authorize(name=None, source_group_name=None,
salt myminion boto_secgroup.authorize mysecgroup ip_protocol=tcp from_port=80 to_port=80 cidr_ip='['10.0.0.0/8', '192.168.0.0/24']' salt myminion boto_secgroup.authorize mysecgroup ip_protocol=tcp from_port=80 to_port=80 cidr_ip='['10.0.0.0/8', '192.168.0.0/24']'
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
group = _get_group(conn, name, vpc_id, group_id, region) group = _get_group(conn, name, vpc_id, group_id, region)
if group: if group:
try: try:
@ -378,9 +375,8 @@ def revoke(name=None, source_group_name=None,
salt myminion boto_secgroup.revoke mysecgroup ip_protocol=tcp from_port=80 to_port=80 cidr_ip='10.0.0.0/8' salt myminion boto_secgroup.revoke mysecgroup ip_protocol=tcp from_port=80 to_port=80 cidr_ip='10.0.0.0/8'
''' '''
conn = _get_conn(region, key, keyid, profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not conn:
return False
group = _get_group(conn, name, vpc_id, group_id, region) group = _get_group(conn, name, vpc_id, group_id, region)
if group: if group:
try: try:
@ -408,37 +404,3 @@ def revoke(name=None, source_group_name=None,
else: else:
log.debug('Failed to remove rule from security group.') log.debug('Failed to remove rule from security group.')
return False return False
def _get_conn(region, key, keyid, profile):
'''
Get a boto connection to ec2.
'''
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']('secgroup.region'):
region = __salt__['config.option']('secgroup.region')
if not region:
region = 'us-east-1'
if not key and __salt__['config.option']('secgroup.key'):
key = __salt__['config.option']('secgroup.key')
if not keyid and __salt__['config.option']('secgroup.keyid'):
keyid = __salt__['config.option']('secgroup.keyid')
try:
conn = boto.ec2.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 ec2 connection for security groups.')
return None
return conn