Examples of calling an API operation of SDK for Python

更新时间:
复制 MD 格式

Call the ListDiscovereesources operation by using Cloud Config SDK for Python to query the resource list.

Step 1: View the API documentation

To query the resource list, call the ListDiscovereesources operation. For more information, see API overview. Review the API documentation for the required data and permissions.

Step 2: Create a RAM user and grant permissions to the RAM user

Identities

You can call this operation by using an Alibaba Cloud account, a RAM user, or a RAM role. For more information about the differences, see Identity. This example uses a RAM user.

Log on to the Resource Access Management (RAM) console, create a RAM user that has the permissions to only call API operations, and then record the AccessKey pair. For more information about how to create a RAM user, see Create a RAM user.

Credentials

An AccessKey pair is generated when you create a RAM user. You can also go to the details page of the RAM user, click the Authentication tab, and then click Create AccessKey. For more information, see Create an access key pair.

Important

The AccessKey secret is displayed only when the RAM user is created. Store the AccessKey secret in a secure location because you cannot retrieve it later.

Authorization

  1. Go to the Users page of the RAM console. Then, find the RAM user and click Add Permissions in the Actions column.

  2. In the Policy section, enter config in the field to search for policies and select the policy based on the principle of least privilege.

    In this example, the system policy AliyunConfigReadOnlyAccess is selected.

    Note
    • Cloud Config provides the following system policies:

      • AliyunConfigFullAccess: the management permissions on all Cloud Config resources.

      • AliyunConfigReadOnlyAccess: the read-only permissions on all Cloud Config resources.

    • For more information about how to create a custom policy, see Create a custom policy and RAM authorization.

  3. Click OK.

Step 3: Call the ListDiscovereesources operation

The following example uses the Python SDK. The process is similar for SDKs in other languages. For more information, see Cloud Config SDK. For other calling methods, see Methods of calling API operations.

Prepare a Python environment

  1. Download and install Python.

    To download Python 3, visit the official website of Python.

  2. Check the version of Python.

    • Linux or macOS

      Open the terminal and run the python --version command.

    • Windows

      • In the Python installation directory, double-click python.exe.

      • Open the Command Prompt.

        Press the Win+R keys to open the Run dialog box, enter python, and then click OK.

Configure environment variables

To configure environment variables in Linux, macOS, and Windows, see Configure environment variables in Linux, macOS, and Windows.

Install dependencies

pip install alibabacloud_credentials
pip install alibabacloud_config20200907==2.2.8
pip install alibabacloud_tea_console

Download the sample code

  1. In OpenAPI Explorer, go to the debugging page for the ListDiscoveredResources operation.

  2. Set the MaxResults parameter to 1. Then, click Initiate Call. You do not need to specify other request parameters.

  3. Click the SDK Sample Code tab. Then, go to the Languages section and click Python.

    The sample code for the Cloud Config SDK for Python is displayed.

  4. Click Download Project to download the sample code package.

  5. Decompress the sample code package on your computer and access the alibabacloud_sample directory.

Run sample code

Run the following command:

python sample.py

The following result is returned:

{
  "DiscoveredResourceProfiles": {
    "TotalCount": 74,
    "NextToken": "h+AaTI5d1xePwRFi+1uN****",
    "MaxResults": 1,
    "DiscoveredResourceProfileList": [
      {
        "ResourceCreationTime": 1688983108000,
        "AccountId": "169827232854****",
        "ResourceId": "actiontrail-policy",
        "Version": 3,
        "ResourceName": "actiontrail-policy",
        "Region": "global",
        "ResourceStatus": "",
        "AvailabilityZone": "",
        "ResourceType": "ACS::RAM::Policy",
        "Tags": "{}",
        "ResourceDeleted": 1
      }
    ]
  },
  "RequestId": "B9A68D93-5FE4-5257-A2B9-6383945A8575"
}