Skip to content

Commit

Permalink
Add asb_encode_binding module (#1)
Browse files Browse the repository at this point in the history
Add asb_encode_binding module
  • Loading branch information
cfchase authored Jul 26, 2017
1 parent c2a9b5f commit 73b7aac
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.retry

36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
asb-modules
=========

This role loads modules for [Ansible Service Broker](https://github.com/openshift/ansible-service-broker) and is intended for execution from [Ansible Playbook Bundles](https://github.com/fusor/ansible-playbook-bundle). It is included in apb-base so all modules should be available if your image is built `FROM ansibleplaybookbundle/apb-base`


Installation and use
----------------

Use the Galaxy client to install the role:

```
$ ansible-galaxy install ansibleplaybookbundle.asb-modules
```

Once installed, use the modules in playbook or role:
```yaml
- name: Encodes fields for Ansible Service Broker
roles:
- ansibleplaybookbundle.asb-modules
tasks:
- name: encode bind credentials
asb_encode_binding:
fields:
ENV_VAR: "value"
ENV_VAR2: "value2"
```
Modules
-------
- [asb_encode_binding](library/asb_encode_binding.py) - Takes a dictionary of fields and makes them available to Ansible Service Broker to read and create a binding when running the action (provision, bind, etc)
License
-------
Apache V2
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for ansible-asb-modules
2 changes: 2 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# handlers file for ansible-asb-modules
76 changes: 76 additions & 0 deletions library/asb_encode_binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/python

ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'community'}


DOCUMENTATION = '''
---
module: asb_encode_binding
short_description: Encodes binding fields for Ansible Service Broker
description:
- Takes a dictionary of fields and makes them available to Ansible Service Broker
to read and create a binding when running the action (provision, bind, etc)
notes: []
requirements: []
author:
- "Red Hat, Inc."
options:
fields:
description:
- 'dictionary of key/value pairs to encode for a binding. Keys will become the injected environment variables.'
required: true
default: {}
'''

EXAMPLES = '''
- name: encode bind credentials
asb_encode_binding:
fields:
POSTGRESQL_HOST: postgresql
POSTGRESQL_PORT: 5432
POSTGRESQL_USER: "{{ postgresql_user }}"
POSTGRESQL_PASSWORD: "{{ postgresql_password }}"
POSTGRESQL_DATABASE: "{{ postgresql_database }}"
'''
RETURN = '''
encoded_fields:
description: string containing encoded fields
returned: success
type: string
sample: eyJURVNUX1ZBUl8xIjogInRlc3QgdmFsdWUgMSIsICJUZXN0VmFsdWUyIjogMn0=
'''

import json
import base64
from ansible.module_utils.basic import AnsibleModule

ENCODED_BINDING_PATH = "/var/tmp/bind-creds"


def main():

argument_spec = dict(
fields=dict(required=True, type='dict')
)

ansible_module = AnsibleModule(argument_spec=argument_spec)

try:
fields_json = json.dumps(ansible_module.params['fields'])
encoded_fields = base64.b64encode(fields_json)
except Exception as error:
ansible_module.fail_json(msg="Error attempting to encode binding: " + str(error))

try:
with open(ENCODED_BINDING_PATH, "w") as binding_file:
binding_file.write(encoded_fields)
except Exception as error:
ansible_module.fail_json(msg="Error attempting to write binding: " + str(error))

ansible_module.exit_json(changed=True, encoded_fields=encoded_fields)


if __name__ == '__main__':
main()
20 changes: 20 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
galaxy_info:
author: ansibleplaybookbundle
description: Ansible Service Broker modules for use in Ansible Playbook Bundles
company: Red Hat, Inc.
license: Apache V2
min_ansible_version: 2.3
# github_branch: master

platforms:
- name: GenericUNIX
versions:
- any

galaxy_tags:
- k8s
- kubernetes
- openshift
- broker

dependencies: []
4 changes: 4 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- debug:
msg: "Ansible Service Broker modules loaded"
verbosity: 1
2 changes: 2 additions & 0 deletions tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost

2 changes: 2 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- include: test_asb_encode_binding.yml
72 changes: 72 additions & 0 deletions tests/test_asb_encode_binding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
- hosts: localhost
vars:
binding_file_path: /var/tmp/bind-creds
roles:
- ansibleplaybookbundle.asb-modules
tasks:
- name: prepare test for clean run
file:
path: "{{ binding_file_path }}"
state: absent

- name: encode bind credentials
asb_encode_binding:
fields:
TEST_VAR_1: "test value 1"
TestValue2: 2
register: apb_result

- debug:
var: apb_result

- name: decode encoded value returned
set_fact:
decoded_apb_result: "{{ apb_result.encoded_fields | b64decode }}"

- debug:
var: decoded_apb_result

- stat:
path: "{{ binding_file_path }}"
register: creds_file_status

- name: Credentials files should exist
debug:
var: creds_file_status.stat.exists

- fail:
msg: Credentials files at {{ binding_file_path }} could not be found
when: not creds_file_status.stat.exists

- name: read credentials file
shell: cat {{ binding_file_path }}
register: encoded_binding
when: creds_file_status.stat.exists
changed_when: False

- name: Credentials files should contain encoded value
debug:
msg: "{{ encoded_binding.stdout }}"
when: creds_file_status.stat.exists

- set_fact:
binding: "{{ encoded_binding.stdout | b64decode | from_json }}"
when: creds_file_status.stat.exists

- name: Credentials should match original values
debug:
var: binding

- fail:
msg: Decoded value for TEST_VAR_1 did not match original ("test value 1")
when: binding["TEST_VAR_1"] != "test value 1"

- fail:
msg: Decoded value for TestValue2 value did not match original (2)
when: binding["TestValue2"] != 2

- name: clean up after testing
file:
path: "{{ binding_file_path }}"
state: absent
2 changes: 2 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# vars file for ansible-asb-modules

0 comments on commit 73b7aac

Please sign in to comment.