summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/class/oc_configmap.py
blob: 87de3e1df6c328b5d353a407251b5eac6b6f68fe (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# pylint: skip-file
# flake8: noqa


# pylint: disable=too-many-arguments
class OCConfigMap(OpenShiftCLI):
    ''' Openshift ConfigMap Class

        ConfigMaps are a way to store data inside of objects
    '''
    def __init__(self,
                 name,
                 from_file,
                 from_literal,
                 state,
                 namespace,
                 kubeconfig='/etc/origin/master/admin.kubeconfig',
                 verbose=False):
        ''' Constructor for OpenshiftOC '''
        super(OCConfigMap, self).__init__(namespace, kubeconfig=kubeconfig, verbose=verbose)
        self.name = name
        self.state = state
        self._configmap = None
        self._inc_configmap = None
        self.from_file = from_file if from_file is not None else {}
        self.from_literal = from_literal if from_literal is not None else {}

    @property
    def configmap(self):
        if self._configmap is None:
            self._configmap = self.get()

        return self._configmap

    @configmap.setter
    def configmap(self, inc_map):
        self._configmap = inc_map

    @property
    def inc_configmap(self):
        if self._inc_configmap is None:
            results = self.create(dryrun=True, output=True)
            self._inc_configmap = results['results']

        return self._inc_configmap

    @inc_configmap.setter
    def inc_configmap(self, inc_map):
        self._inc_configmap = inc_map

    def from_file_to_params(self):
        '''return from_files in a string ready for cli'''
        return ["--from-file={}={}".format(key, value) for key, value in self.from_file.items()]

    def from_literal_to_params(self):
        '''return from_literal in a string ready for cli'''
        return ["--from-literal={}={}".format(key, value) for key, value in self.from_literal.items()]

    def get(self):
        '''return a configmap by name '''
        results = self._get('configmap', self.name)
        if results['returncode'] == 0 and results['results'][0]:
            self.configmap = results['results'][0]

        if results['returncode'] != 0 and '"{}" not found'.format(self.name) in results['stderr']:
            results['returncode'] = 0

        return results

    def delete(self):
        '''delete a configmap by name'''
        return self._delete('configmap', self.name)

    def create(self, dryrun=False, output=False):
        '''Create a configmap

           :dryrun: Product what you would have done. default: False
           :output: Whether to parse output. default: False
        '''

        cmd = ['create', 'configmap', self.name]
        if self.from_literal is not None:
            cmd.extend(self.from_literal_to_params())

        if self.from_file is not None:
            cmd.extend(self.from_file_to_params())

        if dryrun:
            cmd.extend(['--dry-run', '-ojson'])

        results = self.openshift_cmd(cmd, output=output)

        return results

    def update(self):
        '''run update configmap '''
        return self._replace_content('configmap', self.name, self.inc_configmap)

    def needs_update(self):
        '''compare the current configmap with the proposed and return if they are equal'''
        return not Utils.check_def_equal(self.inc_configmap, self.configmap, debug=self.verbose)

    @staticmethod
    # pylint: disable=too-many-return-statements,too-many-branches
    # TODO: This function should be refactored into its individual parts.
    def run_ansible(params, check_mode):
        '''run the ansible idempotent code'''

        oc_cm = OCConfigMap(params['name'],
                            params['from_file'],
                            params['from_literal'],
                            params['state'],
                            params['namespace'],
                            kubeconfig=params['kubeconfig'],
                            verbose=params['debug'])

        state = params['state']

        api_rval = oc_cm.get()

        if 'failed' in api_rval:
            return {'failed': True, 'msg': api_rval}

        #####
        # Get
        #####
        if state == 'list':
            return {'changed': False, 'results': api_rval, 'state': state}

        ########
        # Delete
        ########
        if state == 'absent':
            if not Utils.exists(api_rval['results'], params['name']):
                return {'changed': False, 'state': 'absent'}

            if check_mode:
                return {'changed': True, 'msg': 'CHECK_MODE: Would have performed a delete.'}

            api_rval = oc_cm.delete()

            if api_rval['returncode'] != 0:
                return {'failed': True, 'msg': api_rval}

            return {'changed': True, 'results': api_rval, 'state': state}

        ########
        # Create
        ########
        if state == 'present':
            if not Utils.exists(api_rval['results'], params['name']):

                if check_mode:
                    return {'changed': True, 'msg': 'Would have performed a create.'}

                api_rval = oc_cm.create()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                api_rval = oc_cm.get()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                return {'changed': True, 'results': api_rval, 'state': state}

            ########
            # Update
            ########
            if oc_cm.needs_update():

                api_rval = oc_cm.update()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                api_rval = oc_cm.get()

                if api_rval['returncode'] != 0:
                    return {'failed': True, 'msg': api_rval}

                return {'changed': True, 'results': api_rval, 'state': state}

            return {'changed': False, 'results': api_rval, 'state': state}

        return {'failed': True, 'msg': 'Unknown state passed. {}'.format(state)}