Skip to content

Commit 7e3ecc8

Browse files
authored
Merge pull request #41 from bmrussell/file_operations
File operations
2 parents 6408ddc + 29c143d commit 7e3ecc8

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""Demonstrates creating writing, and deleting files/ folders on a NetApp volume.
2+
"""
3+
import logging
4+
from netapp_ontap import NetAppRestError
5+
from netapp_ontap.resources import Volume
6+
from netapp_ontap.resources import FileInfo
7+
from utils import Argument, parse_args, setup_connection
8+
9+
# REFERENCES:
10+
# https://devnet.netapp.com/restapi.php
11+
# https://pypi.org/project/netapp-ontap/
12+
# https://library.netapp.com/ecmdocs/ECMLP2858435/html/resources/volume.html
13+
# https://library.netapp.com/ecmdocs/ECMLP2885777/html/resources/file_info.html
14+
# https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/FileInfo-Received-list-directory-more-than-one-record/m-p/440962
15+
16+
17+
def list_files(volume, path):
18+
"""Recursively list files on a volume"""
19+
files = FileInfo.get_collection(volume.uuid, path)
20+
for file_info in files:
21+
if file_info.name not in (".", ".."):
22+
if file_info.type == "file":
23+
print(f"{path}{file_info.name}")
24+
elif file_info.type == "directory" and file_info.name != ".snapshot":
25+
print(f"{path}{file_info.name}/")
26+
list_files(volume, f"{path}{file_info.name}/")
27+
28+
29+
def delete(volume, pathname, recursive=False):
30+
"""Delete a file or directory on a volume"""
31+
try:
32+
resource = FileInfo(volume.uuid, path=pathname)
33+
resource.delete(recurse=recursive)
34+
except NetAppRestError as error:
35+
if recursive:
36+
extra = "(recursively) "
37+
else:
38+
extra = ""
39+
message = f"delete: File or dir {pathname} wasn't deleted {extra}on {volume.name} ({error})"
40+
logging.critical(message)
41+
42+
43+
def create_directory(volume, pathname):
44+
"""Create a directory on a volume"""
45+
resource = FileInfo(volume.uuid, pathname)
46+
resource.type = "directory"
47+
resource.unix_permissions = "644"
48+
try:
49+
resource.post()
50+
except NetAppRestError as error:
51+
message = f"create_directory: dir {pathname} wasn't created on {volume.name} ({error})"
52+
logging.critical(message)
53+
54+
55+
def create_file(volume, pathname, contents=""):
56+
"""Create a file on a volume
57+
58+
Args:
59+
volume (string): volume object to create file on
60+
pathname (string): path to file that is to be created
61+
contents (string): contents of file to be written
62+
"""
63+
try:
64+
resource = FileInfo(volume.uuid, pathname)
65+
resource.post(
66+
hydrate=True, data=contents)
67+
resource.patch()
68+
except NetAppRestError as error:
69+
message = f"create_file: File {pathname} was not created on {volume.name} ({error})"
70+
logging.critical(message)
71+
72+
73+
def file_handling(volume_name):
74+
"""Demonstrate file handling on NetApp
75+
Writes some files and directories to the volume then cleans up what it has written
76+
Note: Does not check for existing content.
77+
Args:
78+
volume_name (string): Volume name to us
79+
"""
80+
try:
81+
all_volumes = list(Volume.get_collection(name=volume_name))
82+
for vol in all_volumes:
83+
print(f"Volume: {vol.name} ({vol.uuid})")
84+
create_file(vol, "alice", "lorem ipsum")
85+
86+
create_directory(vol, "bobsfiles")
87+
create_file(vol, "bobsfiles/bob", "lorem ipsum")
88+
89+
create_directory(vol, "bobsfiles/charliesfiles")
90+
create_file(
91+
vol, "bobsfiles/charliesfiles/charlie1", "lorem ipsum")
92+
create_file(
93+
vol, "bobsfiles/charliesfiles/charlie2", "lorem ipsum")
94+
create_file(
95+
vol, "bobsfiles/charliesfiles/charlie3", "lorem ipsum")
96+
97+
list_files(vol, "/")
98+
99+
print("Cleaning up...")
100+
delete(vol, "/alice", False)
101+
delete(vol, "/bobsfiles", True)
102+
print("Done.")
103+
except NetAppRestError as error:
104+
print("Exception :" + str(error))
105+
106+
107+
def main() -> None:
108+
"""Main function"""
109+
110+
arguments = [
111+
Argument("-c", "--cluster", "API server IP:port details"),
112+
Argument("-v", "--volume_name", "Volume Name")]
113+
args = parse_args(
114+
"Demonstrates file and directory creation, file and directory creation deletion",
115+
arguments,
116+
)
117+
118+
setup_connection(args.cluster, args.api_user, args.api_pass)
119+
file_handling(args.volume_name)
120+
121+
122+
if __name__ == "__main__":
123+
main()

0 commit comments

Comments
 (0)