Skip to content

Commit 73b7aac

Browse files
authored
Add asb_encode_binding module (#1)
Add asb_encode_binding module
1 parent c2a9b5f commit 73b7aac

File tree

11 files changed

+220
-0
lines changed

11 files changed

+220
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.retry
2+

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
asb-modules
2+
=========
3+
4+
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`
5+
6+
7+
Installation and use
8+
----------------
9+
10+
Use the Galaxy client to install the role:
11+
12+
```
13+
$ ansible-galaxy install ansibleplaybookbundle.asb-modules
14+
```
15+
16+
Once installed, use the modules in playbook or role:
17+
```yaml
18+
- name: Encodes fields for Ansible Service Broker
19+
roles:
20+
- ansibleplaybookbundle.asb-modules
21+
tasks:
22+
- name: encode bind credentials
23+
asb_encode_binding:
24+
fields:
25+
ENV_VAR: "value"
26+
ENV_VAR2: "value2"
27+
```
28+
29+
Modules
30+
-------
31+
- [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)
32+
33+
License
34+
-------
35+
36+
Apache V2

defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
# defaults file for ansible-asb-modules

handlers/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
# handlers file for ansible-asb-modules

library/asb_encode_binding.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/python
2+
3+
ANSIBLE_METADATA = {'metadata_version': '1.0',
4+
'status': ['preview'],
5+
'supported_by': 'community'}
6+
7+
8+
DOCUMENTATION = '''
9+
---
10+
module: asb_encode_binding
11+
short_description: Encodes binding fields for Ansible Service Broker
12+
description:
13+
- Takes a dictionary of fields and makes them available to Ansible Service Broker
14+
to read and create a binding when running the action (provision, bind, etc)
15+
notes: []
16+
requirements: []
17+
author:
18+
- "Red Hat, Inc."
19+
options:
20+
fields:
21+
description:
22+
- 'dictionary of key/value pairs to encode for a binding. Keys will become the injected environment variables.'
23+
required: true
24+
default: {}
25+
'''
26+
27+
EXAMPLES = '''
28+
- name: encode bind credentials
29+
asb_encode_binding:
30+
fields:
31+
POSTGRESQL_HOST: postgresql
32+
POSTGRESQL_PORT: 5432
33+
POSTGRESQL_USER: "{{ postgresql_user }}"
34+
POSTGRESQL_PASSWORD: "{{ postgresql_password }}"
35+
POSTGRESQL_DATABASE: "{{ postgresql_database }}"
36+
'''
37+
RETURN = '''
38+
encoded_fields:
39+
description: string containing encoded fields
40+
returned: success
41+
type: string
42+
sample: eyJURVNUX1ZBUl8xIjogInRlc3QgdmFsdWUgMSIsICJUZXN0VmFsdWUyIjogMn0=
43+
'''
44+
45+
import json
46+
import base64
47+
from ansible.module_utils.basic import AnsibleModule
48+
49+
ENCODED_BINDING_PATH = "/var/tmp/bind-creds"
50+
51+
52+
def main():
53+
54+
argument_spec = dict(
55+
fields=dict(required=True, type='dict')
56+
)
57+
58+
ansible_module = AnsibleModule(argument_spec=argument_spec)
59+
60+
try:
61+
fields_json = json.dumps(ansible_module.params['fields'])
62+
encoded_fields = base64.b64encode(fields_json)
63+
except Exception as error:
64+
ansible_module.fail_json(msg="Error attempting to encode binding: " + str(error))
65+
66+
try:
67+
with open(ENCODED_BINDING_PATH, "w") as binding_file:
68+
binding_file.write(encoded_fields)
69+
except Exception as error:
70+
ansible_module.fail_json(msg="Error attempting to write binding: " + str(error))
71+
72+
ansible_module.exit_json(changed=True, encoded_fields=encoded_fields)
73+
74+
75+
if __name__ == '__main__':
76+
main()

meta/main.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
galaxy_info:
2+
author: ansibleplaybookbundle
3+
description: Ansible Service Broker modules for use in Ansible Playbook Bundles
4+
company: Red Hat, Inc.
5+
license: Apache V2
6+
min_ansible_version: 2.3
7+
# github_branch: master
8+
9+
platforms:
10+
- name: GenericUNIX
11+
versions:
12+
- any
13+
14+
galaxy_tags:
15+
- k8s
16+
- kubernetes
17+
- openshift
18+
- broker
19+
20+
dependencies: []

tasks/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- debug:
3+
msg: "Ansible Service Broker modules loaded"
4+
verbosity: 1

tests/inventory

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
localhost
2+

tests/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
- include: test_asb_encode_binding.yml

tests/test_asb_encode_binding.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
- hosts: localhost
3+
vars:
4+
binding_file_path: /var/tmp/bind-creds
5+
roles:
6+
- ansibleplaybookbundle.asb-modules
7+
tasks:
8+
- name: prepare test for clean run
9+
file:
10+
path: "{{ binding_file_path }}"
11+
state: absent
12+
13+
- name: encode bind credentials
14+
asb_encode_binding:
15+
fields:
16+
TEST_VAR_1: "test value 1"
17+
TestValue2: 2
18+
register: apb_result
19+
20+
- debug:
21+
var: apb_result
22+
23+
- name: decode encoded value returned
24+
set_fact:
25+
decoded_apb_result: "{{ apb_result.encoded_fields | b64decode }}"
26+
27+
- debug:
28+
var: decoded_apb_result
29+
30+
- stat:
31+
path: "{{ binding_file_path }}"
32+
register: creds_file_status
33+
34+
- name: Credentials files should exist
35+
debug:
36+
var: creds_file_status.stat.exists
37+
38+
- fail:
39+
msg: Credentials files at {{ binding_file_path }} could not be found
40+
when: not creds_file_status.stat.exists
41+
42+
- name: read credentials file
43+
shell: cat {{ binding_file_path }}
44+
register: encoded_binding
45+
when: creds_file_status.stat.exists
46+
changed_when: False
47+
48+
- name: Credentials files should contain encoded value
49+
debug:
50+
msg: "{{ encoded_binding.stdout }}"
51+
when: creds_file_status.stat.exists
52+
53+
- set_fact:
54+
binding: "{{ encoded_binding.stdout | b64decode | from_json }}"
55+
when: creds_file_status.stat.exists
56+
57+
- name: Credentials should match original values
58+
debug:
59+
var: binding
60+
61+
- fail:
62+
msg: Decoded value for TEST_VAR_1 did not match original ("test value 1")
63+
when: binding["TEST_VAR_1"] != "test value 1"
64+
65+
- fail:
66+
msg: Decoded value for TestValue2 value did not match original (2)
67+
when: binding["TestValue2"] != 2
68+
69+
- name: clean up after testing
70+
file:
71+
path: "{{ binding_file_path }}"
72+
state: absent

0 commit comments

Comments
 (0)