Learn how to use Alibaba Cloud CLI to call Resource Directory API operations, with the GetFolder operation as an example.
Key concepts
-
Alibaba Cloud CLI is a general-purpose command-line tool built on OpenAPI that you can use to automate the management of Resource Directory. For more information, see What is Alibaba Cloud CLI?.
-
Starting from version 3.0.282, Alibaba Cloud CLI integrates the Resource Directory API version
2022-04-19. Update your CLI to the latest version to ensure support for API calls. -
The Resource Directory API version
2020-03-31is not supported by Alibaba Cloud CLI.
Step 1: Install Alibaba Cloud CLI
Before you use Alibaba Cloud CLI, you must install it. Choose the installation method for your operating system.
-
Windows: Install the CLI on Windows.
-
macOS: Install the CLI on macOS.
Cloud Shell includes Alibaba Cloud CLI and automatically configures your Cloud Shell, so no manual setup is required. You can use Cloud Shell to debug Alibaba Cloud CLI commands. For more information, see What is Cloud Shell?.
Step 2: Configure Alibaba Cloud CLI
An Alibaba Cloud account has full permissions for all API operations, creating a security risk. We strongly recommend that you create a RAM identity, grant permissions based on the principle of least privilege, and use that RAM identity to call API operations. For the permission policies supported by Resource Directory, see System policies for ResourceManager and Resource Directory authorization.
Before you use Alibaba Cloud CLI, you must configure your credentials, a region ID, and other information. Alibaba Cloud CLI supports multiple credential types. For more information, see Configure and manage credentials. This topic uses AccessKey credentials as an example. Perform the following steps:
-
In the management account of your Resource Directory, create a RAM user and an AccessKey to configure your credentials. For more information, see Create a RAM user and Create an AccessKey.
-
Grant permissions to the RAM user. For example, attach the
AliyunResourceDirectoryReadOnlyAccesspermission policy to grant the RAM user read-only access to Resource Directory. For instructions, see Manage permissions for a RAM user. -
Obtain the region ID for the region you want to access. Alibaba Cloud CLI sends API requests to the specified region. To find available regions for Resource Directory, see Endpoints.
NoteWhen running a command, you can use the
--regionoption to specify a region for the request. This option overrides any default region configured in your credentials or environment variables. For more information, see Command-line options. -
Use the RAM user's AccessKey and the region ID to configure a new credential profile named
AkProfile. For instructions, see Configure and manage credentials.
Step 3: Generate a sample CLI command
-
Go to the GetFolder debugging page in the OpenAPI Portal.
-
On the Parameter Configuration tab, enter the request parameters. Then, click the CLI Example tab to view the generated sample CLI command.
-
Copy the sample command or run it directly in Cloud Shell:
-
Click the Run Command button
to open Cloud Shell and immediately run the command. -
Click the Copy button
to copy the command to the clipboard. You can then paste it into your local terminal to run.
Note-
When you run the command in your local terminal, ensure the parameters are in the correct format. For more information, see Parameter format.
-
By default, OpenAPI Portal adds the
--regionoption to the generated sample command. When you run the command locally, Alibaba Cloud CLI uses the region specified by this option instead of the default region configured in your credentials or environment variables. You can remove or keep this option as needed.
-
Step 4: Call Resource Directory API operations
For a list of Resource Directory API operations supported by Alibaba Cloud CLI, see API overview.
The following example shows how to call the GetFolder command to retrieve the details of the folder fd-Jyl5U7****.
-
Run the following command.
aliyun resourcedirectorymaster GetFolder --FolderId 'fd-Jyl5U7****' -
Expected output:
{ "RequestId": "C2CBCA30-C8DD-423E-B4AD-4FB694C9180C", "Folder": { "FolderId": "fd-Jyl5U7****", "CreateTime": "2021-06-15T06:39:08.521Z", "ParentFolderId": "r-Wm****", "FolderName": "Applications", "ResourceDirectoryPath": "rd-3G****/r-Wm****/fd-Jyl5U7****" } }Note-
If calling a Resource Directory API operation returns an error, check that your request parameters and their values are correct.
-
You can record the returned RequestId or SDK error information and use OpenAPI problem diagnosis for self-service troubleshooting.
-