Examples of calling an API operation of SDK for Python

Updated at: 2024-12-11 01:55

This topic provides an example on how to call the ListDiscovereesources operation of Cloud Config by using SDK for Python to query the resource list.

Step 1: View the API documentation

To query the resource list, you can call the ListDiscovereesources operation. For more information, see List of operations by function. For more information about the data and the permissions that are required to call this operation, see API documentation.

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 between the identities, see Identity. In this topic, a RAM user is used as an example.

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

By default, an AccessKey pair is generated after you create a RAM user. You can also go to the details page of the RAM user. On the Authentication tab, click Create AccessKey to create an AccessKey pair. For more information, see Create an AccessKey pair.

Important

The AccessKey secret of a RAM user is displayed only when the RAM user is created. You cannot view the AccessKey secret after the RAM user is created. Store the AccessKey secret in a secure location.

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 custom policies and RAM authorization.

  3. Click OK.

Step 3: Call the ListDiscovereesources operation

The following example shows how to call the API operation by using SDK for Python. For more information, see Cloud Config. You can also call other API operations based on your business requirements. For more information, see Call methods.

Prepare a Python environment

  1. Download and install Python.

    For more information about how 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

For more information about how 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, access the URL of the ListDiscovereesources 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 system provides the sample code of the Cloud Config SDK for Python.

  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"
}

  • On this page (1, T)
  • Step 1: View the API documentation
  • Step 2: Create a RAM user and grant permissions to the RAM user
  • Identities
  • Credentials
  • Authorization
  • Step 3: Call the ListDiscovereesources operation
  • Prepare a Python environment
  • Configure environment variables
  • Install dependencies
  • Download the sample code
  • Run sample code
Feedback