Azure list Azure Database for PostgreSQL servers in Resource group using Azure Java SDK

jmistry :

What is the best and correct way to list Azure Database for PostgreSQL servers present in my Resource Group using Azure Java SDK? Currently, we have deployments that happen using ARM templates and once the resources have been deployed we want to be able to get the information about those resources from Azure itself. I have tried doing in the following way:

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());

But the list size returned is 0.

However, for virtual machines, I am able to get the information in the following way:

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());
}

So, what is the right way of getting the information about the Azure Database for PostgreSQL using Azure Java SDK?

P.S. Once I get the information regarding Azure Database for PostgreSQL, I would need similar information about the Azure Database for MySQL Servers.

Edit: I have seen this question which was asked 2 years back and would like to know if Azure added Support for Azure Database for PostgreSQL/MySQL servers or not. Azure Java SDK for MySQL/PostgreSQL databases?

Jim Xu :

According to my test, we can use java sdk azure-mgmt-resources to implement your need. For example

  1. Create a service principal
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

enter image description here

  1. code
        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;
                }
            }

enter image description here

Besides, you also can use Azure REST API to implement your need. For more details, please refer to https://docs.microsoft.com/en-us/rest/api/resources/resources

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=418103&siteId=1