|
|
|||
|
||||
This document talks about the ONTAPI API mechanism and shows you how to use
it from C and C++.
| Introduction |
| API concepts and conventions |
Both the client and server side use similar
API functions and concepts to manipulate elements, the values used for
both requests and results. Elements are so named because a subset of XML
is used to package up the query and return results. An element has a name,
child elements, and contents (which can be a string, integer, or array
of elements). API functions that manipulate these things come in several
categories, identified by the prefix of their name. The main ones are:
Several opaque types are manipulated by these functions
The core routines are presented next. Following that is a recap, in C only, of the getting-started tutorial. |
| The ONTAPI core APIs |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Client Setup |
| The first thing a client program must
do is initialize the server context:
#include <netapp_api.h> if (!na_startup(err,
sizeof(err))) { |
|
|
na_server_t*
s; na_elem_t* out; /* /* /* na_server_close(s); |
|
| Next you set the transport parameters. If you're writing
a Windows application, you'll normally want to use Windows DCE/RPC, which
furnishes native Windows authentication and authorization. If you need to
use HTTP, on the other hand, you must furnish a username and password for
use by the server context: bool use_rpc = 1; /* This should be the default for Windows clients */ char user[] = "root"; /* Change this to an actual admin user name */ char passwd[] = "passwd"; /* Change this to the actual admin password, or get it from a prompt or dialog box. */ /* * Set the transport style. Windows users will generally * want to use NA_STYLE_RPC, as that provides native Windows * authentication and authorization for ONTAPI calls. */ if ( use_rpc) { na_server_style(s, NA_STYLE_RPC); } else { /* * This establishes the administrative user login name * and password to be used to authenticate. */ na_server_style(s, NA_STYLE_LOGIN_PASSWORD); na_server_adminuser(s, user, passwd); } |
|
| Client Example #1 - The ONTAPI version of Hello World |
| In the ONTAPI version of HelloWorld, we
set up a session and transport, as above, then get the ONTAP version number
and print it out. When you make an API call you get back a pointer to an instance of na_elem_t , and retreive status and values from it with na_child_* primitives. When you are done with the result, a single call to na_free() frees everything associated with the returned element. Onward... #include <stdio.h> Note: values pulled out of the result (as by na_child_string() in this example) point to memory that is freed when you call na_free() to free the result. You need to copy the value elsewhere if you need to make use of it later. For this example to compile at present, you have to have the following
things in place in your environment:
The development team is working on simplifying these requirements,
so they are likely to change in the official release of this SDK. |
| Client example #2 Taking a named
snapshot |
| This example takes filer, volume and snapshot
parameters from the command line, uses them to create a named snapshot
on the given volume, and does error checking to report the success or failure
of the command. |
|
| #include <stdio.h> #include <netapp_api.h> int main(int argc, char* argv[]) { na_server_t* s; na_elem_t* out; char err[256]; int use_rpc = 0; /* This should be the default for Windows clients */ char user[] = "root"; /* Change this to an actual admin user name */ char passwd[] = "passwd"; /* Change this to the actual admin password */ char* filername; char* volname; char* snapshotname; if (argc < 4) { fprintf(stderr, "Usage: %s <filername> <volname> <newsnapshotname>\n", argv[0]); return -1; } else { filername = argv[1]; volname = argv[2]; snapshotname = argv[3]; } if (!na_startup(err, sizeof(err))) { fprintf(stderr, "Error in na_startup: %s\n", err); return -2; } s = na_server_open(filername, 1, 1); if ( use_rpc) { na_server_style(s, NA_STYLE_RPC); } else { na_server_style(s, NA_STYLE_LOGIN_PASSWORD); na_server_adminuser(s, user, passwd); } out = na_server_invoke(s, "system-get-version", NULL); if (na_results_status(out) != NA_OK) { printf("Error %d: %s\n", na_results_errno(out), na_results_reason(out)); return -3; } else { na_elem_free(out); } out = na_server_invoke(s, "snapshot-create", "snapshot", snapshotname, "volume", volname, NULL); if (na_results_status(out) != NA_OK) { printf("Error %d: %s\n", na_results_errno(out), na_results_reason(out)); return -4; } else { na_elem_free(out); } return 0; } |