Use Alibaba Cloud CLI to call AnalyticDB for PostgreSQL OpenAPI operations
This topic provides an example of calling the AnalyticDB for PostgreSQL DescribeDBClusterNode API to query node information for an instance to demonstrate how to use the Alibaba Cloud CLI to call OpenAPI operations for AnalyticDB for PostgreSQL.
Background
Alibaba Cloud Command Line Interface (Alibaba Cloud CLI) is a general-purpose command-line tool built on OpenAPI. You can use Alibaba Cloud CLI to automate the management and maintenance of AnalyticDB for PostgreSQL. For more information, see What is Alibaba Cloud CLI?.
Step 1: Install Alibaba Cloud CLI
Before you use Alibaba Cloud CLI, you must install it. Alibaba Cloud CLI provides installers for Windows, Linux, and macOS. Select the appropriate installer for your operating system.
-
Windows: Install Alibaba Cloud CLI on Windows.
You can also debug Alibaba Cloud CLI commands in Cloud Shell. Cloud Shell comes with Alibaba Cloud CLI pre-installed and is updated regularly. For more information, see What is Cloud Shell?.
Step 2: Configure Alibaba Cloud CLI
An Alibaba Cloud account has permissions to access all product OpenAPI operations, which poses a high security risk. We strongly recommend that you create a RAM identity, grant permissions to the RAM identity based on the principle of least privilege, and then use the RAM identity to access OpenAPI operations. For information about the policies supported by AnalyticDB for PostgreSQL, see System policies for AnalyticDB for PostgreSQL and Custom policies for AnalyticDB for PostgreSQL.
Before you use Alibaba Cloud CLI, you must configure your credentials, region ID, and other information. Alibaba Cloud CLI supports various types of credentials. For more information, see Configure and manage credential profiles. This topic uses AccessKey credentials as an example. Perform the following steps:
-
Create a RAM user and an AccessKey pair. You will need this AccessKey pair to configure credentials. For more information, see Create a RAM user and Create an AccessKey.
-
Grant permissions to the RAM user. In this example, you must grant the RAM user read-only access to AnalyticDB for PostgreSQL by attaching the
AliyunGPDBReadOnlyAccesspolicy. For more information, see Grant permissions to a RAM user. -
Obtain an available region ID, which you will need to configure credentials. Alibaba Cloud CLI uses the region that you specify to initiate OpenAPI calls. For the available regions of AnalyticDB for PostgreSQL, see Endpoints.
NoteWhen you use Alibaba Cloud CLI, you can add the
--regionoption to a command to specify a region for the request. This option overrides the region specified in the profile and environment variables. For more information, see Command-line options. -
Use the AccessKey pair of the RAM user to configure AccessKey credentials. Name the profile
AkProfile. For more information, see Configure and manage credential profiles.
Step 3: Generate a CLI command example
-
In OpenAPI Explorer, go to the debug page for DescribeDBClusterNode.
-
On the Parameters tab, enter the request parameters, and then click the CLI Example tab to view the generated CLI example.
-
Copy the CLI example or run it in Cloud Shell:
-
Click the Run Command
button to open Cloud Shell and quickly debug the command. -
Click the Copy
button to copy the CLI example to the clipboard. You can then paste it into a local shell to run it, or use it to build an automation script.
NoteWhen you copy the CLI example to your local shell for debugging, make sure the parameters are in the correct format. For more information about Alibaba Cloud CLI parameter formats, see Parameter formats.
By default, OpenAPI Explorer adds the
--regionoption to the generated command example. When you copy the command to run it locally, Alibaba Cloud CLI ignores the region information in your default credential profile and environment variables, and sends the request to the specified region. You can keep or remove this option as needed. -
Step 4: Call AnalyticDB for PostgreSQL OpenAPI
Example 1: List AnalyticDB for PostgreSQL supported APIs
The following example shows how to use the --help option to list the AnalyticDB for PostgreSQL OpenAPI operations supported by Alibaba Cloud CLI. For more information, see API overview.
-
Run the command.
aliyun gpdb --help -
The command returns the following output:
Product: gpdb (AnalyticDB for PostgreSQL) Version: 2016-05-03 Available Api List: AllocateInstancePublicConnection BindDBResourceGroupWithRole CancelUploadDocumentJob CancelUpsertCollectionDataJob CheckHadoopDataSource CheckHadoopNetConnection CheckJDBCSourceNetConnection CheckServiceLinkedRole CreateAccount CreateCollection CreateDBInstance
Example 2: Query instance node information
The following example shows how to use Alibaba Cloud CLI to call the AnalyticDB for PostgreSQL DescribeDBClusterNode
command. The command queries information about master
nodes in a specified instance based on the instance ID.
-
Run the command.
aliyun gpdb DescribeDBClusterNode --DBInstanceId 'gp-bp13ue79qk8y1****' --NodeType master -
The command returns the following output:
{ "Nodes": [ { "Name": "master-10*******" } ], "RequestId": "87E44B48-B306-4AD3-A63B-C8**********", "DBClusterId": "gp-bp13ue79qk8y1****" }NoteIf calling an AnalyticDB for PostgreSQL OpenAPI operation returns an error, use the error code to verify that the request parameters and their values are valid.
You can also record the returned request ID or the SDK error message and use the Alibaba Cloud OpenAPI Diagnostics Platform for self-service diagnostics.