This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRubrikSession.py
230 lines (196 loc) · 8.37 KB
/
RubrikSession.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
import requests, json, sys
from requests.auth import HTTPBasicAuth
class RubrikSession:
class RubrikException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
def __init__(self, mgmt_ip, user, password):
#Prompt for configuration info
self.mgmt_ip = mgmt_ip
self.baseurl = "https://" + self.mgmt_ip + "/api/v1/"
self.internal_baseurl = "https://" + self.mgmt_ip + "/api/internal/"
self.user = user
self.password = password
#Disable ssl warnings for Requests
requests.packages.urllib3.disable_warnings()
#attempt login
self.token = self.login(self.user, self.password)
self.headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer ' + self.token}
def login(self, user, password):
uri = self.baseurl + "session"
data = {'username':user,'password':password}
self.auth = HTTPBasicAuth(user,password)
try:
r = requests.post(uri, data=json.dumps(data), verify=False, auth=self.auth)
r.raise_for_status()
except requests.exceptions.ConnectionError as e:
print e
print "\nFriendly Version: Connection Error - Are you sure this is a valid Rubrik management IP address?"
sys.exit(1)
except requests.exceptions.HTTPError as e:
response = r.json()
if response.has_key('message'):
print response['message']
sys.exit(1)
response = r.json()
print "Rubrik connection successful and session established."
return response['token']
def register_host(self, hostname):
uri = self.baseurl + "host"
data = {'hostname': hostname}
try:
r = requests.post(uri, data=json.dumps(data), verify=False, headers=self.headers)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("HOST Registration Failed: " + response['message'])
response = r.json()
print "Successfully Added HOST: " + hostname
return response
def get_all_hosts(self):
all_hosts = self.get_call("host?primary_cluster_id=local")
return all_hosts
def get_vm(self, vm_name=""):
uri = self.baseurl + 'vmware/vm'
if vm_name is not None:
params = {'limit': 9999, 'name': vm_name}
else:
params = {'limit': 9999}
try:
r = requests.get(uri, params=params, verify=False, headers=self.headers)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("GET VM failed: " + response['message'])
response = r.json()
return response
def get_vm_details(self, id):
uri = self.baseurl + 'vmware/vm/' + id
try:
r = requests.get(uri, verify=False, headers=self.headers)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("GET VM failed: " + response['message'])
response = r.json()
return response
def get_vmdk_details(self, vmdk_id):
uri = self.baseurl + 'vmware/vm/virtual_disk/' + vmdk_id
try:
r = requests.get(uri, verify=False, headers=self.headers)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("GET VMDK failed: " + response['message'])
response = r.json()
return response
def update_vmdk(self, vmdk_id, params):
uri = self.baseurl + 'vmware/vm/virtual_disk/' + vmdk_id
try:
data = params
r = requests.patch(uri, data=json.dumps(data), verify=False, headers=self.headers)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("GET VMDK failed: " + response['message'])
response = r.json()
return response
def get_call(self,call,params={},internal=False):
if internal:
uri = self.internal_baseurl + call
else:
uri = self.baseurl + call
try:
r = requests.get(uri, params=json.dumps(params), verify=False, headers=self.headers)
r.raise_for_status()
except requests.RequestException as e:
print e
raise self.RubrikException("GET call Failed: " + str(e))
except (requests.exceptions.HTTPError, requests.exceptions.RequestException) as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("Call Failed: " + response['message'])
response = r.json()
return response
def patch_call(self, call, data):
uri = self.baseurl + call
try:
r = requests.patch(uri, data=data, verify=False, auth=self.auth)
r.raise_for_status()
except requests.RequestException as e:
print e
raise self.RubrikException("PATCH call failed: " + str(e))
except (requests.exceptions.HTTPError, requests.exceptions.RequestException) as e:
print r
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("Call Failed: " + response['message'])
sys.exit(1)
def post_call(self, call, data, internal=False):
uri = self.baseurl + call
if internal:
uri = self.internal_baseurl + call
else:
uri = self.baseurl + call
try:
r = requests.post(uri, data=data, verify=False, auth=self.auth)
r.raise_for_status()
except requests.RequestException as e:
print e
raise self.RubrikException("POST Call Failed: " + str(e))
except (requests.exceptions.HTTPError, requests.exceptions.RequestException) as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("Call Failed: " + response['message'])
sys.exit(1)
def delete_call(self,call,params={},internal=False):
if internal:
uri = self.internal_baseurl + call
else:
uri = self.baseurl + call
try:
r = requests.delete(uri, params=json.dumps(params), verify=False, headers=self.headers)
r.raise_for_status()
except requests.RequestException as e:
print e
raise self.RubrikException("DELETE call Failed: " + str(e))
except (requests.exceptions.HTTPError, requests.exceptions.RequestException) as e:
print e
response = r.json()
if response.has_key('message'):
print response['message']
raise self.RubrikException("Call Failed: " + response['message'])
def get_filesets_for_hostid(self, hostid, params):
fileset_response = self.get_call('fileset', params)
return fileset_response['data']
def get_per_vm_storage_list(self):
per_vm_storage = self.get_call('stats/per_vm_storage', params={}, internal=True)
return per_vm_storage['data']
def get_envision_table(self, report_id, params={}):
table = self.get_call('report/' + report_id + '/table?limit=9999', params=json.dumps(params), internal=True)
return table
def take_snapshot(self, vm_id, params={}):
snapshot = self.post_call('vmware/vm/' + vm_id + '/snapshot', data=json.dumps(params))
return snapshot