diff --git a/model/gateway/instance.go b/model/gateway/instance.go index 6a6d5535..8bbc91a2 100644 --- a/model/gateway/instance.go +++ b/model/gateway/instance.go @@ -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}"` diff --git a/plugin/api/gateway/api.go b/plugin/api/gateway/api.go index 88a8ea58..7f7aa716 100644 --- a/plugin/api/gateway/api.go +++ b/plugin/api/gateway/api.go @@ -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) } diff --git a/plugin/api/gateway/instance.go b/plugin/api/gateway/instance.go index 3d19baf8..a479c34d 100644 --- a/plugin/api/gateway/instance.go +++ b/plugin/api/gateway/instance.go @@ -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"`