Develop for Azure NetApp Files with REST API using PowerShell
Article
The REST API for the Azure NetApp Files service defines HTTP operations against resources such as the NetApp account, the capacity pool, the volumes, and snapshots. This article helps you get started with using the Azure NetApp Files REST API using PowerShell.
Azure NetApp Files REST API specification
The REST API specification for Azure NetApp Files is published through GitHub:
The output provides a Bearer Token object. You can see the access token by typing $BearerToken.access_token. It looks similar to the following example:
The displayed token is valid for 3600 seconds. After that, you need to request a new token. The token is saved in the variable and will be used in the next step.
You should assign variable values before running the following examples with your own values.
PowerShell variables are accessed by typing $variablename.
PowerShell variables are assigned by using $variablename = “value”.
You use a PUT request to create new objects in Azure NetApp Files, as the following examples show. The body of the PUT request includes the JSON formatted data for the changes. It must be included in the PowerShell command as text or referenced as a file. To reference the body as a file, save the json example to a file and add -body (Get-Content @<filename>) to the PowerShell command.
You need to specify fileSystemId for creating a snapshot. You can obtain the fileSystemId value with a GET request to a volume.
GET request examples
An error occurs if the resource does not exist. You use a GET request to query objects of Azure NetApp Files in a subscription, as the following examples show:
#get capacity pools for NetApp account
Invoke-RestMethod -Method Get -Headers $headers -Uri https://management.azure.com/subscriptions/$SUBID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFACCOUNT/capacityPools?api-version=2022-05-01 | ConvertTo-Json
#get volumes in NetApp account & capacity pool
Invoke-RestMethod -Method Get -Headers $headers -Uri https://management.azure.com/subscriptions/$SUBID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFACCOUNT/capacityPools/$ANFCAPACITYPOOL/volumes?api-version=2022-05-01 | ConvertTo-Json
#get snapshots for a volume
Invoke-RestMethod -Method Get -Headers $headers -Uri https://management.azure.com/subscriptions/$SUBID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFACCOUNT/capacityPools/$ANFCAPACITYPOOL/volumes/$ANFVOLUME/snapshots?api-version=2022-05-01 | ConvertTo-Json
Complete PowerShell scripts
This section shows sample scripts for PowerShell.
<#
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
#>
$Region = "westus2"
$SubID = (Get-AzureRmContext).Subscription.Id
$MyRandomID = Get-Random -Minimum 100 -Maximum 999
$ResourceGroup = "MYTest-RG-" + $MyRandomID
$ANFAccount = "ANFTestAccount-$Region-" + $MyRandomID
$ANFCapacityPool = "ANFTestPool-$Region-" + $MyRandomID
$ANFVolume = "ANFTestVolume-$Region-" + $MyRandomID
$ANFVolumesize = 100GB
$ANFVolumeShareName = "Share-" + $MyRandomID
$ANFSnapshot = "ANFTestSnapshot-$Region-" + $MyRandomID
$ANFServicelevel = "Standard"
$Octet2 = Get-Random -Minimum 1 -Maximum 254
$ANFvnetname = "NetAppFilesVnet-$Octet2-$Region-" + $MyRandomID
$ANFvnetCIDR = "10.$Octet2.64.0/18"
$ANFsubnet = "NetAppFilesSubnet-$Octet2-$Region-" + $MyRandomID
$ANFsubnetCIDR = "10.$Octet2.120.0/28"
$vmsubnet = "VM-subnet-$Octet2-$Region-" + $MyRandomID
$vmsubnetCIDR = "10.$Octet2.121.0/24"
$BearerToken = (az account get-access-token | ConvertFrom-Json).accessToken
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $BearerToken")
$headers.Add("Content-Type", "application/json")
" ** Creating Resource Group $ResourceGroup *************"
$api_version = "2020-01-01"
$body = " {
`"location`": `"$Region`",
`"name`": `"$ResourceGroup`"
},"
$response = Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body -Uri "https://management.azure.com/subscriptions/$SubID/resourcegroups/$ResourceGroup`?api-version=$api_version"
While ($response.properties.provisioningState -notmatch "Succeeded") {
$response.properties.provisioningState
sleep 5
$response = Invoke-RestMethod -Method ‘GET’ -Headers $headers -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup`?api-version=$api_version"
}
" ** Creating Virtual Network $ANFvnetname *************"
$body = "{
`"properties`": {
`"addressSpace`": {
`"addressPrefixes`": [
`"$ANFvnetCIDR`"
]
},
`"subnets`": [
{
`"name`": `"$ANFsubnet`",
`"properties`": {
`"addressPrefix`": `"$ANFsubnetCIDR`",
`"delegations`": [
{
`"name`": `"Microsoft.NetApp`",
`"properties`": {
`"serviceName`": `"Microsoft.NetApp/volumes`"
}
}
]
}
},
{
`"name`": `"$vmsubnet`",
`"properties`": {
`"addressPrefix`": `"$vmsubnetCIDR`"
}
}
]
},
`"location`": `"$Region`"
}"
$api_version = "2020-03-01"
$response = Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.Network/virtualNetworks/$ANFvnetname`?api-version=$api_version"
While ($response.properties.provisioningState -notmatch "Succeeded") {
sleep 5
$response = Invoke-RestMethod -Method ‘GET’ -Headers $headers -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.Network/virtualNetworks/$ANFvnetname`?api-version=$api_version"
$response.properties.provisioningState
}
$ANFsubnetID = $response.properties.subnets.id[0]
" ** Creating ANF Account $ANFAccount *************"
$body = "{
`"name`": `"$ANFAccount`",
`"type`": `"Microsoft.NetApp/netAppAccounts`",
`"location`": `"$Region`",
`"properties`": {
`"name`": `"$ANFAccount`"
}
} "
$api_version = "2020-02-01"
$response = Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount`?api-version=$api_version"
$response.properties.provisioningState
While ($response.properties.provisioningState -notmatch "Succeeded") {
$response.properties.provisioningState
sleep 5
$response = Invoke-RestMethod -Method ‘GET’ -Headers $headers -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount`?api-version=$api_version"
}
" ** Creating Capacity Pool $ANFCapacityPool *************"
$body = "{
`"location`": `"$Region`",
`"properties`": {
`"size`": " + 4TB + ",
`"serviceLevel`": `"Standard`"
}
}"
$api_version = "2020-02-01"
$response = Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool`?api-version=$api_version"
$response.properties.provisioningState
While ($response.properties.provisioningState -notmatch "Succeeded") {
$response.properties.provisioningState
sleep 5
$response = Invoke-RestMethod -Method ‘GET’ -Headers $headers -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount`?api-version=$api_version"
}
" ** Creating ANF Volume $ANFVolume *************"
$body = "{
`"name`": `"$ANFVolume`",
`"type`": `"Microsoft.NetApp/netAppAccounts/capacityPools/volumes`",
`"location`": `"$Region`",
`"properties`": {
`"serviceLevel`": `"$ANFServicelevel`",
`"usageThreshold`": `"$ANFVolumesize`",
`"creationToken`": `"$ANFVolumeShareName`",
`"snapshotId`": `"`",
`"subnetId`": `"$ANFSubnetID`"
}
}"
$api_version = "2020-02-01"
$response = Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool/volumes/$ANFVolume`?api-version=$api_version"
While ($response.properties.provisioningState -notmatch "Succeeded") {
$response.properties.provisioningState
sleep 15
$response = Invoke-RestMethod -Method ‘GET’ -Headers $headers -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool/volumes/$ANFVolume`?api-version=$api_version"
}
$Volume = $response
$FileSystemID = $response.properties.fileSystemId
" ** Creating ANF Volume Snapshot $ANFSnapshot *************"
$body = "{
`"name`": `"$ANFAccount/$ANFCapacityPool/$ANFVolume/$ANFSnapshot`",
`"type`": `"Microsoft.NetApp/netAppAccounts/capacityPools/Volumes/Snapshots`",
`"location`": `"$Region`",
`"properties`": {
`"name`": `"$ANFSnapshot`",
`"fileSystemId`": `"$FileSystemID`"
}
}"
$api_version = '2020-02-01'
$response = Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool/volumes/$ANFVolume/Snapshots/$ANFSnapshot`?api-version=$api_version"
While ($response.properties.provisioningState -notmatch "Succeeded") {
$response.properties.provisioningState
sleep 5
$response = Invoke-RestMethod -Method ‘GET’ -Headers $headers -Uri "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool/volumes/$ANFVolume/Snapshots/$ANFSnapshot`?api-version=$api_version"
}
Learn about the key parts of Azure NetApp Files, including storage terminology and monitoring strategies, to help you optimize your setup and deployment processes.