Software Development Kit (SDK) and API Discussions

Help a Python newb?

TWIELGOS2
9,634 Views

Hey folks,

I'm an old hat at shell scripting, and I have a need to write some scripts in Python against the netapp API.

I'm really new to coding in such an object oriented fashion - last time I did it was college 10 years ago.

I've got a script based on an online example partially working, and I see how it makes some API calls, but I can't figure out how to make calls other than what was in the example.  Here is my program in entirety:

import sys

sys.path.append("/lib/python3.2/NetApp")

from NaServer import *

from NaElement import *

def print_usage():

    print ("Usage: hello_ontapi.py <filer> <user> <password> \n")

    print ("<filer> -- Filer name\n")

    print ("<user> -- User name\n")

    print ("<password> -- Password\n")

    sys.exit (1)

args = len(sys.argv) - 1

if(args < 3):

   print_usage()

filer = sys.argv[1]

user = sys.argv[2]

password = sys.argv[3]

s = NaServer(filer, 1, 6)

s.set_server_type("Filer")

s.set_admin_user(user, password)

s.set_transport_type("HTTPS")

output = s.invoke("system-get-version")

if(output.results_errno() != 0):

   r = output.results_reason()

   print("Failed: \n" + str(r))

else :

   r = output.child_get_string("version")

   print (r + "\n")

cmd = NaElement("volume-list-info")

cmd1 = NaElement("snapshot-list-info")

ret = s.invoke_elem(cmd)

ret1 = s.invoke_elem(cmd1)

volumes = ret.child_get("volumes")

snaps = ret1.child_get("snapshots")

for vol in volumes.children_get():

        print(vol.child_get_string("name"))

        print(vol.child_get_int("size-total"))

        print(vol.child_get_string("mirror-status"))

        print(vol.child_get_int("snapshot-percent-reserved"))

        for snap in snaps.children_get(volume=vol.child_get_string("name")):

                print(snap.child_get_string("name"))

The problem comes up when it tries to invoke the snapshots call.  I get pretty output if we comment out the last two lines of code - it lists all volumes and sizes.  I can't get it to list the snapshots in those volumes.

What am I doing wrong?

1 ACCEPTED SOLUTION

sens
9,634 Views

Hi Timothy,

The line

     for snap in snaps.children_get(volume=vol.child_get_string("name")):

is the reason of not getting any snapshot.

children_get() does not take any parameter.

However, if you just remove the parameter volume=vol.child_get_string("name") from children_get(), you will get list all the snapshots, and the same list will be printed for each volume obtained from the volume-list-info API output.

From your code, I can assume that for each volume, you want to print the snapshots within the volume.

In such a case, you need to invoke snapshot-list-info API for each volume.

You need to modify your code to something as shown below [not showing the first few lines where you print the version string from system-get-version API, I am just starting from the volume-list-info API part]:

    cmd = NaElement("volume-list-info")

    ret = s.invoke_elem(cmd)

    volumes = ret.child_get("volumes")

    for vol in volumes.children_get():

        print (vol.child_get_string("name"))

        print (vol.child_get_int("size-total"))

        print (vol.child_get_string("mirror-status"))

        print (vol.child_get_int("snapshot-percent-reserved"))

        cmd1 = NaElement("snapshot-list-info")

        cmd1.child_add_string("volume", vol.child_get_string("name")) # This is where you set the volume name

        ret1 = s.invoke_elem(cmd1)

        snaps = ret1.child_get("snapshots")

        for snap in snaps.children_get():

            print(snap.child_get_string("name"))

Please feel free to let me know if it solved the problem.

Regards,

Subhabrata Sen.

View solution in original post

5 REPLIES 5

sens
9,634 Views

Hi Timothy,

Nice to know that you are trying out our Python NMSDK.

We will do our best to help you with this.

There are a lot of resources which you can refer to get an idea of how to use the Python NMSDK.

First, you can go through the SDK_help.htm file (available inside NMSDK zip) which gives you detailed information on how to use the NMSDK. More specifically, you can refer to the "NetApp Manageability SDK > Programming Guide > SDK Core APIs > Python Core APIs" Section of SDK_help.htm to know the Core APIs which you can use for setting connection with NetApp storage systems and manage input and output elements.

Secondly, you can see the sample codes to get an idea of how to invoke Data ONTAP APIs. The Python sample codes can be found under "<nmsdk root directory>/src/sample/Data_ONTAP/Python/" directory. For this particular case (snapshot list), you can check the snapman.py sample code under the same directory.

Apart from these recourses, NMSDK provides a GUI utility named ZExplore Development Interface (ZEDI) which aims to develop the code for you in a automated manner. You can get zexplore.exe (for Windows) and zexplore.jar (for all platforms with jre 1.6 or higher) under "<nmsdk root directory>/zedi/" directory. Using this tool, you just need to select the Data ONTAP API and choose the programming language - the code will be instantly developed by ZEDI. For details on how to use ZEDI, you can go through the following links:

* 5 min demo videos:

          video 1.     https://communities.netapp.com/videos/3188

          video 2.     https://communities.netapp.com/videos/2473

* ZEDI User Guide: https://communities.netapp.com/docs/DOC-22866

Please feel free to revert to us if you still face any issue using NMSDK.

Regards,

Subhabrata Sen.

TWIELGOS2
9,634 Views

Thanks, Subhabrata, but that doesn't answer my question.

I appreciate the resource suggestions, but what I'm really looking for is an explanation of why the call to volume-list-info is working but the call to snapshot-list-info is not.

Can you tell?

sens
9,635 Views

Hi Timothy,

The line

     for snap in snaps.children_get(volume=vol.child_get_string("name")):

is the reason of not getting any snapshot.

children_get() does not take any parameter.

However, if you just remove the parameter volume=vol.child_get_string("name") from children_get(), you will get list all the snapshots, and the same list will be printed for each volume obtained from the volume-list-info API output.

From your code, I can assume that for each volume, you want to print the snapshots within the volume.

In such a case, you need to invoke snapshot-list-info API for each volume.

You need to modify your code to something as shown below [not showing the first few lines where you print the version string from system-get-version API, I am just starting from the volume-list-info API part]:

    cmd = NaElement("volume-list-info")

    ret = s.invoke_elem(cmd)

    volumes = ret.child_get("volumes")

    for vol in volumes.children_get():

        print (vol.child_get_string("name"))

        print (vol.child_get_int("size-total"))

        print (vol.child_get_string("mirror-status"))

        print (vol.child_get_int("snapshot-percent-reserved"))

        cmd1 = NaElement("snapshot-list-info")

        cmd1.child_add_string("volume", vol.child_get_string("name")) # This is where you set the volume name

        ret1 = s.invoke_elem(cmd1)

        snaps = ret1.child_get("snapshots")

        for snap in snaps.children_get():

            print(snap.child_get_string("name"))

Please feel free to let me know if it solved the problem.

Regards,

Subhabrata Sen.

TWIELGOS2
9,634 Views

Thanks Subhabrata - I will try this and let you know how it goes

sens
9,634 Views

Hi Timothy,

Was the solution mentioned above helpful for you?

Regards,

Subhabrata Sen.

Public