Created
August 20, 2015 16:14
-
-
Save Xowap/8cade85ee662b7df7edd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# vim: fileencoding=utf-8 tw=100 expandtab ts=4 sw=4 : | |
from __future__ import unicode_literals | |
try: | |
from deis import DeisClient | |
except ImportError: | |
DeisClient = None | |
class DeisError(Exception): | |
pass | |
class DeisApi(object): | |
def __init__(self, client): | |
self._client = client | |
def request(self, method, path, status_codes=None, *args, **kwargs): | |
if status_codes is None: | |
status_codes = {200} | |
# noinspection PyProtectedMember | |
out = self._client._dispatch(method, path, *args, **kwargs) | |
if out.status_code not in status_codes: | |
raise DeisError(out.text) | |
return out.json() | |
def request_all(self, path, *args, **kwargs): | |
next_url = path | |
while next_url is not None: | |
response = self.request('get', next_url, *args, **kwargs) | |
next_url = response['next'] | |
for result in response['results']: | |
yield result | |
def app_list(self): | |
for app in self.request_all('/v1/apps'): | |
yield app | |
def app_exists(self, app_id): | |
for app in self.app_list(): | |
if app['id'] == app_id: | |
return True | |
return False | |
def app_create(self, app_id): | |
self.request('post', '/v1/apps', {201}, json.dumps({ | |
'id': app_id, | |
})) | |
def app_ensure_exists(self, app_id): | |
if not self.app_exists(app_id): | |
self.app_create(app_id) | |
return True | |
return False | |
def app_config_get(self, app_id): | |
return self.request('get', '/v1/apps/{}/config'.format(app_id))['values'] | |
def app_config_set(self, app_id, values): | |
self.request('post', '/v1/apps/{}/config'.format(app_id), {201}, body=json.dumps({ | |
'values': values, | |
})) | |
def app_config_sync(self, app_id, values): | |
actual = self.app_config_get(app_id) | |
to_set = {} | |
actual_keys = set(actual.keys()) | |
expected_keys = set(values.keys()) | |
for key in expected_keys - actual_keys: | |
to_set[key] = values[key] | |
for key in expected_keys & actual_keys: | |
if actual[key] != values[key]: | |
to_set[key] = values[key] | |
if len(to_set): | |
self.app_config_set(app_id, to_set) | |
return True | |
else: | |
return False | |
def main(): | |
module = AnsibleModule( | |
argument_spec={ | |
'state': { | |
'default': 'present', | |
'choices': ['present'], | |
}, | |
'name': { | |
'required': True, | |
}, | |
'config': { | |
'type': 'dict', | |
'default': {}, | |
} | |
} | |
) | |
if DeisClient is None: | |
return module.fail_json(msg='`deis` is required for this module') | |
c = DeisClient() | |
a = DeisApi(c) | |
changed_app = a.app_ensure_exists(module.params['name']) | |
changed_config = a.app_config_sync(module.params['name'], module.params['config']) | |
return module.exit_json(changed=(changed_app or changed_config), msg='App created') | |
from ansible.module_utils.basic import * | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment