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 2022/08/08 06:25:46 UTC

[GitHub] [apisix-dashboard] nic-chen commented on a diff in pull request #2570: feat: add Admin API storage implementation

nic-chen commented on code in PR #2570:
URL: https://github.com/apache/apisix-dashboard/pull/2570#discussion_r939862749


##########
api/internal/core/storage/adminapi.go:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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 storage
+
+import (
+	"context"
+	"fmt"
+	"strings"
+	"time"
+
+	"github.com/go-resty/resty/v2"
+	"github.com/pkg/errors"
+	"github.com/tidwall/gjson"
+
+	"github.com/apache/apisix-dashboard/api/pkg/storage"
+)
+
+const (
+	adminAPIErrorWarp = "failed to request Admin API"
+)
+
+type adminAPIVersion = int
+
+const (
+	adminAPIVersion2 adminAPIVersion = 2
+	adminApiVersion3 adminAPIVersion = 3
+)
+
+// Ensure that the AdminAPI Storage implementation conforms to the storage_api interface definition
+var _ storage_api.Interface = AdminAPIStorage{}
+
+// NewAdminAPIStorage will create an instance of AdminAPIStorage and complete the basic configure
+func NewAdminAPIStorage() *AdminAPIStorage {
+	return &AdminAPIStorage{
+		client: resty.New().
+			SetBaseURL("http://127.0.0.1:9080/apisix/admin/").
+			SetHeader("X-API-KEY", "edd1c9f034335f136f87ad84b625c8f1").
+			SetTimeout(1 * time.Second),
+	}
+}
+
+// AdminAPIStorage is the storage layer implementation of the APISIX Admin API
+type AdminAPIStorage struct {
+	client *resty.Client
+}
+
+func (s AdminAPIStorage) Get(ctx context.Context, key string) (string, error) {
+	resp, err := s.client.R().
+		SetContext(ctx).
+		Get(s.removeKeyPrefix(key))
+	if err != nil {
+		return "", errors.Wrap(err, adminAPIErrorWarp)
+	}
+
+	json := gjson.Parse(resp.String())
+	if json.Get("count").Int() < 1 {
+		return "", errors.Errorf("key: %s is not found", key)
+	}
+
+	return gjson.Get(resp.String(), "node.value").Raw, nil
+}
+
+func (s AdminAPIStorage) List(ctx context.Context, key string) ([]storage_api.Keypair, error) {
+	resp, err := s.client.R().
+		SetContext(ctx).
+		Get(s.removeKeyPrefix(key))
+	if err != nil {
+		return nil, errors.Wrap(err, adminAPIErrorWarp)
+	}
+
+	switch resp.Header().Get("X-API-VERSION") {
+	case "v3":
+		return s.parseList(adminApiVersion3, resp.String()), nil
+	case "v2":
+		fallthrough
+	default:
+		return s.parseList(adminAPIVersion2, resp.String()), nil
+	}
+}
+
+func (s AdminAPIStorage) Create(ctx context.Context, key, val string) error {
+	return s.Update(ctx, key, val)
+}
+
+func (s AdminAPIStorage) Update(ctx context.Context, key, val string) error {
+	resp, err := s.client.R().
+		SetContext(ctx).
+		SetHeader("Content-Type", "application/json").
+		SetBody(val).
+		Put(s.removeKeyPrefix(key))
+	if err != nil {
+		return errors.Wrap(err, adminAPIErrorWarp)
+	}
+
+	if err = s.checkError(resp.String()); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (s AdminAPIStorage) BatchDelete(ctx context.Context, keys []string) error {
+	for _, key := range keys {
+		resp, err := s.client.R().
+			SetContext(ctx).
+			Delete(s.removeKeyPrefix(key))
+		if err != nil {
+			return errors.Wrap(err, adminAPIErrorWarp)
+		}
+
+		if err = s.checkError(resp.String()); err != nil {
+			return fmt.Errorf("delete key[%s] failed: %s", key, err)
+		}
+
+		if gjson.Get(resp.String(), "deleted").Int() < 1 {
+			return errors.Errorf("key: %s is not found", key)
+		}
+	}
+
+	return nil
+}
+
+// Watch watches for changes in data sources and generates event streams
+// Admin API storage implementation does not support Watch capability
+func (s AdminAPIStorage) Watch(_ context.Context, _ string) <-chan storage_api.WatchResponse {
+	return make(chan storage_api.WatchResponse)
+}
+
+// removeKeyPrefix will remove the "/apisix/" prefix of key
+// the pattern of the key is usually like "/apisix/routes"
+func (AdminAPIStorage) removeKeyPrefix(s string) string {
+	return strings.ReplaceAll(s, "/apisix/", "")

Review Comment:
   the prefix needs to be configurated too.



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

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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