Skip to content

Commit 34cc7f2

Browse files
committed
Initial Commit
1 parent 6e4f095 commit 34cc7f2

File tree

8 files changed

+1342
-1
lines changed

8 files changed

+1342
-1
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
#! /usr/bin/env python3
2+
3+
"""
4+
ONTAP REST API Sample Scripts
5+
6+
This script was developed by NetApp to help demonstrate NetApp technologies.
7+
This script is not officially supported as a
8+
standard NetApp product.
9+
10+
Purpose: This Module covers network/interface/service-policy/ CLI usage using ONTAP REST API
11+
12+
Usage: service_policy.py [-h] -c CLUSTER [-u API_USER] [-p API_PASS]
13+
service_policy.py: the following arguments are required: -c/--cluster, -u/--admin, -p/--password
14+
15+
Copyright (c) 2020 NetApp, Inc. All Rights Reserved.
16+
17+
Licensed under the BSD 3-Clause “New” or Revised” License (the "License");
18+
you may not use this file except in compliance with the License.
19+
20+
You may obtain a copy of the License at
21+
https://opensource.org/licenses/BSD-3-Clause
22+
"""
23+
24+
import sys
25+
import base64
26+
import argparse
27+
from getpass import getpass
28+
import logging
29+
import requests
30+
import urllib3 as ur
31+
ur.disable_warnings()
32+
33+
def create_service_policy(cluster: str, headers_inc: str):
34+
"""Module to create service policy"""
35+
print(" \nModule 1 - Create Service Policy")
36+
vserver = input("\n Enter the svm name: ")
37+
policy_name = input(" Enter the new policy name: ")
38+
services_name = []
39+
number = int(input(" Enter the no of services you want to input: "))
40+
for number in range(0, number):
41+
services = input(" Enter the services name(e.g: data-cifs): ")
42+
services_name.append(services)
43+
print("\nList is - ", services_name)
44+
allowed_addresses = []
45+
number = int(input(" Enter the no of addresses you want to input: "))
46+
for number in range(0, number):
47+
addresses = input(" Enter the address name : ")
48+
allowed_addresses.append(addresses)
49+
print("\nList is - ", allowed_addresses)
50+
51+
dataobj = {
52+
'vserver': vserver,
53+
'policy': policy_name,
54+
'services': services_name,
55+
'allowed_addresses': allowed_addresses
56+
}
57+
cli_endpoint = "network/interface/service-policy/"
58+
url = "https://{}/api/private/cli/{}?privilege_level=diagnostic".format(
59+
cluster, cli_endpoint)
60+
try:
61+
response = requests.post(
62+
url,
63+
headers=headers_inc,
64+
json=dataobj,
65+
verify=False)
66+
except requests.exceptions.HTTPError as err:
67+
print(str(err))
68+
sys.exit(1)
69+
except requests.exceptions.RequestException as err:
70+
print(str(err))
71+
sys.exit(1)
72+
url_text = response.json()
73+
if 'error' in url_text:
74+
print(url_text)
75+
sys.exit(1)
76+
else:
77+
print("\n Created Service Policy successfully\n")
78+
79+
80+
def modify_service_policy(cluster: str, headers_inc: str):
81+
"""Module to modify the existing service policy"""
82+
print(" \nModule 2 - Modify Service Policy")
83+
vserver = input("\n Enter the svm name : ")
84+
policy_name = input(" Enter the existing service policy name: ")
85+
service_name = input(" Enter the service name: ")
86+
allowed_addresses = []
87+
number = int(input(" Enter the no of addresses you want to input: "))
88+
for number in range(0, number):
89+
addresses = input(" Enter the address name : ")
90+
allowed_addresses.append(addresses)
91+
print("\n List is - ", allowed_addresses)
92+
print("\n Updating..........\n")
93+
dataobj = {
94+
'vserver': vserver,
95+
'policy': policy_name,
96+
'service': service_name,
97+
'allowed_addresses': allowed_addresses
98+
}
99+
cli_endpoint = "network/interface/service-policy/modify-service"
100+
url = "https://{}/api/private/cli/{}?privilege_level=diagnostic".format(
101+
cluster, cli_endpoint)
102+
try:
103+
response = requests.post(
104+
url,
105+
headers=headers_inc,
106+
json=dataobj,
107+
verify=False)
108+
except requests.exceptions.HTTPError as err:
109+
print(str(err))
110+
sys.exit(1)
111+
except requests.exceptions.RequestException as err:
112+
print(str(err))
113+
sys.exit(1)
114+
url_text = response.json()
115+
if 'error' in url_text:
116+
print(url_text)
117+
sys.exit(1)
118+
else:
119+
print("\n Updated Service Policy successfully\n")
120+
121+
122+
def add_service_policy(cluster: str, headers_inc: str):
123+
"""Module to modify the existing service policy"""
124+
print("\nModule 2 - Add an additional service entry to an existing service policy")
125+
vserver = input("\n Enter the svm name : ")
126+
policy_name = input(" Enter the existing service policy name: ")
127+
service_name = input(" Enter the service name: ")
128+
allowed_addresses = []
129+
number = int(input(" Enter the no of addresses you want to input: "))
130+
for number in range(0, number):
131+
addresses = input(" Enter the address name : ")
132+
allowed_addresses.append(addresses)
133+
print("\n List is - ", allowed_addresses)
134+
print("\n Updating..........\n")
135+
dataobj = {
136+
'vserver': vserver,
137+
'policy': policy_name,
138+
'service': service_name,
139+
'allowed_addresses': allowed_addresses
140+
}
141+
cli_endpoint = "network/interface/service-policy/add-service"
142+
url = "https://{}/api/private/cli/{}?privilege_level=diagnostic".format(
143+
cluster, cli_endpoint)
144+
try:
145+
response = requests.post(
146+
url,
147+
headers=headers_inc,
148+
json=dataobj,
149+
verify=False)
150+
except requests.exceptions.HTTPError as err:
151+
print(str(err))
152+
sys.exit(1)
153+
except requests.exceptions.RequestException as err:
154+
print(str(err))
155+
sys.exit(1)
156+
url_text = response.json()
157+
if 'error' in url_text:
158+
print(url_text)
159+
sys.exit(1)
160+
else:
161+
print("\n Updated Service Policy successfully\n")
162+
163+
def delete_service_policy(cluster: str, headers_inc: str):
164+
"""Module to delete the existing service policy"""
165+
print(" \n Module 3 - Delete Service Policy")
166+
vserver = input("\n Enter the svm name : ")
167+
policy_name = input(" Enter the existing service policy name to delete: ")
168+
print("\n Deleting...........\n")
169+
dataobj = {
170+
'vserver': vserver,
171+
'policy': policy_name
172+
}
173+
cli_endpoint = "network/interface/service-policy/"
174+
url = "https://{}/api/private/cli/{}?privilege_level=diagnostic".format(
175+
cluster, cli_endpoint)
176+
try:
177+
response = requests.delete(
178+
url,
179+
headers=headers_inc,
180+
json=dataobj,
181+
verify=False)
182+
except requests.exceptions.HTTPError as err:
183+
print(str(err))
184+
sys.exit(1)
185+
except requests.exceptions.RequestException as err:
186+
print(str(err))
187+
sys.exit(1)
188+
url_text = response.json()
189+
if 'error' in url_text:
190+
print(url_text)
191+
sys.exit(1)
192+
else:
193+
print("\n Deleted Service Policy successfully\n")
194+
195+
196+
def parse_args() -> argparse.Namespace:
197+
"""Parse the command line arguments from the user"""
198+
parser = argparse.ArgumentParser(
199+
description="This script creates,updates and deletes service-policy via cli passthrough")
200+
parser.add_argument(
201+
"-c", "--cluster", required=True, help="API server IP:port details")
202+
parser.add_argument(
203+
"-u",
204+
"--api_user",
205+
default="admin",
206+
help="API Username")
207+
parser.add_argument("-p", "--api_pass", help="API Password")
208+
parsed_args = parser.parse_args()
209+
# collect the password without echo if not already provided
210+
if not parsed_args.api_pass:
211+
parsed_args.api_pass = getpass()
212+
return parsed_args
213+
214+
215+
if __name__ == "__main__":
216+
217+
logging.basicConfig(
218+
level=logging.INFO,
219+
format="[%(asctime)s] [%(levelname)5s] [%(module)s:%(lineno)s] %(message)s",
220+
)
221+
ARGS = parse_args()
222+
BASE64STRING = base64.encodebytes(
223+
('%s:%s' %
224+
(ARGS.api_user, ARGS.api_pass)).encode()).decode().replace('\n', '')
225+
headers = {
226+
'authorization': "Basic %s " % BASE64STRING,
227+
'content-type': "application/json",
228+
'accept': "application/json"
229+
}
230+
print("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
231+
print("\n THIS MODULE COVERS SERVICE-POLICY USAGE IN CLI PASSTHROUGH VIA ONTAP REST APIS")
232+
print("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
233+
create_service_policy(ARGS.cluster, headers)
234+
print("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
235+
modify_service_policy(ARGS.cluster, headers)
236+
print("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
237+
add_service_policy(ARGS.cluster, headers)
238+
print("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
239+
delete_service_policy(ARGS.cluster, headers)
240+
print("\n===============================END OF MODULE====================================")
241+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#! /usr/bin/env python3
2+
3+
"""
4+
ONTAP REST API Sample Scripts
5+
6+
This script was developed by NetApp to help demonstrate NetApp technologies.
7+
This script is not officially supported as a
8+
standard NetApp product.
9+
10+
Purpose: This Module covers fru-check show CLI command usage via ONTAP REST API
11+
12+
Usage: system_fru_check.py [-h] -c CLUSTER [-u API_USER] [-p API_PASS]
13+
system_fru_check.py: the following arguments are required: -c/--cluster, -u/--ad min, -p/--password
14+
15+
Copyright (c) 2020 NetApp, Inc. All Rights Reserved.
16+
17+
Licensed under the BSD 3-Clause “New” or Revised” License (the "License");
18+
you may not use this file except in compliance with the License.
19+
20+
You may obtain a copy of the License at
21+
https://opensource.org/licenses/BSD-3-Clause
22+
"""
23+
import base64
24+
import argparse
25+
import logging
26+
from getpass import getpass
27+
import texttable as tt
28+
import requests
29+
import urllib3 as ur
30+
ur.disable_warnings()
31+
32+
33+
def get_system_fru_check_cli(cluster: str, headers_inc: str):
34+
"Get system fru_check CLI command"
35+
endpoint = "api/private/cli/system/fru-check"
36+
url = "https://{}/{}?fields=node,serial_number,fru_status,fru_name".format(
37+
cluster, endpoint)
38+
print(url)
39+
response = requests.get(url, headers=headers_inc, verify=False)
40+
return response.json()
41+
42+
43+
def get_fru_check(cluster: str, headers_inc: str):
44+
"Display events call output"
45+
ctr = 0
46+
print()
47+
tmp = dict(get_system_fru_check_cli(cluster, headers_inc))
48+
vols = tmp['records']
49+
tab = tt.Texttable()
50+
header = ['Node', 'Serial Number', 'Fru_name', 'Fru_status']
51+
tab.header(header)
52+
tab.set_cols_align(['c', 'i', 'c', 'c'])
53+
for eventlist in vols:
54+
ctr = ctr + 1
55+
node = eventlist['node']
56+
serial_no = eventlist['serial_number']
57+
fru_name = eventlist['fru_name']
58+
fru_status = eventlist['fru_status']
59+
row = [node, serial_no, fru_name, fru_status]
60+
tab.set_cols_width([20, 20, 20, 20])
61+
tab.add_row(row)
62+
tab.set_cols_align(['c', 'i', 'c', 'c'])
63+
setdisplay = tab.draw()
64+
print(setdisplay)
65+
print("\n Number of records displayed: {}".format(ctr))
66+
67+
68+
def parse_args() -> argparse.Namespace:
69+
"""Parse the command line arguments from the user"""
70+
parser = argparse.ArgumentParser(
71+
description="This script will list system fru-check command in CLI")
72+
parser.add_argument(
73+
"-c", "--cluster", required=True, help="API server IP:port details")
74+
parser.add_argument(
75+
"-u",
76+
"--api_user",
77+
default="admin",
78+
help="API Username")
79+
parser.add_argument("-p", "--api_pass", help="API Password")
80+
parsed_args = parser.parse_args()
81+
# collect the password without echo if not already provided
82+
if not parsed_args.api_pass:
83+
parsed_args.api_pass = getpass()
84+
return parsed_args
85+
86+
87+
if __name__ == "__main__":
88+
89+
logging.basicConfig(
90+
level=logging.INFO,
91+
format="[%(asctime)s] [%(levelname)5s] [%(module)s:%(lineno)s] %(message )s",
92+
)
93+
ARGS = parse_args()
94+
BASE64STRING = base64.encodebytes(
95+
('%s:%s' %
96+
(ARGS.api_user, ARGS.api_pass)).encode()).decode().replace('\n', '')
97+
headers = {
98+
'authorization': "Basic %s " % BASE64STRING,
99+
'content-type': "application/json",
100+
'accept': "application/json"
101+
}
102+
get_fru_check(ARGS.cluster, headers)
103+
print("\n==========END OF MODULE=============")

0 commit comments

Comments
 (0)