Skip to content

Commit 7ef6632

Browse files
authored
multi-function script
./rest_interface.py [ -a | --array ] <array_name> [ -p | --password ] <api token> [ -f | --function ] <function name> [-v | --verbose] where <function name> can be: list_luns [-l | --lun_name] <comma separated list of lun names (no spaces!)> list_hosts [-h | --host_name] <comma separated list of hostnames (no spaces!)> list_hostgroups create_lun [-l | --lun_name] <lun name> [-s | --lun_size] <lun size> where <lun size> can be in G or T (eg. 1T, 20G) create_host [ -h | --host_name ] <hostname> [ -w | --wwn ] <comma separated list of WWNs (no spaces!)> create_hostgroup [ -g | --group_name ] <hostgroup_name> add_host_to_hostgroup [ -h | --host_name ] <comma separated list of hostnames (no spaces!)> [ -g | --group_name ] <hostgroup_name> add_lun_to_hostgroup [ -l | --lun_name ] <lun name> [ -g | --group_name ] <hostgroup_name> add_lun_to_host [ -l | --lun_name ] <lun_na
1 parent a22750c commit 7ef6632

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed

rest_interface.py

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
#!/bin/python
2+
3+
import sys, getopt, purestorage, json
4+
5+
def usage():
6+
print '{} [ -a | --array ] <array_name> [ -p | --password ] <api token> [ -f | --function ] <function name> [-v | --verbose]'.format(sys.argv[0])
7+
print 'where <function name> can be:'
8+
print ' list_luns [-l | --lun_name] <comma separated list of lun names (no spaces!)>'
9+
print ' list_hosts [-h | --host_name] <comma separated list of hostnames (no spaces!)>'
10+
print ' list_hostgroups'
11+
print ''
12+
print ' create_lun [-l | --lun_name] <lun name> [-s | --lun_size] <lun size>'
13+
print ' where <lun size> can be in G or T (eg. 1T, 20G)'
14+
print ' create_host [ -h | --host_name ] <hostname> [ -w | --wwn ] <comma separated list of WWNs (no spaces!)>'
15+
print ' create_hostgroup [ -g | --group_name ] <hostgroup_name>'
16+
print ''
17+
print ' add_host_to_hostgroup [ -h | --host_name ] <comma separated list of hostnames (no spaces!)> [ -g | --group_name ] <hostgroup_name>'
18+
print ' add_lun_to_hostgroup [ -l | --lun_name ] <lun name> [ -g | --group_name ] <hostgroup_name>'
19+
print ' add_lun_to_host [ -l | --lun_name ] <lun_name> [ -h | --host_name ] <hostname>'
20+
print ''
21+
print ' remove_host_from_hostgroup [ -h | --host_name ] <comma separated list of hostnames (no spaces!)> [ -g | --group_name ] <hostgroup_name>'
22+
print ' remove_lun_from_host [ -l | --lun_name ] <lun name> [ -h | --host_name ] <hostname>'
23+
print ' remove_lun_from_hostgroup [ -l | --lun_name ] <lun name> [ -g | --group_name ] <hostgroup_name>'
24+
print ''
25+
print ' delete_host [-h --host_name ] <hostname>'
26+
print ' delete_hostgroup [-g | --group_name ] <hostgroup_name>'
27+
print ' delete_lun [ -l | --lun_name ] <lun_name>'
28+
print ''
29+
print '-v | --verbose: will give you more detail relating to that function.'
30+
print ' without "verbose" any successful actions (creates, adds etc) will simply exit with a 0 return code'
31+
sys.exit(2)
32+
33+
def main(argv):
34+
array_name = ''
35+
api_token = ''
36+
verbose = False
37+
try:
38+
opts, args = getopt.getopt(argv,"?h:l:a:p:f:s:vw:g:",["help", "host_name=", "lun_name=", "array=","password=","function=","lun_size=","verbose","wwn=","group_name="])
39+
except:
40+
print str(getopt.GetoptError)
41+
usage()
42+
for opt, arg in opts:
43+
if opt in ("-?", "--help"):
44+
usage()
45+
elif opt in ("-a", "--array"):
46+
array_name = arg
47+
elif opt in ("-p", "--password"):
48+
api_token = arg
49+
elif opt in ("-f", "--function"):
50+
function = arg
51+
elif opt in ("-h", "--host_name"):
52+
host_name = arg
53+
elif opt in ("-w", "--wwn"):
54+
wwn_list = arg
55+
elif opt in ("-l", "--lun_name"):
56+
lun_name = arg
57+
elif opt in ("-g", "--group_name"):
58+
group_name = arg
59+
elif opt in ("-s", "--lun_size"):
60+
lun_size = arg
61+
elif opt in ("-v", "--verbose"):
62+
verbose = True
63+
64+
# If no function is specified then send a usage message
65+
try:
66+
function
67+
except:
68+
print "ERROR: You must specify a function to perform"
69+
usage()
70+
71+
# We need the following to prevent a security warning
72+
import requests
73+
from requests.packages.urllib3.exceptions import InsecureRequestWarning
74+
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
75+
76+
array = purestorage.FlashArray(array_name, api_token=api_token)
77+
#
78+
if function == 'list_luns':
79+
try:
80+
lun_name
81+
except:
82+
response=array.list_volumes()
83+
if verbose:
84+
print("%-28s%-24s%-26s%s" % ("Name", "Size in GB", "Created", "Serial"))
85+
for x in range(len(response)):
86+
print("%-28s%-24s%-26s%s" % (response[x]['name'], response[x]['size']/1024/1024/1024, response[x]['created'], response[x]['serial']))
87+
else:
88+
for x in range(len(response)):
89+
print response[x]['name']
90+
else:
91+
response=array.list_volumes(names=lun_name, connect=True)
92+
if verbose:
93+
print("%-28s%-24s%-26s%s" % ("Name", "Size in GB", "Host Name", "Host Group"))
94+
for x in range(len(response)):
95+
print("%-28s%-24s%-26s%s" % (response[x]['name'], response[x]['size']/1024/1024/1024, response[x]['host'], response[x]['hgroup']))
96+
else:
97+
print "If you're wanting information on specific lun(s) then use the '-v' option"
98+
#
99+
elif function == 'list_hosts':
100+
# if host_name has been passed then let's just get info about this else do all hosts
101+
try:
102+
host_name
103+
except:
104+
response=array.list_hosts(all=True)
105+
else:
106+
response=array.list_hosts(names=host_name, all=True)
107+
if verbose:
108+
print("%-16s%-26s%-26s%-28s" % ("Name", "WWN", "Host Group", "Presented LUN Name"))
109+
for x in range(len(response)):
110+
print("%-16s%-26s%-26s%-28s" % (response[x]['name'], response[x]['host_wwn'], response[x]['hgroup'], response[x]['vol']))
111+
# response[x]['name']=""
112+
# response[x]['hgroup']=""
113+
else:
114+
response=array.list_hosts()
115+
for x in range(len(response)):
116+
print("%-16s%-26s" % (response[x]['name'], response[x]['hgroup']))
117+
### print response[x]['name']
118+
119+
#
120+
elif function == 'list_hostgroups':
121+
response=array.list_hgroups()
122+
if verbose:
123+
print("%-28s%-26s" % ("Name", "Hosts"))
124+
for x in range(len(response)):
125+
for y in range(len(response[x]['hosts'])):
126+
print("%-28s%s" % (response[x]['name'], response[x]['hosts'][y]))
127+
response[x]['name']=""
128+
else:
129+
for x in range(len(response)):
130+
print response[x]['name']
131+
#
132+
elif function == 'create_lun':
133+
try:
134+
response=array.create_volume(lun_name, lun_size)
135+
if verbose:
136+
print("%-28s%-24s%-26s%s" % ("Name", "Size in GB", "Created", "Serial"))
137+
print("%-28s%-24s%-26s%s" % (response['name'], response['size']/1024/1024/1024, response['created'], response['serial']))
138+
sys.exit(0)
139+
except purestorage.PureHTTPError as response:
140+
y=json.loads(response.text)
141+
print "LUN Creation failed: {}: {}".format(lun_name, y[0]['msg'])
142+
sys.exit(1)
143+
#
144+
elif function == 'create_host':
145+
try:
146+
response=array.create_host(host_name)
147+
if verbose:
148+
print "host creation succeeded: {}".format(host_name)
149+
except purestorage.PureHTTPError as response:
150+
y=json.loads(response.text)
151+
print "host creation failed: {}: {}".format(host_name, y[0]['msg'])
152+
sys.exit(1)
153+
try:
154+
wwn_list=wwn_list.split(',')
155+
response=array.set_host(host_name, wwnlist=wwn_list)
156+
if verbose:
157+
print "WWNs added"
158+
for y in range(len(response['wwn'])):
159+
print "{}".format(response['wwn'][y])
160+
sys.exit(0)
161+
except purestorage.PureHTTPError as response:
162+
print response
163+
y=json.loads(response.text)
164+
print "Failed to allocate WWNs to {}: {}".format(host_name, y[0]['msg'])
165+
sys.exit(1)
166+
#
167+
elif function == 'create_hostgroup':
168+
try:
169+
response=array.create_hgroup(group_name)
170+
if verbose:
171+
print "hostgroup creation succeeded: {}".format(group_name)
172+
sys.exit(0)
173+
except purestorage.PureHTTPError as response:
174+
y=json.loads(response.text)
175+
print "hostgroup creation failed: {}: {}".format(group_name, y[0]['msg'])
176+
sys.exit(1)
177+
#
178+
elif function == 'add_host_to_hostgroup':
179+
try:
180+
host_list=host_name.split(',')
181+
response=array.set_hgroup(group_name, addhostlist=host_list)
182+
if verbose:
183+
print "Succesfully added {} to hostgroup {}".format(host_list, group_name)
184+
sys.exit(0)
185+
except purestorage.PureHTTPError as response:
186+
y=json.loads(response.text)
187+
print "Adding {} to hostgroup {} failed: {}".format(host_list, group_name, y[0]['msg'])
188+
sys.exit(1)
189+
#
190+
elif function == 'add_lun_to_hostgroup':
191+
try:
192+
response=array.connect_hgroup(group_name, lun_name)
193+
if verbose:
194+
print "Succesfully added lun {} to hostgroup {}".format(lun_name, group_name)
195+
sys.exit(0)
196+
except purestorage.PureHTTPError as response:
197+
y=json.loads(response.text)
198+
print "Adding lun {} to hostgroup {} failed: {}".format(lun_name, group_name, y[0]['msg'])
199+
sys.exit(1)
200+
#
201+
elif function == 'add_lun_to_host':
202+
try:
203+
response=array.connect_host(host_name, lun_name)
204+
if verbose:
205+
print "Succesfully added lun {} to host {}: lun={}".format(lun_name, host_name, response['lun'])
206+
sys.exit(0)
207+
except purestorage.PureHTTPError as response:
208+
y=json.loads(response.text)
209+
print "Adding lun {} to host {} failed: {}".format(lun_name, host_name, y[0]['msg'])
210+
sys.exit(1)
211+
#
212+
elif function == 'delete_host':
213+
try:
214+
response=array.delete_host(host_name)
215+
if verbose:
216+
print "Succesfully deleted host {}".format(host_name)
217+
sys.exit(0)
218+
except purestorage.PureHTTPError as response:
219+
y=json.loads(response.text)
220+
print "Deletion of host {} failed: {}".format(host_name, y[0]['msg'])
221+
sys.exit(1)
222+
#
223+
elif function == 'delete_hostgroup':
224+
try:
225+
response=array.delete_hgroup(group_name)
226+
if verbose:
227+
print "Succesfully deleted hostgroup {}".format(group_name)
228+
sys.exit(0)
229+
except purestorage.PureHTTPError as response:
230+
y=json.loads(response.text)
231+
print "Deletion of hostgroup {} failed: {}".format(group_name, y[0]['msg'])
232+
sys.exit(1)
233+
#
234+
elif function == 'delete_lun':
235+
try:
236+
response=array.destroy_volume(lun_name)
237+
if verbose:
238+
print "Succesfully deleted volume {}".format(lun_name)
239+
sys.exit(0)
240+
except purestorage.PureHTTPError as response:
241+
y=json.loads(response.text)
242+
print "Deletion of lun {} failed: {}".format(lun_name, y[0]['msg'])
243+
sys.exit(1)
244+
try:
245+
response=array.eradicate_volume(lun_name)
246+
if verbose:
247+
print "Succesfully eradicated volume {}".format(lun_name)
248+
sys.exit(0)
249+
except purestorage.PureHTTPError as response:
250+
y=json.loads(response.text)
251+
print "eradication of lun {} failed: {}".format(lun_name, y[0]['msg'])
252+
sys.exit(1)
253+
#
254+
elif function == 'remove_host_from_hostgroup':
255+
try:
256+
host_list=host_name.split(',')
257+
response=array.set_hgroup(group_name, remhostlist=host_list)
258+
if verbose:
259+
print "Succesfully removed () from hostgroup {}".format(host_list, group_name)
260+
sys.exit(0)
261+
except purestorage.PureHTTPError as response:
262+
y=json.loads(response.text)
263+
print "Removal of hosts {} from hostgroup {} failed: {}".format(host_list, group_name, y[0]['msg'])
264+
sys.exit(1)
265+
#
266+
### print ' remove_lun_from_host [ -l | --lun_name ] <lun name> [ -h | --host_name ] <hostname>'
267+
elif function == 'delete_hostgroup':
268+
try:
269+
response=array.delete_hgroup(group_name)
270+
if verbose:
271+
print "Succesfully deleted hostgroup {}".format(group_name)
272+
sys.exit(0)
273+
except purestorage.PureHTTPError as response:
274+
y=json.loads(response.text)
275+
print "Deletion of hostgroup {} failed: {}".format(group_name, y[0]['msg'])
276+
sys.exit(1)
277+
#
278+
### print ' remove_lun_from_hostgroup [ -l | --lun_name ] <lun name> [ -g | --group_name ] <hostgroup_name>'
279+
### disconnect_hgroup(hgroup, volume)
280+
281+
elif function == 'delete_hostgroup':
282+
try:
283+
response=array.delete_hgroup(group_name)
284+
if verbose:
285+
print "Succesfully deleted hostgroup {}".format(group_name)
286+
sys.exit(0)
287+
except purestorage.PureHTTPError as response:
288+
y=json.loads(response.text)
289+
print "Deletion of hostgroup {} failed: {}".format(group_name, y[0]['msg'])
290+
sys.exit(1)
291+
#
292+
293+
#disconnect from API
294+
array.invalidate_cookie()
295+
296+
if __name__ == "__main__":
297+
main(sys.argv[1:])

0 commit comments

Comments
 (0)