有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Java SDK按名称查找Azure策略定义?

Azure的策略服务附带许多内置策略。可以使用Azure的Java SDK和Azure资源管理器访问它们。可以使用getByName()method in the SDK获取特定策略的定义

代码如下所示:

AzureResourceManager azureResourceManager = AzureResourceManager
        .authenticate(credential, profile)
        .withSubscription("<my-subscription-id>");       
PolicyDefinition policyDefinition = azureResourceManager.policyDefinitions().getByName("<name>");

为了测试这段代码,我去控制台查找预构建策略的名称。我在文本中找到了两个不同的名字:

enter image description here

定义中的另一个:

enter image description here

但是,尝试使用以下任一名称检索策略定义会导致相同的错误:

Status code 404, The policy definition 'Audit VMs that do not use managed disks' could not be found.

Status code 404, The policy definition '06a78e20-9358-41c9-923c-fb736d382a4d' could not be found

问题:这个方法的名字是什么?还是有更好的方法检索策略定义


共 (1) 个答案

  1. # 1 楼答案

    如果希望在java应用程序中获得一个内置策略,可以使用包com.azure.resourcemanager.resources中的方法PolicyClientImpl.getPolicyDefinitions().getBuiltIn()。此外,请注意,内置策略的名称是guide

    比如

    1. 安装sdk
    <dependency>
        <groupId>com.azure.resourcemanager</groupId>
        <artifactId>azure-resourcemanager-resources</artifactId>
        <version>2.1.0</version>
    </dependency>
    
    1. 密码
    String clientId="";
            String clientSecret="";
            String tenant="";
            String subId="";
            AzureProfile profile = new AzureProfile(tenant,subId, AzureEnvironment.AZURE);
            TokenCredential credential = new ClientSecretCredentialBuilder()
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                    .tenantId(tenant)
                    .build();
            PolicyClientImpl policyClient= new PolicyClientBuilder()
                    .pipeline(HttpPipelineProvider.buildHttpPipeline(credential,profile))
                    .endpoint(profile.getEnvironment().getResourceManagerEndpoint())
                    .subscriptionId(profile.getSubscriptionId())
                    .buildClient();
    
            PolicyDefinitionInner policy = policyClient.getPolicyDefinitions().getBuiltIn("04d53d87-841c-4f23-8a5b-21564380b55e");
            System.out.println(policy.policyType());
            System.out.println(policy.description());
    

    enter image description here