-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoscale.py
156 lines (136 loc) · 5.14 KB
/
coscale.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
156
'''
Send events to CoScale API
==========================
This state is useful for creating and sending events to CoScale API during state runs.
.. code-block:: yaml
coscale-event:
coscale.event:
- baseurl: https://api.coscale.com/
- accesstoken: 42c59aad-40b6-4924-9710-2d1d4a0fb632
- appid: 00006e0e-5d0c-4633-abce-1e424f767a03
- event_name: 'Software updates'
- event_message: 'Updating to version 2.2'
- event_timestamp: 0 #optional
'''
import json
import requests
def _login(accesstoken, url):
'''
Authentication using the Access token to get a HTTPAuthentication token from the CoScale API.
The following parameters are required:
accessToken
The accessToken is used to login to the API
url
The url we need to access in order to get the token
Return the HTTPAuthentication token.
'''
data = {'accessToken': accesstoken}
req = requests.post(url, data=data, timeout=1)
if req.status_code != 200:
raise AttributeError(req.text)
response = json.loads(req.text)
return response['token']
def _eventpush(name, token, url):
'''
Create an event using the event name.
The following parameters are required:
name
The event name
token
HTTPAuthentication token used for authentication
url
We create a POST request to this url to create an event
Return request if there was an error or the event id if the request succeed
'''
data = {'name': name,
'description': '',
'type': '',
'source': 'SALT',
'icon': 'salt'}
headers = {'HTTPAuthorization': token}
req = requests.post(url, data=data, headers=headers, timeout=1)
if req.status_code == 409 or req.status_code == 200:
response = json.loads(req.text)
return (None, response["id"])
return (req, None)
def _eventdatapush(message, timestamp, token, url):
'''
Push event data using message and timestamp.
The following parameters are required:
message
The actual message
timestamp
Unix timestamp in seconds
token
HTTPAuthentication token used for authentication
url
We create a POST request to this url to push event message
Return request response if the request failed or None if succeed.
'''
data = {
'message': message,
'timestamp': timestamp,
'subject': 'a'}
headers = {'HTTPAuthorization': token}
req = requests.post(url, data=data, headers=headers, timeout=1)
if req.status_code != 200:
return req
return None
def event(baseurl, accesstoken, appid, event_name, event_message, timestamp=0):
'''
Deals with login, event creation and event data pushing.
.. code-block:: yaml
coscale-event:
coscale.event:
- baseurl: https://api.coscale.com/
- accessToken: 42c59aad-40b6-4924-9710-1e424f767a03
- appid: 00c59e0e-5d0c-4633-abce-1e4924767a03
- event_name: 'Software updates'
- event_message: 'Updating to version 2.2'
- event_timestamp: 0
The following parameters are required:
baseurl
The url used to create login url, post event url and post data url
accesstoken
The accessToken is used to login to the API
appid
The appid (uuid) used for API connection
event_name
The name of the event, this will appear in the CoScale interface
event_message
The message of the event, this will appear in the CoScale interface
The following parameter is optional:
event_timestamp
Unix timestamp in seconds. Default is ``0``
Return whether the event was successfully sent to the CoScale API.
'''
ret = {'name': event_name,
'changes': {},
'result': False,
'comment': ''}
baseurl = baseurl + 'api/v1/app/' + appid + '/'
try:
token = _login(accesstoken, baseurl + 'login/')
err, event_id = _eventpush(event_name, token, url=baseurl + 'events/')
if err is not None:
if err.status_code == 401:
token = _login(accesstoken, baseurl + 'login/')
err, event_id = _eventpush(name=event_name, token=token, url=baseurl + 'events/')
if err.status_code not in [401, None]:
ret['comment'] = err.text
return ret
url = baseurl + 'events/' + str(event_id) + '/data/'
err = _eventdatapush(event_message, timestamp, token, url=url)
if err is not None:
if err.status_code == 401:
token = _login(accesstoken, baseurl + 'login/')
err = _eventdatapush(event_message, timestamp, token, url=url)
if err.status_code not in [401, None]:
ret['comment'] = err.text
return ret
ret['result'] = True
ret['comment'] = 'Sent event: {0}'.format(event_name)
return ret
except AttributeError as err:
ret['comment'] = err
return ret