mirror of
https://github.com/niusmallnan/steve.git
synced 2025-09-15 22:39:56 +00:00
Refactor
This commit is contained in:
@@ -3,20 +3,19 @@ package apigroups
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rancher/norman/v2/pkg/data"
|
||||
"github.com/rancher/norman/v2/pkg/store/empty"
|
||||
"github.com/rancher/norman/v2/pkg/types"
|
||||
"github.com/rancher/steve/pkg/schemaserver/store/empty"
|
||||
"github.com/rancher/steve/pkg/schemaserver/types"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/discovery"
|
||||
)
|
||||
|
||||
func Register(schemas *types.Schemas, discovery discovery.DiscoveryInterface) {
|
||||
schemas.MustImportAndCustomize(v1.APIGroup{}, func(schema *types.Schema) {
|
||||
func Register(schemas *types.APISchemas, discovery discovery.DiscoveryInterface) {
|
||||
schemas.MustImportAndCustomize(v1.APIGroup{}, func(schema *types.APISchema) {
|
||||
schema.CollectionMethods = []string{http.MethodGet}
|
||||
schema.ResourceMethods = []string{http.MethodGet}
|
||||
schema.Store = NewStore(discovery)
|
||||
schema.Formatter = func(request *types.APIRequest, resource *types.RawResource) {
|
||||
resource.ID = data.Object(resource.Values).String("name")
|
||||
resource.ID = resource.APIObject.Data().String("name")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -34,38 +33,32 @@ func NewStore(discovery discovery.DiscoveryInterface) types.Store {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Store) ByID(apiOp *types.APIRequest, schema *types.Schema, id string) (types.APIObject, error) {
|
||||
groupList, err := e.discovery.ServerGroups()
|
||||
if err != nil {
|
||||
return types.APIObject{}, err
|
||||
}
|
||||
|
||||
if id == "core" {
|
||||
id = ""
|
||||
}
|
||||
|
||||
for _, group := range groupList.Groups {
|
||||
if group.Name == id {
|
||||
return types.ToAPI(group), nil
|
||||
}
|
||||
}
|
||||
|
||||
return types.APIObject{}, nil
|
||||
func (e *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
||||
return types.DefaultByID(e, apiOp, schema, id)
|
||||
}
|
||||
|
||||
func (e *Store) List(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (types.APIObject, error) {
|
||||
groupList, err := e.discovery.ServerGroups()
|
||||
if err != nil {
|
||||
return types.APIObject{}, err
|
||||
func toAPIObject(schema *types.APISchema, group v1.APIGroup) types.APIObject {
|
||||
if group.Name == "" {
|
||||
group.Name = "core"
|
||||
}
|
||||
return types.APIObject{
|
||||
Type: schema.ID,
|
||||
ID: group.Name,
|
||||
Object: group,
|
||||
}
|
||||
|
||||
var result []interface{}
|
||||
}
|
||||
|
||||
func (e *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
|
||||
groupList, err := e.discovery.ServerGroups()
|
||||
if err != nil {
|
||||
return types.APIObjectList{}, err
|
||||
}
|
||||
|
||||
var result types.APIObjectList
|
||||
for _, item := range groupList.Groups {
|
||||
if item.Name == "" {
|
||||
item.Name = "core"
|
||||
}
|
||||
result = append(result, item)
|
||||
result.Objects = append(result.Objects, toAPIObject(schema, item))
|
||||
}
|
||||
|
||||
return types.ToAPI(result), nil
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user