Skip to content

Commit

Permalink
all sanity checks fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ozbillwang committed Apr 11, 2017
1 parent 14c9fff commit 793d0dd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 51 deletions.
130 changes: 82 additions & 48 deletions library/ssm_parameter_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
ANSIBLE_METADATA = {'status': ['stableinterface'],
'supported_by': 'committer',
'version': '1.0'}
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'metadata_version': '1.0'}

DOCUMENTATION = '''
---
module: ssm_parameter_store
short_description: Manage key-vaule pairs in aws parameter store.
short_description: Manage key-value pairs in aws parameter store.
description:
- Manage key-vaule pairs in aws parameter store.
version_added: "2.2"
- Manage key-value pairs in aws parameter store.
version_added: "2.4"
options:
name:
description:
Expand Down Expand Up @@ -66,32 +67,29 @@
- Boolean
required: false
default: True
author: Bill Wang([email protected])
author: Bill Wang ([email protected])
extends_documentation_fragment: aws
requirements: [ botocore, boto3 ]
'''

EXAMPLES = '''
- name: Create or update key/vaule pair in aws parameter store
- name: Create or update key/value pair in aws parameter store
ssm_parameter_store:
name: "Hello"
description: "This is your first key"
value: "World"
register: result
- name: Delete the key
- name: Delete the key
ssm_parameter_store:
name: "Hello"
state: absent
register: result
- name: Create or update secure key/vaule pair in aws parameter store
- name: Create or update secure key/value pair in aws parameter store
ssm_parameter_store:
name: "Hello"
description: "This is your first key"
string_type: "SecureString"
value: "World"
register: result
- name: Retrieving plain-text secret
ssm_parameter_store:
Expand All @@ -114,6 +112,37 @@
register: result
'''

RETURN = '''
put_parameter:
description: Add one or more paramaters to the system.
returned: success
type: dictionary
get_parameter:
description: Get details of a parameter.
returned: success
type: dictionary
contains:
name:
description: The name of the parameter.
returned: success
type: string
sample: "Hello"
type:
description: The type of parameter. Valid values include the following: String, String list, Secure string..
returned: success
type: string
sample: "String"
value:
description: The parameter value.
returned: success
type: string
sample: "World"
delete_parameter:
description: Delete a parameter from the system.
returned: success
type: dictionary
'''

try:
import botocore
import boto3
Expand All @@ -124,88 +153,93 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import boto3_conn, AnsibleAWSError, ec2_argument_spec, get_aws_connection_info


def create_update_parameter(client, module):
changed = False

args = dict(
Name=module.params.get('name'),
Value=module.params.get('value'),
Type=module.params.get('string_type'),
Overwrite=module.params.get('overwrite')
Name=module.params.get('name'),
Value=module.params.get('value'),
Type=module.params.get('string_type'),
Overwrite=module.params.get('overwrite')
)

if module.params.get('description'):
args.update(Description=module.params.get('description'))
args.update(Description=module.params.get('description'))

if module.params.get('string_type') is 'SecureString':
args.update(KeyId=module.params.get('key_id'))
args.update(KeyId=module.params.get('key_id'))

try:
nacl = client.put_parameter(**args)
changed = True
nacl = client.put_parameter(**args)
changed = True
except botocore.exceptions.ClientError as e:
module.fail_json(msg=str(e))
return changed, nacl


def get_parameter(client, module):
changed = False
try:
nacl = client.get_parameters(
Names=[module.params.get('name')],
WithDecryption=module.params.get('decryption')
)
nacl = client.get_parameters(
Names=[module.params.get('name')],
WithDecryption=module.params.get('decryption')
)
except botocore.exceptions.ClientError as e:
module.fail_json(msg=str(e))
return changed, nacl['Parameters']


def delete_parameter(client, module):
changed = False

nacl = dict()

get_nacl = client.get_parameters(
Names=[module.params.get('name')]
Names=[module.params.get('name')]
)
if get_nacl['Parameters']:
try:
nacl = client.delete_parameter(
try:
nacl = client.delete_parameter(
Name=module.params.get('name')
)
changed = True
except botocore.exceptions.ClientError as e:
module.fail_json(msg=str(e))
)
changed = True
except botocore.exceptions.ClientError as e:
module.fail_json(msg=str(e))
return changed, nacl


def main():

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

module = AnsibleModule(argument_spec=argument_spec)

if not HAS_BOTO3:
module.fail_json(msg='boto3 are required.')
state = module.params.get('state').lower()
try:
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 botocore.exceptions.NoCredentialsError as e:
module.fail_json(msg="Can't authorize connection - %s" % str(e))
module.fail_json(msg="Can't authorize connection - %s" % str(e))

invocations = {
"present": create_update_parameter,
"absent": delete_parameter,
"show": get_parameter,
"present": create_update_parameter,
"absent": delete_parameter,
"show": get_parameter,
}
(changed, results) = invocations[state](client, module)
module.exit_json(changed=changed, nacl_id=results)
Expand Down
6 changes: 3 additions & 3 deletions lookup/ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase


class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):

Expand All @@ -40,8 +41,8 @@ def run(self, terms, variables, **kwargs):
for term in terms:
try:
response = client.get_parameters(
Names=[term],
WithDecryption=True
Names=[term],
WithDecryption=True
)
except botocore.exceptions.ClientError as e:
module.fail_json(msg=str(e))
Expand All @@ -51,4 +52,3 @@ def run(self, terms, variables, **kwargs):
return [ret['Parameters'][0]['Value']]
else:
return None

0 comments on commit 793d0dd

Please sign in to comment.