mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
ec2_group: description property is immutable (#19790)
* update integration tests for updated boto exception message * integration tests fail on both "test credential" test cases exception bubbles out of module. instead catch and wrap * ec2_group does not support updating a security group's description AWS security group descriptions are immutable. if ec2_group finds a group that matches by name, but the descriptions do not match, the module does not support this case previously it would check if the group was used, but would not do anything if it was old behavior was erroneous because it could make a user expect that the description change of a group was fine when in fact it did not occur also, it made an expensive check against all ec2 instances for no good reason * comments not doc strings * else must have pass w/o doc-string statement * Catch specific BotoServerException, give context around error when fetching SGs * python3 compatible exception blocks * add traceback to fail_json * two blank lines before first function
This commit is contained in:
parent
3169cbd493
commit
ac74520b6f
2 changed files with 39 additions and 19 deletions
|
@ -140,10 +140,13 @@ EXAMPLES = '''
|
||||||
try:
|
try:
|
||||||
import boto.ec2
|
import boto.ec2
|
||||||
from boto.ec2.securitygroup import SecurityGroup
|
from boto.ec2.securitygroup import SecurityGroup
|
||||||
|
from boto.exception import BotoServerError
|
||||||
HAS_BOTO = True
|
HAS_BOTO = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_BOTO = False
|
HAS_BOTO = False
|
||||||
|
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
def make_rule_key(prefix, rule, group_id, cidr_ip):
|
def make_rule_key(prefix, rule, group_id, cidr_ip):
|
||||||
"""Creates a unique key for an individual group rule"""
|
"""Creates a unique key for an individual group rule"""
|
||||||
|
@ -283,7 +286,13 @@ def main():
|
||||||
# find the group if present
|
# find the group if present
|
||||||
group = None
|
group = None
|
||||||
groups = {}
|
groups = {}
|
||||||
for curGroup in ec2.get_all_security_groups():
|
|
||||||
|
try:
|
||||||
|
security_groups = ec2.get_all_security_groups()
|
||||||
|
except BotoServerError as e:
|
||||||
|
module.fail_json(msg="Error in get_all_security_groups: %s" % e.message, exception=traceback.format_exc())
|
||||||
|
|
||||||
|
for curGroup in security_groups:
|
||||||
groups[curGroup.id] = curGroup
|
groups[curGroup.id] = curGroup
|
||||||
if curGroup.name in groups:
|
if curGroup.name in groups:
|
||||||
# Prioritise groups from the current VPC
|
# Prioritise groups from the current VPC
|
||||||
|
@ -298,36 +307,29 @@ def main():
|
||||||
# Ensure requested group is absent
|
# Ensure requested group is absent
|
||||||
if state == 'absent':
|
if state == 'absent':
|
||||||
if group:
|
if group:
|
||||||
'''found a match, delete it'''
|
# found a match, delete it
|
||||||
try:
|
try:
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
group.delete()
|
group.delete()
|
||||||
except Exception as e:
|
except BotoServerError as e:
|
||||||
module.fail_json(msg="Unable to delete security group '%s' - %s" % (group, e))
|
module.fail_json(msg="Unable to delete security group '%s' - %s" % (group, e.message), exception=traceback.format_exc())
|
||||||
else:
|
else:
|
||||||
group = None
|
group = None
|
||||||
changed = True
|
changed = True
|
||||||
else:
|
else:
|
||||||
'''no match found, no changes required'''
|
# no match found, no changes required
|
||||||
|
pass
|
||||||
|
|
||||||
# Ensure requested group is present
|
# Ensure requested group is present
|
||||||
elif state == 'present':
|
elif state == 'present':
|
||||||
if group:
|
if group:
|
||||||
'''existing group found'''
|
# existing group
|
||||||
# check the group parameters are correct
|
|
||||||
group_in_use = False
|
|
||||||
rs = ec2.get_all_instances()
|
|
||||||
for r in rs:
|
|
||||||
for i in r.instances:
|
|
||||||
group_in_use |= reduce(lambda x, y: x | (y.name == 'public-ssh'), i.groups, False)
|
|
||||||
|
|
||||||
if group.description != description:
|
if group.description != description:
|
||||||
if group_in_use:
|
module.fail_json(msg="Group description does not match existing group. ec2_group does not support this case.")
|
||||||
module.fail_json(msg="Group description does not match, but it is in use so cannot be changed.")
|
|
||||||
|
|
||||||
# if the group doesn't exist, create it now
|
# if the group doesn't exist, create it now
|
||||||
else:
|
else:
|
||||||
'''no match found, create it'''
|
# no match found, create it
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
group = ec2.create_security_group(name, description, vpc_id=vpc_id)
|
group = ec2.create_security_group(name, description, vpc_id=vpc_id)
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,7 @@
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- 'result.failed'
|
- 'result.failed'
|
||||||
- 'result.msg.startswith("value of region must be one of:")'
|
- 'result.msg.startswith("Region asdf querty 1234 does not seem to be available for aws module boto.ec2. If the region definitely exists, you may need to upgrade boto or extend with endpoints_path")'
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
- name: test valid region parameter
|
- name: test valid region parameter
|
||||||
|
@ -155,7 +155,7 @@
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- 'result.failed'
|
- 'result.failed'
|
||||||
- '"EC2ResponseError: 401 Unauthorized" in result.msg'
|
- '"Error in get_all_security_groups: AWS was not able to validate the provided access credentials" in result.msg'
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
- name: test credential parameters
|
- name: test credential parameters
|
||||||
|
@ -172,7 +172,7 @@
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- 'result.failed'
|
- 'result.failed'
|
||||||
- '"EC2ResponseError: 401 Unauthorized" in result.msg'
|
- '"Error in get_all_security_groups: AWS was not able to validate the provided access credentials" in result.msg'
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
- name: test state=absent
|
- name: test state=absent
|
||||||
|
@ -207,6 +207,24 @@
|
||||||
- 'result.changed'
|
- 'result.changed'
|
||||||
- 'result.group_id.startswith("sg-")'
|
- 'result.group_id.startswith("sg-")'
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
- name: test state=present different description raises error
|
||||||
|
ec2_group:
|
||||||
|
name='{{ec2_group_name}}'
|
||||||
|
description='{{ec2_group_description}}CHANGED'
|
||||||
|
ec2_region='{{ec2_region}}'
|
||||||
|
ec2_access_key='{{ec2_access_key}}'
|
||||||
|
ec2_secret_key='{{ec2_secret_key}}'
|
||||||
|
state=present
|
||||||
|
ignore_errors: true
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- name: assert matching group with non-matching description raises error
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- 'result.failed'
|
||||||
|
- '"Group description does not match existing group. ec2_group does not support this case." in result.msg'
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
- name: test state=present (expected changed=false)
|
- name: test state=present (expected changed=false)
|
||||||
ec2_group:
|
ec2_group:
|
||||||
|
|
Loading…
Reference in a new issue