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

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{}
})
}