summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/lib/project.py
blob: 40994741c0c42ce5c56c9ddb14a625772f40a258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# pylint: skip-file
# flake8: noqa


# pylint: disable=too-many-instance-attributes
class ProjectConfig(OpenShiftCLIConfig):
    ''' project config object '''
    def __init__(self, rname, namespace, kubeconfig, project_options):
        super(ProjectConfig, self).__init__(rname, None, kubeconfig, project_options)


class Project(Yedit):
    ''' Class to wrap the oc command line tools '''
    annotations_path = "metadata.annotations"
    kind = 'Project'
    annotation_prefix = 'openshift.io/'

    def __init__(self, content):
        '''Project constructor'''
        super(Project, self).__init__(content=content)

    def get_annotations(self):
        ''' return the annotations'''
        return self.get(Project.annotations_path) or {}

    def add_annotations(self, inc_annos):
        ''' add an annotation to the other annotations'''
        if not isinstance(inc_annos, list):
            inc_annos = [inc_annos]

        annos = self.get_annotations()
        if not annos:
            self.put(Project.annotations_path, inc_annos)
        else:
            for anno in inc_annos:
                for key, value in anno.items():
                    annos[key] = value

        return True

    def find_annotation(self, key):
        ''' find an annotation'''
        annotations = self.get_annotations()
        for anno in annotations:
            if Project.annotation_prefix + key == anno:
                return annotations[anno]

        return None

    def delete_annotation(self, inc_anno_keys):
        ''' remove an annotation from a project'''
        if not isinstance(inc_anno_keys, list):
            inc_anno_keys = [inc_anno_keys]

        annos = self.get(Project.annotations_path) or {}

        if not annos:
            return True

        removed = False
        for inc_anno in inc_anno_keys:
            anno = self.find_annotation(inc_anno)
            if anno:
                del annos[Project.annotation_prefix + anno]
                removed = True

        return removed

    def update_annotation(self, key, value):
        ''' remove an annotation for a project'''
        annos = self.get(Project.annotations_path) or {}

        if not annos:
            return True

        updated = False
        anno = self.find_annotation(key)
        if anno:
            annos[Project.annotation_prefix + key] = value
            updated = True

        else:
            self.add_annotations({Project.annotation_prefix + key: value})

        return updated