|
| 1 | +import purestorage |
| 2 | +import requests |
| 3 | +from requests.packages.urllib3.exceptions import InsecureRequestWarning |
| 4 | +requests.packages.urllib3.disable_warnings(InsecureRequestWarning) |
| 5 | +from base64 import b64encode |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import json |
| 9 | +import getpass |
| 10 | +from optparse import OptionParser |
| 11 | +from datetime import datetime, timedelta |
| 12 | +import time |
| 13 | +from time import gmtime, strftime, strptime |
| 14 | +from operator import itemgetter, attrgetter |
| 15 | + |
| 16 | +# Global Variables |
| 17 | +VERSION = '1.0.0' |
| 18 | +HEADER = 'Pure Storage Create Protection Group Snapshot (' + VERSION + ')' |
| 19 | +BANNER = ('=' * 132) |
| 20 | +DEBUG_LEVEL = 0 |
| 21 | +VERBOSE_FLAG = False |
| 22 | +COOKIE = '' |
| 23 | + |
| 24 | +def create_session(flashArray, user, password): |
| 25 | + |
| 26 | + jsonData = purestorage.FlashArray(flashArray, user, password) |
| 27 | + return(jsonData) |
| 28 | + |
| 29 | +def parsecl(): |
| 30 | + usage = 'usage: %prog [options]' |
| 31 | + version = '%prog ' + VERSION |
| 32 | + description = "This application has been developed using Pure Storage v1.12 RESTful Web Service interfaces. Developed and tested using Python 3.6 on Mac OS 10.12. Please contact ron@purestorage.com for assistance." |
| 33 | + |
| 34 | + parser = OptionParser(usage=usage, version=version, description=description) |
| 35 | + |
| 36 | + parser.add_option('-d', '--debug', |
| 37 | + type = 'int', |
| 38 | + dest = 'DEBUG_LEVEL', |
| 39 | + default = 0, |
| 40 | + help = 'Debug level, used for HTTP debugging') |
| 41 | + |
| 42 | + parser.add_option('-p', '--password', |
| 43 | + action = 'store', |
| 44 | + type = 'string', |
| 45 | + dest = 'password', |
| 46 | + help = 'Pure password') |
| 47 | + |
| 48 | + parser.add_option('-P', '--pgroup', |
| 49 | + action = 'store', |
| 50 | + type = 'string', |
| 51 | + dest = 'pgroup', |
| 52 | + default = '', |
| 53 | + help = 'Protection Group') |
| 54 | + |
| 55 | + parser.add_option('-s', '--server', |
| 56 | + action = 'store', |
| 57 | + type = 'string', |
| 58 | + dest = 'flashArray', |
| 59 | + help = 'Pure FlashArray') |
| 60 | + |
| 61 | + parser.add_option('-S', '--suffix', |
| 62 | + action = 'store', |
| 63 | + type = 'string', |
| 64 | + dest = 'pgsuffix', |
| 65 | + help = 'Protection Group Snapshot suffix') |
| 66 | + |
| 67 | + parser.add_option('-u', '--user', |
| 68 | + action = 'store', |
| 69 | + type = 'string', |
| 70 | + dest = 'user', |
| 71 | + help = 'Pure user name') |
| 72 | + |
| 73 | + parser.add_option('-v', '--verbose', |
| 74 | + action = 'store_true', |
| 75 | + dest = 'VERBOSE_FLAG', |
| 76 | + default = False, |
| 77 | + help = 'Verbose [default: %default]') |
| 78 | + |
| 79 | + (options, args) = parser.parse_args() |
| 80 | + |
| 81 | + ''' |
| 82 | + print("Options:", options) |
| 83 | + print("Args:", args) |
| 84 | + ''' |
| 85 | + |
| 86 | + return(options) |
| 87 | + |
| 88 | +def main(): |
| 89 | + # Setup variables |
| 90 | + global DEBUG_LEVEL |
| 91 | + global VERBOSE_FLAG |
| 92 | + exit_code = 0 |
| 93 | + |
| 94 | + # Check for command line parameters |
| 95 | + options = parsecl() |
| 96 | + password = options.password |
| 97 | + user = options.user |
| 98 | + flashArray = options.flashArray |
| 99 | + pgroup = [options.pgroup] |
| 100 | + pgsuffix = options.pgsuffix |
| 101 | + DEBUG_LEVEL = options.DEBUG_LEVEL |
| 102 | + VERBOSE_FLAG = options.VERBOSE_FLAG |
| 103 | + |
| 104 | + if DEBUG_LEVEL != 0: |
| 105 | + print('Password', password) |
| 106 | + print('User', user) |
| 107 | + print('Flash Array', flashArray) |
| 108 | + print('Protection Group', pgroup) |
| 109 | + print('Snapshot Suffix', pgsuffix) |
| 110 | + print('Debug Level:', DEBUG_LEVEL) |
| 111 | + print('Verbose Flag:', VERBOSE_FLAG) |
| 112 | + |
| 113 | + if flashArray == None: |
| 114 | + sys.exit('Exiting: You must provide FlashArray details') |
| 115 | + |
| 116 | + if user and password == None: |
| 117 | + sys.exit('Exiting: You must provide password if using username') |
| 118 | + |
| 119 | + print(BANNER) |
| 120 | + print(HEADER + ' - ' + flashArray) |
| 121 | + print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime())) |
| 122 | + print(BANNER) |
| 123 | + |
| 124 | + # Create session |
| 125 | + array = create_session(flashArray, user, password) |
| 126 | + |
| 127 | + if pgsuffix == None: |
| 128 | + epoch = int(time.time()) |
| 129 | + pgsuffix = 'SNAP-' + str(epoch) |
| 130 | + |
| 131 | + # Create Snapshot |
| 132 | + jsonData= array.create_pgroup_snapshots(pgroup, suffix=pgsuffix) |
| 133 | + |
| 134 | + if VERBOSE_FLAG: |
| 135 | + print(BANNER) |
| 136 | + print(json.dumps(jsonData, sort_keys=False, indent=4)) |
| 137 | + |
| 138 | + name = (jsonData[0]['name']) |
| 139 | + source = (jsonData[0]['source']) |
| 140 | + cdate = (jsonData[0]['created']) |
| 141 | + c1 = cdate[0:10] |
| 142 | + c2 = cdate[11:19] |
| 143 | + c3 = c1 + ' ' + c2 |
| 144 | + c4 = strptime(c3,'%Y-%m-%d %H:%M:%S') |
| 145 | + created = strftime('%d/%m/%Y %H:%M:%S', c4) |
| 146 | + |
| 147 | + print('{0:40} {1:60} {2:20}'.format('Source', 'Name', 'Created')) |
| 148 | + print('{0:40} {1:60} {2:20}'.format(source, name, created)) |
| 149 | + |
| 150 | + # Close API session |
| 151 | + array.invalidate_cookie() |
| 152 | + |
| 153 | + print(BANNER) |
| 154 | + print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime())) |
| 155 | + print(BANNER) |
| 156 | + sys.exit(exit_code) |
| 157 | + |
| 158 | +main() |
| 159 | + |
| 160 | + |
0 commit comments