services | platforms | author |
---|---|---|
azure-resource-manager |
go |
mcardosos |
This package demonstrates how to manage resources and resource groups using Azure SDK for Go.
If you don't have a Microsoft Azure subscription you can get a FREE trial account here.
On this page
-
If you don't already have it, install Go 1.7.
-
Get the sample. You can use either
go get github.com/Azure-Samples/resource-manager-go-resources-and-groups
or
git clone https://github.com:Azure-Samples/virtual-machines-go-manage.git
-
Install the dependencies using glide.
cd virtual-machines-go-manage glide install
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
Set the following environment variables using the information from the service principle that you created.
export AZURE_TENANT_ID={your tenant id} export AZURE_CLIENT_ID={your client id} export AZURE_CLIENT_SECRET={your client secret} export AZURE_SUBSCRIPTION_ID={your subscription id}
[AZURE.NOTE] On Windows, use
set
instead ofexport
. -
Run the sample.
go run example.go
rg := resources.ResourceGroup{
Location: to.StringPtr(location),
}
_, err := groupsClient.CreateOrUpdate(groupName, rg)
The sample updates the resource group with tags.
rg.Tags = &map[string]*string{
"who rocks": to.StringPtr("golang"),
"where": to.StringPtr("on azure"),
}
_, err := groupsClient.CreateOrUpdate(groupName, rg)
groupsList, err := groupClient.List("", nil)
In this sample, a Key Vault is created, but it can be any resource.
genericResource := resources.GenericResource{
Location: to.StringPtr(location),
Properties: &map[string]interface{}{
"sku": map[string]string{
"Family": "A",
"Name": "standard",
},
"tenantID": tenantID,
"accessPolicies": []string{},
"enabledForDeployment": true,
},
}
_, err := resourcesClient.CreateOrUpdate(groupName, namespace, "", resourceType, resourceName, genericResource, nil)
gr.Tags = &map[string]*string{
"who rocks": to.StringPtr("golang"),
"where": to.StringPtr("on azure"),
}
_, err := resourcesClient.CreateOrUpdate(groupName, namespace, "", resourceType, resourceName, gr, nil)
resourcesList, err := groupsClient.ListResources(groupName, "", "", nil)
Resources can be exported into a json file. The asterisk * indicates all resources should be exported. Later, the json file can be used for template deployment.
// The asterisk * indicates all resources should be exported.
expReq := resources.ExportTemplateRequest{
Resources: &[]string{"*"},
}
template, err := groupsClient.ExportTemplate(groupName, expReq)
onErrorFail(err, "ExportTemplate failed")
prefix, indent := "", " "
exported, err := json.MarshalIndent(template, prefix, indent)
onErrorFail(err, "MarshalIndent failed")
fileTemplate := "%s-template.json"
fileName := fmt.Sprintf(fileTemplate, groupName)
if _, err := os.Stat(fileName); err == nil {
onErrorFail(fmt.Errorf("File '%s' already exists", fileName), "Saving JSON file failed")
}
ioutil.WriteFile(fileName, exported, 0666)
_, err := resourcesClient.Delete(groupName, namespace, "", resourceType, resourceName, nil)
_, err := groupsClient.Delete(groupName, nil)
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.