Ontwikkelen voor Azure NetApp Files met REST API met behulp van PowerShell
De REST API voor de Azure NetApp Files-service definieert HTTP-bewerkingen voor resources zoals het NetApp-account, de capaciteitspool, de volumes en momentopnamen. Dit artikel helpt u aan de slag te gaan met de Azure NetApp Files REST API powershell te gebruiken.
Azure NetApp Files REST API specificatie
De REST API specificatie voor Azure NetApp Files wordt gepubliceerd via GitHub:
https://github.com/Azure/azure-rest-api-specs/tree/master/specification/netapp/resource-manager
Toegang tot de Azure NetApp Files REST API
Installeer de Azure CLI als u dit nog niet hebt gedaan.
Maak een service-principal in uw Azure Active Directory (Azure AD):
Controleer of u voldoende machtigingen hebt.
Voer de volgende opdracht in Azure CLI in:
$RBAC_SP = az ad sp create-for-rbac --name <YOURSPNAMEGOESHERE> --role Contributor | ConvertFrom-JsonAls u de informatie over de service-principal wilt weergeven,
$RBAC_SPtypt u en drukt u op Enter.appId : appID displays here displayName : displayName name : http://SP_Name password : super secret password tenant : your tenant shows hereDe uitvoer wordt opgeslagen in het variabele -object
$RBAC_SP. We gebruiken de$RBAC_SP.appIdwaarden$RBAC_SP.password, en$RBAC_SP.tenant.
Een OAuth-toegangsteken aanvragen:
In de voorbeelden in dit artikel wordt PowerShell gebruikt. U kunt ook verschillende API-hulpprogramma's gebruiken, zoals Postman, Slapeloosheiden Paw.
Met behulp
$RBAC_SPvan de variabele halen we nu het Bearer-token op.$body = "grant_type=client_credentials&client_id=$($RBAC_SP.appId)&client_secret=$($RBAC_SP.password)&resource=https://management.azure.com/" $BearerToken = Invoke-RestMethod -Method Post -body $body -Uri https://login.microsoftonline.com/$($RBAC_SP.tenant)/oauth2/tokenDe uitvoer biedt een Bearer Token-object. U kunt het toegangs token zien door te
$BearerToken.access_tokentypen. Deze ziet er ongeveer uit zoals in het volgende voorbeeld:eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5iQ3dXMTF3M1hrQi14VWFYd0tSU0xqTUhHUSIsImtpZCI6Im5iQ3dXMTF3M1hrQi14VWFYd0tSU0xqTUhHUSJ9Het weergegeven token is 3600 seconden geldig. Daarna moet u een nieuw token aanvragen. Het token wordt opgeslagen in de variabele en wordt gebruikt in de volgende stap.
Maak het
headers-object:$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization", "Bearer $($BearerToken.access_token)") $headers.Add("Content-Type", "application/json")Verzend een testoproep en voeg het token toe om uw toegang tot de REST API:
$SubId = (Get-AzureRmContext).Subscription.Id Invoke-RestMethod -Method Get -Headers $headers -Uri https://management.azure.com/subscriptions/$SubId/providers/Microsoft.Web/sites?api-version=2019-11-01
Voorbeelden van het gebruik van de API
In dit artikel wordt de volgende URL gebruikt voor de basislijn van aanvragen. Deze URL wijst naar de hoofdmap van Azure NetApp Files naamruimte.
https://management.azure.com/subscriptions/$SUBID/resourceGroups/$RESOURCEGROUP/providers/Microsoft.NetApp/netAppAccounts?api-version=2019-11-01
U moet variabele waarden toewijzen voordat u de volgende voorbeelden met uw eigen waarden gaat uitvoeren. PowerShell-variabelen worden gebruikt door te $variablename typen.
PowerShell-variabelen worden toegewezen met behulp van $variablename = “value” .
$Region = “westus2"
$ResourceGroup = “MYTest-RG-63"
$ANFvnetname = “NetAppFilesVnet-63"
$ANFvnetCIDR = “10.63.64.0/18"
$ANFsubnet = “NetAppFilesSubnet-63"
$ANFsubnetCIDR = “10.63.120.0/28"
$ANFsubnetID = “/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.Network/virtualNetworks/$ANFvnetname/subnets/$ANFSubnet"
$ANFAccount = “TestoftheAPI"
$ANFCapacityPool = “ANFTestPool"
$ANFServicelevel = “Standard"
$ANFVolume = “ANFTestVolume"
$ANFVolumeShareName = “Share-TEST"
$ANFVolumesize = 100GB
$ANFSnapshot = “ANFTestSnapshot"
Voorbeelden van PUT-aanvragen
U gebruikt een PUT-aanvraag om nieuwe objecten te maken in Azure NetApp Files, zoals de volgende voorbeelden laten zien. De body van de PUT-aanvraag bevat de in JSON opgemaakte gegevens voor de wijzigingen. Deze moet worden opgenomen in de PowerShell-opdracht als tekst of als een bestand waarnaar wordt verwezen. Als u wilt verwijzen naar de body als een bestand, moet u het json-voorbeeld opslaan in een bestand en toevoegen -body (Get-Content @<filename>) aan de PowerShell-opdracht.
#create a NetApp account
$body = "{
`"name`": `"$ANFAccount`",
`"type`": `"Microsoft.NetApp/netAppAccounts`",
`"location`": `"$Region`",
`"properties`": {
`"name`": `"$ANFAccount`"
}
} "
$api_version = "2020-02-01"
Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount`?api-version=$api_version"
#create a capacity pool
$body = "{
`"location`": `"$Region`",
`"properties`": {
`"size`": " + 4TB + ",
`"serviceLevel`": `"Standard`"
}
}"
$api_version = "2020-02-01"
Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool`?api-version=$api_version"
#create a volume
$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"
Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool/volumes/$ANFVolume`?api-version=$api_version"
#create a volume snapshot
$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'
Invoke-RestMethod -Method 'PUT' -Headers $headers -Body $body "https://management.azure.com/subscriptions/$SubID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts/$ANFAccount/capacityPools/$ANFCapacityPool/volumes/$ANFVolume/Snapshots/$ANFSnapshot`?api-version=$api_version"
JSON-voorbeelden
In het volgende voorbeeld ziet u hoe u een NetApp-account maakt:
{
"name": "MYNETAPPACCOUNT",
"type": "Microsoft.NetApp/netAppAccounts",
"location": "westus2",
"properties": {
"name": "MYNETAPPACCOUNT"
}
}
In het volgende voorbeeld ziet u hoe u een capaciteitspool maakt:
{
"name": "MYNETAPPACCOUNT/POOLNAME",
"type": "Microsoft.NetApp/netAppAccounts/capacityPools",
"location": "westus2",
"properties": {
"name": "POOLNAME",
"size": "4398046511104",
"serviceLevel": "Premium"
}
}
In het volgende voorbeeld ziet u hoe u een nieuw volume maakt. (Het standaardprotocol voor het volume is NFSV3.)
{
"name": "MYNEWVOLUME",
"type": "Microsoft.NetApp/netAppAccounts/capacityPools/volumes",
"location": "westus2",
"properties": {
"serviceLevel": "Premium",
"usageThreshold": "322122547200",
"creationToken": "MY-FILEPATH",
"snapshotId": "",
"subnetId": "/subscriptions/$SUBID/resourceGroups/$RESOURCEGROUP/providers/Microsoft.Network/virtualNetworks/VNETGOESHERE/subnets/MYDELEGATEDSUBNET.sn"
}
}
In het volgende voorbeeld ziet u hoe u een momentopname van een volume maakt:
{
"name": "apitest2/apiPool01/apiVol01/snap02",
"type": "Microsoft.NetApp/netAppAccounts/capacityPools/Volumes/Snapshots",
"location": "westus2",
"properties": {
"name": "snap02",
"fileSystemId": "0168704a-bbec-da81-2c29-503825fe7420"
}
}
Notitie
U moet opgeven voor fileSystemId het maken van een momentopname. U kunt de waarde fileSystemId verkrijgen met een GET-aanvraag naar een volume.
Voorbeelden van GET-aanvragen
Er treedt een fout op als de resource niet bestaat. U gebruikt een GET-aanvraag om objecten van Azure NetApp Files in een abonnement op te vragen, zoals de volgende voorbeelden laten zien:
#get NetApp accounts
Invoke-RestMethod -Method Get -Headers $headers -Uri https://management.azure.com/subscriptions/$SUBID/resourceGroups/$ResourceGroup/providers/Microsoft.NetApp/netAppAccounts?api-version=2019-11-01 | ConvertTo-Json
#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=2019-11-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=2019-11-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=2019-11-01 | ConvertTo-Json
PowerShell-scripts voltooien
In deze sectie ziet u voorbeeldscripts voor 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"
}