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 05:21:07 UTC

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

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



##########
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"

Review comment:
       Put this package after the standard libs.

##########
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)

Review comment:
       It's not a good way, the JSON string will be broken if `p.Key` or `p.Val` has `"` in contents.

##########
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

Review comment:
       Well, we should put embed member at the top of struct definition.

##########
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 {

Review comment:
       You can reference the label implementation of istio: https://github.com/istio/istio/blob/0c0cf2d6fb/pkg/config/labels/instance.go#L55.
   
   Encapsulate the getMatch as `SubsetOf` will be more semantic.




----------------------------------------------------------------
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