1+ import logging
2+ import json
3+ from netapp_ontap import config , HostConnection , NetAppRestError , utils
4+ from netapp_ontap .resources import Volume
5+ from netapp_ontap .resources import file_info , FileInfo
6+
7+ from utils import Argument , parse_args , setup_logging , 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+ def list_files (volume , path ):
17+ """Recursively list files on a volume"""
18+ files = FileInfo .get_collection (volume .uuid , path )
19+ for f in files :
20+ if f .name != "." and f .name != ".." :
21+ if f .type == "file" :
22+ print (f"{ path } { f .name } " )
23+ elif f .type == "directory" and f .name != ".snapshot" :
24+ print (f"{ path } { f .name } /" )
25+ list_files (volume , f"{ path } { f .name } /" )
26+
27+ def delete (volume , pathname , recursive = False ):
28+ """Delete a file or directory on a volume"""
29+ try :
30+ resource = FileInfo (volume .uuid , path = pathname )
31+ resource .delete (recurse = recursive )
32+ except NetAppRestError as error :
33+ if recursive :
34+ extra = "(recursively) "
35+ else :
36+ extra = ""
37+ logging .critical (f"delete: File or directory { pathname } was not deleted { extra } on { volume .name } ({ error } )" )
38+
39+ def create_directory (volume , pathname ):
40+ """Create a directory on a volume"""
41+ resource = FileInfo (volume .uuid , pathname )
42+ resource .type = "directory"
43+ resource .unix_permissions = "644"
44+ try :
45+ resource .post ()
46+ except NetAppRestError as error :
47+ logging .critical (f"create_directory: Directory { pathname } was not created on { volume .name } ({ error } )" )
48+
49+ def create_file (volume , pathname , contents ):
50+ try :
51+ resource = FileInfo (volume .uuid , pathname )
52+ resource .post (hydrate = True , data = "the data to be written to the new file" )
53+ resource .patch ()
54+ except NetAppRestError as error :
55+ logging .critical (f"create_file: File { pathname } was not created on { volume .name } ({ error } )" )
56+
57+ def file_handling (volume_name ):
58+ try :
59+ all_volumes = list (Volume .get_collection ())
60+ for vol in all_volumes :
61+ if vol .name == volume_name :
62+ print (f"Volume: { vol .name } ({ vol .uuid } )" )
63+ create_file (vol , "alice" , "lorem ipsum" )
64+
65+ create_directory (vol , "bobsfiles" )
66+ create_file (vol , "bobsfiles/bob" , "lorem ipsum" )
67+
68+ create_directory (vol , "bobsfiles/charliesfiles" )
69+ create_file (vol , "bobsfiles/charliesfiles/charlie1" , "lorem ipsum" )
70+ create_file (vol , "bobsfiles/charliesfiles/charlie2" , "lorem ipsum" )
71+ create_file (vol , "bobsfiles/charliesfiles/charlie3" , "lorem ipsum" )
72+
73+ list_files (vol , "/" )
74+
75+ print ("Cleaning up..." )
76+ delete (vol , "/alice" , False )
77+ delete (vol , "/bobsfiles" , True )
78+ print ("Done." )
79+ except NetAppRestError as error :
80+ print ("Exception :" + str (error ))
81+
82+ def main () -> None :
83+ """Main function"""
84+
85+ arguments = [
86+ Argument ("-c" , "--cluster" , "API server IP:port details" ),
87+ Argument ("-v" , "--volume_name" , "Volume Name" )]
88+ args = parse_args (
89+ "This script will demo nstrate enumerating volumes, file and directory creation, file and directory creation deletion" ,
90+ arguments ,
91+ )
92+
93+ setup_connection (args .cluster , args .api_user , args .api_pass )
94+ file_handling (args .volume_name )
95+
96+ if __name__ == "__main__" :
97+ main ()
0 commit comments