-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tintri_1_1.py
270 lines (212 loc) · 9.29 KB
/
tintri_1_1.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Tintri, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import sys
import json
import requests
import urllib3
# disable security warnings
requests.packages.urllib3.disable_warnings()
"""
Python functions to assist with Tintri API calls for the explict purpose
of supporting Tintri's python examples.
1.1 - added exceptions
This library was NOT designed to be a general purpose Python library.
"""
API = "/api"
# Exception class for requests errors
class TintriRequestsException(Exception):
def __init__(self, *args):
self._message = args[0]
def __str__(self):
return self._message
# Exception class for API errors
class TintriApiException(Exception):
def __init__(self, *args):
self._message = args[0]
self.status_code = args[1]
self.url = args[2]
self.payload = args[3]
self.response = args[4]
def __str__(self):
return "%s status code=%d url:%s payload:%s response:%s" % \
(self._message, self.status_code, self.url, self.payload, self.response)
# API GET without query string. The session ID can be 'None'. This is for
# the info API.
def api_get(server_name, api, session_id=None):
return api_get_query(server_name, api, None, session_id)
# API GET with query string. The query and session ID can be 'None'.
# The requests get allows for a query params set to 'None'.
def api_get_query(server_name, api, query, session_id):
headers = {'content-type': 'application/json'}
if session_id is not None:
headers['cookie'] = 'JSESSIONID=' + session_id
url = 'https://' + server_name + API + api
try:
# Invoke the API.
r = requests.get(url, headers=headers, params=query, verify=False)
except requests.ConnectionError:
raise TintriRequestsException("GET: API Connection error occurred.")
except requests.HTTPError:
raise TintriRequestsException("HTTP error occurred.")
except requests.Timeout:
raise TintriRequestsException("Request timed out.")
except:
raise TintriRequestsException("An unexpected error " + sys.exc_info()[0] + " occurred.")
# if HTTP Response is not 200 then raise an exception
if r.status_code != 200:
message = "The HTTP response for get call to the server is not 200."
if (query is None):
raise TintriApiException(message, r.status_code, url, "No Payload", r.text)
else:
raise TintriApiException(message, r.status_code, url, query, r.text)
return r
# API DELETE.
def api_delete(server_name, api, session_id):
#Header and URL for delete call
headers = {'content-type': 'application/json',
'cookie': 'JSESSIONID='+session_id }
url = 'https://' + server_name + API + api
try:
# Invoke the API.
r = requests.delete(url, headers=headers, verify=False)
except requests.ConnectionError:
raise TintrRequestsiApiException("API Connection error occurred.")
except requests.HTTPError:
raise TintriRequestsException("HTTP error occurred.")
except requests.Timeout:
raise TintriRequestsException("Request timed out.")
except:
raise TintriRequestsException("An unexpected error " + sys.exc_info()[0] + " occurred.")
return r
# PUT
def api_put(server_name, api, payload, session_id):
headers = {'content-type': 'application/json',
'cookie': 'JSESSIONID='+session_id }
url = 'https://' + server_name + API + api
try:
# Invoke the API.
r = requests.put(url, data=json.dumps(payload),
headers=headers, verify=False)
except requests.ConnectionError:
raise TintriRequestsException("API Connection error occurred.")
except requests.HTTPError:
raise TintriRequestsException("HTTP error occurred.")
except requests.Timeout:
raise TintriRequestsException("Request timed out.")
except:
raise TintriRequestsException("An unexpected error " + sys.exc_info()[0] + " occurred.")
return r
# POST
def api_post(server_name, api, payload, session_id):
headers = {'content-type': 'application/json',
'cookie': 'JSESSIONID='+session_id }
url = 'https://' + server_name + API + api
try:
# Invoke the API.
r = requests.post(url, data=json.dumps(payload),
headers=headers, verify=False)
except requests.ConnectionError:
raise TintriRequestsException("API Connection error occurred.")
except requests.HTTPError:
raise TintriRequestsException("HTTP error occurred.")
except requests.Timeout:
raise TintriRequestsException("Request timed out.")
except:
raise TintriRequestsException("An unexpected error " + sys.exc_info()[0] + " occurred.")
return r
# Login.
def api_login(server_name, user_name, password):
# Payload, header and URL for login call
headers = {'content-type': 'application/json'}
payload = {'username': user_name,
'password': password,
'typeId': 'com.tintri.api.rest.vcommon.dto.rbac.RestApiCredentials'}
url_login = 'https://'+ server_name + API + '/v310/session/login'
try:
# Invoke the login API.
r = requests.post(url_login, data=json.dumps(payload),
headers=headers, verify=False)
except requests.ConnectionError:
raise TintriRequestsException("Login: API Connection error occurred.")
except requests.HTTPError:
raise TintriRequestsException("Login: HTTP error occurred.")
except requests.Timeout:
raise TintriRequestsException("Login: Request timed out.")
except:
raise TintriRequestsException("Login: An unexpected error " + sys.exc_info()[0] +
" occurred.")
# if HTTP Response is not 200 then raise an exception
if r.status_code != 200:
message = "The HTTP response for login call to the server is not 200."
raise TintriApiException(message, r.status_code, url_login, str(payload), r.text)
session_id = r.cookies['JSESSIONID']
return session_id
# Logout
def api_logout(server_name, session_id):
#Header and URL for logout call
headers = {'content-type': 'application/json',
'cookie': 'JSESSIONID='+session_id }
url_logout = 'https://' + server_name + API + '/v310/session/logout'
try:
# Send the logout request.
r = requests.get(url_logout, headers=headers, verify=False)
except requests.ConnectionError:
raise TintriRequestsException("Logout: API Connection error occurred.")
except requests.HTTPError:
raise TintrRequestsiApiException("Logout: HTTP error occurred.")
except requests.Timeout:
raise TintriRequestsException("Logout: Request timed out.")
except:
raise TintriRequestsException("Logout: An unexpected error " + sys.exc_info()[0] +
" occurred.")
# if HTTP Response is not 204 then raise an exception
if r.status_code != 204:
message = "The HTTP response for logout call to the server is not 204."
raise TintriApiException(message, r.status_code, url_logout, "No Payload", r.text)
return
# Return API version information
def api_version(server_name):
r = api_get(server_name, '/info')
return r
# Download a file
def download_file(server_name, report_url, session_id, file_name):
headers = {'content-type': 'application/json'}
try:
r = requests.get(report_url, headers=headers, verify=False, stream=True)
# if HTTP Response is not 200 then raise an exception
if r.status_code != 200:
message = "The HTTP response for get call to the server is not 200."
raise TintriApiException(message, r.status_code, report_url, "No Payload", r.text)
with open(file_name, 'w') as file_h:
for block in r.iter_content(4096):
file_h.write(block)
except requests.ConnectionError:
raise TintriRequestsException("API Connection error occurred.")
except requests.HTTPError:
raise TintriRequestsException("HTTP error occurred.")
except requests.Timeout:
raise TintriRequestsException("Request timed out.")
except Exception as e:
raise TintriRequestsException("An unexpected error: " + e.__str__())