文档首页> 华为云命令行工具服务 KooCLI> 快速入门> 步骤四:查看与执行云服务操作命令
更新时间:2023-05-22 GMT+08:00

步骤四:查看与执行云服务操作命令

本节以Windows系统为例介绍KooCLI的使用,Linux和Mac系统的使用基本相同,可参考。

完成初始化配置后,即可查询KooCLI支持的云服务列表,并执行相关命令。下文以弹性云服务器(ECS)的查询云服务器详情的API为例,说明如何查找与执行命令。

  1. 查询云服务的operation列表

    hcloud <service> --help

    如下所示,“Available Operations”中返回了ECS服务支持的operation列表。

    hcloud ECS --help
    
    KooCLI(Koo Command Line Interface) Version 3.2.8 Copyright(C) 2020-2022 www.huaweicloud.com
    
    Usage:
      hcloud ECS <operation> --param1=value1 --param2=value2 ...
    
    Service:
      ECS
    
    Available Operations:
      AddServerGroupMember             MigrateServer                    NovaShowServerAction
      AssociateServerVirtualIp         NovaAssociateSecurityGroup       NovaShowServerGroup
      AttachServerVolume               NovaAttachInterface              NovaShowServerInterface
      BatchAddServerNics               NovaAttachVolume                 NovaShowServerMetadata
      ...
    • 命令执行时,会使用您配置的认证信息执行请求,部分接口调用涉及云产品计费,请谨慎操作。
    • 云服务支持的operation,也可在API Explorer界面查找。
    • 可使用“hcloud --help”命令查询KooCLI支持的云服务列表。

    若您当前使用的KooCLI为离线模式,会在首次执行命令时自动下载离线元数据包。该数据包下载成功后,其内容不再自动更新。您可执行“hcloud meta download”命令主动更新离线元数据包,以便在离线模式下调用新上线的云服务。也可执行“hcloud configure set --cli-offline=false”命令切换至在线模式使用。

  2. 查询云服务具体operation的帮助信息

    hcloud <service> <operation> --help

    上一步查询得出ECS的operation列表,选取其中的“NovaShowServer”,查询该API的帮助信息。如下所示,返回信息列出API介绍、参数清单与说明。

    hcloud ECS NovaShowServer --help
    
    KooCLI(Koo Command Line Interface) Version 3.2.8 Copyright(C) 2020-2022 www.huaweicloud.com
    
    Service:
      ECS
    
    Description:
      根据云服务器ID,查询云服务器的详细信息。
    
    Method:
      GET
    
    Params:
      --cli-region
        required    string  当前可调用的区域.若命令中未输入,将使用当前配置项中的cli-region
      --project_id
        required    string  path    项目ID。若命令中未输入,将根据认证信息获取指定区域的父级项目ID,或使用当前配置项中的cli-project-id
      --server_id
        required    string  path    云服务器ID。
      --OpenStack-API-Version
        optional    string  header  微版本头。

  3. 执行调用API的命令,获取执行结果。

    hcloud <service> <operation> [--param1=paramValue1 --param2=paramValue2 ...]

    如下所示,命令中输入cli-region(调用的区域),project_id(项目ID)和server_id(云服务器ID)的参数值,按回车执行后,界面返回获取的云服务器信息

    hcloud ECS NovaShowServer --cli-region="ap-southeast-1" --project_id="0dd8cb****************19b5a84546" --server_id="4f06****-****-****-****-****04dd856a"
    {
     "server": {
      "tenant_id": "0dd8cb****************19b5a84546",
      "metadata": {},
      "addresses": {
       "c865****-****-****-****-****efe7e8d8": [
        {
         "OS-EXT-IPS-MAC:mac_addr": "fa:**:**:**:**:**",
         "OS-EXT-IPS:type": "fixed",
         "addr": "192.***.*.**",
         "version": 4
        }
       ]
      },
      "OS-EXT-STS:task_state": null,
      "OS-DCF:diskConfig": "AUTO",
      "OS-EXT-AZ:availability_zone": "ap-southeast-1a",
      "links": [
       {
        "rel": "self",
        "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/v2.1/0dd8cb****************19b5a84546/servers/4f06****-****-****-****-****04dd856a"
       },
       {
        "rel": "bookmark",
        "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/servers/4f06****-****-****-****-****04dd856a"
       }
      ],
      "OS-EXT-STS:power_state": 4,
      "id": "4f06****-****-****-****-****04dd856a",
      "os-extended-volumes:volumes_attached": [
       {
        "id": "aed9****-****-****-****-****0e3219cf"
       }
      ],
      "OS-EXT-SRV-ATTR:host": "51f41ce46********************************38b69b7aa4ea2a8",
      "image": {
       "links": [
        {
         "rel": "bookmark",
         "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/images/67f4****-****-****-****-****38539e09"
        }
       ],
       "id": "67f4****-****-****-****-****38539e09"
      },
      "OS-SRV-USG:terminated_at": null,
      "accessIPv4": "",
      "accessIPv6": "",
      "created": "2022-05-10T12:56:36Z",
      "hostId": "51f41ce46********************************38b69b7aa4ea2a8",
      "OS-EXT-SRV-ATTR:hypervisor_hostname": "cf199aabae********************************bed586126e6f57",
      "flavor": {
       "links": [
        {
         "rel": "bookmark",
         "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/flavors/s6.medium.2"
        }
       ],
       "id": "s6.medium.2"
      },
      "key_name": null,
      "security_groups": [
       {
        "name": "Sys-**********"
       }
      ],
      "OS-EXT-STS:vm_state": "stopped",
      "user_id": "b4d561****************346deaf79e",
      "OS-EXT-SRV-ATTR:instance_name": "instance-*******",
      "name": "ecs-****",
      "OS-SRV-USG:launched_at": "2022-05-10T12:56:53.000000",
      "updated": "2022-05-13T08:05:17Z",
      "status": "SHUTOFF"
     }
    }
    • 使用KooCLI调用API时,您可以在API Explorer上获取CLI示例
    • KooCLI可在API调用过程中根据用户认证信息自动获取IAM用户所属帐号ID、项目ID,同时若配置信息中已配置cli-region值,命令中可不输入。
    • 如果命令中参数值不正确,将会返回报错信息。如下所示:
      hcloud ECS ShowServer --project_id="0dd8cb****************19b5a84546" --cli-region="ap-southeast-1" --server_id="abc"
      {
        "error": {
          "message": "Instance[abc] could not be found.",
          "code": "Ecs.0114"
        }
      }
      错误详情参见错误中心'https://console-intl.huaweicloud.com/apiexplorer/#/errorcenter?keyword=Ecs.0114&product=ECS'