gpt4 book ai didi

azure - 如何使用 Azure SDK for Golang 获取网络使用情况/配额?

转载 作者:行者123 更新时间:2023-12-03 06:06:29 27 4
gpt4 key购买 nike

我尝试使用 Azure SDK for Golang 获取有关网络使用情况和配额的信息,并收到与执行以下 CLI 命令相同的结果:

% az network list-usages  --location centralus --out table
Name CurrentValue Limit
----------------------------------------------------------------- -------------- ----------
Virtual Networks 3 1000
(...)

我的实现(我也尝试使用NewListPager而不是Get方法,但结果相似):

import "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/quota/armquota" 
// (...)

subscriptionID := "<MY_SUBSCRIPTION_ID>"
factory, err := armquota.NewClientFactory(creds, nil)
if err != nil {
return err
}

quotaSvc, err := factory.NewClient()
usagesSvc, err := factory.NewUsagesClient()


quotaResult, err := quotaSvc.Get(ctx, "VirtualNetworks", "subscriptions/"+subscriptionID+"/providers/Microsoft.Network/locations/centralus", nil)
if err == nil {
quotaJson, err := quotaResult.MarshalJSON()
if err == nil {
fmt.Println("Quota result=", string(quotaJson), ", name=", *quotaResult.Name)
} else {
fmt.Println("Cannot marshal quota json")
}
} else {
fmt.Println("Cannot get quota")
}

usageResult, err := usagesSvc.Get(ctx, "VirtualNetworks", "subscriptions/"+subscriptionID+"/providers/Microsoft.Network/locations/centralus", nil)
if err == nil {
usagesJson, err := usageResult.MarshalJSON()
if err == nil {
fmt.Println("Usages result=", string(usagesJson), ", name=", *usageResult.Name)
} else {
fmt.Println("Cannot marshal usages json")
}
} else {
fmt.Println("Cannot get usages")
}

输出:

配额结果:

{
"id":"/subscriptions/<MY_SUBSCRIPTION_ID>/providers/Microsoft.Network/locations/centralus/providers/Microsoft.Quota/quotas/VirtualNetworks",
"name":"VirtualNetworks",
"properties":{
"isQuotaApplicable":false,
"limit":{
"limitObjectType":"LimitValue",
"limitType":"Independent",
"value":1000
},
"name":{
"localizedValue":"Virtual Networks",
"value":"VirtualNetworks"
},
"properties":{

},
"unit":"Count"
},
"type":"Microsoft.Quota/Quotas"
}

使用结果:

{
"id":"/subscriptions/<MY_SUBSCRIPTION_ID>/providers/Microsoft.Network/locations/centralus/providers/Microsoft.Quota/usages/VirtualNetworks",
"name":"VirtualNetworks",
"properties":{
"isQuotaApplicable":false,
"name":{
"localizedValue":"Virtual Networks",
"value":"VirtualNetworks"
},
"properties":{

},
"unit":"Count",
"usages":{

}
},
"type":"Microsoft.Quota/Usages"
}

像通过 Azure CLI 获取限制一样获取使用值 3 的正确方法是什么?

最佳答案

How to get network usages/quotas using Azure SDK for Golang?

在我的环境中,使用 Azure CLI 命令我可以获得如下所示的使用结果:

enter image description here

要获取当前值,您需要使用github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/使用 Go 语言的 network/armnetwork/v4 包。

代码:

package main

import (
"context"
"log"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
)

func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
clientFactory, err := armnetwork.NewClientFactory("your-subscription-id", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
pager := clientFactory.NewUsagesClient().NewListPager("centralus", nil)
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
log.Fatalf("failed to advance page: %v", err)
}

for _, usage := range page.UsagesListResult.Value {
if usage.Name.LocalizedValue != nil && *usage.Name.LocalizedValue == "Virtual Networks" {
log.Printf("Usage Name: %s, Current Value: %d, Limit: %d, Unit: %s",
*usage.Name.LocalizedValue, *usage.CurrentValue, *usage.Limit, *usage.Unit)
}
}
}
}

输出:

2023/10/11 13:07:54 Usage Name: Virtual Networks, Current Value: 14, Limit: 1000, Unit: Count

enter image description here

引用:

Usages - List - REST API (Azure Virtual Networks) | Microsoft Learn

关于azure - 如何使用 Azure SDK for Golang 获取网络使用情况/配额?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77266498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com