Azure NetAppFiles SDK Sample for Go
This project demonstrates how to perform management CRUD operations for Microsoft.NetApp resource provider using GoLang.
In this sample application we perform the following operations:
- NetApp Files Account
- Capacity Pool
- Volumes (one NFSv3 and one NFSv4.1)
- Snapshot NFSv3 volume
- Volume from Snapshot (NFSv3)
- Change the NFSv4.1 Volume size from 100GiB to 200GiB
- Deletions (when cleanup variable is set to true)
- Capacity Pools
If you don't already have a Microsoft Azure subscription, you can get a FREE trial account here.
- Go installed (if not installed yet, follow the official instructions)
- Azure Subscription.
- Subscription needs to be whitelisted for Azure NetApp Files. For more information, please refer to this document.
- Resource Group created
- Virtual Network with a delegated subnet to Microsoft.Netapp/volumes resource. For more information, please refer to Guidelines for Azure NetApp Files network planning
- Adjust variable contents within
example.gofile to match your environment
- For this sample Go console application work, we need to authenticate and the chosen method for this sample is using service principals.
Within an Azure Cloud Shell session, make sure you're logged on at the subscription where you want to be associated with the service principal by default:
bash az account showIf this is not the correct subscription, use
bash az account set -s <subscription name or id>
Create a service principal using Azure CLI
az ad sp create-for-rbac --sdk-auth
Note: this command will automatically assign RBAC contributor role to the service principal at subscription level, you can narrow down the scope to the specific resource group where your tests will create the resources.
Copy the output content and paste it in a file called azureauth.json and secure it with file system permissions
Set an environment variable pointing to the file path you just created, here is an example with Powershell and bash: Powershell
[Environment]::SetEnvironmentVariable("AZURE_AUTH_LOCATION", "C:\sdksample\azureauth.json", "User")
Note: for other Azure Active Directory authentication methods for Go, please refer to Authentication methods in the Azure SDK for Go.
What is example.go doing
Currently, Azure NetApp Files SDK exposes control plane management operations, CRUD operations for its resources like accounts, capacity pools, volumes and snapshots. We start this execution by defining some basic variables that will be used throughout the code to define resource group name, location, account name, virtual network, subnet names, etc.
Note: Please refer to Resource limits for Azure NetApp Files to understand ANF's most current limits.
Next, it will move forward and obtain some non-sensitive information from the file-based authentication file that is used at the initial stages to identify the subscription ID for the test we perform to check if the subnet provided exists before starting creating any ANF resource. Authentication is made on each operation where we obtain an authorizer to pass to each client we instantiate (in Azure Go SDK for NetAppFiles each resource has its own client). For more information about the authentication process used, refer to Use file-based authentication section of Authentication methods in the Azure SDK for Go document.
Then, it will start the CRUD operations by creating one account, then capacity pool, volumes, snapshot and volume from snapshot, in this exact sequence (for more information about Azure NetApp Files storage hierarchy please refer to this document). After all resources are created, it will perform an update to a volume by changing its usage threshold (size) doubling its size in this example.
Finally, the clean up process takes place (not enabled by default, please change variable
example.go file if you want clean up to take place), deleting all resources in the reverse order following the hierarchy otherwise we can't remove resources that have nested resources still live. You will also notice that the clean up process uses a function called
WaitForNoANFResource, at this moment this is required so we can workaround a current ARM behavior of reporting that the object was deleted when in fact its deletion is still in progress. We will also notice some functions called
GetANF<resource type>, these were also created in this sample to be able to get the name of the resource without its hierarchy represented in the
<resource type>.name property, which cannot be used directly in other methods of Azure NetApp Files client like
||Folder that contains screenshots.|
||Sample source code folder.|
||Sample main file.|
||The go.mod file defines the module’s module path, which is also the import path used for the root directory, and its dependency requirements, which are the other modules needed for a successful build.|
||The go.sum file contains hashes for each of the modules and it's versions used in this sample|
||Folder that contains all internal packages dedicated to this sample.|
||Package that allows us to get the
||Provides models for this sample, e.g.
||Contains all functions that directly uses the SDK and some helper functions.|
||Provides various functions to parse resource IDs and get information or perform validations.|
||Provides generic functions.|
||Define what to ignore at commit time.|
||List of changes to the sample.|
||Guidelines for contributing to the sample.|
||This README file.|
||The license for the sample.|
||Microsoft's Open Source Code of Conduct.|
How to run
Go to your GOPATH folder and create the following path
# PowerShell example cd $env:GOPATH/src mkdir ./github.com/Azure-Samples
# Bash example cd $GOPATH/src mkdir -p ./github.com/Azure-Samples
Clone it locally
cd github.com/Azure-Samples git clone https://github.com/Azure-Samples/netappfiles-go-sdk-sample.git
Change folder to netappfiles-go-sdk-sample/netappfiles-go-sdk-sample
Make sure you have the
azureauth.jsonand its environment variable with the path to it defined (as previously described at prerequisites)
Edit file example.go
var()block and change the variables contents as appropriate (names are self-explanatory).
Run the sample
go run .