Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

This package demonstrates how to manage resources and resource groups in Azure using Go.

License

Notifications You must be signed in to change notification settings

Azure-Samples/resource-manager-go-resources-and-groups

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

services platforms author
azure-resource-manager
go
mcardosos

Manage Azure resources and resource groups with Go

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

Run this sample

  1. If you don't already have it, install Go 1.7.

  2. 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
    
  3. Install the dependencies using glide.

    cd virtual-machines-go-manage
    glide install
    
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. 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 of export.

  6. Run the sample.

    go run example.go
    

What does example.go do?

Create resource group

	rg := resources.ResourceGroup{
		Location: to.StringPtr(location),
	}
	_, err := groupsClient.CreateOrUpdate(groupName, rg)

Update the resource group

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)

List resource groups in subscription

groupsList, err := groupClient.List("", nil)

Create a generic resource

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)

Update the resource with tags

	gr.Tags = &map[string]*string{
		"who rocks": to.StringPtr("golang"),
		"where":     to.StringPtr("on azure"),
	}
	_, err := resourcesClient.CreateOrUpdate(groupName, namespace, "", resourceType, resourceName, gr, nil)

List resources inside the resource group

	resourcesList, err := groupsClient.ListResources(groupName, "", "", nil)

Export resource group template to a json file

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)

Delete a generic resource

	_, err := resourcesClient.Delete(groupName, namespace, "", resourceType, resourceName, nil)

Delete the resource group

	_, err := groupsClient.Delete(groupName, nil)

More information


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.

About

This package demonstrates how to manage resources and resource groups in Azure using Go.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages