add gateway instance status api

This commit is contained in:
liugq 2022-02-15 12:10:32 +08:00
parent 7d94108600
commit e1559b0e85
3 changed files with 57 additions and 2 deletions

View File

@ -16,7 +16,7 @@ type Instance struct {
Name string `json:"name,omitempty" elastic_mapping:"name:{type:keyword,fields:{text: {type: text}}}"`
Endpoint string `json:"endpoint,omitempty" elastic_mapping:"endpoint: { type: keyword }"`
Version map[string]interface{} `json:"version,omitempty" elastic_mapping:"version: { type: object }"`
BasicAuth *struct {
BasicAuth struct {
Username string `json:"username,omitempty" config:"username" elastic_mapping:"username:{type:keyword}"`
Password string `json:"password,omitempty" config:"password" elastic_mapping:"password:{type:keyword}"`
} `config:"basic_auth" json:"basic_auth,omitempty" elastic_mapping:"basic_auth:{type:object}"`

View File

@ -20,4 +20,5 @@ func init() {
api.HandleAPIMethod(api.PUT, "/gateway/instance/:instance_id", gateway.updateInstance)
api.HandleAPIMethod(api.DELETE, "/gateway/instance/:instance_id", gateway.deleteInstance)
api.HandleAPIMethod(api.GET, "/gateway/instance/_search", gateway.searchInstance)
api.HandleAPIMethod(api.POST, "/gateway/instance/status", gateway.getInstanceStatus)
}

View File

@ -7,6 +7,7 @@ package gateway
import (
"crypto/tls"
"fmt"
log "github.com/cihub/seelog"
"github.com/segmentio/encoding/json"
"infini.sh/console/model/gateway"
httprouter "infini.sh/framework/core/api/router"
@ -14,7 +15,6 @@ import (
"infini.sh/framework/core/util"
"infini.sh/framework/lib/fasthttp"
"net/http"
log "src/github.com/cihub/seelog"
"strconv"
"strings"
"time"
@ -172,6 +172,60 @@ func (h *GatewayAPI) searchInstance(w http.ResponseWriter, req *http.Request, ps
h.Write(w, res.Raw)
}
func (h *GatewayAPI) getInstanceStatus(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var instanceIDs = []string{}
err := h.DecodeJSON(req, &instanceIDs)
if err != nil {
log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
if len(instanceIDs) == 0 {
h.WriteJSON(w, util.MapStr{}, http.StatusOK)
return
}
q := orm.Query{}
queryDSL := util.MapStr{
"query": util.MapStr{
"terms": util.MapStr{
"_id": instanceIDs,
},
},
}
q.RawQuery = util.MustToJSONBytes(queryDSL)
err, res := orm.Search(&gateway.Instance{}, &q)
if err != nil {
log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
result := util.MapStr{}
for _, item := range res.Result {
instance := util.MapStr(item.(map[string]interface{}))
if err != nil {
log.Error(err)
continue
}
endpoint, _ := instance.GetValue("endpoint")
username, _ := instance.GetValue("basic_auth.username")
if username == nil {
username = ""
}
password, _ := instance.GetValue("basic_auth.password")
if password == nil {
password = ""
}
gid, _ := instance.GetValue("id")
connRes, err := h.doConnect(endpoint.(string), username.(string), password.(string))
if err != nil {
log.Error(err)
}
result[gid.(string)] = connRes
}
h.WriteJSON(w, result, http.StatusOK)
}
type GatewayConnectResponse struct {
ID string `json:"id"`
Name string `json:"name"`