From 07331b47724dbb7cd6952c1a2af54275ace7726e Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 13 Jan 2017 12:37:30 -0500 Subject: lib_openshift modules. This is the first one. oc_route. --- roles/lib_openshift/src/class/oc_route.py | 167 ++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 roles/lib_openshift/src/class/oc_route.py (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_route.py b/roles/lib_openshift/src/class/oc_route.py new file mode 100644 index 000000000..f0cfa5820 --- /dev/null +++ b/roles/lib_openshift/src/class/oc_route.py @@ -0,0 +1,167 @@ +# pylint: skip-file +# flake8: noqa + + +# pylint: disable=too-many-instance-attributes +class OCRoute(OpenShiftCLI): + ''' Class to wrap the oc command line tools ''' + kind = 'route' + + def __init__(self, + config, + verbose=False): + ''' Constructor for OCVolume ''' + super(OCRoute, self).__init__(config.namespace, config.kubeconfig) + self.config = config + self.namespace = config.namespace + self._route = None + + @property + def route(self): + ''' property function for route''' + if not self._route: + self.get() + return self._route + + @route.setter + def route(self, data): + ''' setter function for route ''' + self._route = data + + def exists(self): + ''' return whether a route exists ''' + if self.route: + return True + + return False + + def get(self): + '''return route information ''' + result = self._get(self.kind, self.config.name) + if result['returncode'] == 0: + self.route = Route(content=result['results'][0]) + elif 'routes \"%s\" not found' % self.config.name in result['stderr']: + result['returncode'] = 0 + result['results'] = [{}] + + return result + + def delete(self): + '''delete the object''' + return self._delete(self.kind, self.config.name) + + def create(self): + '''create the object''' + return self._create_from_content(self.config.name, self.config.data) + + def update(self): + '''update the object''' + # need to update the tls information and the service name + return self._replace_content(self.kind, self.config.name, self.config.data) + + def needs_update(self): + ''' verify an update is needed ''' + skip = [] + return not Utils.check_def_equal(self.config.data, self.route.yaml_dict, skip_keys=skip, debug=True) + + @staticmethod + def run_ansible(params, files, check_mode=False): + ''' run the idempotent asnible code + + params comes from the ansible portion for this module + files: a dictionary for the certificates + {'cert': {'path': '', + 'content': '', + 'value': '' + } + } + check_mode: does the module support check mode. (module.check_mode) + ''' + + rconfig = RouteConfig(params['name'], + params['namespace'], + params['kubeconfig'], + files['destcacert']['value'], + files['cacert']['value'], + files['cert']['value'], + files['key']['value'], + params['host'], + params['tls_termination'], + params['service_name']) + + oc_route = OCRoute(rconfig, verbose=params['debug']) + + state = params['state'] + + api_rval = oc_route.get() + + ##### + # Get + ##### + if state == 'list': + return {'changed': False, + 'results': api_rval['results'], + 'state': 'list'} + + ######## + # Delete + ######## + if state == 'absent': + if oc_route.exists(): + + if check_mode: + return {'changed': False, 'msg': 'CHECK_MODE: Would have performed a delete.'} # noqa: E501 + + api_rval = oc_route.delete() + + return {'changed': True, 'results': api_rval, 'state': "absent"} # noqa: E501 + return {'changed': False, 'state': 'absent'} + + if state == 'present': + ######## + # Create + ######## + if not oc_route.exists(): + + if check_mode: + return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a create.'} # noqa: E501 + + # Create it here + api_rval = oc_route.create() + + if api_rval['returncode'] != 0: + return {'failed': True, 'results': api_rval, 'state': "present"} # noqa: E501 + + # return the created object + api_rval = oc_route.get() + + if api_rval['returncode'] != 0: + return {'failed': True, 'results': api_rval, 'state': "present"} # noqa: E501 + + return {'changed': True, 'results': api_rval, 'state': "present"} # noqa: E501 + + ######## + # Update + ######## + if oc_route.needs_update(): + + if check_mode: + return {'changed': True, 'msg': 'CHECK_MODE: Would have performed an update.'} # noqa: E501 + + api_rval = oc_route.update() + + if api_rval['returncode'] != 0: + return {'failed': True, 'results': api_rval, 'state': "present"} # noqa: E501 + + # return the created object + api_rval = oc_route.get() + + if api_rval['returncode'] != 0: + return {'failed': True, 'results': api_rval, 'state': "present"} # noqa: E501 + + return {'changed': True, 'results': api_rval, 'state': "present"} # noqa: E501 + + return {'changed': False, 'results': api_rval, 'state': "present"} + + # catch all + return {'failed': True, 'msg': "Unknown State passed"} -- cgit v1.2.3 From ea33e223e34bb2b8efae6b165f3ac9729357cb46 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 13 Jan 2017 14:29:48 -0500 Subject: Adding oc_edit module to lib_openshift. --- roles/lib_openshift/src/class/oc_edit.py | 94 +++++++++++++++++++++++++++++++ roles/lib_openshift/src/class/oc_route.py | 1 + 2 files changed, 95 insertions(+) create mode 100644 roles/lib_openshift/src/class/oc_edit.py (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_edit.py b/roles/lib_openshift/src/class/oc_edit.py new file mode 100644 index 000000000..0734e2085 --- /dev/null +++ b/roles/lib_openshift/src/class/oc_edit.py @@ -0,0 +1,94 @@ +# pylint: skip-file +# flake8: noqa + +class Edit(OpenShiftCLI): + ''' Class to wrap the oc command line tools + ''' + # pylint: disable=too-many-arguments + def __init__(self, + kind, + namespace, + resource_name=None, + kubeconfig='/etc/origin/master/admin.kubeconfig', + separator='.', + verbose=False): + ''' Constructor for OpenshiftOC ''' + super(Edit, self).__init__(namespace, kubeconfig) + self.namespace = namespace + self.kind = kind + self.name = resource_name + self.kubeconfig = kubeconfig + self.separator = separator + self.verbose = verbose + + def get(self): + '''return a secret by name ''' + return self._get(self.kind, self.name) + + def update(self, file_name, content, force=False, content_type='yaml'): + '''run update ''' + if file_name: + if content_type == 'yaml': + data = yaml.load(open(file_name)) + elif content_type == 'json': + data = json.loads(open(file_name).read()) + + changes = [] + yed = Yedit(filename=file_name, content=data, separator=self.separator) + for key, value in content.items(): + changes.append(yed.put(key, value)) + + if any([not change[0] for change in changes]): + return {'returncode': 0, 'updated': False} + + yed.write() + + atexit.register(Utils.cleanup, [file_name]) + + return self._replace(file_name, force=force) + + return self._replace_content(self.kind, self.name, content, force=force, sep=self.separator) + + @staticmethod + def run_ansible(params, check_mode): + '''run the ansible idempotent code''' + + ocedit = Edit(params['kind'], + params['namespace'], + params['name'], + kubeconfig=params['kubeconfig'], + separator=params['separator'], + verbose=params['debug']) + + api_rval = ocedit.get() + + ######## + # Create + ######## + if not Utils.exists(api_rval['results'], params['name']): + return {"failed": True, 'msg': api_rval} + + ######## + # Update + ######## + if check_mode: + return {'changed': True, 'msg': 'CHECK_MODE: Would have performed edit'} + + api_rval = ocedit.update(params['file_name'], + params['content'], + params['force'], + params['file_format']) + + if api_rval['returncode'] != 0: + return {"failed": True, 'msg': api_rval} + + if 'updated' in api_rval and not api_rval['updated']: + return {"changed": False, 'results': api_rval, 'state': 'present'} + + # return the created object + api_rval = ocedit.get() + + if api_rval['returncode'] != 0: + return {"failed": True, 'msg': api_rval} + + return {"changed": True, 'results': api_rval, 'state': 'present'} diff --git a/roles/lib_openshift/src/class/oc_route.py b/roles/lib_openshift/src/class/oc_route.py index f0cfa5820..05b1be409 100644 --- a/roles/lib_openshift/src/class/oc_route.py +++ b/roles/lib_openshift/src/class/oc_route.py @@ -64,6 +64,7 @@ class OCRoute(OpenShiftCLI): skip = [] return not Utils.check_def_equal(self.config.data, self.route.yaml_dict, skip_keys=skip, debug=True) + # pylint: disable=too-many-return-statements,too-many-branches @staticmethod def run_ansible(params, files, check_mode=False): ''' run the idempotent asnible code -- cgit v1.2.3