Azure 使用 Azure Java SDK 在资源组中列出 Azure Database for PostgreSQL 服务器
Azure list Azure Database for PostgreSQL servers in Resource group using Azure Java SDK
使用 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
的信息的正确方法是什么?
P.S。
获得有关 Azure Database for PostgreSQL 的信息后,我将需要有关 Azure Database for MySQL Servers
.
的类似信息
编辑:我看到这个问题是 2 年前提出的,想知道 Azure 是否为 PostgreSQL/MySQL 服务器添加了对 Azure 数据库的支持。
Azure Java SDK for MySQL/PostgreSQL databases?
根据我的测试,我们可以使用java sdk azure-mgmt-resources
来实现你的需求。例如
- 创建服务主体
az login
# it will create a service pricipal and assign a contributor rolen to the sp
az ad sp create-for-rbac -n "MyApp" --scope "/subscriptions/<subscription id>" --sdk-auth
- 代码
String tenantId = "<the tenantId you copy >";
String clientId = "<the clientId you copy>";
String clientSecret= "<the clientSecre you copy>";
String subscriptionId = "<the subscription id you copy>";
ApplicationTokenCredentials creds = new
ApplicationTokenCredentials(clientId,domain,secret,AzureEnvironment.AZURE);
RestClient restClient =new RestClient.Builder()
.withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
.withSerializerAdapter(new AzureJacksonAdapter())
.withReadTimeout(150, TimeUnit.SECONDS)
.withLogLevel(LogLevel.BODY)
.withResponseBuilderFactory(new AzureResponseBuilder.Factory())
.withCredentials(creds)
.build();
ResourceManager resourceClient= ResourceManager.authenticate(restClient).withSubscription(subscriptionId);
ResourceManagementClientImpl client= resourceClient.inner();
String filter="resourceType eq 'Microsoft.DBforPostgreSQL/servers'"; //The filter to apply on the operation
String expand=null;//The $expand query parameter. You can expand createdTime and changedTime.For example, to expand both properties, use $expand=changedTime,createdTime
Integer top =null;// The number of results to return. If null is passed, returns all resource groups.
PagedList<GenericResourceInner> results= client.resources().list(filter, null,top);
while (true) {
for (GenericResourceInner resource : results.currentPage().items()) {
System.out.println(resource.id());
System.out.println(resource.name());
System.out.println(resource.type());
System.out.println(resource.location());
System.out.println(resource.sku().name());
System.out.println("------------------------------");
}
if (results.hasNextPage()) {
results.loadNextPage();
} else {
break;
}
}
此外,您还可以使用 Azure REST API 来实现您的需求。详情请参考https://docs.microsoft.com/en-us/rest/api/resources/resources
所以,我通过以下方式实现了它,它可以被视为一种替代方式......
在 Github (https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/postgresql) 上查看 java 存储库的 Azure SDK,看起来他们在测试版中有它,所以我在 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>
在代码中,以下是我如何做的要点:
我已经创建了一个服务主体,并随身携带了它的信息。
但是,任何尝试此操作的人都需要 clientId
、tenantId
、clientSecret
和 subscriptionId
,就像@Jim Xu 解释的那样。
// 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
class指com.microsoft.azure.management.postgresql.v2017_12_01.Server
另外,如果你看一下 Azure
class,你会发现他们内部是这样做的。
作为参考,您可以在 Azure
class 中使用 SqlServerManager sqlServerManager
并查看他们如何使用它并创建经过身份验证的管理器,以防您想要使用某些服务仍在 preview
或 beta
.
使用 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
的信息的正确方法是什么?
P.S。
获得有关 Azure Database for PostgreSQL 的信息后,我将需要有关 Azure Database for MySQL Servers
.
编辑:我看到这个问题是 2 年前提出的,想知道 Azure 是否为 PostgreSQL/MySQL 服务器添加了对 Azure 数据库的支持。 Azure Java SDK for MySQL/PostgreSQL databases?
根据我的测试,我们可以使用java sdk azure-mgmt-resources
来实现你的需求。例如
- 创建服务主体
az login
# it will create a service pricipal and assign a contributor rolen to the sp
az ad sp create-for-rbac -n "MyApp" --scope "/subscriptions/<subscription id>" --sdk-auth
- 代码
String tenantId = "<the tenantId you copy >";
String clientId = "<the clientId you copy>";
String clientSecret= "<the clientSecre you copy>";
String subscriptionId = "<the subscription id you copy>";
ApplicationTokenCredentials creds = new
ApplicationTokenCredentials(clientId,domain,secret,AzureEnvironment.AZURE);
RestClient restClient =new RestClient.Builder()
.withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
.withSerializerAdapter(new AzureJacksonAdapter())
.withReadTimeout(150, TimeUnit.SECONDS)
.withLogLevel(LogLevel.BODY)
.withResponseBuilderFactory(new AzureResponseBuilder.Factory())
.withCredentials(creds)
.build();
ResourceManager resourceClient= ResourceManager.authenticate(restClient).withSubscription(subscriptionId);
ResourceManagementClientImpl client= resourceClient.inner();
String filter="resourceType eq 'Microsoft.DBforPostgreSQL/servers'"; //The filter to apply on the operation
String expand=null;//The $expand query parameter. You can expand createdTime and changedTime.For example, to expand both properties, use $expand=changedTime,createdTime
Integer top =null;// The number of results to return. If null is passed, returns all resource groups.
PagedList<GenericResourceInner> results= client.resources().list(filter, null,top);
while (true) {
for (GenericResourceInner resource : results.currentPage().items()) {
System.out.println(resource.id());
System.out.println(resource.name());
System.out.println(resource.type());
System.out.println(resource.location());
System.out.println(resource.sku().name());
System.out.println("------------------------------");
}
if (results.hasNextPage()) {
results.loadNextPage();
} else {
break;
}
}
此外,您还可以使用 Azure REST API 来实现您的需求。详情请参考https://docs.microsoft.com/en-us/rest/api/resources/resources
所以,我通过以下方式实现了它,它可以被视为一种替代方式...... 在 Github (https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/postgresql) 上查看 java 存储库的 Azure SDK,看起来他们在测试版中有它,所以我在 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>
在代码中,以下是我如何做的要点:
我已经创建了一个服务主体,并随身携带了它的信息。
但是,任何尝试此操作的人都需要 clientId
、tenantId
、clientSecret
和 subscriptionId
,就像@Jim Xu 解释的那样。
// 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
class指com.microsoft.azure.management.postgresql.v2017_12_01.Server
另外,如果你看一下 Azure
class,你会发现他们内部是这样做的。
作为参考,您可以在 Azure
class 中使用 SqlServerManager sqlServerManager
并查看他们如何使用它并创建经过身份验证的管理器,以防您想要使用某些服务仍在 preview
或 beta
.