Skip to content

Commit

Permalink
Merge branch 'check_mode' into mq
Browse files Browse the repository at this point in the history
  • Loading branch information
fotto committed Mar 28, 2021
2 parents e774b24 + db59239 commit afbe9fe
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 37 deletions.
19 changes: 11 additions & 8 deletions plugins/modules/mq_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,7 @@ def create_broker(conn, module):
module.fail_json_aws(RuntimeError, msg="At least one security group must be specified on broker creation")
#
changed = True
if not module.check_mode:
result = conn.create_broker(**kwargs)
else:
result = {
'BrokerArn': 'fakeArn',
'BrokerId': 'fakeId'
}
result = conn.create_broker(**kwargs)
#
return {'broker': result, 'changed': changed}

Expand Down Expand Up @@ -469,7 +463,6 @@ def update_broker(conn, module, broker_id):
return {'broker': result, 'changed': changed}



def ensure_absent(conn, module):
result = {
'BrokerName': module.params['broker_name'],
Expand All @@ -496,6 +489,12 @@ def ensure_absent(conn, module):


def ensure_present(conn, module):
if module.check_mode:
return {'broker': {
'BrokerArn': 'fakeArn',
'BrokerId': 'fakeId'
}, 'changed': True}
#
broker_id = get_broker_id(conn, module)
if broker_id:
return update_broker(conn, module, broker_id)
Expand Down Expand Up @@ -548,6 +547,10 @@ def main():
module.exit_json(**compound_result)
elif module.params['state'] == 'restarted':
broker_id = get_broker_id(connection, module)
if module.check_mode:
module.exit_json(broker={
'BrokerId': broker_id if broker_id else 'fakeId'
}, changed=True)
if not broker_id:
module.fail_json_aws(RuntimeError,
msg="Cannot find broker with name {0}.".format(module.params['broker_name']))
Expand Down
29 changes: 19 additions & 10 deletions plugins/modules/mq_broker_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ def get_broker_info(conn, module):
try:
return conn.describe_broker(BrokerId=module.params['broker_id'])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't get broker details.")
if module.check_mode:
return {
'BrokerId': module.params['broker_id'],
}
else:
module.fail_json_aws(e, msg="Couldn't get broker details.")


def get_current_configuration(conn, module, cfg_id, cfg_revision):
Expand Down Expand Up @@ -185,16 +190,20 @@ def ensure_config(conn, module):
broker_id = module.params['broker_id']
broker_info = get_broker_info(conn, module)
changed = False
current_cfg = broker_info['Configurations']['Current']
if 'Pending' in broker_info['Configurations']:
current_cfg = broker_info['Configurations']['Pending']
current_cfg_encoded = get_current_configuration(conn, module,
current_cfg['Id'],
current_cfg['Revision'])['Data']
if IS_PYTHON3:
current_cfg_decoded = base64.b64decode(current_cfg_encoded.encode()).decode()
if module.check_mode and 'Configurations' not in broker_info:
# not result from get_broker_info(). use requeste config
current_cfg_decoded = module.params['config_xml']
else:
current_cfg_decoded = base64.b64decode(current_cfg_encoded)
current_cfg = broker_info['Configurations']['Current']
if 'Pending' in broker_info['Configurations']:
current_cfg = broker_info['Configurations']['Pending']
current_cfg_encoded = get_current_configuration(conn, module,
current_cfg['Id'],
current_cfg['Revision'])['Data']
if IS_PYTHON3:
current_cfg_decoded = base64.b64decode(current_cfg_encoded.encode()).decode()
else:
current_cfg_decoded = base64.b64decode(current_cfg_encoded)
if is_same_config(current_cfg_decoded, module.params['config_xml']):
return {
'changed': changed,
Expand Down
14 changes: 13 additions & 1 deletion plugins/modules/mq_broker_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ def get_broker_info(conn, module, broker_id):
try:
return conn.describe_broker(BrokerId=broker_id)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Couldn't get broker details.")
if module.check_mode:
module.exit_json(broker={
'BrokerId': broker_id,
'BrokerName': 'fakeName'
})
else:
module.fail_json_aws(e, msg="Couldn't get broker details.")


def main():
Expand All @@ -109,6 +115,12 @@ def main():
try:
if not broker_id:
broker_id = get_broker_id(connection, module)
if not broker_id:
if module.check_mode:
module.exit_json(broker={
'BrokerId': 'fakeId',
'BrokerName': broker_name if broker_name else 'fakeName'
})
result = get_broker_info(connection, module, broker_id)
except botocore.exceptions.ClientError as e:
module.fail_json_aws(e)
Expand Down
9 changes: 8 additions & 1 deletion plugins/modules/mq_user_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ def get_user_info(conn, module):
try:
response_records = get_user_records(conn, module)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Failed to describe users')
if module.check_mode:
# return empty set for unknown broker in check mode
if DEFAULTS['as_dict']:
return {}
else:
return []
else:
module.fail_json_aws(e, msg='Failed to describe users')
#
if not module.params['skip_pending_create'] and not module.params['skip_pending_delete']:
# we can simply return the sub-object from the response
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/targets/mq/tasks/test_mq_broker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- result_c1.broker['BrokerName'] == broker_name
- result_c1.broker['BrokerState'] == 'CREATION_IN_PROGRESS'
- ( result_c1.broker['StorageType'] | upper ) == 'EFS'
when: not ansible_check_mode
- debug:
msg: "Wait until broker {{ broker_name }} ({{ broker_id }}) enters running state. This may take several minutes"
- name: wait for startup
Expand All @@ -63,6 +64,7 @@
until: result.broker['BrokerState'] == 'RUNNING'
retries: 15
delay: 60
when: not ansible_check_mode
- name: repeat creation
# amazon.aws.mq_broker:
mq_broker:
Expand Down Expand Up @@ -95,6 +97,7 @@
- result_c2.broker['BrokerId'] == broker_id
- result_c2.broker['BrokerName'] == broker_name
- ( result_c2.broker['StorageType'] | upper ) == 'EFS'
when: not ansible_check_mode
- name: update broker
# amazon.aws.mq_broker:
mq_broker:
Expand All @@ -113,6 +116,7 @@
that:
- ( result.changed | bool)
- result.broker['BrokerId'] == broker_id
when: not ansible_check_mode
- name: reboot broker to make pending changes active
# amazon.aws.mq_broker:
mq_broker:
Expand All @@ -139,6 +143,7 @@
that:
- result.changed | bool
- result_r1.broker['BrokerState'] == 'REBOOT_IN_PROGRESS'
when: not ansible_check_mode
- debug:
msg: "Wait until reboot of broker {{ broker_name }} ({{ broker_id }}) is finished. This may take several minutes"
- name: wait for reboot
Expand All @@ -153,6 +158,7 @@
until: result.broker['BrokerState'] == 'RUNNING'
retries: 15
delay: 60
when: not ansible_check_mode
- name: get details after update
# amazon.aws.mq_broker_info:
mq_broker_info:
Expand All @@ -172,6 +178,7 @@
- not ( result_u1.broker['AutoMinorVersionUpgrade'] | bool )
# the next one checks that changes to create-only parameters are silently ignore
- result_u1.broker['StorageType'] == result_c1.broker['StorageType']
when: not ansible_check_mode
- name: repeat update broker
# amazon.aws.mq_broker:
mq_broker:
Expand Down Expand Up @@ -201,6 +208,7 @@
- result_u2.broker['BrokerId'] == result_u1.broker['BrokerId']
- result_u2.broker['StorageType'] == result_u1.broker['StorageType']
- result_u2.broker['EngineVersion'] == result_u1.broker['EngineVersion']
when: not ansible_check_mode
# here we can put in tests for mq_broker_config
- name: delete broker
# amazon.aws.mq_broker:
Expand All @@ -218,6 +226,7 @@
fail_msg: broker delete failed
that:
- ( result.changed | bool)
when: not ansible_check_mode
- name: get details after delete
# amazon.aws.mq_broker_info:
mq_broker_info:
Expand All @@ -233,6 +242,7 @@
fail_msg: broker delete too fast?
that:
- result_d1.broker['BrokerState'] == 'DELETION_IN_PROGRESS'
when: not ansible_check_mode
- name: repeat broker deletion
# amazon.aws.mq_broker:
mq_broker:
Expand All @@ -249,6 +259,7 @@
fail_msg: didn't detect DELETION_IN_PROGRESS in progress
that:
- not ( result.changed | bool)
when: not ansible_check_mode
- name: deletion unknown broker - simulates re-deletion of completely deleted broker
# amazon.aws.mq_broker:
mq_broker:
Expand All @@ -265,4 +276,5 @@
fail_msg: deletion of unknown broker return unexpected result
that:
- not ( result.changed | bool)
when: not ansible_check_mode

3 changes: 3 additions & 0 deletions tests/integration/targets/mq/tasks/test_mq_broker_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- result.broker['BrokerId'] == broker_id
- result.configuration['id'] == result.broker['Configurations']['Pending']['Id']
- result.configuration['revision'] == result.broker['Configurations']['Pending']['Revision']
when: not ansible_check_mode
- name: test 2 - send (almost) same config again
# amazon.aws.mq_broker_info:
mq_broker_config:
Expand All @@ -58,6 +59,7 @@
fail_msg: test2 failed
that:
- not (result.changed | bool )
when: not ansible_check_mode
- name: test 3 - send new config with custom description and request reboot
# amazon.aws.mq_broker_info:
mq_broker_config:
Expand All @@ -77,3 +79,4 @@
that:
- result.changed | bool
- result.broker['BrokerState'] == 'REBOOT_IN_PROGRESS'
when: not ansible_check_mode
12 changes: 12 additions & 0 deletions tests/integration/targets/mq/tasks/test_mq_user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- result.user['Username'] == usernames[0]
- not (result.user['Pending']['ConsoleAccess'] | bool)
- result.user['Pending']['Groups'] | length == 0
when: not ansible_check_mode
- name: test2 - create user with console access and group list
# amazon.aws.mq_user:
mq_user:
Expand All @@ -67,6 +68,7 @@
- result.user['Username'] == usernames[1]
- result.user['Pending']['ConsoleAccess'] | bool
- result.user['Pending']['Groups'] | length == 2
when: not ansible_check_mode
- name: test3 - create user with defined password
# amazon.aws.mq_user:
mq_user:
Expand All @@ -87,6 +89,7 @@
- result.user['Username'] == usernames[2]
- not (result.user['Pending']['ConsoleAccess'] | bool)
- result.user['Pending']['Groups'] | length == 0
when: not ansible_check_mode
- name: test4 - update user password - ignore mode
# amazon.aws.mq_user:
mq_user:
Expand All @@ -104,6 +107,7 @@
fail_msg: test4 failed
that:
- not (result.changed | bool)
when: not ansible_check_mode
- name: test5 - update user password - force mode
# amazon.aws.mq_user:
mq_user:
Expand All @@ -122,6 +126,7 @@
fail_msg: test5 failed
that:
- result.changed | bool
when: not ansible_check_mode
- name: test6 - update console access - same value
# amazon.aws.mq_user:
mq_user:
Expand All @@ -139,6 +144,7 @@
fail_msg: test6 failed
that:
- not (result.changed | bool)
when: not ansible_check_mode
- name: test7 - update console access - new value
# amazon.aws.mq_user:
mq_user:
Expand All @@ -158,6 +164,7 @@
- result.changed | bool
- not( result.user['Pending']['ConsoleAccess'] | bool )
- result.user['Pending']['Groups'] | length == 2
when: not ansible_check_mode
- name: test8 - update group list - same list but different order
# amazon.aws.mq_user:
mq_user:
Expand All @@ -175,6 +182,7 @@
fail_msg: test8 failed
that:
- not (result.changed | bool)
when: not ansible_check_mode
- name: test9 - update group list - add element
# amazon.aws.mq_user:
mq_user:
Expand All @@ -193,6 +201,7 @@
that:
- result.changed | bool
- result.user['Pending']['Groups'] | length == 3
when: not ansible_check_mode
- name: test10 - update group list - remove element
# amazon.aws.mq_user:
mq_user:
Expand All @@ -211,6 +220,7 @@
that:
- result.changed | bool
- result.user['Pending']['Groups'] | length == 2
when: not ansible_check_mode
- name: test11 - update group list - set to empty list
# amazon.aws.mq_user:
mq_user:
Expand All @@ -229,6 +239,7 @@
that:
- result.changed | bool
- result.user['Pending']['Groups'] | length == 0
when: not ansible_check_mode
- name: delete all users
# amazon.aws.mq_user:
mq_user:
Expand Down Expand Up @@ -257,3 +268,4 @@
fail_msg: test13 failed
that:
- not(result.changed | bool)
when: not ansible_check_mode
21 changes: 4 additions & 17 deletions tests/integration/targets/mq/tasks/test_mq_user_info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,7 @@
aws_secret_key: "{{ aws_secret_access_key }}"
security_token: "{{ aws_session_token }}"
loop: "{{ delete_users | flatten(levels=1) }}"
- name: test1 - list all users with custom limit
# amazon.aws.mq_user_info:
mq_user_info:
broker_id: "{{ broker_id }}"
region: "{{ aws_region }}"
max_results: 5
aws_access_key: "{{ aws_access_key_id }}"
aws_secret_key: "{{ aws_secret_access_key }}"
security_token: "{{ aws_session_token }}"
register: result
- name: test1 - verify
#ansible.builtin.assert:
assert:
fail_msg: test1 failed
that:
- (result.users | length) == 5
- name: test2 - list all users as dict
- name: test2 - list all users
# amazon.aws.mq_user_info:
mq_user_info:
broker_id: "{{ broker_id }}"
Expand All @@ -86,6 +70,7 @@
- result.users['info_user1']
- result.users['info_user2']
- result.users['info_user3']
when: not ansible_check_mode
- name: test3 - list only user currently being active until next broker reboot
# amazon.aws.mq_user_info:
mq_user_info:
Expand All @@ -106,6 +91,7 @@
- not ('info_user3' in result.users)
- not ('info_user4' in result.users)
- result.users['info_user5']
when: not ansible_check_mode
- name: test4 - list only user that will be active after next broker reboot
# amazon.aws.mq_user_info:
mq_user_info:
Expand All @@ -126,3 +112,4 @@
- result.users['info_user3']
- result.users['info_user4']
- not ('info_user5' in result.users)
when: not ansible_check_mode

0 comments on commit afbe9fe

Please sign in to comment.