use json encoding for route body parameters, implement modify user/group/pool operations, simplify schema by using comon lib struct tags and RequireAll and AtLeastOne helper functions, properly implement locadb as Backend interface
This commit is contained in:
+121
-9
@@ -9,32 +9,38 @@ import (
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
path string
|
||||
data map[string]common.Pool
|
||||
}
|
||||
|
||||
func LoadDB(localDBPath string) (DB, error) {
|
||||
db := DB{}
|
||||
var db *DB
|
||||
|
||||
func (db *DB) load(localDBPath string) error {
|
||||
db.data = make(map[string]common.Pool)
|
||||
db.path = localDBPath
|
||||
|
||||
root, err := os.OpenRoot(".")
|
||||
if err != nil {
|
||||
return db, err
|
||||
return err
|
||||
}
|
||||
defer root.Close()
|
||||
|
||||
content, err := root.ReadFile(localDBPath)
|
||||
if err != nil {
|
||||
return db, err
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &db.data)
|
||||
if err != nil {
|
||||
return db, err
|
||||
return err
|
||||
}
|
||||
return db, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveDB(localDBPath string, db DB) error {
|
||||
json, err := json.Marshal(db.data)
|
||||
func (db *DB) save() error {
|
||||
localDBPath := db.path
|
||||
// write to file with pretty print for readability reasons
|
||||
json, err := json.MarshalIndent(db.data, "", "\t")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -42,10 +48,116 @@ func SaveDB(localDBPath string, db DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func NewClientFromCredentials(config common.LocalDBConfig, username common.Username, password string) (common.Backend, int, error) {
|
||||
if db != nil {
|
||||
return *db, http.StatusOK, nil
|
||||
} else {
|
||||
// load localdb if this is the first time
|
||||
db = &DB{}
|
||||
err := db.load(config.Path)
|
||||
if err != nil {
|
||||
return *db, http.StatusInternalServerError, err
|
||||
} else {
|
||||
return *db, http.StatusOK, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (localdb DB) GetPool(poolname string) (common.Pool, []string, int, error) {
|
||||
pool, ok := localdb.data[poolname]
|
||||
if !ok {
|
||||
return pool, []string{}, http.StatusNotFound, fmt.Errorf("pool %s not in localdb", poolname)
|
||||
return pool, []string{}, http.StatusNotFound, fmt.Errorf("localdb pool %s does not exist", poolname)
|
||||
}
|
||||
return pool, []string{}, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (localdb DB) NewPool(poolname string, pool common.Pool) (int, error) {
|
||||
_, ok := localdb.data[poolname]
|
||||
if ok {
|
||||
return http.StatusBadRequest, fmt.Errorf("localdb pool %s already exists", poolname)
|
||||
}
|
||||
localdb.data[poolname] = pool
|
||||
err := localdb.save()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
} else {
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (localdb DB) ModPool(poolname string, pool common.Pool) (int, error) {
|
||||
_, ok := localdb.data[poolname]
|
||||
if !ok {
|
||||
return http.StatusBadRequest, fmt.Errorf("localdb pool %s does not exist", poolname)
|
||||
}
|
||||
old_pool := localdb.data[poolname]
|
||||
MergeNonZero(&old_pool, &pool)
|
||||
err := localdb.save()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
} else {
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (localdb DB) DelPool(poolname string) (int, error) {
|
||||
_, ok := localdb.data[poolname]
|
||||
if !ok {
|
||||
return http.StatusBadRequest, fmt.Errorf("localdb pool %s does not exist", poolname)
|
||||
}
|
||||
delete(localdb.data, poolname)
|
||||
err := localdb.save()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
} else {
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (localdb DB) NewGroup(groupname common.Groupname, group common.Group) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement groups")
|
||||
}
|
||||
|
||||
func (localdb DB) ModGroup(groupname common.Groupname, group common.Group) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement groups")
|
||||
}
|
||||
|
||||
func (localdb DB) GetGroup(groupname common.Groupname) (common.Group, []string, int, error) {
|
||||
return common.Group{}, []string{}, http.StatusNotImplemented, fmt.Errorf("localdb does not implement groups")
|
||||
}
|
||||
|
||||
func (localdb DB) DelGroup(groupname common.Groupname) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement groups")
|
||||
}
|
||||
|
||||
func (localdb DB) AddGroupToPool(groupname common.Groupname, poolname string) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement groups")
|
||||
}
|
||||
|
||||
func (localdb DB) DelGroupFromPool(groupname common.Groupname, poolname string) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement groups")
|
||||
}
|
||||
|
||||
func (localdb DB) NewUser(username common.Username, user common.User) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement users")
|
||||
}
|
||||
|
||||
func (localdb DB) ModUser(username common.Username, user common.User) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement users")
|
||||
}
|
||||
|
||||
func (localdb DB) GetUser(username common.Username) (common.User, int, error) {
|
||||
return common.User{}, http.StatusNotImplemented, fmt.Errorf("localdb does not implement users")
|
||||
}
|
||||
|
||||
func (localdb DB) DelUser(username common.Username) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement users")
|
||||
}
|
||||
|
||||
func (localdb DB) AddUserToGroup(username common.Username, groupname common.Groupname) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement users")
|
||||
}
|
||||
|
||||
func (localdb DB) DelUserFromGroup(username common.Username, groupname common.Groupname) (int, error) {
|
||||
return http.StatusNotImplemented, fmt.Errorf("localdb does not implement users")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package localdb
|
||||
|
||||
import "reflect"
|
||||
|
||||
// MergeNonZero overwrites fields in dst with fields in src iff src is not a zero value.
|
||||
func MergeNonZero[T any](dst *T, src *T) {
|
||||
vDst := reflect.ValueOf(dst).Elem()
|
||||
vSrc := reflect.ValueOf(src).Elem()
|
||||
|
||||
for i := 0; i < vDst.NumField(); i++ {
|
||||
dstField := vDst.Field(i)
|
||||
srcField := vSrc.Field(i)
|
||||
|
||||
// Only set if the field is exported (CanSet) and the source is not a zero value
|
||||
if dstField.CanSet() && !srcField.IsZero() {
|
||||
dstField.Set(srcField)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user