Skip to content

Commit

Permalink
Aws ssm multiple fixes (ansible#35569)
Browse files Browse the repository at this point in the history
* aws ssm parameter lookup test case - fails demonstrating no exception when parameter missing

* aws ssm parameter lookup - fail in case parameter doesn't exist

* aws ssm parameter lookup test case - failing case for nice return from path lookup

* aws ssm parameter lookup - convert incoming taglist to a key-value dictionary

* aws ssm parameter lookup - pep8 / style clean up

* aws_ssm lookup plugin rewrite for more standard interface

* aws_ssm module and lookup - introduce integration test and fix:

* aws_ssm module and lookup - error case integraton test and many PEP8 and other cleanups

* aws ssm parameter lookup - Various fixes in response to review + recursive fix & test

* aws ssm parameter lookup - more in response to review - shertel/abadger

* aws ssm parameter lookup unit test - move to mocker according to abadger

* aws ssm parameter lookup - integrate with new documentation fragment

* aws ssm parameter lookup - accept either aws_profile or boto_profile

* aws ssm parameter lookup - eliminate lookup document fragment until env vars are fixed later
  • Loading branch information
mikedlr authored and s-hertel committed Feb 6, 2018
1 parent 49eb0c4 commit d31ded4
Show file tree
Hide file tree
Showing 6 changed files with 453 additions and 156 deletions.
89 changes: 40 additions & 49 deletions lib/ansible/modules/cloud/amazon/aws_ssm_parameter_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
description:
- region.
required: false
author: Bill Wang ([email protected])
author:
- Bill Wang ([email protected])
- Michael De La Rue (@mikedlr)
extends_documentation_fragment: aws
requirements: [ botocore, boto3 ]
'''
Expand Down Expand Up @@ -107,13 +109,11 @@
type: dictionary
'''

import traceback
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import HAS_BOTO3, camel_dict_to_snake_dict
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info

try:
from botocore.exceptions import ClientError, NoCredentialsError
from botocore.exceptions import ClientError
except ImportError:
pass # will be captured by imported HAS_BOTO3

Expand All @@ -139,64 +139,54 @@ def create_update_parameter(client, module):
response = client.put_parameter(**args)
changed = True
except ClientError as e:
module.fail_json(msg=e.message, exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))
module.fail_json_aws(e, msg="setting parameter")

return changed, response


def delete_parameter(client, module):
changed = False
response = {}

try:
get_response = client.get_parameters(
Names=[module.params.get('name')]
response = client.delete_parameter(
Name=module.params.get('name')
)
except ClientError as e:
module.fail_json(msg=e.message, exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))

if get_response['Parameters']:
try:
response = client.delete_parameter(
Name=module.params.get('name')
)
changed = True
except ClientError as e:
module.fail_json(msg=e.message, exception=traceback.format_exc(),
**camel_dict_to_snake_dict(e.response))

return changed, response


def main():
if e.response['Error']['Code'] == 'ParameterNotFound':
return False, {}
module.fail_json_aws(e, msg="deleting parameter")

return True, response


def setup_client(module):
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
connection = boto3_conn(module, conn_type='client', resource='ssm', region=region, endpoint=ec2_url, **aws_connect_params)
return connection


def setup_module_object():
argument_spec = dict(
name=dict(required=True),
description=dict(),
value=dict(required=False),
state=dict(default='present', choices=['present', 'absent']),
string_type=dict(default='String', choices=['String', 'StringList', 'SecureString']),
decryption=dict(default=True, type='bool'),
key_id=dict(default="alias/aws/ssm"),
overwrite=dict(default=True, type='bool'),
region=dict(required=False),
)

argument_spec = ec2_argument_spec()
argument_spec.update(
dict(
name=dict(required=True),
description=dict(),
value=dict(required=False),
state=dict(default='present', choices=['present', 'absent']),
string_type=dict(default='String', choices=['String', 'StringList', 'SecureString']),
decryption=dict(default=True, type='bool'),
key_id=dict(default="alias/aws/ssm"),
overwrite=dict(default=True, type='bool'),
region=dict(required=False),
)
return AnsibleAWSModule(
argument_spec=argument_spec,
)

module = AnsibleModule(argument_spec=argument_spec)

if not HAS_BOTO3:
module.fail_json(msg='boto3 are required.')
def main():
module = setup_module_object()
state = module.params.get('state')
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
client = boto3_conn(module, conn_type='client', resource='ssm', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except NoCredentialsError as e:
module.fail_json(msg="Can't authorize connection - %s" % str(e))
client = setup_client(module)

invocations = {
"present": create_update_parameter,
Expand All @@ -205,5 +195,6 @@ def main():
(changed, response) = invocations[state](client, module)
module.exit_json(changed=changed, response=response)


if __name__ == '__main__':
main()
Loading

0 comments on commit d31ded4

Please sign in to comment.