-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathlun_operations.py
More file actions
executable file
·218 lines (186 loc) · 6.26 KB
/
Copy pathlun_operations.py
File metadata and controls
executable file
·218 lines (186 loc) · 6.26 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#! /usr/bin/env python3
"""
ONTAP REST API Python Sample Scripts
This script was developed by NetApp to help demonstrate
NetApp technologies. This script is not officially
supported as a standard NetApp product.
Purpose: THE FOLLOWING SCRIPT SHOWS LUN OPERATIONS USING REST API.
usage: python3 lun_operations.py [-h] -c CLUSTER [-u API_USER]
[-p API_PASS]
Copyright (c) 2020 NetApp, Inc. All Rights Reserved.
Licensed under the BSD 3-Clause "New or Revised" License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://opensource.org/licenses/BSD-3-Clause
"""
import sys
import requests
import urllib3 as ur
from utils import Argument, parse_args, setup_logging, setup_connection
from utils import get_size, show_svm, show_volume, show_lun, get_key_lun
ur.disable_warnings()
def list_lun(cluster: str, headers_inc: str) -> None:
"""Lists LUN"""
print("======================")
print()
lun_api_url = "https://{}/api/storage/luns".format(
cluster)
try:
response = requests.get(lun_api_url, headers=headers_inc, verify=False)
except requests.exceptions.HTTPError as err:
print(str(err))
sys.exit(1)
except requests.exceptions.RequestException as err:
print(str(err))
sys.exit(1)
url_text = response.json()
if 'error' in url_text:
print(url_text)
sys.exit(1)
lundict = dict(response.json())
luns = lundict['records']
print()
print(" List of LUNs :- ")
for lun in luns:
print("=====")
print("LUN Name = %s" % lun['name'])
print("LUN UUID = %s" % lun['uuid'])
def create_lun(cluster: str, headers_inc: str) -> None:
"""Create a LUN"""
print("======================")
print()
show_svm(cluster, headers_inc)
print()
svm_name = input("Enter the name of the SVM :- ")
print()
show_volume(cluster, headers_inc, svm_name)
print()
vol_name = input(
"Choose the volume on which you would like to create the LUN : ")
print()
lun_name = input("Enter the name of the LUN : ")
lun_name_ext = "/vol/" + vol_name + "/" + lun_name
os_type = input("Enter the name of the OS-TYPE : ")
lun_size = input("Enter the LUN size in MBs :")
l_size = get_size(lun_size)
payload2 = {
"comment": lun_name,
"location": {
"logical_unit": lun_name,
"volume": {
"name": vol_name
}
},
"name": lun_name_ext,
"os_type": os_type,
"space": {
"guarantee": {
"requested": bool("")
},
"size": l_size
},
"svm": {
"name": svm_name
}
}
url = "https://{}/api/storage/luns".format(cluster)
try:
response = requests.post(
url,
headers=headers_inc,
json=payload2,
verify=False)
except requests.exceptions.HTTPError as err:
print(str(err))
sys.exit(1)
except requests.exceptions.RequestException as err:
print(str(err))
sys.exit(1)
url_text = response.json()
if 'error' in url_text:
print(url_text)
sys.exit(1)
print("LUN created successfully...")
def update_lun(cluster, headers_inc):
""" Update LUN"""
print("======================")
print()
show_lun(cluster, headers_inc)
lun_name = input("Enter the name of the LUN :- ")
lun_new_name = input(
"Enter the new name of the LUN to be updated [Full-Path]:- ")
lun_uuid = get_key_lun(lun_name, cluster, headers_inc)
print(lun_uuid)
lunpatchobj = {"name": lun_new_name}
print(lunpatchobj)
url = "https://{}/api/storage/luns/{}".format(cluster, lun_uuid)
try:
response = requests.patch(
url,
headers=headers_inc,
json=lunpatchobj,
verify=False)
except requests.exceptions.HTTPError as err:
print(str(err))
sys.exit(1)
except requests.exceptions.RequestException as err:
print(str(err))
sys.exit(1)
url_text = response.json()
if 'error' in url_text:
print(url_text)
sys.exit(1)
print("LUN has been updated with the new name")
def delete_lun(cluster, headers_inc):
""" Delete LUN"""
print("======================")
print()
show_lun(cluster, headers_inc)
lun_name = input("Enter the name of the LUN to be deleted:- ")
lun_uuid = get_key_lun(lun_name, cluster, headers_inc)
print(lun_uuid)
url = "https://{}/api/storage/luns/{}".format(cluster, lun_uuid)
try:
response = requests.delete(
url,
headers=headers_inc,
verify=False)
except requests.exceptions.HTTPError as err:
print(str(err))
sys.exit(1)
except requests.exceptions.RequestException as err:
print(str(err))
sys.exit(1)
url_text = response.json()
if 'error' in url_text:
print(url_text)
sys.exit(1)
print("LUN has been deleted.")
def lun_ops(cluster, headers_inc):
"""LUN Operations"""
print()
print("THE FOLLOWING SCRIPT SHOWS LUN OPERATIONS USING REST API :- ")
print("============================================================")
print()
lunbool = input(
"Choose the LUN Operation would you like to do? [list/create/update/delete] ")
if lunbool == 'list':
list_lun(cluster, headers_inc)
if lunbool == 'create':
create_lun(cluster, headers_inc)
if lunbool == 'update':
update_lun(cluster, headers_inc)
if lunbool == 'delete':
delete_lun(cluster, headers_inc)
def main() -> None:
"""Main function"""
arguments = [
Argument("-c", "--cluster", "API server IP:port details")]
args = parse_args(
"Demonstrates LUN Operations using REST API.", arguments,
)
setup_logging()
headers = setup_connection(args.api_user, args.api_pass)
lun_ops(args.cluster, headers)
if __name__ == "__main__":
main()