From b5de5357042c146a930097d52c3920808af89c42 Mon Sep 17 00:00:00 2001 From: Joel Diaz Date: Fri, 27 Jan 2017 20:39:06 +0000 Subject: oc_label ansible module used for adding/removing labels on various OpenShift objects --- roles/lib_openshift/src/class/oc_label.py | 291 ++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 roles/lib_openshift/src/class/oc_label.py (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_label.py b/roles/lib_openshift/src/class/oc_label.py new file mode 100644 index 000000000..9c65b1760 --- /dev/null +++ b/roles/lib_openshift/src/class/oc_label.py @@ -0,0 +1,291 @@ +# pylint: skip-file +# flake8: noqa + +# pylint: disable=too-many-instance-attributes +class OCLabel(OpenShiftCLI): + ''' Class to wrap the oc command line tools ''' + + # pylint allows 5 + # pylint: disable=too-many-arguments + def __init__(self, + name, + namespace, + kind, + kubeconfig, + labels=None, + selector=None, + verbose=False): + ''' Constructor for OCLabel ''' + super(OCLabel, self).__init__(namespace, kubeconfig) + self.name = name + self.namespace = namespace + self.kind = kind + self.kubeconfig = kubeconfig + self.labels = labels + self.selector = selector + + def get_current_labels(self): + ''' get the current labels on object ''' + + return self.get()['results']['labels'] + + def compare_labels(self, host_labels): + ''' compare incoming labels against current labels''' + + for label in self.labels: + if label['key'] not in host_labels or \ + label['value'] != host_labels[label['key']]: + return False + return True + + def all_user_labels_exist(self): + ''' return whether all the labels already exist ''' + + current_labels = self.get_current_labels() + + for current_host_labels in current_labels: + rbool = self.compare_labels(current_host_labels) + if rbool == False: + return False + return True + + def any_label_exists(self): + ''' return whether any single label already exists ''' + current_labels = self.get_current_labels() + + for current_host_labels in current_labels: + for label in self.labels: + if label['key'] in current_host_labels: + return True + return False + + def get_user_keys(self): + ''' go through list of user key:values and return all keys ''' + + user_keys = [] + for label in self.labels: + user_keys.append(label['key']) + + return user_keys + + def get_current_label_keys(self): + ''' collect all the current label keys ''' + + current_label_keys = [] + current_labels = self.get_current_labels() + for current_host_labels in current_labels: + for key in current_host_labels.keys(): + current_label_keys.append(key) + + return list(set(current_label_keys)) + + def get_extra_current_labels(self): + ''' return list of labels that are currently stored, but aren't + in user-provided list ''' + + current_labels = self.get_current_labels() + extra_labels = [] + user_label_keys = self.get_user_keys() + current_label_keys = self.get_current_label_keys() + + for current_key in current_label_keys: + if current_key not in user_label_keys: + extra_labels.append(current_key) + + return extra_labels + + def extra_current_labels(self): + ''' return whether there are labels currently stored that user + hasn't directly provided ''' + extra_labels = self.get_extra_current_labels() + + if len(extra_labels) > 0: + return True + else: + return False + + def replace(self): + ''' replace currently stored labels with user provided labels ''' + cmd = self.cmd_template() + + # First delete any extra labels + extra_labels = self.get_extra_current_labels() + if len(extra_labels) > 0: + for label in extra_labels: + cmd.append("{}-".format(label)) + + # Now add/modify the user-provided label list + if len(self.labels) > 0: + for label in self.labels: + cmd.append("{}={}".format(label['key'], label['value'])) + + # --overwrite for the case where we are updating existing labels + cmd.append("--overwrite") + return self.openshift_cmd(cmd) + + def get(self): + '''return label information ''' + + result_dict = {} + label_list = [] + + if self.name: + result = self._get(resource=self.kind, rname=self.name, selector=self.selector) + + if 'labels' in result['results'][0]['metadata']: + label_list.append(result['results'][0]['metadata']['labels']) + else: + label_list.append({}) + + else: + result = self._get(resource=self.kind, selector=self.selector) + + for item in result['results'][0]['items']: + if 'labels' in item['metadata']: + label_list.append(item['metadata']['labels']) + else: + label_list.append({}) + + result_dict['labels'] = label_list + result_dict['item_count'] = len(label_list) + result['results'] = result_dict + + return result + + def cmd_template(self): + ''' boilerplate oc command for modifying lables on this object ''' + # let's build the cmd with what we have passed in + cmd = [] + if self.namespace: + cmd = cmd + ["-n", self.namespace] + + if self.selector: + cmd = cmd + ["--selector", self.selector] + + cmd = cmd + ["--config", self.kubeconfig, "label", self.kind] + + if self.name: + cmd = cmd + [self.name] + + return cmd + + def add(self): + ''' add labels ''' + cmd = self.cmd_template() + + for label in self.labels: + cmd.append("{}={}".format(label['key'], label['value'])) + + cmd.append("--overwrite") + + return self.openshift_cmd(cmd) + + def delete(self): + '''delete the labels''' + cmd = self.cmd_template() + for label in self.labels: + cmd.append("{}-".format(label['key'])) + + return self.openshift_cmd(cmd) + + @staticmethod + def run_ansible(params, check_mode=False): + ''' run the idempotent ansible code + + prams comes from the ansible portion of this module + check_mode: does the module support check mode. (module.check_mode) + ''' + oc_label = OCLabel(params['name'], + params['namespace'], + params['kind'], + params['kubeconfig'], + params['labels'], + params['selector'], + verbose=params['debug']) + + state = params['state'] + name = params['name'] + selector = params['selector'] + + api_rval = oc_label.get() + + ##### + # Get + ##### + if state == 'list': + return {'changed': False, 'results': api_rval['results'], 'state': "list"} + + ####### + # Add + ####### + if state == 'add': + if not (name or selector): + return {'failed': True, + 'msg': "Param 'name' or 'selector' is required if state == 'add'"} + if not oc_label.all_user_labels_exist(): + if check_mode: + return {'changed': False, 'msg': 'Would have performed an addition.'} + api_rval = oc_label.add() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + return {'changed': True, 'results': api_rval, 'state': "add"} + + return {'changed': False, 'state': "add"} + + ######## + # Delete + ######## + if state == 'absent': + if not (name or selector): + return {'failed': True, + 'msg': "Param 'name' or 'selector' is required if state == 'absent'"} + + if oc_label.any_label_exists(): + if check_mode: + return {'changed': False, 'msg': 'Would have performed a delete.'} + + api_rval = oc_label.delete() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + return {'changed': True, 'results': api_rval, 'state': "absent"} + + return {'changed': False, 'state': "absent"} + + if state == 'present': + ######## + # Update + ######## + if not (name or selector): + return {'failed': True, + 'msg': "Param 'name' or 'selector' is required if state == 'present'"} + # if all the labels passed in don't already exist + # or if there are currently stored labels that haven't + # been passed in + if not oc_label.all_user_labels_exist() or \ + oc_label.extra_current_labels(): + if check_mode: + return {'changed': False, 'msg': 'Would have made changes.'} + + api_rval = oc_label.replace() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + # return the created object + api_rval = oc_label.get() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + return {'changed': True, 'results': api_rval, 'state': "present"} + + return {'changed': False, 'results': api_rval, 'state': "present"} + + return {'failed': True, + 'changed': False, + 'results': 'Unknown state passed. %s' % state, + 'state': "unknown"} -- cgit v1.2.3 From 94fd71fa8fba70205519e18625975b83373c1535 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 31 Jan 2017 11:09:57 -0500 Subject: Adding unit test. Fixed redudant calls to get. --- roles/lib_openshift/src/class/oc_label.py | 48 ++++++++++++++++--------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_label.py b/roles/lib_openshift/src/class/oc_label.py index 9c65b1760..4fc2ffc13 100644 --- a/roles/lib_openshift/src/class/oc_label.py +++ b/roles/lib_openshift/src/class/oc_label.py @@ -1,6 +1,7 @@ # pylint: skip-file # flake8: noqa + # pylint: disable=too-many-instance-attributes class OCLabel(OpenShiftCLI): ''' Class to wrap the oc command line tools ''' @@ -22,12 +23,22 @@ class OCLabel(OpenShiftCLI): self.kind = kind self.kubeconfig = kubeconfig self.labels = labels + self._curr_labels = None self.selector = selector - def get_current_labels(self): - ''' get the current labels on object ''' + @property + def current_labels(self): + '''property for the current labels''' + if self._curr_labels is None: + results = self.get() + self._curr_labels = results['labels'] + + return self._curr_labels - return self.get()['results']['labels'] + @current_labels.setter + def current_labels(self, data): + '''property setter for current labels''' + self._curr_labels = data def compare_labels(self, host_labels): ''' compare incoming labels against current labels''' @@ -41,9 +52,7 @@ class OCLabel(OpenShiftCLI): def all_user_labels_exist(self): ''' return whether all the labels already exist ''' - current_labels = self.get_current_labels() - - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: rbool = self.compare_labels(current_host_labels) if rbool == False: return False @@ -51,9 +60,8 @@ class OCLabel(OpenShiftCLI): def any_label_exists(self): ''' return whether any single label already exists ''' - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for label in self.labels: if label['key'] in current_host_labels: return True @@ -72,8 +80,7 @@ class OCLabel(OpenShiftCLI): ''' collect all the current label keys ''' current_label_keys = [] - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for key in current_host_labels.keys(): current_label_keys.append(key) @@ -83,7 +90,6 @@ class OCLabel(OpenShiftCLI): ''' return list of labels that are currently stored, but aren't in user-provided list ''' - current_labels = self.get_current_labels() extra_labels = [] user_label_keys = self.get_user_keys() current_label_keys = self.get_current_label_keys() @@ -100,7 +106,7 @@ class OCLabel(OpenShiftCLI): extra_labels = self.get_extra_current_labels() if len(extra_labels) > 0: - return True + return True else: return False @@ -146,8 +152,9 @@ class OCLabel(OpenShiftCLI): else: label_list.append({}) - result_dict['labels'] = label_list - result_dict['item_count'] = len(label_list) + self.current_labels = label_list + result_dict['labels'] = self.current_labels + result_dict['item_count'] = len(self.current_labels) result['results'] = result_dict return result @@ -155,17 +162,12 @@ class OCLabel(OpenShiftCLI): def cmd_template(self): ''' boilerplate oc command for modifying lables on this object ''' # let's build the cmd with what we have passed in - cmd = [] - if self.namespace: - cmd = cmd + ["-n", self.namespace] + cmd = ["label", self.kind] if self.selector: - cmd = cmd + ["--selector", self.selector] - - cmd = cmd + ["--config", self.kubeconfig, "label", self.kind] - - if self.name: - cmd = cmd + [self.name] + cmd.extend(["--selector", self.selector]) + elif self.name: + cmd.extend([self.name]) return cmd -- cgit v1.2.3 From f94417164f57891eb016ca5e98c6e713e1f66726 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 2 Feb 2017 09:36:01 -0500 Subject: Fixing linters. --- roles/lib_openshift/src/class/oc_label.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_label.py b/roles/lib_openshift/src/class/oc_label.py index 4fc2ffc13..8e1ba9ceb 100644 --- a/roles/lib_openshift/src/class/oc_label.py +++ b/roles/lib_openshift/src/class/oc_label.py @@ -54,7 +54,7 @@ class OCLabel(OpenShiftCLI): for current_host_labels in self.current_labels: rbool = self.compare_labels(current_host_labels) - if rbool == False: + if not rbool: return False return True @@ -107,8 +107,8 @@ class OCLabel(OpenShiftCLI): if len(extra_labels) > 0: return True - else: - return False + + return False def replace(self): ''' replace currently stored labels with user provided labels ''' @@ -190,6 +190,7 @@ class OCLabel(OpenShiftCLI): return self.openshift_cmd(cmd) + # pylint: disable=too-many-branches,too-many-return-statements @staticmethod def run_ansible(params, check_mode=False): ''' run the idempotent ansible code -- cgit v1.2.3