Wenn man eine bestimmte Aufgabe öfter wie nur einmal erledigen muss, lohnt es sich oft die Zeit zu investieren um ein Script zu entwickeln. Mit jeder Wiederholung gewinnt man diese Zeit wieder zurück.

Historisch betrachtet gibt es viele Wege diese Aufgabe zu erfüllen: Pearl Scirpte, Batch Scripte, PowerShell, PowerCLI, Python usw.

Dazu kommt das wir heutzutage immer mehr Orchestrierungsmethoden einsetzen, die komplexe Aufgaben automatisiert übernehmen.

Vermutlich gibt es so viele Methoden bestimmte Aufgaben zu automatisieren, wie es Aufgaben gibt die man sinnvoller Weise automatisiert.

 

Wichtig ist, dass diese Tools mit allen Komponenten der Infrastruktur, die zu verwalten sind, kommunizieren können. Es gab ellegante und zusammengeschusterte Möglichkeiten um mit den unterschiedlichen Komponenten zu kommunizieren. Über die vergangenen Jahre hat sich ein bestimmtes Hilfsmittel um diese Aufgaben zu erfüllen durchgesetzt: RESTful API. Benutzt man diese API kann der Administrator selber entscheiden auf welche Art und Weise er auf die einzelnen Kompontenen der Infrastruktur zugreift.

 

Mit der in Nimble OS 2.3 vorgestellten RESTful API, können Aufgaben auf Nimble Storage Systemen automatisiert werden.

 

Auf viele Objekte von Nimble kann jetzt via REST zugegriffen werden. Mit Nimble OS 2.3 gibt es Zugriff auf:

(Dieser Teil wurde bewusst nicht übersetzt)

 

ObjectCreateReadUpdateDelete
Access Control Records
Arrays
Audit Log
CHAP Users
Initiator Groups
Initiators
Jobs
Performance Policies
Pools
Protection Schedules
Protection Templates
Replication Partners
Snapshot Collections
Snapshots
Software Versions
Tokens
Users
Versions
Volume Collections
Volumes

 

Zu den üblichen "CRUD" (Create, Read, Update, Delete) Operationen, gibt es für Volumes / Volume Collections noch die Möglichkeit von Promote, Demote und Restore.

Für eine genaue Übersicht über alle möglichen Befehle und deren Attribute empfehle ich einen Blick auf die Nimble OS API Reference.

 

Die folgenden Beispiele habe ich nicht ins Deutsche übersetzt.

Ich persönlich habe keine Erfahrung mit RestAPI und verlasse mich deshalb lieber auf die Kompetenz meines Kollegen Julian Cates.

 

Now let’s take a look at some example usage. In all instances, the API is accessed through the use of objects written in JavaScript Object Notation (JSON) and the HTTPS protocol. The first step is to submit a JSON object containing a username and password used to authenticate with the array. The array will return an authentication token which is then used with subsequent API calls.

 

 

Using the REST APIs with Linux and Curl

 

Since the objects are submitted via HTTPS, the most straightforward way to demonstrate the functionality is through the use of curl on Linux. In these examples, we’ll also make use of the jq utility, which is a very useful way to parse and visualize the JSON objects which the array returns.

 

In the first example, we’ll obtain a token from the array by submitting our authentication credentials. We’ll need to pass the JSON object containing the username and password. That object in JSON would look like this:

 

{"data":{"password":"mypassword","username":"admin"}}

 

For our example, we’ve assigned this object to a variable called $DATASTRING, and we’re also using a variable for the array name or IP:

rest1.png

 

In the JSON response we receive back from the array, the session_token is what we’re looking for. We will use that token in the header of our future requests. For example, let’s look at how we might go about getting a listing of all the volumes on this particular array. That request would look something like this:

 

curl -k -X 'GET' -H "X-Auth-Token:$TOKEN" https://$ARRAY:5392/v1/volumes 2>/dev/null | ./jq -r .data


To break that down somewhat, we are submitting a GET request through curl and providing a custom header with the –H option.  In this case, we’re using it to submit our authentication token, once again conveniently stored in a variable, $TOKEN. Once again, we’re piping everything to jq in order to help us visualize it.  The abridged output looks like this:

rest2.png

 

Now let’s take it one step further and get all the details about one of our volumes.  We’ll reference it by ID (stored in the $VOLUME variable) in our JSON request:

 

curl -k -X 'GET' -H "X-Auth-Token:$TOKEN" https://$ARRAY:5392/v1/volumes/$VOLUME 2>/dev/null | ./jq -r .data

rest3.png

 

As you can see, there are quite a few volume attributes that can be accessed.  For our last Linux based example, we’ll get a little bit fancy and use jq to just give us a couple of fields that we are interested in – name and size – and use some native Linux tools to sort it alphabetically and apply some formatting to make it easier to read.  The full command line looks like this:

 

curl -k -X 'GET' -H "X-Auth-Token:$TOKEN" https://$ARRAY:5392/v1/volumes/detail 2>/dev/null | ./jq  -r '.data[] | "\(.name) \(.size)"' | sort | awk '{printf "%-50s %s\n", $1, $2}'

rest4.png

 

There you have the Nimble Storage REST API demonstrated in its most primal form. Some of the command lines may look a bit complex at first glance, and mostly this is due to the nature of Linux syntax and the specifics of parsing the response we get back from the array.

 

Using the REST APIs with Windows PowerShell

 

Next, I’d like to run through some similar examples using my personal favorite means of invoking the APIs – Windows PowerShell.

 

One of the cool things about using PowerShell to access the Nimble REST API is that PowerShell has some built-in functions to format JSON and invoke REST APIs. This makes it incredibly intuitive to use. Let’s take a look at how we might perform a similar task in PowerShell as we did in Linux.  In the following example, we will first obtain an authentication token, and then use that token to obtain a listing of volumes and their sizes.

 

The PowerShell code would look something like this:

 

###########################

# Enable HTTPS

###########################

 

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

 

###########################

# Get Token

###########################

 

$array = "<ARRAYNAME>"

$username = "admin"

$password = "<PASSWORD>"

 

 

$data = @{

       username = $username

       password = $password

}

 

$body = convertto-json (@{ data = $data })

 

$uri = "https://" + $array + ":5392/v1/tokens"

$token = Invoke-RestMethod -Uri $uri -Method Post -Body $body

$token = $token.data.session_token

 

###########################

# Get Volume List

###########################

 

$header = @{ "X-Auth-Token" = $token }

$uri = "https://" + $array + ":5392/v1/volumes"

$volume_list = Invoke-RestMethod -Uri $uri -Method Get -Header $header

$vol_array = @();

 

foreach ($volume_id in $volume_list.data.id){

 

       $uri = "https://" + $array + ":5392/v1/volumes/" + $volume_id

       $volume = Invoke-RestMethod  -Uri $uri -Method Get -Header $header

       $vol_array += $volume.data

 

}

 

###########################

# Print Results

###########################

 

$vol_array | sort-object name | select name,size | format-table -autosize


Executing this powershell code would result in the following:

rest5.png

 

As you can see in these few short examples, whatever your preferred automation or orchestration solution is, using it along with the Nimble REST API is a powerful way to programmatically interact with your storage array. We’ve only just scratched the surface here of what is possible. I would encourage you to grab a copy of the API reference, download the attached ZIP file for many more examples, and then go try your hand at it.

 

And if you come up with something really cool, share it here on the Nimble Connect community. We’d love to see it!