Python SDK example: List clusters

Updated at:
Copy as MD

This article shows how to use the Alibaba Cloud SDK for Python to call the DescribeDBClusters API to list your AnalyticDB for MySQL clusters.

Review API documentation

Before calling an API, review its documentation to understand the required parameters and permissions. For more information, see the API overview for the data warehouse edition and the API overview for the enterprise, basic, and lakehouse editions.

Create a RAM user and grant permissions

If you already have a RAM user with the required permissions, you can skip this section.

  1. Create a RAM user:

    1. Go to the Users page in the RAM console and click Creator.

    2. Set the Username to adb-openapi-operator. For Access Mode, select Programmatic Access.

    3. Click OK. After creating the RAM user, save the AccessKey ID and store the AccessKey secret securely, as it cannot be retrieved again.

  2. Grant permissions:

    1. Go to the Users page, find the RAM user that you created, and click Add Permissions in the Actions column.

    2. In the search box, enter AliyunADB and select the AliyunADBReadOnlyAccess policy.

      Note

      The AliyunADBReadOnlyAccess policy grants permissions to query AnalyticDB for MySQL clusters. You can also select the AliyunADBFullAccess policy, which grants full control to query and modify AnalyticDB for MySQL clusters. Alternatively, you can create a custom policy. For more information, see Create custom policies.

    3. Click OK to complete the authorization.

Call the API

This article shows how to call the API by using the Alibaba Cloud SDK for Python. The process is similar for SDKs in other languages. For more information, see AnalyticDB for MySQL SDK.

Set up Python environment

Download and install Python 3. If Python is already installed, run python --version to check the version.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • For Linux and macOS systems: Replace <access_key_id> and <access_key_secret> in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands in your terminal.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • For Windows systems: Add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables and set them to your AccessKey ID and AccessKey secret. Then, restart your Windows system for the changes to take effect.

Install dependencies

Run the following commands in your terminal to install the dependencies.

pip install alibabacloud_credentials
pip install alibabacloud_adb20190315
pip install alibabacloud_tea_console

Download sample code

  1. Go to the debug page for the DescribeDBClusters API in API Explorer.

  2. On the Parameter Configuration tab on the left, set the required parameters. For example, set RegionId to cn-hangzhou.

  3. On the SDK Sample Code tab on the right, select Python and click Download project to download the code package.

  4. Decompress the package and navigate to the alibabacloud_sample directory.

Run the code

Run the following command.

python sample.py

The following is a sample response.

[
  LOG
] {
  "headers":{
    "date":"Tue, 01 Aug 2023 02:05:31 GMT",
    "content-type":"application/json;charset=utf-8",
    "content-length":"1042","connection":"keep-alive",
    "keep-alive":"timeout=25","vary":"Accept-Encoding",
    "access-control-allow-origin":"*",
    "access-control-expose-headers":"*",
    "x-acs-request-id":"A239D19A-3862-52A9-8D85-09E65F07****",
    "x-acs-trace-id":"a1f567557dc98fb5f368346302e6****",
    "etag":"1uiSaYPuCXwyGD9ECHaa****"
  },
  "statusCode":200,
  "body":{
    "Items":{
      "DBCluster":[
        {
          "Category":"MIXED_STORAGE",
          "CommodityCode":"ads",
          "ComputeResource":"32Core128GBNEW",
          "ConnectionString":"am-bp1255hwaziiskcnu9****.ads.aliyuncs.com",
          "CreateTime":"2023-07-31T05:40:36Z",
          "DBClusterDescription":"am-bp1255hwaziis****",
          "DBClusterId":"am-bp1255hwaziis****",
          "DBClusterNetworkType":"VPC",
          "DBClusterStatus":"Running",
          "DBClusterType":"Common",
          "DBNodeClass":"E32",
          "DBNodeCount":1,
          "DBNodeStorage":100,
          "DBVersion":"3.0",
          "DiskType":"cloud_essd",
          "DtsJobId":"",
          "ElasticIOResource":1,
          "Engine":"AnalyticDB",
          "ExecutorCount":2,
          "LockMode":"Unlock",
          "LockReason":"",
          "Mode":"flexible",
          "PayType":"Postpaid",
          "Port":"3306",
          "RdsInstanceId":"",
          "RegionId":"cn-hangzhou",
          "ResourceGroupId":"rg-acfmz7u4zzr****",
          "StorageResource":"24Core192GB",
          "Tags":{"Tag":[]},
          "VPCCloudInstanceId":"am-bp1255hwaziis****-controller",
          "VPCId":"vpc-bp1ov7as4yvz4kxei****",
          "VSwitchId":"vsw-bp14jupk2fa0avw8v****",
          "ZoneId":"cn-hangzhou-h"
        }
      ]
    },
    "PageNumber":1,
    "PageSize":30,
    "RequestId":"A239D19A-3862-52A9-8D85-09E65F07B98C","TotalCount":1
  }
}