首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >io.netty.channel.ChannelInitializer exceptionCaught使用azure访问成本管理api到csp帐户。以前有人解决过这个问题吗?

io.netty.channel.ChannelInitializer exceptionCaught使用azure访问成本管理api到csp帐户。以前有人解决过这个问题吗?
EN

Stack Overflow用户
提问于 2022-07-01 08:37:36
回答 1查看 85关注 0票数 0

提前谢谢。我正试图通过编程方式访问每个azure订阅的成本,以便在应用程序中表示它。我使用了下面的代码,但它提供了这个netty错误,它似乎处理无效的访问。你以前解决过这个问题吗?我有三个假设:

  • 我对我现在拥有的凭据没有足够的访问权限。关于第一个假设,我认为是错误的,因为凭据是由计费管理员创建的。我认为这会导致一个与此相关的错误。

正如我在1中发现的那样,

  • 似乎不可能在csp帐户中访问成本。我找到了这个2,这是否意味着这个成本不能用蔚蓝的java访问,但是我必须执行特定的gets?但奇怪的是,它没有给出错误“此订阅的成本管理数据不可用”3

  • ,因为没有引入一些凭据。如下所示,导出了4个AZURE_CLIENT_ID、AZURE_TENANT_ID和AZURE_CLIENT_SECRET变量(如果没有,则会出现另一个错误,即azure凭据无法进行身份验证)。但是,如果您查看示例查询,就会发现一个与客户有关的id,我不知道如何找到.

代码语言:javascript
复制
import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.core.util.Context;
import com.azure.resourcemanager.billing.BillingManager;
import com.azure.resourcemanager.costmanagement.CostManagementManager;
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.OperatorType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// Sample to query in a table
public class Query {

  public static void main(String[] args) {

    AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
    TokenCredential credential = new DefaultAzureCredentialBuilder()
            .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
            .build();
    // System.out.println(profile.getTenantId());
    CostManagementManager manager = CostManagementManager
            .authenticate(credential, profile);
    System.out.println(System.getenv("AZURE_TENANT_ID"));
    System.out.println(System.getenv("AZURE_CLIENT_ID"));
    System.out.println(System.getenv("AZURE_CLIENT_SECRET"));

    try{
        customerQueryGroupingModern(manager);
    }
     catch (final Exception e) {
        System.out.println(e);
    }
  }


  //https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/costmanagement/azure-resourcemanager-costmanagement/src/samples/java/com/azure/resourcemanager/costmanagement/QueryUsageSamples.java
      /**
     * Sample code: CustomerQueryGrouping-Modern.
     *
     * @param costManagementManager Entry point to CostManagementManager.
     */
    public static void customerQueryGroupingModern(
        com.azure.resourcemanager.costmanagement.CostManagementManager costManagementManager) {
        costManagementManager
            .queries()
            .usageWithResponse(
                "providers/Microsoft.Billing/billingAccounts/xxxxxxxx/customers/5678",
                new QueryDefinition()
                    .withType(ExportType.USAGE)
                    .withTimeframe(TimeframeType.THE_LAST_MONTH)
                    .withDataset(
                        new QueryDataset()
                            .withGranularity(GranularityType.fromString("None"))
                            .withAggregation(
                                mapOf(
                                    "totalCost",
                                    new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
                            .withGrouping(
                                Arrays
                                    .asList(
                                        new QueryGrouping()
                                            .withType(QueryColumnType.DIMENSION)
                                            .withName("ResourceGroup")))),
                Context.NONE);
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-30 09:31:08

我在google中寻找类似的错误,据说这个问题与一个不包括在内的netty包有关。在自述中,它被描述为使用netty进行身份验证,但没有编写包。最后,我在sdk的资源管理器中找到了一个,并将其包含在pom.xml中:

代码语言:javascript
复制
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-core-http-netty</artifactId>
        <version>1.12.4</version>
    </dependency>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72826560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档