GithubHelp home page GithubHelp logo

isabella232 / resource-manager-go-resources-and-groups Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azure-samples/resource-manager-go-resources-and-groups

0.0 0.0 0.0 11 KB

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

License: MIT License

Go 100.00%

resource-manager-go-resources-and-groups's Introduction

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 [email protected] with any additional questions or comments.

resource-manager-go-resources-and-groups's People

Contributors

allclark avatar mcardosos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.