Skip to content

Commit

Permalink
adding fact module and example playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
markramach committed Dec 19, 2016
1 parent abc5b5e commit 9919ee5
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
6 changes: 3 additions & 3 deletions clc_ansible_module/clc_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
else:
REQUESTS_FOUND = True

class ClcMetaFact:
class ClcMeta:

def __init__(self, module):

Expand Down Expand Up @@ -130,9 +130,9 @@ def main():
The main function. Instantiates the module and calls process_request.
:return: none
"""
argument_dict = ClcMetaFact._define_module_argument_spec()
argument_dict = ClcMeta._define_module_argument_spec()
module = AnsibleModule(supports_check_mode=True, **argument_dict)
clc_meta_fact = ClcMetaFact(module)
clc_meta_fact = ClcMeta(module)

changed, response = clc_meta_fact.process_request()
module.exit_json(changed=changed, meta=response)
Expand Down
110 changes: 110 additions & 0 deletions clc_ansible_module/clc_meta_fact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/python

try:
import requests
except ImportError:
REQUESTS_FOUND = False
else:
REQUESTS_FOUND = True

class ClcMetaFact:

def __init__(self, module):

if not REQUESTS_FOUND:
self.module.fail_json(
msg='requests library is required for this module')

self.module = module
self.api_url = ''
self.headers = {}
self._set_clc_credentials_from_env()

def _set_clc_credentials_from_env(self):
"""
Set the CLC Credentials by reading environment variables
:return: none
"""
env = os.environ
v2_api_token = env.get('CLC_V2_API_TOKEN', False)
v2_api_username = env.get('CLC_V2_API_USERNAME', False)
v2_api_passwd = env.get('CLC_V2_API_PASSWD', False)
clc_alias = env.get('CLC_ACCT_ALIAS', False)
self.api_url = env.get('CLC_V2_API_URL', 'https://api.ctl.io')
self.meta_api_url = env.get('META_API_URL', 'https://api.runner.io')

if v2_api_token and clc_alias:

self.v2_api_token = v2_api_token
self.clc_alias = clc_alias

elif v2_api_username and v2_api_passwd:

r = requests.post(self.api_url + '/v2/authentication/login', json={
'username': v2_api_username,
'password': v2_api_passwd
})

if r.status_code not in [200]:
self.module.fail_json(
msg='Failed to authenticate with clc V2 api.')

r = r.json()
self.v2_api_token = r['bearerToken']
self.clc_alias = r['accountAlias']

else:
return self.module.fail_json(
msg="You must set the CLC_V2_API_USERNAME and CLC_V2_API_PASSWD "
"environment variables")

def process_request(self):
params = self.module.params
criteria = ' referenceId: "' + params.get('referenceId') + '" '
if params.get('jobId'):
criteria += ' jobId: "' + params.get('jobId') + '" '
if params.get('executionId'):
criteria += ' jobId: "' + params.get('jobId') + '" '
if params.get('name'):
criteria += ' name: "' + params.get('name') + '" '

gq = '{ metadata(' + criteria + ') { id referenceId jobId executionId name description data { ... on Config { type key value } ... on Instance { type value } } } }'

r = requests.post(self.meta_api_url + '/meta/' + self.clc_alias,
data=gq, headers={ 'Authorization': 'Bearer ' + self.v2_api_token, 'Content-Type' : 'text/plain' }, verify=False)

if r.status_code not in [200]:
# self.module.fail_json(msg='Failed to fetch metadata facts.')
self.module.exit_json(changed=True, content={ 'data' : r.text })

self.module.exit_json(changed=True, content={ 'data' : r.json() })


@staticmethod
def _define_module_argument_spec():
"""
Define the argument spec for the ansible module
:return: argument spec dictionary
"""
argument_spec = dict(
jobId=dict(required=False, default=False),
executionId=dict(required=False, default=False),
referenceId=dict(required=True),
name=dict(required=False, default=False))

return {"argument_spec": argument_spec}


def main():
"""
The main function. Instantiates the module and calls process_request.
:return: none
"""
argument_dict = ClcMetaFact._define_module_argument_spec()
module = AnsibleModule(supports_check_mode=True, **argument_dict)
ClcMetaFact(module).process_request()

from ansible.module_utils.basic import * # pylint: disable=W0614

if __name__ == '__main__':
main()
47 changes: 47 additions & 0 deletions example-playbooks/example_clc_meta_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
- name: Meta Test
hosts: localhost
connection: local
gather_facts: false
tasks:

- name: Set CLC Metadata Config
clc_meta:
state: present
referenceId: demo
jobId: "{{ jobId }}"
executionId: "{{ executionId }}"
name: configuration_value
description: Configuration value description.
data:
type: CONFIG
key: asdf
value: fdsamodified
register: result

- debug: msg={{result}}

- name: Set CLC Metadata Instance
clc_meta:
state: present
referenceId: demo
jobId: "{{ jobId }}"
executionId: "{{ executionId }}"
name: instance_value
description: Instance value description.
data:
type: INSTANCE
value: UC1WFTCTEST02
register: result

- debug: msg={{result}}

- name: Get CLC Metadata Instance
clc_meta_fact:
referenceId: demo
name: configuration_value
jobId: "{{ jobId }}"
executionId: "{{ executionId }}"
register: result

- debug: msg={{result}}

0 comments on commit 9919ee5

Please sign in to comment.