运行 Azure SDK for Go 返回错误:MSI 不可用

huangapple go评论154阅读模式
英文:

Running Azure SDK for Go return an error: MSI not available

问题

我正在尝试运行 Golang Azure SDK 代码,以获取我的订阅中的 RG(资源组)列表,但是我遇到了以下错误:

2022/01/22 20:25:58 MSI not available
exit status 1


import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
	"github.com/Azure/go-autorest/autorest/azure/auth"
	"github.com/Azure/go-autorest/autorest/to"
	"log"
	"os"
)

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

	if err != nil {
		log.Fatal(err)
	}

	subscriptionID := os.Getenv("AZURE_SUB_ID")

	// 读取资源组
	resGrpClient := resources.NewGroupsClient(subscriptionID)
	resGrpClient.Authorizer = authorize

	// 读取资源组内的资源
	resClient := resources.NewClient(subscriptionID)
	resClient.Authorizer = authorize

	for resGrpPage, err := resGrpClient.List(context.Background(), "", nil); resGrpPage.NotDone(); err = resGrpPage.Next() {

		if err != nil {
			log.Fatal(err)
		}

		for _, resGrp := range resGrpPage.Values() {
			fmt.Println("Resource Group Name: ", to.String(resGrp.Name))
			resList, _ := resClient.ListByResourceGroup(context.Background(), to.String(resGrp.Name), "", "", nil)

			for _, res := range resList.Values() {
				fmt.Println("\t- Resource Name: ", to.String(res.Name), " | Resource Type: ", to.String(res.Type))
			}
		}

	}

}

我正在使用 Goland,并尝试在 WSL Ubuntu 中运行该应用程序。

英文:

I am trying to run Golang Azure SDK code to get a list of RGs in my subscriptions but I am getting the following error:

2022/01/22 20:25:58 MSI not available
exit status 1


import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
	"github.com/Azure/go-autorest/autorest/azure/auth"
	"github.com/Azure/go-autorest/autorest/to"
	"log"
	"os"
)

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

	if err != nil {
		log.Fatal(err)
	}

	subscriptionID := os.Getenv("AZURE_SUB_ID")

	//Read resource groups
	resGrpClient := resources.NewGroupsClient(subscriptionID)
	resGrpClient.Authorizer = authorize

	//Read resources within the resource group
	resClient := resources.NewClient(subscriptionID)
	resClient.Authorizer = authorize

	for resGrpPage, err := resGrpClient.List(context.Background(), "", nil); resGrpPage.NotDone(); err = resGrpPage.Next() {

		if err != nil {
			log.Fatal(err)
		}

		for _, resGrp := range resGrpPage.Values() {
			fmt.Println("Resource Group Name: ", to.String(resGrp.Name))
			resList, _ := resClient.ListByResourceGroup(context.Background(), to.String(resGrp.Name), "", "", nil)

			for _, res := range resList.Values() {
				fmt.Println("\t- Resource Name: ", to.String(res.Name), " | Resource Type: ", to.String(res.Type))
			}
		}

	}

}

I am using Goland and trying to run the app in WSL Ubuntu

答案1

得分: 2

请阅读此文档使用基于环境的身份验证

您有几个选项,并且它们需要特定的环境变量存在。在我的情况下,我使用客户端凭据,所以当我运行我的代码时,我需要这三个环境变量存在。

  1. AZURE_CLIENT_ID
  2. AZURE_CLIENT_SECRET
  3. AZURE_TENANT_ID
英文:

Please read this documentation Use environment-based authentication

You have a couple of options
and they need specific environments variables to be present.
In my case, I use Client credentials so I need to have these 3 envs present when I run my code.

  1. AZURE_CLIENT_ID
  2. AZURE_CLIENT_SECRET
  3. AZURE_TENANT_ID

答案2

得分: 1

解决方案是使用auth.NewAuthorizerFromCLI(),因为auth.NewAuthorizerFromEnvironment不使用Cli,而MSI代表托管系统标识。

英文:

Solution is to use auth.NewAuthorizerFromCLI(), as auth.NewAuthorizerFromEnvironment does not use the Cli and MSI stands for managed system identity.

huangapple
  • 本文由 发表于 2022年1月23日 03:35:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/70816449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定