CLI integration example

Updated at:

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.

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

Important

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.

  1. 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.

  2. Grant permissions to the RAM user. For this example, you must grant the AliyunMSEReadOnlyAccess permission policy to the RAM user for read-only access to Microservices Engine. For more information, see Manage RAM user permissions.

  3. 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.

    Note

    When using Alibaba Cloud CLI, you can add the --region option 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.

  4. Configure an AccessKey-based credential profile named AkProfile with 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

  1. 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.

  2. In the Parameters section, enter the request parameters, and then click the CLI Example tab to view the generated sample command.

  3. Copy the sample CLI command or run it in Cloud Shell:

    • Click the Run Command image button to open Cloud Shell and debug the command.

    • Click the Copy image button, then paste the command into your local shell tool to run.

    Note

    When 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 --region option 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.

  1. Run the following command.

    aliyun mse --help
  2. 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****.

  1. Run the following command.

    aliyun mse ListTagResources --RegionId 'cn-hangzhou' --ResourceType CLUSTER --ResourceId.1 'mse-cn-7pp2st2****' 
  2. 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"
          }
        ]
      }
    }
    Note

    If 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.