ONTAP Rest API Discussions
ONTAP Rest API Discussions
Can anyone help me with providing the correct syntax to enable/disable file system analytics?
I am specifically trying to use the CLI command method through the REST API.
So. Using the REST API explicitly to enable/disable file system analytics for a specific volume does work using the following syntax:
curl -siku admin:Netapp1! -X PATCH "https://cluster1/api/storage/volumes/d3573f3b-2db8-11ef-8c73-005056b7beb4" -d '{"analytics.state":"off"}'
However, trying to use the equivalent above command via CLI through REST API doesnt work?
curl -siku admin:Netapp1! --request PATCH "https://cluster1/api/private/cli/volume?vserver=svm1_cluster1&volume=vol1" -d '{"analytics":"on"}'
HTTP/1.1 400 Bad Request
curl -siku admin:Netapp1! --request PATCH "https://cluster1/api/private/cli/volumes/e128068c-2db8-11ef-a91a-005056b765ba" -d '{"analytics.state":"on"}'
HTTP/1.1 404 Not Found
curl -siku admin:Netapp1! -X PATCH "https://cluster1/api/private/cli/volume/analytics?volume=vol1" -d '{"analytics.state":"on"}'
HTTP/1.1 405 Method Not Allowed
I believe I am close. But I dont seem to be using the right syntax using the CLI command through REST API method.
Any help would be appreciated.
Hi,
Here's a PowerShell Example for you:
Param(
[Parameter(Mandatory = $True, HelpMessage="The Cluster name or IP Address")]
[String]$Cluster,
[Parameter(Mandatory = $True, HelpMessage="The Vserver Name")]
[String]$Vserver,
[Parameter(Mandatory = $True, HelpMessage="The Volume Name")]
[String]$Volume,
[Parameter(Mandatory = $True, HelpMessage="The Volume Analytics state")]
[ValidateSet("on","off")]
[String]$State,
[Parameter(Mandatory = $False, HelpMessage = "The Credentials to authenticate to ONTAP")]
[System.Management.Automation.PSCredential]$Credential
)
#'------------------------------------------------------------------------------
Function Get-OntapAuthorization{
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True, HelpMessage = "The Credential to authenticate to the ONTAP cluster")]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]$Credential
)
#'--------------------------------------------------------------------------
#'Set the authentication header to connect to the cluster.
#'--------------------------------------------------------------------------
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName + ':' + $Credential.GetNetworkCredential().Password))
$headers = @{
"Authorization" = "Basic $auth"
"Accept" = "application/json"
"Content-Type" = "application/json"
}
Return $headers;
}#'End Function Get-OntapAuthorization.
#'------------------------------------------------------------------------------
Function Invoke-OntapRestApi{
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True, HelpMessage = "The Cluster Name or IP Address")]
[String]$Cluster,
[Parameter(Mandatory = $True, HelpMessage = "The REST API Method to Invoke")]
[ValidateSet("get","patch","post","delete")]
[String]$Method,
[Parameter(Mandatory = $False, HelpMessage = "The JSON payload used for creating or modifying an object")]
[String]$Body,
[Parameter(Mandatory = $True, HelpMessage = "The URI to invoke")]
[String]$Uri,
[Parameter(Mandatory = $True, HelpMessage = "The Credential to authenticate to the Cluster")]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]$Credential
)
#'--------------------------------------------------------------------------
#'Exit if the body is not provided when calling the post or patch method.
#'--------------------------------------------------------------------------
If(($Method -eq "post" -Or $Method -eq "patch") -And ($Null -eq $Body)){
Write-Warning -Message "The body parameter must be provided when invoking the patch or post method"
Return $Null;
}
#'--------------------------------------------------------------------------
#'Set the Authentication header.
#'--------------------------------------------------------------------------
$headers = Get-OntapAuthorization -Credential $Credential
#'--------------------------------------------------------------------------
#'Invoke the command via the private REST CLI.
#'--------------------------------------------------------------------------
Try{
If($Method -eq "post" -Or $Method -eq "patch"){
$bodyJson = $body | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100
$response = Invoke-RestMethod -Method $Method -Body $bodyJson -Uri $Uri -Headers $headers -ErrorAction Stop
Write-Host "JSON Payload`: $bodyJson"
}Else{
$response = Invoke-RestMethod -Method $Method -Uri $Uri -Headers $headers -ErrorAction Stop
}
Write-Host "Invoked ""$Method"" on URI`: $Uri"
}Catch{
Write-Warning -Message $("Failed Invoking ""$Method"" on URI`: ""$Uri"". Error " + $_.Exception.Message)
Return $Null;
}
Return $response;
}#'End Function Invoke-OntapRestApi.
#'------------------------------------------------------------------------------
#'Enumerate the Volume.
#'------------------------------------------------------------------------------
$headers = Get-OntapAuthorization -Credential $Credential
$uri = "https://$cluster/api/storage/volumes?name=$Volume&svm.name=$Vserver"
Try{
$response = Invoke-RestMethod -Method GET -Uri $Uri -Headers $headers -ErrorAction Stop
Write-Host "Enumerated volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"" using URI`: $uri"
}Catch{
Write-Warning -Message $("Failed Invoking ""$Method"" on URI`: ""$Uri"". Error " + $_.Exception.Message)
}
If($Null -ne $response){
[String]$volumeId = $response.records.uuid
}Else{
Write-Warning -Message "The volume ""$Volume"" was not found on vserver ""$Vserver"" on Cluster ""$Cluster"""
Exit 0
}
#'------------------------------------------------------------------------------
#'Set the Volume Analytics state.
#'------------------------------------------------------------------------------
$uri = "https://$cluster/api/storage/volumes/$volumeId"
$analytics = @{"analytics.state" = $State}
$body = $analytics | ConvertTo-Json
Try{
$response = Invoke-OntapRestApi -Cluster $Cluster -Uri $uri -Method Patch -Body $body -Credential $credential -ErrorAction Stop
Write-Host "Successfully Set Volume analytics state to ""$State"" for volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"" using URI ""$uri"""
}Catch{
Write-Warning -Message $("Failed Setting Volume analytics state to ""$State"" for volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"" using URI ""$uri"". Error " + $_.Exception.Message)
Exit -1
}
#'------------------------------------------------------------------------------
Usage:
PS C:\Scripts> .\EnableVolumeAnalytics.ps1 -Cluster 192.168.100.2 -Vserver vserver1 -Volume cifs_data_001 -State on -Credential $credential
Enumerated volume "cifs_data_001" on vserver "vserver1" on cluster "192.168.100.2" using URI: https://192.168.100.2/api/storage/volumes?name=cifs_data_001&svm.name=vserver1
JSON Payload: {"analytics.state":"on"}
Invoked "Patch" on URI: https://192.168.100.2/api/storage/volumes/d07c117b-10f8-11ef-ac71-005056a0bb1e
Successfully Set Volume analytics state to "on" for volume "cifs_data_001" on vserver "vserver1" on cluster "192.168.100.2" using URI "https://192.168.100.2/api/storage/volumes/d07c117b-10f8-11ef-ac71-005056a0bb1e"
PS C:\Scripts> .\EnableVolumeAnalytics.ps1 -Cluster 192.168.100.2 -Vserver vserver1 -Volume cifs_data_001 -State off -Credential $credential
Enumerated volume "cifs_data_001" on vserver "vserver1" on cluster "192.168.100.2" using URI: https://192.168.100.2/api/storage/volumes?name=cifs_data_001&svm.name=vserver1
JSON Payload: {"analytics.state":"off"}
Invoked "Patch" on URI: https://192.168.100.2/api/storage/volumes/d07c117b-10f8-11ef-ac71-005056a0bb1e
Successfully Set Volume analytics state to "off" for volume "cifs_data_001" on vserver "vserver1" on cluster "192.168.100.2" using URI "https://192.168.100.2/api/storage/volumes/d07c117b-10f8-11ef-ac71-005056a0bb1e"
Hope that helps
/Matt
Thank you for your response.
Looking at your code. You're using the NetApp Rest API explicitly. The method I am trying to use is the private CLI passthrough with the ONTAP REST API. This method would eliminate the need to not only download the modules but also the need to invoke the import module(s) into my script.
Hi,
Correct, technically you can use either the native REST API's (public or private) directly using REST. The NetApp.ONTAP PowerShell Module will invoke a system version check to determine if you are running ONTAP 9.6 or greater and default to using the REST API under the covers. You can leverage it to avoid having to write your own functions but there's nothing stopping you from writing code to interactive with ONTAP's REST API's without using PowerShell Modules.
This method also works with the private CLI passthrough. You can run any command in ONTAP natively via HTTPS using this method. Here's an example:
Hope the examples helps
/Matt