summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift_api/library/oc_secret.py
diff options
context:
space:
mode:
authorKenny Woodson <kwoodson@redhat.com>2016-03-24 15:39:24 -0400
committerKenny Woodson <kwoodson@redhat.com>2016-03-24 16:11:50 -0400
commite25f7c86d6c31e325cb2c1790e8f6049d05332d0 (patch)
tree3832c016fe87a37a165cec415285998608401a5a /roles/lib_openshift_api/library/oc_secret.py
parent2f6fc58b92232feab4b1c6e312526555edaef6f8 (diff)
downloadopenshift-e25f7c86d6c31e325cb2c1790e8f6049d05332d0.tar.gz
openshift-e25f7c86d6c31e325cb2c1790e8f6049d05332d0.tar.bz2
openshift-e25f7c86d6c31e325cb2c1790e8f6049d05332d0.tar.xz
openshift-e25f7c86d6c31e325cb2c1790e8f6049d05332d0.zip
Adding deployment config and refactored.
Diffstat (limited to 'roles/lib_openshift_api/library/oc_secret.py')
-rw-r--r--roles/lib_openshift_api/library/oc_secret.py85
1 files changed, 54 insertions, 31 deletions
diff --git a/roles/lib_openshift_api/library/oc_secret.py b/roles/lib_openshift_api/library/oc_secret.py
index d69d490ac..96a0f1db1 100644
--- a/roles/lib_openshift_api/library/oc_secret.py
+++ b/roles/lib_openshift_api/library/oc_secret.py
@@ -9,9 +9,6 @@ import shutil
import subprocess
import yaml
-# The base class is here to share methods.
-# Currently there is only 1 but will grow in the future.
-# pylint: disable=too-few-public-methods
class OpenShiftCLI(object):
''' Class to wrap the oc command line tools '''
def __init__(self,
@@ -23,6 +20,37 @@ class OpenShiftCLI(object):
self.verbose = verbose
self.kubeconfig = kubeconfig
+ def replace(self, fname, force=False):
+ '''return all pods '''
+ cmd = ['replace', '-f', fname]
+ if force:
+ cmd = ['replace', '--force', '-f', fname]
+ return self.oc_cmd(cmd)
+
+ def create(self, fname):
+ '''return all pods '''
+ return self.oc_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])
+
+ def get(self, resource, rname=None):
+ '''return a secret by name '''
+ cmd = ['get', resource, '-o', 'json', '-n', self.namespace]
+ if rname:
+ cmd.append(rname)
+
+ rval = self.oc_cmd(cmd, output=True)
+
+ # Ensure results are retuned in an array
+ if rval.has_key('items'):
+ rval['results'] = rval['items']
+ elif not isinstance(rval['results'], list):
+ rval['results'] = [rval['results']]
+
+ return rval
+
def oc_cmd(self, cmd, output=False):
'''Base command for oc '''
#cmds = ['/usr/bin/oc', '--config', self.kubeconfig]
@@ -63,21 +91,33 @@ class OpenShiftCLI(object):
class Utils(object):
''' utilities for openshiftcli modules '''
@staticmethod
+ def create_file(rname, data, ftype=None):
+ ''' create a file in tmp with name and contents'''
+ path = os.path.join('/tmp', rname)
+ with open(path, 'w') as fds:
+ if ftype == 'yaml':
+ fds.write(yaml.dump(data, default_flow_style=False))
+
+ elif ftype == 'json':
+ fds.write(json.dumps(data))
+ else:
+ fds.write(data)
+
+ # Register cleanup when module is done
+ atexit.register(Utils.cleanup, [path])
+ return path
+
+ @staticmethod
def create_files_from_contents(data):
'''Turn an array of dict: filename, content into a files array'''
files = []
for sfile in data:
- path = os.path.join('/tmp', sfile['path'])
- with open(path, 'w') as fds:
- fds.write(sfile['content'])
+ path = Utils.create_file(sfile['path'], sfile['content'])
files.append(path)
- # Register cleanup when module is done
- atexit.register(Utils.cleanup, files)
return files
-
@staticmethod
def cleanup(files):
'''Clean up on exit '''
@@ -106,7 +146,6 @@ class Utils(object):
''' Find the specified result by name'''
rval = None
for result in results:
- #print "%s == %s" % (result['metadata']['name'], name)
if result.has_key('metadata') and result['metadata']['name'] == _name:
rval = result
break
@@ -166,7 +205,7 @@ class Utils(object):
print "keys are not equal in dict"
return False
- result = Utils.check_def_equal(user_def[key], value)
+ result = Utils.check_def_equal(user_def[key], value, debug=debug)
if not result:
if debug:
print "dict returned false"
@@ -192,7 +231,7 @@ class Secret(OpenShiftCLI):
kubeconfig='/etc/origin/master/admin.kubeconfig',
verbose=False):
''' Constructor for OpenshiftOC '''
- super(Secret, OpenShiftCLI).__init__(namespace, kubeconfig)
+ super(Secret, self).__init__(namespace, kubeconfig)
self.namespace = namespace
self.name = secret_name
self.kubeconfig = kubeconfig
@@ -200,23 +239,11 @@ class Secret(OpenShiftCLI):
def get_secrets(self):
'''return a secret by name '''
- cmd = ['get', 'secrets', '-o', 'json', '-n', self.namespace]
- if self.name:
- cmd.append(self.name)
-
- rval = self.oc_cmd(cmd, output=True)
-
- # Ensure results are retuned in an array
- if rval.has_key('items'):
- rval['results'] = rval['items']
- elif not isinstance(rval['results'], list):
- rval['results'] = [rval['results']]
-
- return rval
+ return self.get('secrets', self.name)
def delete_secret(self):
'''return all pods '''
- return self.oc_cmd(['delete', 'secrets', self.name, '-n', self.namespace])
+ return self.delete('secrets', self.name)
def secret_new(self, files=None, contents=None):
'''Create a secret with all pods '''
@@ -243,13 +270,9 @@ class Secret(OpenShiftCLI):
with open(sfile_path, 'w') as sfd:
sfd.write(json.dumps(secret['results']))
- cmd = ['replace', '-f', sfile_path]
- if force:
- cmd = ['replace', '--force', '-f', sfile_path]
-
atexit.register(Utils.cleanup, [sfile_path])
- return self.oc_cmd(cmd)
+ return self.replace(sfile_path, force=force)
def prep_secret(self, files=None, contents=None):
''' return what the secret would look like if created