Introduction to JSON

I mentioned in the last post that the PAPI uses JSON as the data-interchange format so it's critical you understand how to leverage it if you want to use the PAPI to create, modify, or delete resources.  You can learn more about JSON at www.json.org but the key principle is that it is completely programming language independent and is built on two structures:

  • A collection of name/value pairs
  • An ordered list of values

Example output from getting a system object would look like this:

{

"<object>": {

     "<property>": <value>,

     ...
}

 

So, how do we know what JSON text we need to POST to create an object?  It's simple, we can just take a look at the PAPI self documentation by sending the following request "GET /platform/1/quota/quotas?describe".  Here is the relevant Powershell code:

#Get PAPI self documentation for quotas

$resource = "/platform/1/quota/quotas?describe"

$uri = $baseurl + $resource

$ISIObject = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get

$ISIObject

You can see in the output below that the self documentation will tell you exactly what you need to POST to create a new quota. Pay close attention to the required properties since they may not be the same properties required for the corresponding "isi" command.

selfdocumentation.png

Now that we know what's required for the POST, the following is an example JSON string we can use to create a directory hard quota:

$QuotaObject = @"

{"type":"directory","include_snapshots": false,"container": true, "path": /ifs/home/user1", "enforced": true, "thresholds": {"hard":10000000},"thresholds_include_overhead": false}

"@ 

 

With the JSON string completed, all that's left is to build the Invoke-RestMethod parameters and submit.  Notice in the code below that we specify the JSON string as the body of the POST and that the content type is "application/json":

$headers = @{"Authorization"="Basic $($EncodedPassword)"}

$uri = $baseurl + $resource

$ISIObject = Invoke-RestMethod -Uri $uri -Headers $headers -Body $QuotaObject -ContentType "application/json; charset=utf-8" -Method POST

Write-Host "   Resulting Quota ID: "  $ISIObject.id

 

Putting It All Together

So let's use everything we've learned so far to script what would normally be a tedious, manual process.  Let's assume you have many home directories for your users under a single parent directory (ex. "/ifs/home") and you want to set a directory quota for each of these directories.  We already know how to set the quota on each individual directory based on the information above but how do we get the path to each user home directory?  The answer is that we can leverage the Isilon RESTful Access to the Namespace (RAN) API to access the namespace just like we did for other resources.  The following code will get the subdirectories of a specified path and then set a directory quota on each subdirectory:

# Get subdirectories of path specified

$resource = '/namespace/' + $path

$uri = $baseurl + $resource

$ISIObject = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get

#Loop through each directory and set the quota

ForEach($folder in $ISIObject.children) {

#Create quota

$resource ="/platform/1/quota/quotas"

Write-Host "Setting a $quotasize byte quota on $quotapath"

$QuotaObject = @"

{"type":"directory","include_snapshots": false,"container": true, "path": "$quotapath", "enforced": true, "thresholds": {"hard":$quotasize},"thresholds_include_overhead": false}
"@

$headers = @{"Authorization"="Basic $($EncodedPassword)"}

$uri = $baseurl + $resource

$ISIObject2 = Invoke-RestMethod -Uri $uri -Headers $headers -Body $QuotaObject -ContentType "application/json; charset=utf-8" -Method POST

Write-Host "   Resulting Quota ID: "  $ISIObject2.id

}

 

Here is the output from running the script attached to this post:

setquotacommandline.png

setquotaoutput.png

Hopefully between these two blog posts you now you have all of the information you need to create your own automation scripts using Powershell and the Platform API and the RAN API.