Browse Source

Merge pull request #1704 from kwoodson/yeditfix

yedit now handles periods in key names
Matt Woodson 9 years ago
parent
commit
9db67b8095

+ 6 - 0
roles/lib_openshift_api/build/ansible/edit.py

@@ -18,6 +18,12 @@ def main():
                       choices=['dc', 'deploymentconfig',
                                'svc', 'service',
                                'scc', 'securitycontextconstraints',
+                               'ns', 'namespace', 'project', 'projects',
+                               'is', 'imagestream',
+                               'istag', 'imagestreamtag',
+                               'bc', 'buildconfig',
+                               'routes',
+                               'node',
                                'secret',
                               ]),
             file_name=dict(default=None, type='str'),

+ 7 - 0
roles/lib_openshift_api/build/ansible/obj.py

@@ -19,6 +19,13 @@ def main():
                       type='str',
                       choices=['dc', 'deploymentconfig',
                                'svc', 'service',
+                               'scc', 'securitycontextconstraints',
+                               'ns', 'namespace', 'project', 'projects',
+                               'is', 'imagestream',
+                               'istag', 'imagestreamtag',
+                               'bc', 'buildconfig',
+                               'routes',
+                               'node',
                                'secret',
                               ]),
             delete_after=dict(default=False, type='bool'),

+ 21 - 7
roles/lib_openshift_api/library/oc_edit.py

@@ -285,8 +285,8 @@ class YeditException(Exception):
 
 class Yedit(object):
     ''' Class to modify yaml files '''
-    re_valid_key = r"(((\[-?\d+\])|(\w+)).?)+$"
-    re_key = r"(?:\[(-?\d+)\])|(\w+)"
+    re_valid_key = r"(((\[-?\d+\])|([a-zA-Z-./]+)).?)+$"
+    re_key = r"(?:\[(-?\d+)\])|([a-zA-Z-./]+)"
 
     def __init__(self, filename=None, content=None, content_type='yaml'):
         self.content = content
@@ -326,11 +326,13 @@ class Yedit(object):
         if key_indexes[-1][0]:
             if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1:
                 del data[int(key_indexes[-1][0])]
+                return True
 
         # expected dict entry
         elif key_indexes[-1][1]:
             if isinstance(data, dict):
                 del data[key_indexes[-1][1]]
+                return True
 
     @staticmethod
     def add_entry(data, key, item=None):
@@ -447,7 +449,7 @@ class Yedit(object):
         return entry
 
     def delete(self, key):
-        ''' put key, value into a yaml file '''
+        ''' remove key from a dict'''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -455,11 +457,14 @@ class Yedit(object):
         if not entry:
             return  (False, self.yaml_dict)
 
-        Yedit.remove_entry(self.yaml_dict, key)
+        result = Yedit.remove_entry(self.yaml_dict, key)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def put(self, key, value):
-        ''' put key, value into a yaml file '''
+        ''' put key, value into a dict '''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -468,11 +473,14 @@ class Yedit(object):
         if entry == value:
             return (False, self.yaml_dict)
 
-        Yedit.add_entry(self.yaml_dict, key, value)
+        result = Yedit.add_entry(self.yaml_dict, key, value)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def create(self, key, value):
-        ''' create the file '''
+        ''' create a yaml file '''
         if not self.exists():
             self.yaml_dict = {key: value}
             return (True, self.yaml_dict)
@@ -545,6 +553,12 @@ def main():
                       choices=['dc', 'deploymentconfig',
                                'svc', 'service',
                                'scc', 'securitycontextconstraints',
+                               'ns', 'namespace', 'project', 'projects',
+                               'is', 'imagestream',
+                               'istag', 'imagestreamtag',
+                               'bc', 'buildconfig',
+                               'routes',
+                               'node',
                                'secret',
                               ]),
             file_name=dict(default=None, type='str'),

+ 22 - 7
roles/lib_openshift_api/library/oc_obj.py

@@ -285,8 +285,8 @@ class YeditException(Exception):
 
 class Yedit(object):
     ''' Class to modify yaml files '''
-    re_valid_key = r"(((\[-?\d+\])|(\w+)).?)+$"
-    re_key = r"(?:\[(-?\d+)\])|(\w+)"
+    re_valid_key = r"(((\[-?\d+\])|([a-zA-Z-./]+)).?)+$"
+    re_key = r"(?:\[(-?\d+)\])|([a-zA-Z-./]+)"
 
     def __init__(self, filename=None, content=None, content_type='yaml'):
         self.content = content
@@ -326,11 +326,13 @@ class Yedit(object):
         if key_indexes[-1][0]:
             if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1:
                 del data[int(key_indexes[-1][0])]
+                return True
 
         # expected dict entry
         elif key_indexes[-1][1]:
             if isinstance(data, dict):
                 del data[key_indexes[-1][1]]
+                return True
 
     @staticmethod
     def add_entry(data, key, item=None):
@@ -447,7 +449,7 @@ class Yedit(object):
         return entry
 
     def delete(self, key):
-        ''' put key, value into a yaml file '''
+        ''' remove key from a dict'''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -455,11 +457,14 @@ class Yedit(object):
         if not entry:
             return  (False, self.yaml_dict)
 
-        Yedit.remove_entry(self.yaml_dict, key)
+        result = Yedit.remove_entry(self.yaml_dict, key)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def put(self, key, value):
-        ''' put key, value into a yaml file '''
+        ''' put key, value into a dict '''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -468,11 +473,14 @@ class Yedit(object):
         if entry == value:
             return (False, self.yaml_dict)
 
-        Yedit.add_entry(self.yaml_dict, key, value)
+        result = Yedit.add_entry(self.yaml_dict, key, value)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def create(self, key, value):
-        ''' create the file '''
+        ''' create a yaml file '''
         if not self.exists():
             self.yaml_dict = {key: value}
             return (True, self.yaml_dict)
@@ -575,6 +583,13 @@ def main():
                       type='str',
                       choices=['dc', 'deploymentconfig',
                                'svc', 'service',
+                               'scc', 'securitycontextconstraints',
+                               'ns', 'namespace', 'project', 'projects',
+                               'is', 'imagestream',
+                               'istag', 'imagestreamtag',
+                               'bc', 'buildconfig',
+                               'routes',
+                               'node',
                                'secret',
                               ]),
             delete_after=dict(default=False, type='bool'),

+ 15 - 7
roles/lib_openshift_api/library/oc_secret.py

@@ -285,8 +285,8 @@ class YeditException(Exception):
 
 class Yedit(object):
     ''' Class to modify yaml files '''
-    re_valid_key = r"(((\[-?\d+\])|(\w+)).?)+$"
-    re_key = r"(?:\[(-?\d+)\])|(\w+)"
+    re_valid_key = r"(((\[-?\d+\])|([a-zA-Z-./]+)).?)+$"
+    re_key = r"(?:\[(-?\d+)\])|([a-zA-Z-./]+)"
 
     def __init__(self, filename=None, content=None, content_type='yaml'):
         self.content = content
@@ -326,11 +326,13 @@ class Yedit(object):
         if key_indexes[-1][0]:
             if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1:
                 del data[int(key_indexes[-1][0])]
+                return True
 
         # expected dict entry
         elif key_indexes[-1][1]:
             if isinstance(data, dict):
                 del data[key_indexes[-1][1]]
+                return True
 
     @staticmethod
     def add_entry(data, key, item=None):
@@ -447,7 +449,7 @@ class Yedit(object):
         return entry
 
     def delete(self, key):
-        ''' put key, value into a yaml file '''
+        ''' remove key from a dict'''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -455,11 +457,14 @@ class Yedit(object):
         if not entry:
             return  (False, self.yaml_dict)
 
-        Yedit.remove_entry(self.yaml_dict, key)
+        result = Yedit.remove_entry(self.yaml_dict, key)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def put(self, key, value):
-        ''' put key, value into a yaml file '''
+        ''' put key, value into a dict '''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -468,11 +473,14 @@ class Yedit(object):
         if entry == value:
             return (False, self.yaml_dict)
 
-        Yedit.add_entry(self.yaml_dict, key, value)
+        result = Yedit.add_entry(self.yaml_dict, key, value)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def create(self, key, value):
-        ''' create the file '''
+        ''' create a yaml file '''
         if not self.exists():
             self.yaml_dict = {key: value}
             return (True, self.yaml_dict)

+ 4 - 1
roles/lib_yaml_editor/build/ansible/yedit.py

@@ -45,13 +45,16 @@ def main():
 
         if rval:
             rval = yamlfile.put(module.params['key'], value)
+            if rval[0]:
+                yamlfile.write()
             module.exit_json(changed=rval[0], results=rval[1], state="present")
 
         if not module.params['content']:
             rval = yamlfile.create(module.params['key'], value)
         else:
-            yamlfile.write()
             rval = yamlfile.load()
+        yamlfile.write()
+
         module.exit_json(changed=rval[0], results=rval[1], state="present")
 
     module.exit_json(failed=True,

+ 15 - 7
roles/lib_yaml_editor/build/src/yedit.py

@@ -6,8 +6,8 @@ class YeditException(Exception):
 
 class Yedit(object):
     ''' Class to modify yaml files '''
-    re_valid_key = r"(((\[-?\d+\])|(\w+)).?)+$"
-    re_key = r"(?:\[(-?\d+)\])|(\w+)"
+    re_valid_key = r"(((\[-?\d+\])|([a-zA-Z-./]+)).?)+$"
+    re_key = r"(?:\[(-?\d+)\])|([a-zA-Z-./]+)"
 
     def __init__(self, filename=None, content=None, content_type='yaml'):
         self.content = content
@@ -47,11 +47,13 @@ class Yedit(object):
         if key_indexes[-1][0]:
             if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1:
                 del data[int(key_indexes[-1][0])]
+                return True
 
         # expected dict entry
         elif key_indexes[-1][1]:
             if isinstance(data, dict):
                 del data[key_indexes[-1][1]]
+                return True
 
     @staticmethod
     def add_entry(data, key, item=None):
@@ -168,7 +170,7 @@ class Yedit(object):
         return entry
 
     def delete(self, key):
-        ''' put key, value into a yaml file '''
+        ''' remove key from a dict'''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -176,11 +178,14 @@ class Yedit(object):
         if not entry:
             return  (False, self.yaml_dict)
 
-        Yedit.remove_entry(self.yaml_dict, key)
+        result = Yedit.remove_entry(self.yaml_dict, key)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def put(self, key, value):
-        ''' put key, value into a yaml file '''
+        ''' put key, value into a dict '''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -189,11 +194,14 @@ class Yedit(object):
         if entry == value:
             return (False, self.yaml_dict)
 
-        Yedit.add_entry(self.yaml_dict, key, value)
+        result = Yedit.add_entry(self.yaml_dict, key, value)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def create(self, key, value):
-        ''' create the file '''
+        ''' create a yaml file '''
         if not self.exists():
             self.yaml_dict = {key: value}
             return (True, self.yaml_dict)

+ 19 - 8
roles/lib_yaml_editor/library/yedit.py

@@ -29,8 +29,8 @@ class YeditException(Exception):
 
 class Yedit(object):
     ''' Class to modify yaml files '''
-    re_valid_key = r"(((\[-?\d+\])|(\w+)).?)+$"
-    re_key = r"(?:\[(-?\d+)\])|(\w+)"
+    re_valid_key = r"(((\[-?\d+\])|([a-zA-Z-./]+)).?)+$"
+    re_key = r"(?:\[(-?\d+)\])|([a-zA-Z-./]+)"
 
     def __init__(self, filename=None, content=None, content_type='yaml'):
         self.content = content
@@ -70,11 +70,13 @@ class Yedit(object):
         if key_indexes[-1][0]:
             if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1:
                 del data[int(key_indexes[-1][0])]
+                return True
 
         # expected dict entry
         elif key_indexes[-1][1]:
             if isinstance(data, dict):
                 del data[key_indexes[-1][1]]
+                return True
 
     @staticmethod
     def add_entry(data, key, item=None):
@@ -191,7 +193,7 @@ class Yedit(object):
         return entry
 
     def delete(self, key):
-        ''' put key, value into a yaml file '''
+        ''' remove key from a dict'''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -199,11 +201,14 @@ class Yedit(object):
         if not entry:
             return  (False, self.yaml_dict)
 
-        Yedit.remove_entry(self.yaml_dict, key)
+        result = Yedit.remove_entry(self.yaml_dict, key)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def put(self, key, value):
-        ''' put key, value into a yaml file '''
+        ''' put key, value into a dict '''
         try:
             entry = Yedit.get_entry(self.yaml_dict, key)
         except KeyError as _:
@@ -212,11 +217,14 @@ class Yedit(object):
         if entry == value:
             return (False, self.yaml_dict)
 
-        Yedit.add_entry(self.yaml_dict, key, value)
+        result = Yedit.add_entry(self.yaml_dict, key, value)
+        if not result:
+            return (False, self.yaml_dict)
+
         return (True, self.yaml_dict)
 
     def create(self, key, value):
-        ''' create the file '''
+        ''' create a yaml file '''
         if not self.exists():
             self.yaml_dict = {key: value}
             return (True, self.yaml_dict)
@@ -268,13 +276,16 @@ def main():
 
         if rval:
             rval = yamlfile.put(module.params['key'], value)
+            if rval[0]:
+                yamlfile.write()
             module.exit_json(changed=rval[0], results=rval[1], state="present")
 
         if not module.params['content']:
             rval = yamlfile.create(module.params['key'], value)
         else:
-            yamlfile.write()
             rval = yamlfile.load()
+        yamlfile.write()
+
         module.exit_json(changed=rval[0], results=rval[1], state="present")
 
     module.exit_json(failed=True,