Skip to content

Commit 4783ad3

Browse files
committed
Initial Commit
1 parent 3a830b3 commit 4783ad3

File tree

8 files changed

+779
-0
lines changed

8 files changed

+779
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#! /usr/bin/env python3
2+
3+
"""
4+
ONTAP REST API Python Client Library Sample Scripts
5+
This script was developed by NetApp to help demonstrate NetApp
6+
technologies. This script is not officially supported as a
7+
standard NetApp product.
8+
9+
Purpose: Script to list volumes using ONTAP REST API Python Client Library.
10+
11+
usage: python3 file_system_analytics.py [-h] -c CLUSTER -vs SVM_NAME [-u API_USER]
12+
[-p API_PASS]
13+
14+
Copyright (c) 2020 NetApp, Inc. All Rights Reserved.
15+
Licensed under the BSD 3-Clause "New" or "Revised" License (the "License");
16+
you may not use this file except in compliance with the License.
17+
You may obtain a copy of the License at
18+
https://opensource.org/licenses/BSD-3-Clause
19+
20+
"""
21+
22+
from netapp_ontap.resources import FileInfo
23+
from utils import Argument, parse_args, setup_logging, setup_connection, Volume
24+
25+
26+
def file_system_analytics_pycl(vol_name: str, svm_name: str, path: str) -> None:
27+
""" List File system analytics of a volume in an SVM """
28+
volume = Volume.find(**{'svm.name': svm_name, 'name': vol_name})
29+
30+
resource = FileInfo(volume.uuid, path)
31+
resource.get(return_metadata=True, fields="*")
32+
print()
33+
print("Path:", resource.path)
34+
print("Bytes Used:", resource.bytes_used)
35+
print("Accessed Time: ", resource.accessed_time)
36+
print("Changed Time: ", resource.changed_time)
37+
print("Inode: ", resource.inode_number)
38+
print("unix_permissions", resource.unix_permissions)
39+
40+
41+
def main() -> None:
42+
"""Main function"""
43+
arguments = [
44+
Argument("-c", "--cluster", "API server IP:port details"),
45+
Argument("-v", "--volume_name", "Volume Name"),
46+
Argument("-a", "--path", "path"),
47+
Argument("-vs", "--svm_name", "SVM Name")]
48+
args = parse_args(
49+
"This script will list analytics of a volume in an SVM", arguments)
50+
setup_logging()
51+
setup_connection(args.cluster, args.api_user, args.api_pass)
52+
file_system_analytics_pycl(args.volume_name, args.svm_name, args.path)
53+
54+
55+
if __name__ == "__main__":
56+
main()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#! /usr/bin/env python3
2+
3+
"""
4+
ONTAP REST API Python Client Library Sample Scripts
5+
This script was developed by NetApp to help demonstrate NetApp
6+
technologies. This script is not officially supported as a
7+
standard NetApp product.
8+
9+
Purpose: Script to use CLI commands using ONTAP REST API Python Client Library.
10+
11+
usage: python3 system_node_power_update.py [-h] -c CLUSTER -vs SVM_NAME [-u API_USER]
12+
[-p API_PASS]
13+
14+
Copyright (c) 2020 NetApp, Inc. All Rights Reserved.
15+
Licensed under the BSD 3-Clause "New" or "Revised" License (the "License");
16+
you may not use this file except in compliance with the License.
17+
You may obtain a copy of the License at
18+
https://opensource.org/licenses/BSD-3-Clause
19+
20+
"""
21+
22+
import json
23+
import time
24+
from netapp_ontap.resources import CLI
25+
from utils import Argument, parse_args, setup_logging, setup_connection
26+
27+
28+
def system_node_power_on() -> None:
29+
""" system node power on module"""
30+
node_name = input("\n Enter the node name: ")
31+
response = CLI().execute(
32+
"system node power on",
33+
body={
34+
"node": node_name},
35+
privilege_level="diagnostic")
36+
time.sleep(15)
37+
response = CLI().execute(
38+
"system node power show", status="on")
39+
print(json.dumps(response.http_response.json(), indent=4))
40+
41+
42+
def system_node_power_off() -> None:
43+
""" System node power off module """
44+
node = input("\n Enter the node name: ")
45+
response = CLI().execute(
46+
"system node power off", body={
47+
"node": node}, privilege_level="diagnostic")
48+
time.sleep(5)
49+
response = CLI().execute(
50+
"system node power show", status="off")
51+
print(json.dumps(response.http_response.json(), indent=4))
52+
53+
54+
def check_system_power() -> None:
55+
"""Module to retrieve user input for system node power"""
56+
print("\n===============================================================")
57+
print("This Module covers System node power CLI command usage in PCL")
58+
print("===============================================================")
59+
print("\n 1. CLI Command >> system node power on")
60+
print("\n 2. CLI Command >> system node power off")
61+
option = input("\n\nEnter the option: ")
62+
if option == '1':
63+
system_node_power_on()
64+
if option == '2':
65+
system_node_power_off()
66+
67+
68+
def main() -> None:
69+
"""Main function"""
70+
arguments = [
71+
Argument("-c", "--cluster", "API server IP:port details")]
72+
args = parse_args(
73+
"This script will update system power status ON/OFF", arguments)
74+
setup_logging()
75+
setup_connection(args.cluster, args.api_user, args.api_pass)
76+
check_system_power()
77+
78+
79+
if __name__ == "__main__":
80+
main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /usr/bin/env python3
2+
3+
"""
4+
ONTAP REST API Python Client Library Sample Scripts
5+
This script was developed by NetApp to help demonstrate NetApp
6+
technologies. This script is not officially supported as a
7+
standard NetApp product.
8+
9+
Purpose: Script to use CLI commands using ONTAP REST API Python Client Library.
10+
11+
usage: python3 system_power_status.py [-h] -c CLUSTER -vs SVM_NAME [-u API_USER]
12+
[-p API_PASS]
13+
14+
Copyright (c) 2020 NetApp, Inc. All Rights Reserved.
15+
Licensed under the BSD 3-Clause "New" or "Revised" License (the "License");
16+
you may not use this file except in compliance with the License.
17+
You may obtain a copy of the License at
18+
https://opensource.org/licenses/BSD-3-Clause
19+
20+
"""
21+
import json
22+
from netapp_ontap.resources import CLI
23+
from utils import Argument, parse_args, setup_logging, setup_connection
24+
25+
26+
def system_power_status_pycl() -> None:
27+
""" list system power status """
28+
response = CLI().execute(
29+
"system node power show", status="on")
30+
print(json.dumps(response.http_response.json(), indent=4))
31+
32+
33+
def main() -> None:
34+
"""Main function"""
35+
arguments = [
36+
Argument("-c", "--cluster", "API server IP:port details")]
37+
args = parse_args(
38+
"This script will list ONTAP System Power status", arguments)
39+
setup_logging()
40+
setup_connection(args.cluster, args.api_user, args.api_pass)
41+
system_power_status_pycl()
42+
43+
44+
if __name__ == "__main__":
45+
main()
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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,
14+
-u admin, -p/--password
15+
16+
Copyright (c) 2020 NetApp, Inc. All Rights Reserved.
17+
18+
Licensed under the BSD 3-Clause “New” or Revised” License (the "License");
19+
you may not use this file except in compliance with the License.
20+
21+
You may obtain a copy of the License at
22+
https://opensource.org/licenses/BSD-3-Clause
23+
"""
24+
import base64
25+
import argparse
26+
import logging
27+
from getpass import getpass
28+
import texttable as tt
29+
import requests
30+
import urllib3 as ur
31+
ur.disable_warnings()
32+
33+
34+
def get_system_node_power(cluster: str, headers_inc: str):
35+
"Get system fru_check CLI command"
36+
endpoint = "api/private/cli/system/node/power"
37+
url = "https://{}/{}?fields=node,status".format(
38+
cluster, endpoint)
39+
print(url)
40+
response = requests.get(url, headers=headers_inc, verify=False)
41+
return response.json()
42+
43+
44+
def get_system_node(cluster: str, headers_inc: str):
45+
"Display events call output"
46+
ctr = 0
47+
print()
48+
tmp = dict(get_system_node_power(cluster, headers_inc))
49+
vols = tmp['records']
50+
tab = tt.Texttable()
51+
header = ['Node', 'Status']
52+
tab.header(header)
53+
tab.set_cols_align(['c', 'c'])
54+
for eventlist in vols:
55+
ctr = ctr + 1
56+
node = eventlist['node']
57+
status = eventlist['status']
58+
row = [node, status]
59+
tab.set_cols_width([20, 20])
60+
tab.add_row(row)
61+
tab.set_cols_align(['c', 'c'])
62+
setdisplay = tab.draw()
63+
print(setdisplay)
64+
print("\n Number of records displayed: {}".format(ctr))
65+
66+
67+
def parse_args() -> argparse.Namespace:
68+
"""Parse the command line arguments from the user"""
69+
parser = argparse.ArgumentParser(
70+
description="This script will list system fru-check command in CLI")
71+
parser.add_argument(
72+
"-c", "--cluster", required=True, help="API server IP:port details")
73+
parser.add_argument(
74+
"-u",
75+
"--api_user",
76+
default="admin",
77+
help="API Username")
78+
parser.add_argument("-p", "--api_pass", help="API Password")
79+
parsed_args = parser.parse_args()
80+
# collect the password without echo if not already provided
81+
if not parsed_args.api_pass:
82+
parsed_args.api_pass = getpass()
83+
return parsed_args
84+
85+
86+
if __name__ == "__main__":
87+
88+
logging.basicConfig(
89+
level=logging.INFO,
90+
format="[%(asctime)s] [%(levelname)5s] [%(module)s:%(lineno)s] %(message )s",
91+
)
92+
ARGS = parse_args()
93+
BASE64STRING = base64.encodebytes(
94+
('%s:%s' %
95+
(ARGS.api_user, ARGS.api_pass)).encode()).decode().replace('\n', '')
96+
headers = {
97+
'authorization': "Basic %s " % BASE64STRING,
98+
'content-type': "application/json",
99+
'accept': "application/json"
100+
}
101+
get_system_node(ARGS.cluster, headers)
102+
print("\n==========END OF MODULE=============")
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 system/node/power/off CLI diagnostic mode 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+
34+
def system_node_power_off(cluster: str, headers_inc: str):
35+
"""Module to Turn off power nodes"""
36+
print(" \n System node power off")
37+
node_name = input("\n Enter the Node name to turn OFF : ")
38+
dataobj = {
39+
'node': node_name
40+
}
41+
cli_endpoint = "api/private/cli/system/node/power/off"
42+
url = "https://{}/{}?privilege_level=diagnostic".format(
43+
cluster, cli_endpoint)
44+
try:
45+
response = requests.post(
46+
url,
47+
headers=headers_inc,
48+
json=dataobj,
49+
verify=False)
50+
except requests.exceptions.HTTPError as err:
51+
print(str(err))
52+
sys.exit(1)
53+
except requests.exceptions.RequestException as err:
54+
print(str(err))
55+
sys.exit(1)
56+
url_text = response.json()
57+
if 'error' in url_text:
58+
print(url_text)
59+
sys.exit(1)
60+
else:
61+
print("\n Turned OFF the node", node_name)
62+
63+
64+
def parse_args() -> argparse.Namespace:
65+
"""Parse the command line arguments from the user"""
66+
parser = argparse.ArgumentParser(
67+
description="This script creates,updates and deletes service-policy via cli passthrough")
68+
parser.add_argument(
69+
"-c", "--cluster", required=True, help="API server IP:port details")
70+
parser.add_argument(
71+
"-u",
72+
"--api_user",
73+
default="admin",
74+
help="API Username")
75+
parser.add_argument("-p", "--api_pass", help="API Password")
76+
parsed_args = parser.parse_args()
77+
# collect the password without echo if not already provided
78+
if not parsed_args.api_pass:
79+
parsed_args.api_pass = getpass()
80+
return parsed_args
81+
82+
83+
if __name__ == "__main__":
84+
85+
logging.basicConfig(
86+
level=logging.INFO,
87+
format="[%(asctime)s] [%(levelname)5s] [%(module)s:%(lineno)s] %(message)s",
88+
)
89+
ARGS = parse_args()
90+
BASE64STRING = base64.encodebytes(
91+
('%s:%s' %
92+
(ARGS.api_user, ARGS.api_pass)).encode()).decode().replace('\n', '')
93+
headers = {
94+
'authorization': "Basic %s " % BASE64STRING,
95+
'content-type': "application/json",
96+
'accept': "application/json"
97+
}
98+
print("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
99+
print("\n THIS MODULE COVERS SERVICE-POLICY USAGE IN CLI PASSTHROUGH VIA ONTAP REST APIS")
100+
print("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
101+
system_node_power_off(ARGS.cluster, headers)

0 commit comments

Comments
 (0)