Add userpreference

This commit is contained in:
Darren Shepherd
2021-03-01 22:26:26 -07:00
parent 0e4549656b
commit 6b2a9678f1
5 changed files with 130 additions and 0 deletions

1
go.mod
View File

@@ -11,6 +11,7 @@ replace (
)
require (
github.com/adrg/xdg v0.3.1
github.com/gorilla/mux v1.7.3
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/imdario/mergo v0.3.8 // indirect

2
go.sum
View File

@@ -44,6 +44,8 @@ github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/adrg/xdg v0.3.1 h1:uIyL9BYfXaFgDyVRKE8wjtm6ETQULweQqTofphEFJYY=
github.com/adrg/xdg v0.3.1/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

View File

@@ -14,6 +14,7 @@ import (
"github.com/rancher/steve/pkg/resources/common"
"github.com/rancher/steve/pkg/resources/counts"
"github.com/rancher/steve/pkg/resources/formatters"
"github.com/rancher/steve/pkg/resources/userpreferences"
"github.com/rancher/steve/pkg/schema"
steveschema "github.com/rancher/steve/pkg/schema"
"github.com/rancher/steve/pkg/stores/proxy"
@@ -27,6 +28,7 @@ func DefaultSchemas(ctx context.Context, baseSchema *types.APISchemas, ccache cl
subscribe.Register(baseSchema)
apiroot.Register(baseSchema, []string{"v1"}, "proxy:/apis")
cluster.Register(ctx, baseSchema, cg, schemaFactory)
userpreferences.Register(baseSchema)
return nil
}

View File

@@ -0,0 +1,105 @@
package userpreferences
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/adrg/xdg"
"github.com/rancher/apiserver/pkg/store/empty"
"github.com/rancher/apiserver/pkg/types"
"k8s.io/apiserver/pkg/endpoints/request"
)
var (
rancherSchema = "management.cattle.io.preference"
)
type localStore struct {
empty.Store
}
func confDir() string {
return filepath.Join(xdg.ConfigHome, "steve")
}
func confFile() string {
return filepath.Join(confDir(), "prefs.json")
}
func set(data map[string]interface{}) error {
if err := os.MkdirAll(confDir(), 0700); err != nil {
return err
}
bytes, err := json.Marshal(data)
if err != nil {
return err
}
return ioutil.WriteFile(confFile(), bytes, 0600)
}
func get() (map[string]string, error) {
data := UserPreference{}
f, err := os.Open(confFile())
if os.IsNotExist(err) {
return map[string]string{}, nil
} else if err != nil {
return nil, err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&data); err != nil {
return nil, err
}
return data.Data, nil
}
func getUserName(apiOp *types.APIRequest) string {
user, ok := request.UserFrom(apiOp.Context())
if !ok {
return "local"
}
return user.GetName()
}
func (l *localStore) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
data, err := get()
if err != nil {
return types.APIObject{}, err
}
return types.APIObject{
Type: "userpreference",
ID: getUserName(apiOp),
Object: UserPreference{
Data: data,
},
}, nil
}
func (l *localStore) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
obj, err := l.ByID(apiOp, schema, "")
if err != nil {
return types.APIObjectList{}, err
}
return types.APIObjectList{
Objects: []types.APIObject{
obj,
},
}, nil
}
func (l *localStore) Update(apiOp *types.APIRequest, schema *types.APISchema, data types.APIObject, id string) (types.APIObject, error) {
err := set(data.Data())
if err != nil {
return types.APIObject{}, err
}
return l.ByID(apiOp, schema, "")
}
func (l *localStore) Delete(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
return l.Update(apiOp, schema, types.APIObject{
Object: map[string]interface{}{},
}, "")
}

View File

@@ -0,0 +1,20 @@
package userpreferences
import (
"net/http"
"github.com/rancher/apiserver/pkg/types"
)
type UserPreference struct {
Data map[string]string `json:"data"`
}
func Register(schemas *types.APISchemas) {
schemas.InternalSchemas.TypeName("userpreference", UserPreference{})
schemas.MustImportAndCustomize(UserPreference{}, func(schema *types.APISchema) {
schema.CollectionMethods = []string{http.MethodGet}
schema.ResourceMethods = []string{http.MethodGet, http.MethodPut, http.MethodDelete}
schema.Store = &localStore{}
})
}