Skip to content

Commit 4100c91

Browse files
committed
Updates and new scripts
List_PGSnaps - lists protection group snapshots Pure_List_PGs - lists protection groups using purestorage module
1 parent 16093da commit 4100c91

File tree

3 files changed

+499
-89
lines changed

3 files changed

+499
-89
lines changed

List_PGSnaps.py

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

0 commit comments

Comments
 (0)