diff options
author | Kenny Woodson <kwoodson@redhat.com> | 2016-04-07 11:58:43 -0400 |
---|---|---|
committer | Kenny Woodson <kwoodson@redhat.com> | 2016-04-07 11:58:43 -0400 |
commit | bf2c3b10bd37cbae5c0408589a8d7ac41d6f44de (patch) | |
tree | 87f2caa31f4734a96b0d9d7b10573daf2c459e50 /roles/lib_openshift_api/library/oc_obj.py | |
parent | a3be3eb5b68d4ef280495bd552b749fafe06b427 (diff) | |
parent | f55366615e4498987e44d8e370b5829c7a414168 (diff) | |
download | openshift-bf2c3b10bd37cbae5c0408589a8d7ac41d6f44de.tar.gz openshift-bf2c3b10bd37cbae5c0408589a8d7ac41d6f44de.tar.bz2 openshift-bf2c3b10bd37cbae5c0408589a8d7ac41d6f44de.tar.xz openshift-bf2c3b10bd37cbae5c0408589a8d7ac41d6f44de.zip |
Merge pull request #1719 from kwoodson/oadmrouter
oadm_router first attempt.
Diffstat (limited to 'roles/lib_openshift_api/library/oc_obj.py')
-rw-r--r-- | roles/lib_openshift_api/library/oc_obj.py | 67 |
1 files changed, 47 insertions, 20 deletions
diff --git a/roles/lib_openshift_api/library/oc_obj.py b/roles/lib_openshift_api/library/oc_obj.py index c058072e3..f0ea66aee 100644 --- a/roles/lib_openshift_api/library/oc_obj.py +++ b/roles/lib_openshift_api/library/oc_obj.py @@ -27,12 +27,12 @@ yaml.add_constructor(u'tag:yaml.org,2002:timestamp', timestamp_constructor) # pylint: disable=too-few-public-methods class OpenShiftCLI(object): - ''' Class to wrap the oc command line tools ''' + ''' Class to wrap the command line tools ''' def __init__(self, namespace, kubeconfig='/etc/origin/master/admin.kubeconfig', verbose=False): - ''' Constructor for OpenshiftOC ''' + ''' Constructor for OpenshiftCLI ''' self.namespace = namespace self.verbose = verbose self.kubeconfig = kubeconfig @@ -65,15 +65,15 @@ class OpenShiftCLI(object): cmd = ['-n', self.namespace, 'replace', '-f', fname] if force: cmd.append('--force') - return self.oc_cmd(cmd) + return self.openshift_cmd(cmd) def _create(self, fname): '''return all pods ''' - return self.oc_cmd(['create', '-f', fname, '-n', self.namespace]) + return self.openshift_cmd(['create', '-f', fname, '-n', self.namespace]) def _delete(self, resource, rname): '''return all pods ''' - return self.oc_cmd(['delete', resource, rname, '-n', self.namespace]) + return self.openshift_cmd(['delete', resource, rname, '-n', self.namespace]) def _get(self, resource, rname=None): '''return a secret by name ''' @@ -81,7 +81,7 @@ class OpenShiftCLI(object): if rname: cmd.append(rname) - rval = self.oc_cmd(cmd, output=True) + rval = self.openshift_cmd(cmd, output=True) # Ensure results are retuned in an array if rval.has_key('items'): @@ -91,10 +91,15 @@ class OpenShiftCLI(object): return rval - def oc_cmd(self, cmd, output=False): + def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json'): '''Base command for oc ''' #cmds = ['/usr/bin/oc', '--config', self.kubeconfig] - cmds = ['/usr/bin/oc'] + cmds = [] + if oadm: + cmds = ['/usr/bin/oadm'] + else: + cmds = ['/usr/bin/oc'] + cmds.extend(cmd) rval = {} @@ -112,18 +117,21 @@ class OpenShiftCLI(object): proc.wait() stdout = proc.stdout.read() stderr = proc.stderr.read() - rval = {"returncode": proc.returncode, "results": results, + "cmd": ' '.join(cmds), } if proc.returncode == 0: if output: - try: - rval['results'] = json.loads(stdout) - except ValueError as err: - if "No JSON object could be decoded" in err.message: - err = err.message + if output_type == 'json': + try: + rval['results'] = json.loads(stdout) + except ValueError as err: + if "No JSON object could be decoded" in err.message: + err = err.message + elif output_type == 'raw': + rval['results'] = stdout if self.verbose: print stdout @@ -226,11 +234,13 @@ class Utils(object): # Disabling too-many-branches. This is a yaml dictionary comparison function # pylint: disable=too-many-branches,too-many-return-statements @staticmethod - def check_def_equal(user_def, result_def, debug=False): + def check_def_equal(user_def, result_def, skip_keys=None, debug=False): ''' Given a user defined definition, compare it with the results given back by our query. ''' # Currently these values are autogenerated and we do not need to check them skip = ['metadata', 'status'] + if skip_keys: + skip.extend(skip_keys) for key, value in result_def.items(): if key in skip: @@ -239,11 +249,27 @@ class Utils(object): # Both are lists if isinstance(value, list): if not isinstance(user_def[key], list): + if debug: + print 'user_def[key] is not a list' return False - # lists should be identical - if value != user_def[key]: - return False + for values in zip(user_def[key], value): + if isinstance(values[0], dict) and isinstance(values[1], dict): + if debug: + print 'sending list - list' + print type(values[0]) + print type(values[1]) + result = Utils.check_def_equal(values[0], values[1], skip_keys=skip_keys, debug=debug) + if not result: + print 'list compare returned false' + return False + + elif value != user_def[key]: + if debug: + print 'value should be identical' + print value + print user_def[key] + return False # recurse on a dictionary elif isinstance(value, dict): @@ -262,10 +288,11 @@ class Utils(object): print "keys are not equal in dict" return False - result = Utils.check_def_equal(user_def[key], value, debug=debug) + result = Utils.check_def_equal(user_def[key], value, skip_keys=skip_keys, debug=debug) if not result: if debug: print "dict returned false" + print result return False # Verify each key, value pair is the same @@ -549,7 +576,7 @@ class OCObject(OpenShiftCLI): data = Utils.get_resource_file(files[0], content_type) # if equal then no need. So not equal is True - return not Utils.check_def_equal(data, objects['results'][0], True) + return not Utils.check_def_equal(data, objects['results'][0], skip_keys=None, debug=False) else: data = content |