0

Description

I wanted to perform the following task using GO SDK for which I couldn't found any good example:

  • Establishing Connection with Azure
  • Stopping a VM instance
  • Restarting the VM instance
  1. The first part is establishing the connection for which I used:
package main

import (
        "fmt"
        "github.com/Azure/go-autorest/autorest/azure/auth"
)

func main() {
  authorizer, err := auth.NewAuthorizerFromEnvironment()

not sure it is the correct one. Anything else we need to set up here.

  1. For stopping the VM couldn't get the exact function but in the Go SDK code I found one:

https://github.com/Azure-Samples/azure-sdk-for-go-samples/blob/ffcdafe9818d55dbc2134db1548e1ed10b4a6092/compute/vm.go#L168

  1. Same for starting the VM:

https://github.com/Azure-Samples/azure-sdk-for-go-samples/blob/ffcdafe9818d55dbc2134db1548e1ed10b4a6092/compute/vm.go#L184

How to use it in GO SDK using the client?

UDIT GAURAV
  • 53
  • 2
  • 5
  • Any more updates on this question? Does it solve the problem or what's else you need? If it works for you please accept it. – Charles Xu Feb 01 '21 at 01:30
  • It mostly solves my problem Thanks for this @CharlesXu. So by using file bases authentication we pass values in azure.auth which also contains the subscription id so can we fetch and use this subscription id from the azure.auth file for creating client. If so then how? – UDIT GAURAV Feb 19 '21 at 21:37

1 Answers1

1

Of course, you can use the environment to authenticate and already set all the necessary environment variables. Then there is no problem with the samples.

If you're not familiar with the necessary environment variables, I recommend you use file-based authentication. It's more simple.

You can simply create the VM client with your select authorizer:

vmClient := compute.NewVirtualMachinesClient("subcriptionID")

This VM client will help you start and stop the VM as you want.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39