java - 如何列出与我的 Azure 资源关联的策略?
问题描述
对于我的 Azure 帐户中的每个资源,我想列出一些关于它的基本信息,然后找到与之关联的策略。使用 Azure 的 Java SDK,这是我目前所拥有的:
AzureResourceManager azureResourceManager = AzureResourceManager
.authenticate(credential, profile)
.withSubscription("<my-subscription-id>");
for(GenericResource resource : azureResourceManager.genericResources().list())
{
System.out.println("Resource Name: " + resource.name());
System.out.println("Resource ID: " + resource.id());
PagedIterable<PolicyAssignment> policiesAssignmentsForThisResource = azureResourceManager.policyAssignments().listByResource(resource.id());
for(PolicyAssignment policyAssignment : policiesAssignmentsForThisResource)
{
System.out.println("Policy Assignment Display Name: " + policyAssignment.displayName());
}
}
这将列出资源名称和 ID,但是当尝试遍历策略分配时,它会引发以下错误:
IllegalArgumentException: Parameter parentResourcePath is required and cannot be null.
有没有办法解决这个错误?有没有更好的方法来查找资源的策略?
这listByResource()
是我正在使用的方法:
解决方案
如果您想检查与一个资源关联的那些策略的状态,请参考以下代码。
注意:这是版本 1 API,而不是当前版本 2 API,并且此代码仍处于测试阶段。
开发工具包
<dependency>
<groupId>com.microsoft.azure.policyinsights.v2019_10_01</groupId>
<artifactId>azure-mgmt-policyinsights</artifactId>
<version>1.0.0-beta-2</version>
</dependency>
代码
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId,
tenant,
clientSecret,
AzureEnvironment.AZURE);
RestClient restClient= new RestClient.Builder()
.withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER)
.withCredentials(credentials)
.withSerializerAdapter( new AzureJacksonAdapter())
.withResponseBuilderFactory(new AzureResponseBuilder.Factory())
.build();
PolicyInsightsClientImpl policyInsightsClient = new PolicyInsightsClientImpl(restClient);
PagedList<PolicyStateInner> policys = policyInsightsClient.policyStates().listQueryResultsForResource(
PolicyStatesResource.DEFAULT,
"/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/andywin7"
);
for(PolicyStateInner policy : policys){
System.out.println(policy.complianceState());
}
推荐阅读
- pywinauto - 如何在pywinauto中连接到control.exe的子窗口
- docker - docker build -“连接时出错:此错误可能表明 docker 守护程序没有运行。”
- docker - 如何将我的 docker 容器放在生产服务器上?
- html - 在不进入第二个 div 的情况下向左浮动
- java - 如何在 textView 中显示来自 SQLite 的值?
- css - Bootstrap轮播如何更改从一个图像到另一个图像的过渡时间
- c# - 如何通过 element.findElements[n].findElement() 查找表格的内部元素
- powershell - Powershell - 是否可以在管道内进行管道传输?- 基于 Get-AzureAdUser 列表的 Get-AzureADDevices
- ios - 显示/隐藏列表部分 SwiftUI
- javascript - Javascript 等待循环 Promies.all 完成