All Products
Search
Document Center

Data Management:API usage example

Last Updated:Feb 26, 2024

This topic describes how to use the Alibaba Cloud SDK for Python to call the ListInstances operation of Data Management (DMS) to query the details of instances.

View the API documentation

Before you call API operations, we recommend that you read the corresponding API documentation to understand the parameters and permissions that are required. For more information, see List of operations by function.

Create a RAM user and grant permissions to the RAM user

If you have created a RAM user and completed authorization, you can skip this step.

  1. Create a RAM user.

    1. Go to the Users page of the RAM console, and click Create User.

    2. Set the Logon Name parameter to dms-openapi-operator. Select OpenAPI Access for Access Mode.

    3. Click OK. On the page that appears, save the AccessKet ID and Accesskey secret of the RAM user that you created.

  2. Grant permissions to the RAM user.

    1. Go to the Users page of the RAM console. Find the RAM user that you created and click Add Permissions in the Actions column.

    2. Enter Aliyundms in the search box and select the AliyunDMSFullAccess policy in the search results.

      Note

      AliyunDMSFullAccess: permissions to manage DMS

      AliyunDMSReadOnlyAccess: Read-only access permissions to DMS.

    3. Click OK.

Call an API operation

This topic describes how to use the Alibaba Cloud SDK for Python to call API operations. The SDKs for other languages can also be used to call API operations in a similar way. For more information, visit DMS SDK.

Prepare a Python environment

Download and install Python 3. If you have installed Python, you can view the Python version by running the following command: python --version.

Configure environment variables

This section describes how to configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your operating system.

  • If you use the Linux or macOS operating system, replace <access_key_id> and <access_key_secret> in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands on your device.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • If you use a Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, specify your AccessKey ID and AccessKey secret by using the environment variables, and then restart the system.

Install dependencies

Run the following commands on your terminal device to install dependencies.

pip install alibabacloud_credentials
pip install alibabacloud_dms_enterprise20181101
pip install alibabacloud_tea_console

Download the sample code

  1. Visit ListInstances in OpenAPI Explorer where you can debug APIs.

  2. On the Parameters tab on the left, configure the request parameters. In this example, no parameters need to be specified.

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

  4. Decompress the package to an on-premises directory and go to the alibabacloud_sample directory.

Run the code

Execute the following code on the terminal device:

python sample.py

Sample result:

[
    LOG
] {
    "headers": {
        "date":"Wed, 19 Jul 2023 03:11:46 GMT",
        "content-type":"application/json;charset=utf-8",
        "transfer-encoding":"chunked",
        "connection":"keep-alive",
        "keep-alive":"timeout=25",
        "vary":"Accept-Encoding",
        "access-control-allow-origin":"*",
        "access-control-expose-headers":"*",
        "x-acs-request-id":"D960D80C-01CD-5FA4-AB12-7FA90BE0D35D",
        "x-acs-trace-id":"8b51178c7317cbb3e0ba9b1223eba55a"
  },
  "statusCode":200,
  "body": {
      "InstanceList": {
              "Instance": [
                      { 
                         "DataLinkName":"dbl_mysql_7",
                         "DatabasePassword":"******",
                         "DatabaseUser":"root",
                         "DbaId":54****,
                         "DbaNickName":"db_doc",
                         "DdlOnline":0,
                         "EcsRegion":"cn-hangzhou",
                         "EnvType":"product",
                         "ExportTimeout":86400,
                         "Host":"172.20.XXX.XXX",
                         "InstanceAlias":"zjq-test",
                         "InstanceId":220****,
                         "InstanceSource":"ECS_OWN",
                         "InstanceType":"mysql",
                         "OwnerIdList":{
                             "OwnerIds":[54****]
                               },
                         "OwnerNameList":{
                             "OwnerNames":["db_doc"]
                               },
                         "Port":3306,
                         "QueryTimeout":7200,
                         "SafeRuleId":829***,
                         "SellSitd":false,
                         "StandardGroup":{
                               "GroupMode":"NONE_CONTROL",
                               "GroupName":"Flexible Management"
                                },
                         "State":"NORMAL",
                         "UseDsql":"1"
                     },
                     {
                         "DataLinkName":"dbl_sls_1",
                         "DatabasePassword":"******",
                         "DatabaseUser":"LTA****",
                         "DbaId":54****,
                         "DbaNickName":"db_doc",
                         "DdlOnline":0,
                         "EcsRegion":"cn-hangzhou",
                         "EnvType":"product",
                         "ExportTimeout":86400,
                         "Host":"cn-hangzhou.log.aliyuncs.com",
                         "InstanceAlias":"DMSTEST",
                         "InstanceId":22****,
                         "InstanceSource":"FILE",
                         "InstanceType":"sls",
                         "OwnerIdList":{
                               "OwnerIds":[54****]
                                },
                          "OwnerNameList":{
                               "OwnerNames":["db_doc"]
                                },
                          "Port":0,
                          "QueryTimeout":7200,
                          "SafeRuleId":82****,
                          "SellSitd":false,
                          "Sid":"dmstest",
                          "StandardGroup":{
                               "GroupMode":"NONE_CONTROL",
                               "GroupName":"Flexible Management"
                                },
                           "State":"NORMAL",
                           "UseDsql":"1"
                     }

      }
    ]
  },
      "RequestId":"D960D80C-01CD-5FA4-AB12-7FA90BE0D35D",
      "Success":true,
      "TotalCount":2
   }
 }