Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various changes #34

Merged
merged 5 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/access/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ByID(context *types.APIContext, version *types.APIVersion, typeName string,
return convert.ToObj(item, into)
}

func List(context *types.APIContext, version *types.APIVersion, typeName string, opts types.QueryOptions, into interface{}) error {
func List(context *types.APIContext, version *types.APIVersion, typeName string, opts *types.QueryOptions, into interface{}) error {
schema := context.Schemas.Schema(version, typeName)
if schema == nil {
return fmt.Errorf("failed to find schema " + typeName)
Expand Down
2 changes: 1 addition & 1 deletion api/builtin/api_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (a *APIRootStore) ByID(apiContext *types.APIContext, schema *types.Schema,
return nil, nil
}

func (a *APIRootStore) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (a *APIRootStore) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
var roots []map[string]interface{}

for _, version := range apiContext.Schemas.Versions() {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ListHandler(request *types.APIContext) error {

if request.ID == "" {
opts := parse.QueryOptions(request, request.Schema)
data, err = store.List(request, request.Schema, opts)
data, err = store.List(request, request.Schema, &opts)
} else if request.Link == "" {
data, err = store.ByID(request, request.Schema, request.ID)
} else {
Expand Down
4 changes: 2 additions & 2 deletions api/handler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/rancher/norman/types/convert"
)

func QueryFilter(opts types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
func QueryFilter(opts *types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
return ApplyQueryOptions(opts, data)
}

func ApplyQueryOptions(options types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
func ApplyQueryOptions(options *types.QueryOptions, data []map[string]interface{}) []map[string]interface{} {
data = ApplyQueryConditions(options.Conditions, data)
data = ApplySort(options.Sort, data)
return ApplyPagination(options.Pagination, data)
Expand Down
4 changes: 2 additions & 2 deletions api/writer/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var (
<!DOCTYPE html>
<!-- If you are reading this, there is a good chance you would prefer sending an
"Accept: application/json" header and receiving actual JSON responses. -->
<link rel="stylesheet" type="text/css" href="https://releases.rancher.com/api-ui/1.1.2/ui.min.css" />
<script src="https://releases.rancher.com/api-ui/1.1.2/ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://releases.rancher.com/api-ui/1.1.3/ui.min.css" />
<script src="https://releases.rancher.com/api-ui/1.1.3/ui.min.js"></script>
<script>
var user = "admin";
var curlUser='${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}';
Expand Down
9 changes: 7 additions & 2 deletions pkg/subscribe/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ func handler(apiContext *types.APIContext) error {
if err != nil {
return err
}

header := `{"name":"resource.change","data":`
if item[".removed"] == true {
header = `{"name":"resource.removed","data":`
}
schema := apiContext.Schemas.Schema(apiContext.Version, convert.ToString(item["type"]))
if schema != nil {
buffer := &bytes.Buffer{}
if err := jsonWriter.VersionBody(apiContext, &schema.Version, buffer, item); err != nil {
return err
}
if _, err := messageWriter.Write([]byte(`{"name":"resource.change","data":`)); err != nil {
if _, err := messageWriter.Write([]byte(header)); err != nil {
return err
}
if _, err := messageWriter.Write(buffer.Bytes()); err != nil {
Expand All @@ -115,7 +120,7 @@ func handler(apiContext *types.APIContext) error {
func streamStore(ctx context.Context, eg *errgroup.Group, apiContext *types.APIContext, schema *types.Schema, result chan map[string]interface{}) {
eg.Go(func() error {
opts := parse.QueryOptions(apiContext, schema)
events, err := schema.Store.Watch(apiContext, schema, opts)
events, err := schema.Store.Watch(apiContext, schema, &opts)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions store/crd/crd_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ func (c *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id st
return store.Delete(apiContext, schema, id)
}

func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (c *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
store, ok := c.schemaStores[key(schema)]
if !ok {
return nil, nil
}
return store.List(apiContext, schema, opt)
}

func (c *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (c *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
store, ok := c.schemaStores[key(schema)]
if !ok {
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion store/empty/empty_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func (e *Store) Update(apiContext *types.APIContext, schema *types.Schema, data
return nil, nil
}

func (e *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (e *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
return nil, nil
}
9 changes: 6 additions & 3 deletions store/proxy/proxy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (p *Store) byID(apiContext *types.APIContext, schema *types.Schema, id stri
return p.singleResult(apiContext, schema, req)
}

func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
namespace := getNamespace(apiContext, opt)

req := p.common(namespace, p.k8sClient.Get())
Expand All @@ -99,7 +99,7 @@ func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt typ
return apiContext.AccessControl.FilterList(apiContext, result, p.authContext), nil
}

func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
namespace := getNamespace(apiContext, opt)

req := p.common(namespace, p.k8sClient.Get())
Expand All @@ -126,6 +126,9 @@ func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt ty
for event := range watcher.ResultChan() {
data := event.Object.(*unstructured.Unstructured)
p.fromInternal(schema, data.Object)
if event.Type == watch.Deleted && data.Object != nil {
data.Object[".removed"] = true
}
result <- apiContext.AccessControl.Filter(apiContext, data.Object, p.authContext)
}
close(result)
Expand All @@ -144,7 +147,7 @@ func (d *unstructuredDecoder) Decode(data []byte, defaults *schema.GroupVersionK
return into, defaults, ejson.Unmarshal(data, &into)
}

func getNamespace(apiContext *types.APIContext, opt types.QueryOptions) string {
func getNamespace(apiContext *types.APIContext, opt *types.QueryOptions) string {
if val, ok := apiContext.SubContext["namespaces"]; ok {
return convert.ToString(val)
}
Expand Down
4 changes: 2 additions & 2 deletions store/schema/schema_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id stri
return nil, nil
}

func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (s *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
return nil, nil
}

func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
schemaMap := apiContext.Schemas.SchemasForVersion(*apiContext.Version)
schemas := make([]*types.Schema, 0, len(schemaMap))
schemaData := make([]map[string]interface{}, 0, len(schemaMap))
Expand Down
11 changes: 9 additions & 2 deletions store/subtype/subtype_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ func NewSubTypeStore(subType string, store types.Store) *Store {
}
}

func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if data != nil {
data["kind"] = p.subType
}
return p.Store.Create(apiContext, schema, data)
}

func (p *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
return p.Store.List(apiContext, schema, opt)
}

func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
opt.Conditions = append(opt.Conditions, types.NewConditionFromString("type", types.ModifierEQ, p.subType))
return p.Store.Watch(apiContext, schema, opt)
}
4 changes: 2 additions & 2 deletions store/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (t *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id stri
return t.Transformer(apiContext, data)
}

func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
c, err := t.Store.Watch(apiContext, schema, opt)
if err != nil {
return nil, err
Expand All @@ -50,7 +50,7 @@ func (t *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt ty
return result, nil
}

func (t *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
func (t *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
data, err := t.Store.List(apiContext, schema, opt)
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions store/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (s *StoreWrapper) ByID(apiContext *types.APIContext, schema *types.Schema,
return nil, err
}

return apiContext.FilterObject(types.QueryOptions{
return apiContext.FilterObject(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, data), nil
}

func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema, opts types.QueryOptions) ([]map[string]interface{}, error) {
func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema, opts *types.QueryOptions) ([]map[string]interface{}, error) {
opts.Conditions = append(opts.Conditions, apiContext.SubContextAttributeProvider.Query(apiContext, schema)...)
data, err := s.store.List(apiContext, schema, opts)
if err != nil {
Expand All @@ -36,7 +36,7 @@ func (s *StoreWrapper) List(apiContext *types.APIContext, schema *types.Schema,
return apiContext.FilterList(opts, data), nil
}

func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) (chan map[string]interface{}, error) {
func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
c, err := s.store.Watch(apiContext, schema, opt)
if err != nil || c == nil {
return nil, err
Expand All @@ -45,7 +45,7 @@ func (s *StoreWrapper) Watch(apiContext *types.APIContext, schema *types.Schema,
result := make(chan map[string]interface{})
go func() {
for item := range c {
item = apiContext.FilterObject(types.QueryOptions{
item = apiContext.FilterObject(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, item)
if item != nil {
Expand Down Expand Up @@ -85,7 +85,7 @@ func (s *StoreWrapper) Update(apiContext *types.APIContext, schema *types.Schema
return nil, err
}

return apiContext.FilterObject(types.QueryOptions{
return apiContext.FilterObject(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, data), nil
}
Expand All @@ -109,7 +109,7 @@ func validateGet(apiContext *types.APIContext, schema *types.Schema, id string)
return err
}

if apiContext.Filter(types.QueryOptions{
if apiContext.Filter(&types.QueryOptions{
Conditions: apiContext.SubContextAttributeProvider.Query(apiContext, schema),
}, existing) == nil {
return httperror.NewAPIError(httperror.NotFound, "failed to find "+id)
Expand Down
8 changes: 8 additions & 0 deletions types/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func Capitalize(s string) string {
return strings.ToUpper(s[:1]) + s[1:]
}

func Uncapitalize(s string) string {
if len(s) <= 1 {
return strings.ToLower(s)
}

return strings.ToLower(s[:1]) + s[1:]
}

func LowerTitle(input string) string {
runes := []rune(input)
for i := 0; i < len(runes); i++ {
Expand Down
2 changes: 1 addition & 1 deletion types/mapper/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewObject(mappers ...types.Mapper) Object {
Mappers: append([]types.Mapper{
&Embed{Field: "metadata"},
&Embed{Field: "spec", Optional: true},
&ReadOnly{Field: "status", Optional: true},
&ReadOnly{Field: "status", Optional: true, SubFields: true},
Drop{"kind"},
Drop{"apiVersion"},
&Scope{
Expand Down
31 changes: 23 additions & 8 deletions types/mapper/read_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type ReadOnly struct {
Field string
Optional bool
Field string
Optional bool
SubFields bool
}

func (r ReadOnly) FromInternal(data map[string]interface{}) {
Expand All @@ -15,12 +16,28 @@ func (r ReadOnly) FromInternal(data map[string]interface{}) {
func (r ReadOnly) ToInternal(data map[string]interface{}) {
}

func (r ReadOnly) readOnly(field types.Field, schema *types.Schema, schemas *types.Schemas) types.Field {
field.Create = false
field.Update = false

if r.SubFields {
subSchema := schemas.Schema(&schema.Version, field.Type)
if subSchema != nil {
for name, field := range subSchema.ResourceFields {
field.Create = false
field.Update = false
subSchema.ResourceFields[name] = field
}
}
}

return field
}

func (r ReadOnly) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
if r.Field == "*" {
for name, field := range schema.ResourceFields {
field.Create = false
field.Update = false
schema.ResourceFields[name] = field
schema.ResourceFields[name] = r.readOnly(field, schema, schemas)
}
return nil
}
Expand All @@ -33,9 +50,7 @@ func (r ReadOnly) ModifySchema(schema *types.Schema, schemas *types.Schemas) err
}

field := schema.ResourceFields[r.Field]
field.Create = false
field.Update = false
schema.ResourceFields[r.Field] = field
schema.ResourceFields[r.Field] = r.readOnly(field, schema, schemas)

return nil
}
4 changes: 4 additions & 0 deletions types/schema_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (s *Schema) CanList() bool {
return slice.ContainsString(s.CollectionMethods, http.MethodGet)
}

func (s *Schema) CanCreate() bool {
return slice.ContainsString(s.CollectionMethods, http.MethodPost)
}

func (s *Schema) CanUpdate() bool {
return slice.ContainsString(s.ResourceMethods, http.MethodPut)
}
Expand Down
12 changes: 6 additions & 6 deletions types/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ActionHandler func(actionName string, action *Action, request *APIContext)

type RequestHandler func(request *APIContext) error

type QueryFilter func(opts QueryOptions, data []map[string]interface{}) []map[string]interface{}
type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}

type Validator func(request *APIContext, data map[string]interface{}) error

Expand Down Expand Up @@ -100,11 +100,11 @@ func (r *APIContext) WriteResponse(code int, obj interface{}) {
r.ResponseWriter.Write(r, code, obj)
}

func (r *APIContext) FilterList(opts QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
func (r *APIContext) FilterList(opts *QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
return r.QueryFilter(opts, obj)
}

func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{}) map[string]interface{} {
func (r *APIContext) FilterObject(opts *QueryOptions, obj map[string]interface{}) map[string]interface{} {
opts.Pagination = nil
result := r.QueryFilter(opts, []map[string]interface{}{obj})
if len(result) == 0 {
Expand All @@ -113,7 +113,7 @@ func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{})
return result[0]
}

func (r *APIContext) Filter(opts QueryOptions, obj interface{}) interface{} {
func (r *APIContext) Filter(opts *QueryOptions, obj interface{}) interface{} {
switch v := obj.(type) {
case []map[string]interface{}:
return r.FilterList(opts, v)
Expand Down Expand Up @@ -161,9 +161,9 @@ type URLBuilder interface {

type Store interface {
ByID(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt QueryOptions) ([]map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt *QueryOptions) ([]map[string]interface{}, error)
Create(apiContext *APIContext, schema *Schema, data map[string]interface{}) (map[string]interface{}, error)
Update(apiContext *APIContext, schema *Schema, data map[string]interface{}, id string) (map[string]interface{}, error)
Delete(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
Watch(apiContext *APIContext, schema *Schema, opt QueryOptions) (chan map[string]interface{}, error)
Watch(apiContext *APIContext, schema *Schema, opt *QueryOptions) (chan map[string]interface{}, error)
}