I'm trying to get information about network usages and quota using Azure SDK for Golang and receive the same result as executing the following CLI command:
% az network list-usages --location centralus --out table
Name CurrentValue Limit
----------------------------------------------------------------- -------------- ----------
Virtual Networks 3 1000
(...)
My implementation (I also tried to use NewListPager
instead of Get
method, but the result is similar):
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")
}
Output:
Quota result:
{
"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"
}
Usages result:
{
"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"
}
What's the correct way to get the usages value 3
exactly like when getting limits via Azure CLI?
In my environment, Using the Azure CLI command I can get the
usage result
like the below:To get the
current value
you need to usegithub.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4
package using Go lang.Code:
Output:
Reference:
Usages - List - REST API (Azure Virtual Networks) | Microsoft Learn