Skip to content

Commit

Permalink
创建前校验
Browse files Browse the repository at this point in the history
  • Loading branch information
linfang-canway committed Oct 31, 2023
1 parent 01a2de1 commit 0e7f15d
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 7 deletions.
16 changes: 11 additions & 5 deletions bcs-services/bcs-bscp/pkg/dal/vault/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"fmt"

"bscp.io/pkg/kit"
"bscp.io/pkg/types"
)

const (
Expand All @@ -26,12 +27,17 @@ const (
)

// UpsertKv 创建|更新kv
func (s *set) UpsertKv(kit *kit.Kit, bizID, appID uint32, key, content string) (int, error) {
func (s *set) UpsertKv(kit *kit.Kit, opt *types.UpsertKvOption) (int, error) {

if err := opt.Validate(); err != nil {
return 0, err
}

data := map[string]interface{}{
"data": content,
"type": opt.KvType,
"value": opt.Value,
}
secret, err := s.cli.KVv2(MountPath).Put(kit.Ctx, fmt.Sprintf(kvPath, bizID, appID, key), data)
secret, err := s.cli.KVv2(MountPath).Put(kit.Ctx, fmt.Sprintf(kvPath, opt.BizID, opt.AppID, opt.Key), data)
if err != nil {
return 0, err
}
Expand All @@ -41,9 +47,9 @@ func (s *set) UpsertKv(kit *kit.Kit, bizID, appID uint32, key, content string) (
}

// GetLastKv 获取最新的kv
func (s *set) GetLastKv(kit *kit.Kit, bizID, appID uint32, key string) (string, error) {
func (s *set) GetLastKv(kit *kit.Kit, opt *types.GetLastKvOpt) (string, error) {

kv, err := s.cli.KVv2(MountPath).Get(kit.Ctx, fmt.Sprintf(kvPath, bizID, appID, key))
kv, err := s.cli.KVv2(MountPath).Get(kit.Ctx, fmt.Sprintf(kvPath, opt.BizID, opt.AppID, opt.Key))
if err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions bcs-services/bcs-bscp/pkg/dal/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"bscp.io/pkg/cc"
"bscp.io/pkg/kit"
"bscp.io/pkg/types"
)

// Set ...
Expand All @@ -27,9 +28,9 @@ type Set interface {
// CreateMountPath 创建挂载目录
CreateMountPath(path string, config *vault.MountInput) error
// UpsertKv 创建|更新kv
UpsertKv(kit *kit.Kit, bizID, appID uint32, key, content string) (int, error)
UpsertKv(kit *kit.Kit, opt *types.UpsertKvOption) (int, error)
// GetLastKv 获取最新的kv
GetLastKv(kit *kit.Kit, bizID, appID uint32, key string) (string, error)
GetLastKv(kit *kit.Kit, opt *types.GetLastKvOpt) (string, error)
// GetKvByVersion 根据版本获取kv
GetKvByVersion(kit *kit.Kit, bizID, appID uint32, key string, version int) (string, error)
}
Expand Down
168 changes: 168 additions & 0 deletions bcs-services/bcs-bscp/pkg/types/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
Tencent is pleased to support the open source community by making Basic Service Configuration Platform available.
Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://opensource.org/licenses/MIT
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 types

import (
"encoding/json"
"errors"
"fmt"
"strconv"

"gopkg.in/yaml.v3"
)

// ListKvOption defines options to list kv.
type ListKvOption struct {
BizID uint32 `json:"biz_id"`
AppID uint32 `json:"app_id"`
Name string `json:"name"`
ID uint32 `json:"id"`
SearchKey string `json:"search_key"`
All bool `json:"all"`
Page *BasePage `json:"page"`
}

func (opt *ListKvOption) Validate(po *PageOption) error {

Check warning on line 35 in bcs-services/bcs-bscp/pkg/types/kv.go

View workflow job for this annotation

GitHub Actions / bcs-bscp

exported: exported method ListKvOption.Validate should have comment or be unexported (revive)
if opt.BizID <= 0 {
return errors.New("invalid biz id, should >= 1")
}
if opt.AppID <= 0 {
return errors.New("invalid app id, should >= 1")
}

if opt.Page == nil {
return errors.New("page is null")
}

if err := opt.Page.Validate(po); err != nil {
return err
}

return nil
}

// KvType is the type of kv
type KvType string

const (
// KvStr is the type for string kv
KvStr KvType = "string"
// KvNumber is the type for number kv
KvNumber KvType = "number"
// KvText is the type for text kv
KvText KvType = "text"
// KvJson is the type for json kv
KvJson KvType = "json"
// KvYAML is the type for yaml kv
KvYAML KvType = "yaml"
)

func (k KvType) Validate(value string) error {

Check warning on line 70 in bcs-services/bcs-bscp/pkg/types/kv.go

View workflow job for this annotation

GitHub Actions / bcs-bscp

exported: exported method KvType.Validate should have comment or be unexported (revive)

if value == "" {
return errors.New("kv value is null")
}

switch k {
case KvStr:
return nil
case KvNumber:
if isStringConvertibleToNumber(value) {
return fmt.Errorf("value is not a number")
}
return nil
case KvText:
return nil
case KvJson:
if !json.Valid([]byte(value)) {
return fmt.Errorf("value is not a json")
}
return nil
case KvYAML:
var data interface{}
if err := yaml.Unmarshal([]byte(value), &data); err != nil {
return fmt.Errorf("value is not a yaml, err: %v", err)
}
return nil
default:
return errors.New("revision not set")
}
}

func isStringConvertibleToNumber(s string) bool {
_, err := strconv.Atoi(s)
if err == nil {
return true
}

_, err = strconv.ParseFloat(s, 64)
return err == nil

}

// UpsertKvOption ...
type UpsertKvOption struct {
BizID uint32
AppID uint32
Key string
Value string
KvType KvType
}

// Validate ...
func (o *UpsertKvOption) Validate() error {
if o.BizID <= 0 {
return errors.New("invalid biz id, should >= 1")
}

if o.AppID <= 0 {
return errors.New("invalid app id, should >= 1")
}

if o.Key == "" {
return errors.New("kv key is required")
}

if o.Value == "" {
return errors.New("kv value is required")
}

if err := o.KvType.Validate(o.Value); err != nil {
return err
}

return nil
}

// GetLastKvOpt ...
type GetLastKvOpt struct {
BizID uint32
AppID uint32
Key string
}

// Validate ...
func (o *GetLastKvOpt) Validate() error {
if o.BizID <= 0 {
return errors.New("invalid biz id, should >= 1")
}

if o.AppID <= 0 {
return errors.New("invalid app id, should >= 1")
}

if o.Key == "" {
return errors.New("kv key is required")
}
return nil
}

0 comments on commit 0e7f15d

Please sign in to comment.