mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Followed @ryan-lane's suggestion to remove duplicated code from boto_vpc and instead call into that module
This commit is contained in:
parent
3a38a440b7
commit
fae1199276
1 changed files with 18 additions and 110 deletions
|
@ -107,7 +107,7 @@ def exists(name=None, region=None, key=None, keyid=None, profile=None,
|
|||
log.debug(e)
|
||||
return False
|
||||
|
||||
group = _get_group(conn, name, vpc_id, vpc_name, group_id, region)
|
||||
group = _get_group(conn, name, vpc_id, group_id, region, key, keyid, profile)
|
||||
if group:
|
||||
return True
|
||||
else:
|
||||
|
@ -115,101 +115,16 @@ def exists(name=None, region=None, key=None, keyid=None, profile=None,
|
|||
|
||||
|
||||
def _check_vpc(vpc_id, vpc_name, region, key, keyid, profile):
|
||||
'''
|
||||
Check whether a VPC with the given name or id exists.
|
||||
Returns the vpc_id or None. Raises SaltInvocationError if
|
||||
both vpc_id and vpc_name are None. Optionally raise a
|
||||
CommandExecutionError if the VPC does not exist.
|
||||
'''
|
||||
|
||||
if not _exactly_one((vpc_name, vpc_id)):
|
||||
raise SaltInvocationError('One (but not both) of vpc_id or vpc_name '
|
||||
'must be provided.')
|
||||
if vpc_name:
|
||||
vpc_id = _get_id(vpc_name=vpc_name, region=region, key=key, keyid=keyid,
|
||||
profile=profile)
|
||||
elif not _find_vpcs(vpc_id=vpc_id, region=region, key=key, keyid=keyid,
|
||||
profile=profile):
|
||||
log.info('VPC {0} does not exist.'.format(vpc_id))
|
||||
data = __salt__['boto_vpc.get_id'](name=vpc_name, region=region,
|
||||
key=key, keyid=keyid, profile=profile)
|
||||
try:
|
||||
return data.get('id')
|
||||
except TypeError:
|
||||
return None
|
||||
return vpc_id
|
||||
|
||||
|
||||
def _get_id(vpc_name=None, cidr=None, tags=None, region=None, key=None,
|
||||
keyid=None, profile=None):
|
||||
'''
|
||||
Given VPC properties, return the VPC id if a match is found.
|
||||
'''
|
||||
|
||||
if vpc_name and not any((cidr, tags)):
|
||||
vpc_id = _cache_id(vpc_name, region=region,
|
||||
key=key, keyid=keyid,
|
||||
profile=profile)
|
||||
if vpc_id:
|
||||
return vpc_id
|
||||
|
||||
vpc_ids = _find_vpcs(vpc_name=vpc_name, cidr=cidr, tags=tags, region=region,
|
||||
key=key, keyid=keyid, profile=profile)
|
||||
if vpc_ids:
|
||||
log.info("Matching VPC: {0}".format(" ".join(vpc_ids)))
|
||||
if len(vpc_ids) == 1:
|
||||
vpc_id = vpc_ids[0]
|
||||
if vpc_name:
|
||||
_cache_id(vpc_name, vpc_id,
|
||||
region=region, key=key,
|
||||
keyid=keyid, profile=profile)
|
||||
return vpc_id
|
||||
else:
|
||||
raise CommandExecutionError('Found more than one VPC matching the criteria.')
|
||||
else:
|
||||
log.info('No VPC found.')
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
||||
def _find_vpcs(vpc_id=None, vpc_name=None, cidr=None, tags=None,
|
||||
region=None, key=None, keyid=None, profile=None):
|
||||
|
||||
'''
|
||||
Given VPC properties, find and return matching VPC ids.
|
||||
'''
|
||||
|
||||
if all((vpc_id, vpc_name)):
|
||||
raise SaltInvocationError('Only one of vpc_name or vpc_id may be '
|
||||
'provided.')
|
||||
|
||||
if not any((vpc_id, vpc_name, tags, cidr)):
|
||||
raise SaltInvocationError('At least one of the following must be '
|
||||
'provided: vpc_id, vpc_name, cidr or tags.')
|
||||
|
||||
# Special connection to 'vpc' since 'ec2' connex don't provide get_all_vpcs()
|
||||
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
|
||||
conn = __utils__['boto.get_connection']('vpc', module=None, region=region,
|
||||
key=key, keyid=keyid, profile=profile)
|
||||
|
||||
filter_parameters = {'filters': {}}
|
||||
|
||||
if vpc_id:
|
||||
filter_parameters['vpc_ids'] = [vpc_id]
|
||||
|
||||
if cidr:
|
||||
filter_parameters['filters']['cidr'] = cidr
|
||||
|
||||
if vpc_name:
|
||||
filter_parameters['filters']['tag:Name'] = vpc_name
|
||||
|
||||
if tags:
|
||||
for tag_name, tag_value in six.iteritems(tags):
|
||||
filter_parameters['filters']['tag:{0}'.format(tag_name)] = tag_value
|
||||
|
||||
vpcs = conn.get_all_vpcs(**filter_parameters)
|
||||
log.debug('The filters criteria {0} matched the following VPCs:{1}'.format(filter_parameters, vpcs))
|
||||
|
||||
if vpcs:
|
||||
return [vpc.id for vpc in vpcs]
|
||||
else:
|
||||
return []
|
||||
|
||||
|
||||
def _split_rules(rules):
|
||||
'''
|
||||
Split rules with combined grants into individual rules.
|
||||
|
@ -235,20 +150,13 @@ def _split_rules(rules):
|
|||
return split
|
||||
|
||||
|
||||
def _get_group(conn, name=None, vpc_id=None, vpc_name=None, group_id=None, region=None): # pylint: disable=W0613
|
||||
def _get_group(conn=None, name=None, vpc_id=None, group_id=None,
|
||||
region=None, key=None, keyid=None, profile=None): # pylint: disable=W0613
|
||||
'''
|
||||
Get a group object given a name, name and vpc_id/vpc_name or group_id. Return
|
||||
a boto.ec2.securitygroup.SecurityGroup object if the group is found, else
|
||||
return None.
|
||||
'''
|
||||
|
||||
if not vpc_id and vpc_name:
|
||||
try:
|
||||
vpc_id = _check_vpc(vpc_id, vpc_name, region, key, keyid, profile)
|
||||
except boto.exception.BotoServerError as e:
|
||||
log.debug(e)
|
||||
return None
|
||||
|
||||
if name:
|
||||
if vpc_id is None:
|
||||
log.debug('getting group for {0}'.format(name))
|
||||
|
@ -341,7 +249,7 @@ def get_group_id(name, vpc_id=None, vpc_name=None, region=None, key=None,
|
|||
log.debug(e)
|
||||
return False
|
||||
|
||||
group = _get_group(conn, name, vpc_id, vpc_name, region)
|
||||
group = _get_group(conn, name, vpc_id, region, key, keyid, profile)
|
||||
if group:
|
||||
return group.id
|
||||
else:
|
||||
|
@ -395,7 +303,7 @@ def get_config(name=None, group_id=None, region=None, key=None, keyid=None,
|
|||
log.debug(e)
|
||||
return None
|
||||
|
||||
sg = _get_group(conn, name, vpc_id, vpc_name, group_id, region)
|
||||
sg = _get_group(conn, name, vpc_id, group_id, region, key, keyid, profile)
|
||||
if sg:
|
||||
ret = odict.OrderedDict()
|
||||
ret['name'] = sg.name
|
||||
|
@ -460,7 +368,7 @@ def delete(name=None, group_id=None, region=None, key=None, keyid=None,
|
|||
log.debug(e)
|
||||
return False
|
||||
|
||||
group = _get_group(conn, name, vpc_id, vpc_name, group_id, region)
|
||||
group = _get_group(conn, name, vpc_id, group_id, region, key, keyid, profile)
|
||||
if group:
|
||||
deleted = conn.delete_security_group(group_id=group.id)
|
||||
if deleted:
|
||||
|
@ -497,7 +405,7 @@ def authorize(name=None, source_group_name=None,
|
|||
log.debug(e)
|
||||
return False
|
||||
|
||||
group = _get_group(conn, name, vpc_id, vpc_name, group_id, region)
|
||||
group = _get_group(conn, name, vpc_id, group_id, region, key, keyid, profile)
|
||||
if group:
|
||||
try:
|
||||
added = None
|
||||
|
@ -523,13 +431,13 @@ def authorize(name=None, source_group_name=None,
|
|||
log.error(msg)
|
||||
return False
|
||||
except boto.exception.EC2ResponseError as e:
|
||||
log.debug(e)
|
||||
msg = ('Failed to add rule to security group {0} with id {1}.'
|
||||
.format(group.name, group.id))
|
||||
log.error(msg)
|
||||
log.error(e)
|
||||
return False
|
||||
else:
|
||||
log.debug('Failed to add rule to security group.')
|
||||
log.error('Failed to add rule to security group.')
|
||||
return False
|
||||
|
||||
|
||||
|
@ -554,7 +462,7 @@ def revoke(name=None, source_group_name=None,
|
|||
log.debug(e)
|
||||
return False
|
||||
|
||||
group = _get_group(conn, name, vpc_id, vpc_name, group_id, region)
|
||||
group = _get_group(conn, name, vpc_id, group_id, region, key, keyid, profile)
|
||||
if group:
|
||||
try:
|
||||
revoked = None
|
||||
|
@ -581,11 +489,11 @@ def revoke(name=None, source_group_name=None,
|
|||
log.error(msg)
|
||||
return False
|
||||
except boto.exception.EC2ResponseError as e:
|
||||
log.debug(e)
|
||||
msg = ('Failed to remove rule from security group {0} with id {1}.'
|
||||
.format(group.name, group.id))
|
||||
log.error(msg)
|
||||
log.error(e)
|
||||
return False
|
||||
else:
|
||||
log.debug('Failed to remove rule from security group.')
|
||||
log.error('Failed to remove rule from security group.')
|
||||
return False
|
||||
|
|
Loading…
Add table
Reference in a new issue