forked from ktelep/EMCUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_cli.py
158 lines (133 loc) · 6.15 KB
/
new_cli.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
#!/usr/bin/python
"""EMC Command Line Interface"""
import sys
import logging
import getpass
from EMCUnity import *
from enum import Enum
from argparse import ArgumentParser
cli = ArgumentParser(description=__doc__)
subparsers = cli.add_subparsers(dest="subcommand")
def argument(*name_or_flags, **kwargs):
"""Convenience function to properly format arguments to pass to the
subcommand decorator.
"""
return name_or_flags, kwargs
def subcommand(args=[], parent=subparsers):
"""Decorator to define a new subcommand in a sanity-preserving way.
The function will be stored in the ``func`` variable when the parser
parses arguments so that it can be called directly like so::
args = cli.parse_args()
args.func(args)
Usage example::
@subcommand([argument("-d", help="Enable debug mode", action="store_true")])
def subcommand(args):
print(args)
Then on the command line::
$ python cli.py subcommand -d
"""
def decorator(func):
parser = parent.add_parser(func.__name__, description=func.__doc__)
for arg in args:
parser.add_argument(*arg[0], **arg[1])
parser.set_defaults(func=func)
return decorator
@subcommand()
def nothing(args):
print("Nothing special!")
@subcommand([argument("-e", "--host", dest='host', default="usdwest01emc", type=str, help="Hostname of EMC Unity Device")])
def hosts(args):
unity = Unity(args.host, 'restapi', 'NceSsa7I3STo!')
hosts = unity.host()
print hosts
@subcommand([argument("-e", "--host", dest='host', default="usdwest01emc", type=str, help="Hostname of EMC Unity Device"),
argument("-f", "--item_filter", dest='item_filter', default=None, type=str, help="filter of specific UnityLUN object"),
argument("-i", "--item_id", dest='item_id', default=None, type=str, help="id of specific UnityLUN object"),
argument("-n", "--item_name", dest='item_name', default=None, type=str, help="Name of specific UnityLUN object")])
def luns(args):
''' Query the existing LUNS'''
unity = Unity(args.host, 'restapi', 'NceSsa7I3STo!')
print unity
print unity.is_auth
luns = unity.lun(item_filter=args.item_filter, item_id=args.item_id, item_name=args.item_name)
for lun in luns:
print lun.name
print lun.health
print lun.id
print lun
#print lun.hostAccess
@subcommand([argument("-e", "--host", dest='host', default="usdwest01emc", type=str, help="Hostname of EMC Unity Device"),
argument("-p", "--pool", dest='pool_id', default="pool_1", type=str, help="Name of Pool of str arg"),
argument("-s", "--size", dest='size', default="1099511627776", type=str, help="Size of Pool as unsigned integer (default of 1TB)"),
argument("-d", "--description", dest='lun_description', default=None, type=str, help="Description of new Lun object"),
argument("lun_name", metavar='lun_name', type=str, help="Name for LUN")])
def create_lun(args):
""" Creates a new block LUN in pool_id, returns a lun object """
#unity = Unity(args.host, 'restapi', 'NceSsa7I3STo!')
unity = Unity(args.host, 'restapi', 'NceSsa7I3STo!')
host_access_list = generateHostAccessList()
lun_parameters = {
'pool': {'id':args.pool_id},
'isThinEnabled':'true',
'isCompressionEnabled':'true',
'size': args.size,
'hostAccess' : host_access_list
}
payload = {'name':args.lun_name,
'lunParameters': lun_parameters }
response = unity.post('/types/storageResource/action/createLun', payload)
new_id = response.json()['content']['storageResource']['id']
return unity.lun(item_id=new_id)
def generateHostAccessList(hosts=""):
'''Generate hostAccess list from list of hosts'''
if hosts == "":
hostAccessList = [{'host': {'id': 'Host_11'}, 'accessMask': 1},
{'host': {'id': 'Host_9'}, 'accessMask': 1},
{'host': {u'id': u'Host_8'}, 'accessMask': 1},
{'host': {u'id': u'Host_7'}, 'accessMask': 1},
{'host': {u'id': u'Host_6'}, 'accessMask': 1},
{'host': {u'id': u'Host_5'}, 'accessMask': 1},
{'host': {u'id': u'Host_4'}, 'accessMask': 1},
{'host': {u'id': u'Host_3'}, 'accessMask': 1},
{'host': {u'id': u'Host_2'}, 'accessMask': 1},
{'host': {u'id': u'Host_10'}, 'accessMask': 1}]
return hostAccessList
#@subcommand([argument("-e", "--host", dest='host', default="usdwest01emc", type=str, help="Hostname of EMC Unity Device")])
@subcommand()
def test(args):
''' Currently Testing authenticated responses'''
unity = Unity(args.emc, args.user, 'NceSsa7I3STo!')
#url_path = '/types/snap/instances?fields=name'
snapSchedules = '/types/snapSchedule/instances'
snapSch_1 = '/instances/snapSchedule/snapSch_1'
url_path = snapSch_1
response = unity.unity_request(url_path)
print response.json()
#[0]['content']
#print "Is it Authorized?: {}".format(unity.is_auth)
#print unity.snap()['0']['entries']['content']
#print (vars(unity))
#print unity.headers
@subcommand()
def status(args):
''' Default Subprocess when none is given '''
unity = Unity(args.emc, args.user, 'NceSsa7I3STo!')
print unity.name
print unity.model
print unity.software
print "Is Authenticated: {}".format(unity.is_auth)
if __name__ == "__main__":
'''CLI Top Level Parsing'''
cli.add_argument('-u', '--user', dest='user', type=str, default="restapi", help='Username used for EMC Array login')
cli.add_argument('-p', '--password', dest='password', action='store_true', help='Password for EMC Array login')
cli.add_argument('-e', '--emc', dest='emc', default="usdwest01emc", type=str, help='Name of EMC device')
args = cli.parse_args()
unity = Unity(args.emc, args.user, 'NceSsa7I3STo!')
#unity = Unity(args.host, 'restapi', 'NceSsa7I3STo!')
if args.subcommand is None:
print unity.name
print unity.model
print unity.software
cli.print_help()
else:
args.func(args)