Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File operations #41

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions examples/python_client_library/file_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import logging

Check failure on line 1 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 C0114: Missing module docstring (missing-module-docstring) Raw Output: python_client_library/file_operations.py:1:0: C0114: Missing module docstring (missing-module-docstring)
from netapp_ontap import NetAppRestError
from netapp_ontap.resources import Volume
from netapp_ontap.resources import FileInfo
from utils import Argument, parse_args, setup_connection

# REFERENCES
# https://devnet.netapp.com/restapi.php
# https://pypi.org/project/netapp-ontap/
# https://library.netapp.com/ecmdocs/ECMLP2858435/html/resources/volume.html
# https://library.netapp.com/ecmdocs/ECMLP2885777/html/resources/file_info.html
# https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/FileInfo-Received-list-directory-more-than-one-record/m-p/440962


def list_files(volume, path):
"""Recursively list files on a volume"""
files = FileInfo.get_collection(volume.uuid, path)
for f in files:

Check failure on line 18 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 C0103: Variable name "f" doesn't conform to snake_case naming style (invalid-name) Raw Output: python_client_library/file_operations.py:18:8: C0103: Variable name "f" doesn't conform to snake_case naming style (invalid-name)
if f.name != "." and f.name != "..":

Check failure on line 19 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 R1714: Consider merging these comparisons with 'in' by using 'f.name not in ('.', '..')'. Use a set instead if elements are hashable. (consider-using-in) Raw Output: python_client_library/file_operations.py:19:11: R1714: Consider merging these comparisons with 'in' by using 'f.name not in ('.', '..')'. Use a set instead if elements are hashable. (consider-using-in)
if f.type == "file":
print(f"{path}{f.name}")
elif f.type == "directory" and f.name != ".snapshot":
print(f"{path}{f.name}/")
list_files(volume, f"{path}{f.name}/")


def delete(volume, pathname, recursive=False):
"""Delete a file or directory on a volume"""
try:
resource = FileInfo(volume.uuid, path=pathname)
resource.delete(recurse=recursive)
except NetAppRestError as error:
if recursive:
extra = "(recursively) "
else:
extra = ""
logging.critical(

Check failure on line 37 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation) Raw Output: python_client_library/file_operations.py:37:8: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)
f"delete: File or directory {pathname} was not deleted {extra}on {volume.name} ({error})")

Check failure on line 38 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 C0301: Line too long (102/100) (line-too-long) Raw Output: python_client_library/file_operations.py:38:0: C0301: Line too long (102/100) (line-too-long)


def create_directory(volume, pathname):
"""Create a directory on a volume"""
resource = FileInfo(volume.uuid, pathname)
resource.type = "directory"
resource.unix_permissions = "644"
try:
resource.post()
except NetAppRestError as error:
logging.critical(

Check failure on line 49 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation) Raw Output: python_client_library/file_operations.py:49:8: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)
f"create_directory: Directory {pathname} was not created on {volume.name} ({error})")


def create_file(volume, pathname, contents):

Check failure on line 53 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 C0116: Missing function or method docstring (missing-function-docstring) Raw Output: python_client_library/file_operations.py:53:0: C0116: Missing function or method docstring (missing-function-docstring)
try:
resource = FileInfo(volume.uuid, pathname)
resource.post(
hydrate=True, data="the data to be written to the new file")
resource.patch()
except NetAppRestError as error:
logging.critical(
f"create_file: File {pathname} was not created on {volume.name} ({error})")


def file_handling(volume_name):
try:
all_volumes = list(Volume.get_collection(name=volume_name))
for vol in all_volumes:
print(f"Volume: {vol.name} ({vol.uuid})")
create_file(vol, "alice", "lorem ipsum")

create_directory(vol, "bobsfiles")
create_file(vol, "bobsfiles/bob", "lorem ipsum")

create_directory(vol, "bobsfiles/charliesfiles")
create_file(
vol, "bobsfiles/charliesfiles/charlie1", "lorem ipsum")
create_file(
vol, "bobsfiles/charliesfiles/charlie2", "lorem ipsum")
create_file(
vol, "bobsfiles/charliesfiles/charlie3", "lorem ipsum")

list_files(vol, "/")

print("Cleaning up...")
delete(vol, "/alice", False)
delete(vol, "/bobsfiles", True)
print("Done.")
except NetAppRestError as error:
print("Exception :" + str(error))


def main() -> None:
"""Main function"""

arguments = [
Argument("-c", "--cluster", "API server IP:port details"),
Argument("-v", "--volume_name", "Volume Name")]
args = parse_args(
"This script will demonstrate enumerating volumes, file and directory creation, file and directory creation deletion",

Check failure on line 99 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 C0301: Line too long (126/100) (line-too-long) Raw Output: python_client_library/file_operations.py:99:0: C0301: Line too long (126/100) (line-too-long)
arguments,
)

setup_connection(args.cluster, args.api_user, args.api_pass)
file_handling(args.volume_name)


if __name__ == "__main__":
main()

Check failure on line 108 in examples/python_client_library/file_operations.py

View workflow job for this annotation

GitHub Actions / reviewdog

[] reported by reviewdog 🐶 C0304: Final newline missing (missing-final-newline) Raw Output: python_client_library/file_operations.py:108:0: C0304: Final newline missing (missing-final-newline)
Loading