-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
executable file
·155 lines (126 loc) · 4.36 KB
/
update.py
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
#!/usr/bin/env python3
""" Update Rancher app answers using API """
import os
import requests
class RancherAPI: # pylint: disable=too-few-public-methods
""" Make calls to Rancher API """
_CALLER = {
'GET': requests.get,
'PUT': requests.put,
'POST': requests.post,
}
def __init__(self, api, token, check_ssl=True):
self.api = api
self.token = token
self.headers = {
'Authorization': "Bearer %s" % token,
'Accept': 'application/json',
}
self.verify = check_ssl
@staticmethod
def _url_join(*args):
return "/".join([a.strip('/') for a in args])
def call(self, url='', method='get', data=None):
""" Make an API call """
method = method.upper()
req = self._CALLER.get(method)
url = url.replace(self.api, '')
return req(
self._url_join(self.api, url),
headers=self.headers,
json=data,
verify=self.verify
)
def __call__(self, *args, **kwargs):
return self.call(*args, **kwargs)
class App:
""" Represents an application installed inside Rancher """
def __init__(self):
self.ressource_id = ""
self.data = {}
self.name = ""
self.answers = {}
self.links = {}
self.revisionId = ''
self.api: RancherAPI
def update(self):
""" Update the application with new answers """
self.data['answers'] = self.answers
res = self.api(
self.links.get('update'),
method='put',
data=self.data,
)
return res
def merge_answers(self, answers):
""" Merge answers block with that new one """
self.answers.update(answers)
class Project: # pylint: disable=too-few-public-methods
""" Represents a project in Rancher """
def __init__(self):
self.ressource_id = None
self.links = []
self.api: RancherAPI
def app(self, name) -> App:
""" Return Application that have this name """
res = self.api(self.links.get('apps') + '?name=%s' % name)
data = res.json().get('data')[0]
app = App()
app.data = data
app.api = self.api
app.ressource_id = data.get('id')
app.name = data.get('name')
app.answers = data.get('answers')
app.revisionId = data.get('appRevisionId')
app.links = data.get('links')
return app
class Rancher: # pylint: disable=too-few-public-methods
""" Initial Rancher API class to get projects """
def __init__(self, api='', token='', check_ssl='', cluster=''):
self.ressource_id = None
self.links = {}
self.name = cluster
self.api: RancherAPI = RancherAPI(api, token, check_ssl)
self._init_links()
def _init_links(self):
cluster_url = self.api().json().get('links').get('clusters')
print(cluster_url)
res = self.api.call(cluster_url + '?name=' + self.name)
data = res.json().get('data')[0]
self.links = data.get('links')
self.ressource_id = data.get('id')
def project(self, name) -> Project:
""" Return a Project having that name """
call = self.links.get('projects') + '?name=%s' % name
res = self.api.call(call)
data = res.json().get('data')[0]
prj = Project()
prj.ressource_id = data.get('id')
prj.links = data.get('links')
prj.api = self.api
return prj
def __main():
api_url = os.environ.get('PLUGIN_API')
chek_ssl = os.environ.get('PLUGIN_VERIFY', 'true') != 'false'
project_name = os.environ.get('PLUGIN_PROJECT', 'Default')
app_name = os.environ.get('PLUGIN_APP')
cluster_name = os.environ.get('PLUGIN_CLUSTER')
token = os.environ.get('PLUGIN_TOKEN', None)
answer_keys = os.environ.get('PLUGIN_KEYS', None).split(',')
answer_values = os.environ.get('PLUGIN_VALUES', None).split(',')
rancher = Rancher(
cluster=cluster_name,
api=api_url,
token=token,
check_ssl=chek_ssl
)
project = rancher.project(project_name)
app = project.app(app_name)
answers = dict(zip(answer_keys, answer_values))
app.merge_answers(answers)
print(app.answers)
print("Changing answers to", app.answers)
res = app.update()
print(res.json())
if __name__ == '__main__':
__main()