Azure NetApp Files Snapshot Policy SDK Sample for Go
This project demonstrates how to create a snapshot policy for Microsoft.NetApp resource provider using Go SDK. This process can be used with other supported protocol types as well.
In this sample application we perform the following operations:
- NetApp account
- Snapshot policy
- Capacity pool
- NFS v3 volume with a snapshot policy assigned
- NetApp account
- Snapshot policy
- Clean up created resources (not enabled by default)
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)
Subscription needs to be enabled for Azure NetApp Files. For more information, please refer to this document.
Register the snapshot policy Feature. For more information, see Registration steps in Manage snapshots by using Azure NetApp Files. (This step is applicable only during the preview state of the snapshot policy feature.)
Resource Group created
Virtual Network with a delegated subnet to Microsoft.Netapp/volumes resource. For more information, see Guidelines for Azure NetApp Files network planning.
Adjust variable contents within the
example.gofile to match your environment.
For this sample Go console application work, authentication is required. The chosen method for this sample is service principals:
Within an Azure Cloud Shell session, make sure you're logged in at the subscription where you want to be associated with the service principal by default:
az account show
If this is not the correct subscription, use:
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. (Make sure it is not inside of any repo.)
Set an environment variable pointing to the file path you just created. The following example uses Powershell and bash:
[Environment]::SetEnvironmentVariable("AZURE_AUTH_LOCATION", "C:\sdksample\azureauth.json", "User")
Note: for other Azure Active Directory authentication methods for Go, see Authentication methods in the Azure SDK for Go.
What does example.go do
This sample project demonstrates how to create and update Snapshot Policies in Azure NetApp Files for an NFSv3 enabled volume. (This process is the same for NFSv4.1/CIFS volumes). Similar to other examples, the authentication method is based on a service principal. This project will create a NetApp Account, a capacity pool, then a snapshot policy, and finally a single volume using the Standard service level, and assign it to the snapshot policy previously created.
In addition, we use some non-sensitive information from the file-based authentication file where we obtain the subscription ID in the initial stages. This information is used for the test we perform to check if the provided subnet exists before creating any Azure NetApp Files resources, failing execution if it is missing.
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, see the Use file-based authentication section of Authentication methods in the Azure SDK for Go.
Lastly, the clean-up process takes place. (Note that the process is not enabled by default; you need to change the variable
var() section if you want to perform the clean-up). The process deletes all resources in the reverse order following the hierarchy; otherwise, resources that have nested resources cannot be removed. The following deletion order: volume -> capacity pool -> snapshot policy -> NetApp account. If there is an error during the application execution, the clean-up may not take place, and you will need to manually perform this task. The clean-up process uses a function called
WaitForNoANFResource, while other parts of the code uses
WaitForANFResource. Currently, this behavior is required in order to work around a current ARM behavior that reports that the object was deleted when in fact its deletion is still in progress. (Similarly, the current ARM states that the volume is fully created, while this is still finishing up.) Also, you will see functions called
GetANF<resource type>. These functions were created in this sample 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
Note: See Resource limits for Azure NetApp Files for Azure NetApp Files limits.
||Microsoft's Open Source Code of Conduct.|
||GitHub's issue report that describes necessary info while opening a new issue|
||GitHub's pull request template.|
||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.|
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 the sample locally:
cd github.com/Azure-Samples git clone https://github.com/Azure-Samples/netappfiles-go-snapshot-policy-sdk-sample.git
Change folder to netappfiles-go-snapshot-policy-sdk-sample/netappfiles-go-snapshot-policy-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 .