From 7d94108600eba78b403f69af3d0710fd1042a536 Mon Sep 17 00:00:00 2001 From: liugq Date: Mon, 14 Feb 2022 18:09:09 +0800 Subject: [PATCH] change group to tags field --- main.go | 2 - model/gateway/group.go | 13 --- model/gateway/group_instance.go | 13 --- model/gateway/instance.go | 2 +- plugin/api/gateway/api.go | 3 - plugin/api/gateway/group.go | 77 --------------- plugin/api/gateway/helper.go | 166 ++++++++++++++++---------------- plugin/api/gateway/instance.go | 106 +------------------- 8 files changed, 88 insertions(+), 294 deletions(-) delete mode 100644 model/gateway/group.go delete mode 100644 model/gateway/group_instance.go delete mode 100644 plugin/api/gateway/group.go diff --git a/main.go b/main.go index 803e5e83..1a1cbfcb 100644 --- a/main.go +++ b/main.go @@ -122,8 +122,6 @@ func main() { orm.RegisterSchemaWithIndexName(elastic.CommonCommand{}, "commands") orm.RegisterSchemaWithIndexName(elastic.TraceTemplate{}, "trace-template") orm.RegisterSchemaWithIndexName(gateway.Instance{} , "gateway-instance") - orm.RegisterSchemaWithIndexName(gateway.Group{} , "gateway-group") - orm.RegisterSchemaWithIndexName(gateway.InstanceGroup{} , "gateway-instance-group") api.RegisterSchema() diff --git a/model/gateway/group.go b/model/gateway/group.go deleted file mode 100644 index b98294b9..00000000 --- a/model/gateway/group.go +++ /dev/null @@ -1,13 +0,0 @@ -/* Copyright © INFINI Ltd. All rights reserved. - * web: https://infinilabs.com - * mail: hello#infini.ltd */ - -package gateway - -import "infini.sh/framework/core/orm" - -type Group struct { - orm.ORMObjectBase - Name string `json:"name,omitempty" elastic_mapping:"name:{type:keyword,fields:{text: {type: text}}}"` - Owner string `json:"owner,omitempty" config:"owner" elastic_mapping:"owner:{type:keyword}"` -} diff --git a/model/gateway/group_instance.go b/model/gateway/group_instance.go deleted file mode 100644 index c4aae676..00000000 --- a/model/gateway/group_instance.go +++ /dev/null @@ -1,13 +0,0 @@ -/* Copyright © INFINI Ltd. All rights reserved. - * web: https://infinilabs.com - * mail: hello#infini.ltd */ - -package gateway - -import "infini.sh/framework/core/orm" - -type InstanceGroup struct { - orm.ORMObjectBase - GroupID string `json:"group_id,omitempty" elastic_mapping:"group_id: { type: keyword }"` - InstanceID string `json:"instance_id,omitempty" elastic_mapping:"instance_id: { type: keyword }"` -} \ No newline at end of file diff --git a/model/gateway/instance.go b/model/gateway/instance.go index 66090d60..6a6d5535 100644 --- a/model/gateway/instance.go +++ b/model/gateway/instance.go @@ -21,6 +21,6 @@ type Instance struct { 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}"` Owner string `json:"owner,omitempty" config:"owner" elastic_mapping:"owner:{type:keyword}"` - Group string `json:"group,omitempty"` + Tags [] string `json:"tags,omitempty"` Description string `json:"description,omitempty" config:"description" elastic_mapping:"description:{type:keyword}"` } diff --git a/plugin/api/gateway/api.go b/plugin/api/gateway/api.go index 591d3420..88a8ea58 100644 --- a/plugin/api/gateway/api.go +++ b/plugin/api/gateway/api.go @@ -20,7 +20,4 @@ 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.GET, "/gateway/group/:group_id", gateway.getGroup) - api.HandleAPIMethod(api.GET, "/gateway/group/_search", gateway.searchGroup) } diff --git a/plugin/api/gateway/group.go b/plugin/api/gateway/group.go deleted file mode 100644 index acbcb892..00000000 --- a/plugin/api/gateway/group.go +++ /dev/null @@ -1,77 +0,0 @@ -/* Copyright © INFINI Ltd. All rights reserved. - * web: https://infinilabs.com - * mail: hello#infini.ltd */ - -package gateway - -import ( - "fmt" - "infini.sh/console/model/gateway" - httprouter "infini.sh/framework/core/api/router" - "infini.sh/framework/core/orm" - "infini.sh/framework/core/util" - "net/http" - log "src/github.com/cihub/seelog" - "strconv" - "strings" -) - -func (h *GatewayAPI) getGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - id := ps.MustGetParameter("group_id") - - obj := gateway.Group{} - obj.ID = id - - exists, err := orm.Get(&obj) - if !exists || err != nil { - h.WriteJSON(w, util.MapStr{ - "_id": id, - "found": false, - }, http.StatusNotFound) - return - } - if err != nil { - h.WriteError(w, err.Error(), http.StatusInternalServerError) - return - } - - h.WriteJSON(w, util.MapStr{ - "found": true, - "_id": id, - "_source": obj, - }, 200) -} - -func (h *GatewayAPI) searchGroup(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - - var ( - name = h.GetParameterOrDefault(req, "name", "") - queryDSL = `{"query":{"bool":{"must":[%s]}}, "size": %d, "from": %d}` - strSize = h.GetParameterOrDefault(req, "size", "20") - strFrom = h.GetParameterOrDefault(req, "from", "0") - mustBuilder = &strings.Builder{} - ) - if name != "" { - mustBuilder.WriteString(fmt.Sprintf(`{"prefix":{"name.text": "%s"}}`, name)) - } - size, _ := strconv.Atoi(strSize) - if size <= 0 { - size = 20 - } - from, _ := strconv.Atoi(strFrom) - if from < 0 { - from = 0 - } - - q := orm.Query{} - queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String(), size, from) - q.RawQuery = []byte(queryDSL) - - err, res := orm.Search(&gateway.Group{}, &q) - if err != nil { - log.Error(err) - h.WriteError(w, err.Error(), http.StatusInternalServerError) - return - } - h.Write(w, res.Raw) -} \ No newline at end of file diff --git a/plugin/api/gateway/helper.go b/plugin/api/gateway/helper.go index adb525cb..e345364e 100644 --- a/plugin/api/gateway/helper.go +++ b/plugin/api/gateway/helper.go @@ -4,87 +4,87 @@ package gateway -import ( - "fmt" - "infini.sh/console/model/gateway" - "infini.sh/framework/core/orm" - "infini.sh/framework/core/util" -) - -func fetchInstanceGroup(instanceID string) (string, error){ - // fetch gateway instance group - q := orm.Query{} - q.RawQuery = []byte(fmt.Sprintf(`{"size": 1, "query":{"term":{"instance_id":{"value":"%s"}}}}`, instanceID)) - err, res := orm.Search(&gateway.InstanceGroup{}, &q) - if err != nil { - return "", err - } - if len(res.Result) > 0 { - if rowMap, ok := res.Result[0].(map[string]interface{}); ok { - return rowMap["group_id"].(string), nil - } - } - return "", nil -} - -func fetchInstanceGroupByID(instanceIDs []interface{})([]interface{}, error){ - if len(instanceIDs) == 0 { - return nil, nil - } - // fetch gateway instance groups - esQuery := util.MapStr{ - "query": util.MapStr{ - "terms": util.MapStr{ - "instance_id": instanceIDs, - }, - }, - } - q := orm.Query{} - q.RawQuery = util.MustToJSONBytes(esQuery) - err, res := orm.Search(&gateway.InstanceGroup{}, &q) - return res.Result, err -} -func fetchGroupByID(groupIDs []interface{})([]interface{}, error){ - if len(groupIDs) == 0 { - return nil, nil - } - // fetch gateway groups - esQuery := util.MapStr{ - "query": util.MapStr{ - "terms": util.MapStr{ - "_id": groupIDs, - }, - }, - } - q := orm.Query{} - q.RawQuery = util.MustToJSONBytes(esQuery) - err, res := orm.Search(&gateway.Group{}, &q) - return res.Result, err -} - -func pickElasticsearchColumnValues(result []interface{}, columnName string) []interface{}{ - if len(result) == 0 { - return nil - } - columnValues := make([]interface{}, 0, len(result)) - for _, row := range result { - if rowMap, ok := row.(map[string]interface{}); ok { - columnValues = append(columnValues, rowMap[columnName]) - } - } - return columnValues -} - -func getRelationshipMap(result []interface{}, key string, value string) map[string]interface{}{ - if len(result) == 0 { - return nil - } - resultMap := map[string]interface{}{} - for _, row := range result { - if rowMap, ok := row.(map[string]interface{}); ok { - resultMap[rowMap[key].(string)] = rowMap[value] - } - } - return resultMap -} +//import ( +// "fmt" +// "infini.sh/console/model/gateway" +// "infini.sh/framework/core/orm" +// "infini.sh/framework/core/util" +//) +// +//func fetchInstanceGroup(instanceID string) (string, error){ +// // fetch gateway instance group +// q := orm.Query{} +// q.RawQuery = []byte(fmt.Sprintf(`{"size": 1, "query":{"term":{"instance_id":{"value":"%s"}}}}`, instanceID)) +// err, res := orm.Search(&gateway.InstanceGroup{}, &q) +// if err != nil { +// return "", err +// } +// if len(res.Result) > 0 { +// if rowMap, ok := res.Result[0].(map[string]interface{}); ok { +// return rowMap["group_id"].(string), nil +// } +// } +// return "", nil +//} +// +//func fetchInstanceGroupByID(instanceIDs []interface{})([]interface{}, error){ +// if len(instanceIDs) == 0 { +// return nil, nil +// } +// // fetch gateway instance groups +// esQuery := util.MapStr{ +// "query": util.MapStr{ +// "terms": util.MapStr{ +// "instance_id": instanceIDs, +// }, +// }, +// } +// q := orm.Query{} +// q.RawQuery = util.MustToJSONBytes(esQuery) +// err, res := orm.Search(&gateway.InstanceGroup{}, &q) +// return res.Result, err +//} +//func fetchGroupByID(groupIDs []interface{})([]interface{}, error){ +// if len(groupIDs) == 0 { +// return nil, nil +// } +// // fetch gateway groups +// esQuery := util.MapStr{ +// "query": util.MapStr{ +// "terms": util.MapStr{ +// "_id": groupIDs, +// }, +// }, +// } +// q := orm.Query{} +// q.RawQuery = util.MustToJSONBytes(esQuery) +// err, res := orm.Search(&gateway.Group{}, &q) +// return res.Result, err +//} +// +//func pickElasticsearchColumnValues(result []interface{}, columnName string) []interface{}{ +// if len(result) == 0 { +// return nil +// } +// columnValues := make([]interface{}, 0, len(result)) +// for _, row := range result { +// if rowMap, ok := row.(map[string]interface{}); ok { +// columnValues = append(columnValues, rowMap[columnName]) +// } +// } +// return columnValues +//} +// +//func getRelationshipMap(result []interface{}, key string, value string) map[string]interface{}{ +// if len(result) == 0 { +// return nil +// } +// resultMap := map[string]interface{}{} +// for _, row := range result { +// if rowMap, ok := row.(map[string]interface{}); ok { +// resultMap[rowMap[key].(string)] = rowMap[value] +// } +// } +// return resultMap +//} diff --git a/plugin/api/gateway/instance.go b/plugin/api/gateway/instance.go index 66d987d5..3d19baf8 100644 --- a/plugin/api/gateway/instance.go +++ b/plugin/api/gateway/instance.go @@ -10,7 +10,6 @@ import ( "github.com/segmentio/encoding/json" "infini.sh/console/model/gateway" httprouter "infini.sh/framework/core/api/router" - "infini.sh/framework/core/elastic" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" "infini.sh/framework/lib/fasthttp" @@ -30,25 +29,12 @@ func (h *GatewayAPI) createInstance(w http.ResponseWriter, req *http.Request, ps return } - var groupID = obj.Group - obj.Group = "" err = orm.Create(obj) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) log.Error(err) return } - if groupID != "" { - groupInstance := &gateway.InstanceGroup{} - groupInstance.InstanceID = obj.ID - groupInstance.GroupID = groupID - err = orm.Create(groupInstance) - if err != nil { - h.WriteError(w, err.Error(), http.StatusInternalServerError) - log.Error(err) - return - } - } h.WriteJSON(w, util.MapStr{ "_id": obj.ID, @@ -71,7 +57,6 @@ func (h *GatewayAPI) getInstance(w http.ResponseWriter, req *http.Request, ps ht }, http.StatusNotFound) return } - obj.Group, err = fetchInstanceGroup(id) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) log.Error(err) @@ -98,12 +83,6 @@ func (h *GatewayAPI) updateInstance(w http.ResponseWriter, req *http.Request, ps }, http.StatusNotFound) return } - oldGroup, err := fetchInstanceGroup(id) - if err != nil { - h.WriteError(w, err.Error(), http.StatusInternalServerError) - log.Error(err) - return - } id = obj.ID create := obj.Created @@ -114,50 +93,10 @@ func (h *GatewayAPI) updateInstance(w http.ResponseWriter, req *http.Request, ps log.Error(err) return } - if obj.Group != oldGroup { - delQuery := util.MapStr{ - "query": util.MapStr{ - "bool": util.MapStr{ - "must": []util.MapStr{ - //{ - // "term": util.MapStr{ - // "group_id": util.MapStr{ - // "value": oldGroup, - // }, - // }, - //}, - { - "term": util.MapStr{ - "instance_id": util.MapStr{ - "value": id, - }, - }, - }, - }, - }, - }, - } - err = orm.DeleteBy(&gateway.InstanceGroup{}, util.MustToJSONBytes(delQuery)) - if err != nil { - h.WriteError(w, err.Error(), http.StatusInternalServerError) - log.Error(err) - return - } - err = orm.Create(&gateway.InstanceGroup{ - GroupID: obj.Group, - InstanceID: id, - }) - if err != nil { - h.WriteError(w, err.Error(), http.StatusInternalServerError) - log.Error(err) - return - } - } //protect obj.ID = id obj.Created = create - obj.Group = "" err = orm.Update(&obj) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) @@ -202,14 +141,14 @@ func (h *GatewayAPI) deleteInstance(w http.ResponseWriter, req *http.Request, ps func (h *GatewayAPI) searchInstance(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { var ( - name = h.GetParameterOrDefault(req, "name", "") + keyword = h.GetParameterOrDefault(req, "keyword", "") queryDSL = `{"query":{"bool":{"must":[%s]}}, "size": %d, "from": %d}` strSize = h.GetParameterOrDefault(req, "size", "20") strFrom = h.GetParameterOrDefault(req, "from", "0") mustBuilder = &strings.Builder{} ) - if name != "" { - mustBuilder.WriteString(fmt.Sprintf(`{"prefix":{"name": "%s"}}`, name)) + if keyword != "" { + mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword)) } size, _ := strconv.Atoi(strSize) if size <= 0 { @@ -230,44 +169,7 @@ func (h *GatewayAPI) searchInstance(w http.ResponseWriter, req *http.Request, ps h.WriteError(w, err.Error(), http.StatusInternalServerError) return } - //fetch relationship - instanceIDs := pickElasticsearchColumnValues(res.Result, "id") - instanceGroups, err := fetchInstanceGroupByID(instanceIDs) - if err != nil { - log.Error(err) - h.WriteError(w, err.Error(), http.StatusInternalServerError) - return - } - //fetch group - - groupIDs := pickElasticsearchColumnValues(instanceGroups, "group_id") - groups, err := fetchGroupByID(groupIDs) - if err != nil { - log.Error(err) - h.WriteError(w, err.Error(), http.StatusInternalServerError) - return - } - groupsMap := getRelationshipMap(groups, "id", "name") - relationshipMap := getRelationshipMap(instanceGroups, "instance_id", "group_id") - - resultRes := &elastic.SearchResponse{} - err = util.FromJSONBytes(res.Raw, resultRes) - if err != nil { - h.WriteError(w, err.Error(), http.StatusInternalServerError) - log.Error(err) - return - } - for _, hit := range resultRes.Hits.Hits { - hit.Source["group"] = relationshipMap[hit.ID] - } - - h.WriteJSON(w, struct{ - elastic.SearchResponse - Groups interface{} `json:"groups"` - }{ - SearchResponse: *resultRes, - Groups: groupsMap, - }, http.StatusOK) + h.Write(w, res.Raw) } type GatewayConnectResponse struct {