-
Notifications
You must be signed in to change notification settings - Fork 9
/
List_Volumes.py
304 lines (235 loc) · 8.71 KB
/
List_Volumes.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from base64 import b64encode
import os
import sys
import json
import getpass
from optparse import OptionParser
from datetime import datetime, timedelta
import time
from time import gmtime, strftime, strptime
from operator import itemgetter, attrgetter
# Global Variables
VERSION = '1.1.0'
DEFAULT_API_VERSION = '1.12'
HEADER = 'Pure Storage List Volumes (' + VERSION + ')'
BANNER = ('=' * 102)
DEBUG_LEVEL = 0
VERBOSE_FLAG = False
COOKIE = ''
def create_session(flashArray, user, password, api_token):
global COOKIE
# Set-up HTTP header
userAgent = 'Jakarta Commons-HttpClient/3.1'
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'Cookie' : COOKIE}
#Establish Session, if no token provide need to create an API token first
if user:
data = {
'password': user,
'username': password
}
params = json.dumps(data)
path = '/api' + API_VERSION + '/auth/apitoken'
url = 'https://%s%s'%(flashArray,path)
# Perform action
response = requests.post(url, params, headers=hdrs, verify=False)
COOKIE = response.cookies
if DEBUG_LEVEL == 2:
print('Status', response.status_code)
print('Reason', response.reason)
print('Text', response.text)
print('Data', response.json)
print('HTTP Header:', response.headers)
print('Cookie', COOKIE)
print('')
if (response.reason) != 'OK':
print(BANNER)
sys.exit('Exiting: invalid username / password combination')
jsonString = response.text
jsonData = json.loads(jsonString)
api_token = (jsonData['api_token'])
data = {
'api_token': api_token
}
params = json.dumps(data)
path = '/api/'+ API_VERSION + '/auth/session'
url = 'https://%s%s'%(flashArray,path)
# Perform action
print('Attempting to create session')
response = requests.post(url, params, headers=hdrs, verify=False)
COOKIE = response.cookies
if DEBUG_LEVEL == 2:
print('Status', response.status_code)
print('Reason', response.reason)
print('Text', response.text)
print('Data', response.json)
print('HTTP Header:', response.headers)
print('Cookie', COOKIE)
print('')
if (response.reason) != 'OK':
print(BANNER)
sys.exit('Exiting: Unable to establish session')
jsonString = response.text
jsonData = json.loads(jsonString)
if VERBOSE_FLAG:
print(BANNER)
print(json.dumps(jsonData, sort_keys=False, indent=4))
name = (jsonData['username'])
print('Welcome', name)
def post_url(flashArray,path,params):
# Set-up HTTP header
userAgent = 'Jakarta Commons-HttpClient/3.1'
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent}
url = 'https://%s%s'%(flashArray,path)
# Perform action
response = requests.post(url, params, headers=hdrs, cookie=COOKIE, verify=False)
if DEBUG_LEVEL != 0:
print('Response Status:', response.status_code)
print('Reason:', response.reason)
print('Text', response.text)
print('Data', response.json)
print('HTTP Header:', response.headers)
print('Cookie', COOKIE)
print('')
jsonString = response.text
jsonData = json.loads(jsonString)
return(jsonData)
def get_url(flashArray,path,params):
# Set-up HTTP header
userAgent = 'Jakarta Commons-HttpClient/3.1'
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent}
url = 'https://%s%s'%(flashArray,path)
payload = params
# Perform action
response = requests.get(url, headers=hdrs, cookies=COOKIE, verify=False)
if DEBUG_LEVEL != 0:
print('Response Status:', response.status_code)
print('Reason:', response.reason)
print('Text', response.text)
print('Data', response.json)
print('HTTP Header:', response.headers)
print('Cookie:', COOKIE)
jsonString = response.text
jsonData = json.loads(jsonString)
return(jsonData)
def list_volumes(flashArray):
data = ''
params = json.dumps(data)
path = '/api/' + API_VERSION + '/volume?space=true'
# Perform action
jsonData = get_url(flashArray,path,params)
if VERBOSE_FLAG:
print(BANNER)
print(json.dumps(jsonData, sort_keys=False, indent=4))
# Count of returned rows
res = len(jsonData)
if res == 0:
print('No Volumes found')
else:
x = 0
print(BANNER)
print('{0:60} {1:>20} {2:>20}'.format('Volume Name', 'Size (GB)', 'Used (GB)'))
print(BANNER)
while (x<res):
#
name = (jsonData[x]['name'])
size = (jsonData[x]['size'])
used = (jsonData[x]['volumes'])
sz = float(size)
us = float(used)
sizegb = round((sz/1024/1024/104),2)
usedgb = round((us/1024/1024/104),2)
print('{0:60} {1:20} {2:20}'.format(name, sizegb, usedgb))
x = x + 1
def parsecl():
usage = 'usage: %prog [options]'
version = '%prog ' + VERSION
description = "Please contact ron@purestorage.com for any assistance."
parser = OptionParser(usage=usage, version=version, description=description)
parser.add_option('-d', '--debug',
type = 'int',
dest = 'DEBUG_LEVEL',
default = 0,
help = 'Debug level, used for HTTP debugging')
parser.add_option('-p', '--password',
action = 'store',
type = 'string',
dest = 'password',
help = 'Pure password')
parser.add_option('-s', '--server',
action = 'store',
type = 'string',
dest = 'flashArray',
help = 'Pure FlashArray')
parser.add_option('-t', '--token',
action = 'store',
type = 'string',
dest = 'api_token',
help = 'Pure Api Token')
parser.add_option('-V', '--apiversion',
action = 'store',
type = 'string',
dest = 'API_VERSION',
help = 'Pure FlashArray API Version')
parser.add_option('-u', '--user',
action = 'store',
type = 'string',
dest = 'user',
help = 'Pure user name')
parser.add_option('-v', '--verbose',
action = 'store_true',
dest = 'VERBOSE_FLAG',
default = False,
help = 'Verbose [default: %default]')
(options, args) = parser.parse_args()
'''
print("Options:", options)
print("Args:", args)
'''
if options.api_token and options.user:
parser.error('options --token and --user are mutually exclusive')
return(options)
def main():
# Setup variables
global DEBUG_LEVEL
global VERBOSE_FLAG
global API_VERSION
exit_code = 0
# Check for command line parameters
options = parsecl()
password = options.password
user = options.user
flashArray = options.flashArray
api_token = options.api_token
DEBUG_LEVEL = options.DEBUG_LEVEL
VERBOSE_FLAG = options.VERBOSE_FLAG
API_VERSION = options.API_VERSION
if DEBUG_LEVEL != 0:
print('Password', password)
print('User', user)
print('Flash Array', flashArray)
print('Api Token', api_token)
print('Debug Level:', DEBUG_LEVEL)
print('Verbose Flag:', VERBOSE_FLAG)
if flashArray == None:
sys.exit('Exiting: You must provide FlashArray details')
if API_VERSION == None:
API_VERSION = DEFAULT_API_VERSION
if api_token == None and user == None:
sys.exit('Exiting: You must provide either API Token details or username and password')
if user and password == None:
sys.exit('Exiting: You must provide password if using username')
print(BANNER)
print(HEADER + ' - ' + flashArray)
print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime()))
print(BANNER)
# Create session
create_session(flashArray, user, password, api_token)
list_volumes(flashArray)
print(BANNER)
print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime()))
print(BANNER)
sys.exit(exit_code)
main()