You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/19 01:55:49 UTC

[GitHub] [apisix-dashboard] starsz commented on a change in pull request #1072: feat: support labels list

starsz commented on a change in pull request #1072:
URL: https://github.com/apache/apisix-dashboard/pull/1072#discussion_r546174112



##########
File path: api/internal/handler/label/label.go
##########
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package label
+
+import (
+	"encoding/json"
+	"fmt"
+	"github.com/shiningrush/droplet/data"
+	"net/http"
+	"reflect"
+	"sort"
+	"strings"
+
+	"github.com/gin-gonic/gin"
+	"github.com/shiningrush/droplet"
+	"github.com/shiningrush/droplet/wrapper"
+	wgin "github.com/shiningrush/droplet/wrapper/gin"
+
+	"github.com/apisix/manager-api/internal/core/entity"
+	"github.com/apisix/manager-api/internal/core/store"
+	"github.com/apisix/manager-api/internal/handler"
+	"github.com/apisix/manager-api/internal/utils"
+)
+
+type Handler struct {
+	routeStore    store.Interface
+	serviceStore  store.Interface
+	upstreamStore store.Interface
+	sslStore      store.Interface
+	consumerStore store.Interface
+}
+
+var _ json.Marshaler = Pair{}
+
+type Pair struct {
+	Key string
+	Val string
+}
+
+func (p Pair) MarshalJSON() ([]byte, error) {
+	res := fmt.Sprintf("{\"%s\":\"%s\"}", p.Key, p.Val)
+	return []byte(res), nil
+}
+
+func NewHandler() (handler.RouteRegister, error) {
+	return &Handler{
+		routeStore:    store.GetStore(store.HubKeyRoute),
+		serviceStore:  store.GetStore(store.HubKeyService),
+		upstreamStore: store.GetStore(store.HubKeyUpstream),
+		sslStore:      store.GetStore(store.HubKeySsl),
+		consumerStore: store.GetStore(store.HubKeyConsumer),
+	}, nil
+}
+
+func (h *Handler) ApplyRoute(r *gin.Engine) {
+	r.GET("/api/labels/:type", wgin.Wraps(h.List,
+		wrapper.InputType(reflect.TypeOf(ListInput{}))))
+}
+
+type ListInput struct {
+	Type  string `auto_read:"type,path" validate:"required"`
+	Label string `auto_read:"label,query"`
+	store.Pagination
+}
+
+func getMatch(reqLabels, labels map[string]string) map[string]string {
+	if len(reqLabels) == 0 {
+		return labels
+	}
+
+	var res = make(map[string]string)
+	for k, v := range labels {
+		l, exist := reqLabels[k]
+		if exist && ((l == "") || v == l) {
+			res[k] = v
+		}
+	}
+
+	return res
+}
+
+// swagger:operation GET /api/labels getLabelsList
+//
+// Return the labels list among `route,ssl,consumer,upstream,service`
+// according to the specified page number and page size, and can search labels by label.
+//
+// ---
+// produces:
+// - application/json
+// parameters:
+// - name: page
+//   in: query
+//   description: page number
+//   required: false
+//   type: integer
+// - name: page_size
+//   in: query
+//   description: page size
+//   required: false
+//   type: integer
+// - name: label
+//   in: query
+//   description: label filter of labels
+//   required: false
+//   type: string
+// responses:
+//   '0':
+//     description: list response
+//     schema:
+//       type: array
+//       items:
+//         "$ref": "#/definitions/service"
+//   default:
+//     description: unexpected error
+//     schema:
+//       "$ref": "#/definitions/ApiError"
+func (h *Handler) List(c droplet.Context) (interface{}, error) {
+	input := c.Input().(*ListInput)
+
+	typ := input.Type
+	reqLabels, err := utils.GenLabelMap(input.Label)
+	if err != nil {
+		return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
+			fmt.Errorf("%s: \"%s\"", err.Error(), input.Label)
+	}
+
+	var items []interface{}
+	switch typ {

Review comment:
       Because the `type` is a keyword in golang.So I often use `typ` instead of `type`.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org