gpt4 book ai didi

java - Azure 使用 Azure Java SDK 在资源组中列出 Azure Database for PostgreSQL 服务器

转载 作者:行者123 更新时间:2023-12-03 00:04:11 26 4
gpt4 key购买 nike

使用 Azure Java SDK 列出我的资源组中存在的 Azure Database for PostgreSQL 服务器的最佳、正确方法是什么?目前,我们使用 ARM 模板进行部署,一旦部署了资源,我们希望能够从 Azure 本身获取有关这些资源的信息。我尝试过以下方式:

PagedList<SqlServer> azureSqlServers = azure1.sqlServers().listByResourceGroup("resourceGrpName");
//PagedList<SqlServer> azureSqlServers = azure1.sqlServers().list();
for(SqlServer azureSqlServer : azureSqlServers) {
System.out.println(azureSqlServer.fullyQualifiedDomainName());
}
System.out.println(azureSqlServers.size());

但是返回的列表大小是0

但是,对于虚拟机,我可以通过以下方式获取信息:

PagedList<VirtualMachine> vms = azure1.virtualMachines().listByResourceGroup("resourceGrpName");
for (VirtualMachine vm : vms) {
System.out.println(vm.name());
System.out.println(vm.powerState());
System.out.println(vm.size());
System.out.println(vm.tags());
}

那么,使用 Azure Java SDK 获取有关 Azure Database for PostgreSQL 的信息的正确方法是什么?

附注获得有关 Azure Database for PostgreSQL 的信息后,我需要有关 Azure Database for MySQL 服务器 的类似信息。

编辑:我在 2 年前看到过这个问题,想知道 Azure 是否添加了对 Azure Database for PostgreSQL/MySQL 服务器的支持。 Azure Java SDK for MySQL/PostgreSQL databases?

最佳答案

所以,我用以下方式实现了它,它可以被视为另一种方式......查看 Github 上的 Azure SDK for java 存储库 ( https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/postgresql ),看起来他们有 beta 版,所以我在 mvnrepository 中搜索了 pom。我在项目中导入了以下 pom(azure-mgmt-postgresql 仍处于测试阶段):

<!-- https://mvnrepository.com/artifact/com.microsoft.azure.postgresql.v2017_12_01/azure-mgmt-postgresql -->
<dependency>
<groupId>com.microsoft.azure.postgresql.v2017_12_01</groupId>
<artifactId>azure-mgmt-postgresql</artifactId>
<version>1.0.0-beta-5</version>
</dependency>

在代码中,以下是我的做法要点:我已经创建了一个服务主体并拥有其信息。但是,任何尝试此操作的人都需要使用 clientIdtenantIdclientSecretsubscriptionId,就像 @Jim 那样徐解释道。

// create the credentials object
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, tenantId, clientSecret, AzureEnvironment.AZURE);

// build a rest client object configured with the credentials created above
RestClient restClient = new RestClient.Builder()
.withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER)
.withCredentials(credentials)
.withSerializerAdapter(new AzureJacksonAdapter())
.withResponseBuilderFactory(new AzureResponseBuilder.Factory())
.withInterceptor(new ProviderRegistrationInterceptor(credentials))
.withInterceptor(new ResourceManagerThrottlingInterceptor())
.build();

// use the PostgreSQLManager
PostgreSQLManager psqlManager = PostgreSQLManager.authenticate(restClient, subscriptionId);
PagedList<Server> azurePsqlServers = psqlManager.servers().listByResourceGroup(resourceGrpName);
for(Server azurePsqlServer : azurePsqlServers) {
System.out.println(azurePsqlServer.fullyQualifiedDomainName());
System.out.println(azurePsqlServer.userVisibleState().toString());
System.out.println(azurePsqlServer.sku().name());
}

注意:Server 类引用 com.microsoft.azure.management.postgresql.v2017_12_01.Server

此外,如果您查看 Azure 类,您会注意到这就是他们内部的工作方式。

作为引用,您可以在 Azure 类中使用 SqlServerManager sqlServerManager 并查看他们如何使用它并创建经过身份验证的管理器,以防您想使用某些服务仍处于预览版测试版

关于java - Azure 使用 Azure Java SDK 在资源组中列出 Azure Database for PostgreSQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60158256/

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