文档首页/ 部署 CodeArts Deploy/ API参考/ 应用示例/ 示例3:查询指定主机集群的主机列表
更新时间:2024-06-27 GMT+08:00
分享

示例3:查询指定主机集群的主机列表

场景描述

本章以软件开发生产线用户为例,指导您通过API查询指定主机集群的主机列表。

API的调用方法请参见如何调用API

前提条件

用户已经创建软件开发生产线项目,并且项目下有主机集群。

总体思路

先通过用户token查询主机集群列表,再通过主机集群group_id,查询指定主机集群的主机列表,步骤如下:

  1. 查询用户的所有主机集群
  2. 查询某主机集群对应的主机列表

步骤1:查询用户的所有主机集群

  • 接口信息:
    URI:GET /v2/host-groups

    API Explorer在线调试请参见:查询主机集群列表

  • 请求示例:
    GET  https://{endpoint}/v2/host-groups?region_name=cn-north-7&project_id=6039d4480efc4dddb178abff98719913&offset=1&limit=10&sort_key=create_time&sort_dir=DESC
  • 响应示例:
    { 
      "total" : 1, 
      "host_groups" : [ { 
        "name" : "testwyk", 
        "description" : "11122211", 
        "os" : "linux", 
        "nick_name" : "AB边账号", 
        "id" : 200001291, 
        "group_id" : "ab7647b0863c4e969c8949d38d591339", 
        "region_name" : "cn-north-7", 
        "project_id" : "6039d4480efc4dddb178abff98719913", 
        "permission" : { 
          "can_view" : true, 
          "can_edit" : true, 
          "can_delete" : true, 
          "can_add_host" : true, 
          "can_manage" : true 
        }, 
        "created_by" : { 
          "user_id" : "6baa7454109d47c192f22078fe6cda20", 
          "user_name" : "devcloud_devcloud_l00490255_01" 
        }, 
        "updated_by" : { 
          "user_id" : "6baa7454109d47c192f22078fe6cda20", 
          "user_name" : "devcloud_devcloud_l00490255_01" 
        }, 
        "auto_connection_test_switch" : 0, 
        "slave_cluster_id" : "", 
        "created_time" : "2021-04-01 17:05:53", 
        "updated_time" : "2021-04-21 14:29:14", 
        "host_count" : 1, 
        "project_name" : null 
      } ] 
    }

步骤2:查询主机列表

  • 接口信息:
    URI:GET /v2/host-groups/{group_id}/hosts

    API Explorer在线调试请参见:查询主机列表

  • 请求示例:
    GET  https://{endpoint}/v2/host-groups/ab7647b0863c4e969c8949d38d591339/hosts?offset=0&limit=10&sort_key=AS_PROXY&sort_dir=asc&with_auth=false
  • 响应示例:
    { 
       "hosts" : [ { 
         "hostname" : "100.101.28.203", 
         "ip" : "100.101.28.203", 
         "port" : 22, 
         "os" : "linux", 
         "authorization" : { 
           "username" : "root", 
           "password" : null, 
           "private_key" : null, 
           "trusted_type" : 0 
         }, 
         "permission" : { 
           "can_view" : true, 
           "can_edit" : true, 
           "can_delete" : true, 
           "can_add_host" : true, 
           "can_connection_test" : true 
         }, 
         "nickName" : "AB边账号", 
         "id" : 200001149, 
         "host_id" : "2cc913cc9a494f09b7320801ebacad02", 
         "group_id" : "ab7647b0863c4e969c8949d38d591339", 
         "as_proxy" : false, 
         "proxy_host_id" : null, 
         "owner_id" : "6baa7454109d47c192f22078fe6cda20", 
         "owner_name" : "devcloud_devcloud_l00490255_01", 
         "updator_id" : "6baa7454109d47c192f22078fe6cda20", 
         "updator_name" : "devcloud_devcloud_l00490255_01", 
         "connection_status" : "success", 
         "install_icagent" : false, 
         "create_time" : "2021-04-15 11:01:51", 
         "update_time" : "2021-04-21 15:04:24", 
         "connection_result" : "连接成功", 
         "lastest_connection_time" : "2021-04-15 11:02:00", 
         "nick_name" : "AB边账号", 
         "proxy_host" : null, 
         "group_name" : null, 
         "project_id" : "6039d4480efc4dddb178abff98719913", 
         "project_name" : null 
       } ], 
       "total" : 1, 
       "group_name" : "test" 
     }

相关文档