Skip to content

Commit 5ad092a

Browse files
committed
Fixed formatting issue
Performed right align on storage headings.
1 parent 890a269 commit 5ad092a

File tree

1 file changed

+323
-0
lines changed

1 file changed

+323
-0
lines changed

List_Volumes.py

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
import urllib.request
2+
import urllib.parse
3+
import http.client
4+
#import purestorage
5+
import requests
6+
from requests.packages.urllib3.exceptions import InsecureRequestWarning
7+
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
8+
from base64 import b64encode
9+
import os
10+
import sys
11+
import json
12+
import getpass
13+
from optparse import OptionParser
14+
from datetime import datetime, timedelta
15+
import time
16+
from time import gmtime, strftime, strptime
17+
from operator import itemgetter, attrgetter
18+
19+
# Global Variables
20+
VERSION = '1.0.0'
21+
HEADER = 'Pure Storage List Volumes (' + VERSION + ')'
22+
BANNER = ('=' * 102)
23+
DEBUG_LEVEL = 0
24+
VERBOSE_FLAG = False
25+
COOKIE = ''
26+
27+
def create_session(flashArray, user, password, api_token):
28+
global COOKIE
29+
30+
# Set-up HTTP header
31+
userAgent = 'Jakarta Commons-HttpClient/3.1'
32+
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'Cookie' : COOKIE}
33+
34+
#Establish Session, if no token provide need to create an API token first
35+
36+
if user:
37+
data = {
38+
'password': user,
39+
'username': password
40+
}
41+
params = json.dumps(data)
42+
path = '/api/1.4/auth/apitoken'
43+
url = 'http://%s%s'%(flashArray,path)
44+
45+
# Perform action
46+
conn = http.client.HTTPSConnection(flashArray)
47+
conn.set_debuglevel(DEBUG_LEVEL)
48+
49+
conn.request('POST', path, params, headers=hdrs)
50+
51+
response = conn.getresponse()
52+
response_data = response.read()
53+
COOKIE = response.getheader('set-cookie')
54+
55+
if DEBUG_LEVEL != 0:
56+
print('Status', response.status)
57+
print('Reason', response.reason)
58+
print('msg', response.msg)
59+
print('HTTP Header:', response.info())
60+
print('Content-Lenghth', response.getheader('Content-Length'))
61+
print('Cookie', COOKIE)
62+
63+
if (response.reason) != 'OK':
64+
print(BANNER)
65+
sys.exit('Exiting: invalid username / password combination')
66+
67+
jsonString = response_data.decode('utf8')
68+
jsonData = json.loads(jsonString)
69+
70+
api_token = (jsonData['api_token'])
71+
72+
'''
73+
print('u', user)
74+
print('p', password)
75+
print('t', api_token)
76+
'''
77+
78+
data = {
79+
'api_token': api_token
80+
}
81+
82+
params = json.dumps(data)
83+
path = '/api/1.4/auth/session'
84+
url = 'http://%s%s'%(flashArray,path)
85+
86+
# Perform action
87+
conn = http.client.HTTPSConnection(flashArray)
88+
conn.set_debuglevel(DEBUG_LEVEL)
89+
90+
conn.request('POST', path, params, headers=hdrs)
91+
92+
response = conn.getresponse()
93+
response_data = response.read()
94+
95+
COOKIE = response.getheader('set-cookie')
96+
97+
if DEBUG_LEVEL != 0:
98+
print('Status', response.status)
99+
print('Reason', response.reason)
100+
print('msg', response.msg)
101+
print('HTTP Header:', response.info())
102+
print('Content-Lenghth', response.getheader('Content-Length'))
103+
print('Cookie', COOKIE)
104+
105+
if (response.reason) != 'OK':
106+
print(BANNER)
107+
sys.exit('Exiting: Unable to establish session')
108+
109+
if VERBOSE_FLAG:
110+
print(BANNER)
111+
print(json.dumps(jsonData, sort_keys=False, indent=4))
112+
113+
jsonString = response_data.decode('utf8')
114+
jsonData = json.loads(jsonString)
115+
116+
name = (jsonData['username'])
117+
118+
#array = purestorage.FlashArray(flashArray, user, password)
119+
120+
print('Welcome', name)
121+
122+
123+
def post_url(flashArray,path,params):
124+
# Set-up HTTP header
125+
userAgent = 'Jakarta Commons-HttpClient/3.1'
126+
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'Cookie' : COOKIE}
127+
128+
conn = http.client.HTTPSConnection(flashArray)
129+
conn.set_debuglevel(DEBUG_LEVEL)
130+
131+
conn.request('POST', path, params, headers=hdrs)
132+
133+
response = conn.getresponse()
134+
response_data = response.read()
135+
136+
if DEBUG_LEVEL != 0:
137+
print('Response Status:', response.status)
138+
print('Reason:', response.reason)
139+
print('msg', response.msg)
140+
print('HTTP Header:', response.info())
141+
print('Content-Lenghth', response.getheader('Content-Length'))
142+
print('Cookie', COOKIE)
143+
144+
jsonData = response_data.decode('utf8')
145+
return(jsonData)
146+
147+
148+
def get_url(flashArray,path,params):
149+
# Set-up HTTP header
150+
userAgent = 'Jakarta Commons-HttpClient/3.1'
151+
hdrs= {'Content-Type' : 'application/json', 'User-agent' : userAgent, 'Cookie' : COOKIE}
152+
153+
conn = http.client.HTTPSConnection(flashArray)
154+
conn.set_debuglevel(DEBUG_LEVEL)
155+
156+
conn.request("GET", path, headers=hdrs)
157+
158+
response = conn.getresponse()
159+
response_data = response.read()
160+
161+
if DEBUG_LEVEL != 0:
162+
print('Response Status:', response.status)
163+
print('Reason:', response.reason)
164+
print('msg', response.msg)
165+
print('HTTP Header:', response.info())
166+
print(response.getheader('Content-Length'))
167+
print('Cookie:', COOKIE)
168+
169+
jsonData = response_data.decode('utf8')
170+
return(jsonData)
171+
172+
173+
def list_volumes(flashArray):
174+
data = ''
175+
params = json.dumps(data)
176+
177+
path = '/api/1.4/volume?space=true'
178+
url = 'http://%s%s'%(flashArray,path)
179+
180+
# Perform action
181+
#jsonData = array.list_volumes()
182+
183+
jsonString = get_url(flashArray,path,params)
184+
jsonData = json.loads(jsonString)
185+
186+
if VERBOSE_FLAG:
187+
print(BANNER)
188+
print(json.dumps(jsonData, sort_keys=False, indent=4))
189+
190+
# Count of returned rows
191+
res = len(jsonData)
192+
193+
if res == 0:
194+
print('No Volumes found')
195+
else:
196+
x = 0
197+
print(BANNER)
198+
print('{0:60} {1:>20} {2:>20}'.format('Volume Name', 'Size (GB)', 'Used (GB)'))
199+
print(BANNER)
200+
while (x<res):
201+
#
202+
name = (jsonData[x]['name'])
203+
size = (jsonData[x]['size'])
204+
used = (jsonData[x]['volumes'])
205+
206+
sz = float(size)
207+
us = float(used)
208+
209+
sizegb = round((sz/1024/1024/104),2)
210+
usedgb = round((us/1024/1024/104),2)
211+
212+
print('{0:60} {1:20} {2:20}'.format(name, sizegb, usedgb))
213+
214+
x = x + 1
215+
216+
217+
def parsecl():
218+
usage = 'usage: %prog [options]'
219+
version = '%prog ' + VERSION
220+
description = "Please contact ron@purestorage.com for any assistance."
221+
222+
parser = OptionParser(usage=usage, version=version, description=description)
223+
224+
225+
parser.add_option('-d', '--debug',
226+
type = 'int',
227+
dest = 'DEBUG_LEVEL',
228+
default = 0,
229+
help = 'Debug level, used for HTTP debugging')
230+
231+
parser.add_option('-p', '--password',
232+
action = 'store',
233+
type = 'string',
234+
dest = 'password',
235+
help = 'Pure password')
236+
237+
parser.add_option('-s', '--server',
238+
action = 'store',
239+
type = 'string',
240+
dest = 'flashArray',
241+
help = 'Pure FlashArray')
242+
243+
parser.add_option('-t', '--token',
244+
action = 'store',
245+
type = 'string',
246+
dest = 'api_token',
247+
help = 'Pure Api Token')
248+
249+
parser.add_option('-u', '--user',
250+
action = 'store',
251+
type = 'string',
252+
dest = 'user',
253+
help = 'Pure user name')
254+
255+
parser.add_option('-v', '--verbose',
256+
action = 'store_true',
257+
dest = 'VERBOSE_FLAG',
258+
default = False,
259+
help = 'Verbose [default: %default]')
260+
261+
(options, args) = parser.parse_args()
262+
263+
'''
264+
print("Options:", options)
265+
print("Args:", args)
266+
'''
267+
268+
if options.api_token and options.user:
269+
parser.error('options --token and --user are mutually exclusive')
270+
271+
return(options)
272+
273+
def main():
274+
# Setup variables
275+
global DEBUG_LEVEL
276+
exit_code = 0
277+
278+
# Check for command line parameters
279+
options = parsecl()
280+
password = options.password
281+
user = options.user
282+
flashArray = options.flashArray
283+
api_token = options.api_token
284+
DEBUG_LEVEL = options.DEBUG_LEVEL
285+
VERBOSE_FLAG = options.VERBOSE_FLAG
286+
287+
if DEBUG_LEVEL != 0:
288+
print('Password', password)
289+
print('User', user)
290+
print('Flash Array', flashArray)
291+
print('Api Token', api_token)
292+
print('Debug Level:', DEBUG_LEVEL)
293+
294+
if flashArray == None:
295+
sys.exit('Exiting: You must provide FlashArray details')
296+
297+
if api_token == None and user == None:
298+
sys.exit('Exiting: You must provide either API Token details or username and password')
299+
300+
if user and password == None:
301+
sys.exit('Exiting: You must provide password if using username')
302+
303+
print(BANNER)
304+
print(HEADER + ' - ' + flashArray)
305+
print(strftime('%Y/%m/%d %H:%M:%S %Z', gmtime()))
306+
print(BANNER)
307+
308+
# Create session
309+
create_session(flashArray, user, password, api_token)
310+
311+
list_volumes(flashArray)
312+
313+
# Close API session
314+
#array.invalidate_cookie()
315+
316+
print(BANNER)
317+
print(strftime('%Y/%m/%d %H:%M:%S %Z', gmtime()))
318+
print(BANNER)
319+
sys.exit(exit_code)
320+
321+
main()
322+
323+

0 commit comments

Comments
 (0)