CLI integration example
Alibaba Cloud CLI is a general-purpose command-line tool built on OpenAPI. You can use Alibaba Cloud CLI to automate the management and maintenance of Microservices Engine (MSE). This topic uses the ListTagResources API operation as an example to demonstrate how to use Alibaba Cloud CLI to call the OpenAPI operations of Microservices Engine.
Prerequisites
We recommend reading What is Alibaba Cloud CLI.
Step 1: Install Alibaba Cloud CLI
To use Alibaba Cloud CLI, you must first install it. It can be installed on Windows, Linux, and macOS. Choose the installation guide for your operating system.
-
Windows: Install Alibaba Cloud CLI on Windows.
Alternatively, you can debug Alibaba Cloud CLI commands in Cloud Shell. Cloud Shell includes Alibaba Cloud CLI and automatically configures your identity credentials, requiring no manual setup. For more information, see What is Cloud Shell?.
Step 2: Configure Alibaba Cloud CLI
Using an Alibaba Cloud account for OpenAPI operations is a high security risk because the account has full management and access permissions. We strongly recommend that you create a RAM user, grant permissions based on the principle of least privilege, and use this identity to access OpenAPI operations. For the permission policies supported by Microservices Engine, see System policies for MSE and Custom policies for MSE.
Before you use Alibaba Cloud CLI, you must configure your identity credentials, region ID, and other information. Alibaba Cloud CLI supports multiple types of identity credentials. For more information, see Configure and manage credentials. This topic uses an AccessKey-based credential as an example.
-
Create a RAM user and an AccessKey pair, which are required to configure your identity credential. For more information, see Create a RAM user and Create an AccessKey.
-
Grant permissions to the RAM user. For this example, you must grant the
AliyunMSEReadOnlyAccesspermission policy to the RAM user for read-only access to Microservices Engine. For more information, see Manage RAM user permissions. -
Obtain an available region ID. Alibaba Cloud CLI sends OpenAPI calls to the region you specify. For a list of available regions for Microservices Engine, see Endpoints.
NoteWhen using Alibaba Cloud CLI, you can add the
--regionoption to a command to send the request to a specific region. This option overrides the default region configured in your profile and environment variables. For more information, see Command-line options. -
Configure an AccessKey-based credential profile named
AkProfilewith the RAM user's AccessKey pair and the region ID. For more information, see Configure and manage credentials.
Step 3: Generate a sample CLI command
-
In OpenAPI Explorer, go to the ListTagResources debugging page.
In the panel on the right, click the CLI Example tab. In the parameter configuration section, specify required parameters such as RegionId (region) and ResourceType (resource type). The corresponding CLI command is automatically generated on the right, for example,
aliyun mse ListTagResources --region cn-hangzhou. Click the copy icon next to the sample code to copy the command. -
In the Parameters section, enter the request parameters, and then click the CLI Example tab to view the generated sample command.
-
Copy the sample CLI command or run it in Cloud Shell:
-
Click the Run Command
button to open Cloud Shell and debug the command. -
Click the Copy
button, then paste the command into your local shell tool to run.
NoteWhen debugging a sample CLI command in your local shell tool, ensure the parameter format is correct. For more information about the parameter format for Alibaba Cloud CLI commands, see Understand command parameters.
OpenAPI Explorer adds the
--regionoption to the generated sample command by default. When you run the command in your local CLI, this option takes precedence over the region information in your default profile and environment variables. You can remove or keep this option as needed. -
Step 4: Call Microservices Engine OpenAPI
Example 1: List available Microservices Engine OpenAPI operations
The following example shows how to use the --help option to obtain the list of Microservices Engine OpenAPI supported by Alibaba Cloud CLI. You can also view the supported OpenAPI in API Overview.
-
Run the following command.
aliyun mse --help -
Sample output:
Product: mse (Microservices Engine) Version: 2019-05-31 Available Api List: AddAuthPolicy Creates a service authentication rule. AddAuthResource Gateway authentication: Creates authorization information. AddBlackWhiteList Adds an IP address to a blacklist or whitelist. AddGateway Adds a gateway. AddGatewayAuth AddGatewayAuthConsumer Adds a consumer for gateway authentication. AddGatewayDomain Associates a domain name with a gateway. AddGatewayRoute Adds a gateway route. AddGatewayServiceVersion Adds a service version.
Example 2: List tags
The following example shows how to use Alibaba Cloud CLI to call the ListTagResources command of Microservices Engine to query the tags added to the resource with the ID mse-cn-7pp2st2****.
-
Run the following command.
aliyun mse ListTagResources --RegionId 'cn-hangzhou' --ResourceType CLUSTER --ResourceId.1 'mse-cn-7pp2st2****' -
Sample output:
{ "NextToken": "AAAAAV3MpHK1AP0pfERHZN5pu6ng3BeUyo2zbY5wgL1DBa2T", "RequestId": "92ED7C9C-84A2-595F-87FE-354F84C0F739", "Message": "The request was processed successfully.", "ErrorCode": "mse-100-000", "Success": true, "TagResources": { "TagResource": [ { "ResourceId": "mse-cn-7pp2st2****", "ResourceType": "cluster", "TagValue": "prd", "TagKey": "env" } ] } }NoteIf you receive an error when calling an Microservices Engine OpenAPI operation, use the returned error code to verify that your request parameters and their values are correct.
You can also record the RequestId or SDK error message and use Alibaba Cloud OpenAPI Diagnostics for self-service diagnostics.