Implement context list

This commit is contained in:
Djordje Lukic 2020-04-26 22:07:50 +02:00
parent 3bb4fe163c
commit e2c7370a82
6 changed files with 125 additions and 42 deletions

View file

@ -1,32 +0,0 @@
/*
Copyright (c) 2020 Docker Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package context
type TypeContext struct {
Type string
}

View file

@ -56,9 +56,16 @@ func ContextStore(ctx context.Context) Store {
return s
}
// Store
type Store interface {
// Get returns the context with with name, it returns an error if the
// context doesn't exist
Get(name string) (*Metadata, error)
// Create creates a new context, it returns an error if a context with the
// same name exists already.
Create(name string, data interface{}, endpoints map[string]interface{}) error
// List returns the list of created contexts
List() ([]*Metadata, error)
}
type store struct {
@ -92,23 +99,33 @@ func (s *store) Get(name string) (*Metadata, error) {
}
meta := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name), metaFile)
return read(meta)
}
func read(meta string) (*Metadata, error) {
bytes, err := ioutil.ReadFile(meta)
if err != nil {
return nil, err
}
r := &Metadata{
Endpoints: make(map[string]interface{}),
var r untypedContextMetadata
if err := json.Unmarshal(bytes, &r); err != nil {
return nil, err
}
result := &Metadata{
Name: r.Name,
Endpoints: r.Endpoints,
}
typed := getter()
if err := json.Unmarshal(bytes, typed); err != nil {
return r, err
if err := json.Unmarshal(r.Metadata, typed); err != nil {
return nil, err
}
r.Metadata = reflect.ValueOf(typed).Elem().Interface()
result.Metadata = reflect.ValueOf(typed).Elem().Interface()
return r, nil
return result, nil
}
func (s *store) Create(name string, data interface{}, endpoints map[string]interface{}) error {
@ -137,6 +154,28 @@ func (s *store) Create(name string, data interface{}, endpoints map[string]inter
return ioutil.WriteFile(filepath.Join(metaDir, metaFile), bytes, 0644)
}
func (s *store) List() ([]*Metadata, error) {
root := filepath.Join(s.root, contextsDir, metadataDir)
c, err := ioutil.ReadDir(root)
if err != nil {
return nil, err
}
var result []*Metadata
for _, fi := range c {
if fi.IsDir() {
meta := filepath.Join(root, fi.Name(), metaFile)
r, err := read(meta)
if err != nil {
return nil, err
}
result = append(result, r)
}
}
return result, nil
}
func contextdirOf(name string) string {
return digest.FromString(name).Encoded()
}
@ -147,9 +186,15 @@ type Metadata struct {
Endpoints map[string]interface{} `json:",omitempty"`
}
type untypedContextMetadata struct {
Metadata json.RawMessage `json:"metadata,omitempty"`
Endpoints map[string]interface{} `json:"endpoints,omitempty"`
Name string `json:"name,omitempty"`
}
type TypeContext struct {
Type string
Description string
Type string `json:",omitempty"`
Description string `json:",omitempty"`
}
func getter() interface{} {

View file

@ -29,6 +29,7 @@ package store
import (
_ "crypto/sha256"
"fmt"
"io/ioutil"
"os"
"testing"
@ -64,10 +65,38 @@ func TestCreate(t *testing.T) {
func TestGet(t *testing.T) {
setup(t, func(t *testing.T, store Store) {
err := store.Create("test", nil, nil)
err := store.Create("test", TypeContext{
Type: "type",
Description: "description",
}, nil)
assert.Nil(t, err)
meta, err := store.Get("test")
assert.Nil(t, err)
assert.NotNil(t, meta)
assert.Equal(t, "test", meta.Name)
m, ok := meta.Metadata.(TypeContext)
assert.Equal(t, ok, true)
fmt.Printf("%#v\n", meta)
assert.Equal(t, "description", m.Description)
assert.Equal(t, "type", m.Type)
})
}
func TestList(t *testing.T) {
setup(t, func(t *testing.T, store Store) {
err := store.Create("test1", TypeContext{}, nil)
assert.Nil(t, err)
err = store.Create("test2", TypeContext{}, nil)
assert.Nil(t, err)
contexts, err := store.List()
assert.Nil(t, err)
assert.Equal(t, len(contexts), 2)
assert.Equal(t, contexts[0].Name, "test1")
assert.Equal(t, contexts[1].Name, "test2")
})
}