Files
kubevela/pkg/apiserver/datastore/datastore.go
barnettZQG 044c4bf73c Feat: add RBAC support (#3493)
* Feat: add the rbac data model

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add some api about the project

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add CRUD about the project and the project user

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add CRUD about the role and perm check filter function

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: update swagger config

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add default roles and perm policies

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add perm check filter for all webservice

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: change the method that find project name

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: query applications and envs by user perm

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: support get login user info

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Fix: change default permissions

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: change PermPolicy to Permission

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add some unit test and fix the e2e test error

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Fix: change some comment word

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Fix: e2e api path error

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
2022-03-28 16:03:11 +08:00

167 lines
4.5 KiB
Go

/*
Copyright 2021 The KubeVela Authors.
Licensed 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 datastore
import (
"context"
"fmt"
"reflect"
"time"
)
var (
// ErrPrimaryEmpty Error that primary key is empty.
ErrPrimaryEmpty = NewDBError(fmt.Errorf("entity primary is empty"))
// ErrTableNameEmpty Error that table name is empty.
ErrTableNameEmpty = NewDBError(fmt.Errorf("entity table name is empty"))
// ErrNilEntity Error that entity is nil
ErrNilEntity = NewDBError(fmt.Errorf("entity is nil"))
// ErrRecordExist Error that entity primary key is exist
ErrRecordExist = NewDBError(fmt.Errorf("data record is exist"))
// ErrRecordNotExist Error that entity primary key is not exist
ErrRecordNotExist = NewDBError(fmt.Errorf("data record is not exist"))
// ErrIndexInvalid Error that entity index is invalid
ErrIndexInvalid = NewDBError(fmt.Errorf("entity index is invalid"))
// ErrEntityInvalid Error that entity is invalid
ErrEntityInvalid = NewDBError(fmt.Errorf("entity is invalid"))
)
// DBError datastore error
type DBError struct {
err error
}
func (d *DBError) Error() string {
return d.err.Error()
}
// NewDBError new datastore error
func NewDBError(err error) error {
return &DBError{err: err}
}
// Config datastore config
type Config struct {
Type string
URL string
Database string
}
// Entity database data model
type Entity interface {
SetCreateTime(time time.Time)
SetUpdateTime(time time.Time)
PrimaryKey() string
TableName() string
ShortTableName() string
Index() map[string]string
}
// NewEntity Create a new object based on the input type
func NewEntity(in Entity) (Entity, error) {
if in == nil {
return nil, ErrNilEntity
}
t := reflect.TypeOf(in)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
new := reflect.New(t)
return new.Interface().(Entity), nil
}
// SortOrder is the order of sort
type SortOrder int
const (
// SortOrderAscending defines the order of ascending for sorting
SortOrderAscending = SortOrder(1)
// SortOrderDescending defines the order of descending for sorting
SortOrderDescending = SortOrder(-1)
)
// SortOption describes the sorting parameters for list
type SortOption struct {
Key string
Order SortOrder
}
// FuzzyQueryOption defines the fuzzy query search filter option
type FuzzyQueryOption struct {
Key string
Query string
}
// InQueryOption defines the include search filter option
type InQueryOption struct {
Key string
Values []string
}
// IsNotExistQueryOption means the value is empty
type IsNotExistQueryOption struct {
Key string
}
// FilterOptions filter query returned items
type FilterOptions struct {
Queries []FuzzyQueryOption
In []InQueryOption
IsNotExist []IsNotExistQueryOption
}
// ListOptions list api options
type ListOptions struct {
FilterOptions
Page int
PageSize int
SortBy []SortOption
}
// DataStore datastore interface
type DataStore interface {
// Add adds entity to database, Name() and TableName() can't return zero value.
Add(ctx context.Context, entity Entity) error
// BatchAdd will adds batched entities to database, Name() and TableName() can't return zero value.
BatchAdd(ctx context.Context, entities []Entity) error
// Put will update entity to database, Name() and TableName() can't return zero value.
Put(ctx context.Context, entity Entity) error
// Delete entity from database, Name() and TableName() can't return zero value.
Delete(ctx context.Context, entity Entity) error
// Get entity from database, Name() and TableName() can't return zero value.
Get(ctx context.Context, entity Entity) error
// List entities from database, TableName() can't return zero value, if no matches, it will return a zero list without error.
List(ctx context.Context, query Entity, options *ListOptions) ([]Entity, error)
// Count entities from database, TableName() can't return zero value.
Count(ctx context.Context, entity Entity, options *FilterOptions) (int64, error)
// IsExist Name() and TableName() can't return zero value.
IsExist(ctx context.Context, entity Entity) (bool, error)
}