Files
fission-src/controller/resourceStore.go
T
Soam Vasani 94899a88a7 Some sort of generic etcd-backed store
- A generic Resource interface, which is serializable and has
  a key() method to get an identifying key.

- A resourceStore, which has create, read, update, delete and list.
  This wraps etcd key operations and serialization/deserialization in
  a convenient service.  Serialization is provided to the
  resourceStore as a 'serialization' interface.  This can be JSON or
  anything else.

- The resourceStore also has file operations -- this has read, write
  and delete operations.  The general idea is to store metadata in
  etcd and data on the fileStore (which could also be an object store,
  we don't really use any file-specific properties).  Files are also
  versioned -- each writeFile writes to a new UUID and that UUID is
  stored in an etcd in-order list.  readFile can retrieve an arbitrary
  version by UUID, or just get the latest contents.
2016-09-10 01:01:27 -07:00

192 lines
4.3 KiB
Go

/*
Copyright 2016 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"errors"
"github.com/coreos/etcd/client"
"github.com/satori/go.uuid"
"golang.org/x/net/context"
"reflect"
)
type (
resource interface {
key() string
}
serializer interface {
serialize(r resource) ([]byte, error)
deserialize(buf []byte, r resource) error
}
resourceStore struct {
*fileStore
client.KeysAPI
serializer
}
)
func makeResourceStore(fs *fileStore, ks client.KeysAPI, s serializer) *resourceStore {
return &resourceStore{fileStore: fs, KeysAPI: ks, serializer: s}
}
func getTypeName(r resource) (string, error) {
typ := reflect.TypeOf(r)
if typ.Kind().String() == "ptr" {
typ = typ.Elem()
}
typName := typ.Name()
if len(typName) == 0 {
return "", errors.New("Failed to get type")
}
return typName, nil
}
func getKey(r resource) (string, error) {
typName, err := getTypeName(r)
if err != nil {
return "", err
}
rkey := r.key()
return (typName + "/" + rkey), nil
}
func (rs *resourceStore) create(r resource) error {
key, err := getKey(r)
if err != nil {
return err
}
serialized, err := rs.serializer.serialize(r)
if err != nil {
return err
}
_, err = rs.KeysAPI.Set(context.Background(), key, string(serialized),
&client.SetOptions{PrevExist: client.PrevNoExist})
return err
}
func (rs *resourceStore) read(rkey string, res resource) error {
typName, err := getTypeName(res)
if err != nil {
return err
}
key := typName + "/" + rkey
resp, err := rs.KeysAPI.Get(context.Background(), key, nil)
if err != nil {
return err
}
return rs.serializer.deserialize([]byte(resp.Node.Value), res)
}
func (rs *resourceStore) update(r resource) error {
key, err := getKey(r)
if err != nil {
return err
}
serialized, err := rs.serializer.serialize(r)
if err != nil {
return err
}
_, err = rs.KeysAPI.Set(context.Background(), key, string(serialized),
&client.SetOptions{PrevExist: client.PrevExist})
return err
}
func (rs *resourceStore) delete(typename, rkey string) error {
key := typename + "/" + rkey
_, err := rs.KeysAPI.Delete(context.Background(), key, nil) // ignore response
return err
}
func (rs *resourceStore) getAll(key string) ([]string, error) {
resp, err := rs.KeysAPI.Get(context.Background(), key, &client.GetOptions{Recursive: true})
if err != nil {
return nil, err
}
res := make([]string, 0, len(resp.Node.Nodes))
for _, n := range resp.Node.Nodes {
res = append(res, n.Value)
}
return res, nil
}
func (rs *resourceStore) writeFile(parentKey string, contents []byte) (string, string, error) {
uid := uuid.NewV4().String()
err := rs.fileStore.write(uid, contents)
if err != nil {
return "", "", err
}
resp, err := rs.KeysAPI.CreateInOrder(context.Background(), parentKey, uid, nil)
if err != nil {
_ = rs.fileStore.delete(uid)
return "", "", err
}
return resp.Node.Key, uid, nil
}
func (rs *resourceStore) readFile(key string, uid *string) ([]byte, error) {
resp, err := rs.KeysAPI.Get(context.Background(), key, &client.GetOptions{Sort: true})
if err != nil {
return nil, err
}
if uid == nil {
// get latest
n := resp.Node.Nodes
uid = &n[len(n)-1].Value
} else {
// validate uid is in the list
found := false
for _, u := range resp.Node.Nodes {
if *uid == u.Value {
found = true
break
}
}
if !found {
return nil, errors.New("Invalid UUID")
}
}
contents, err := rs.fileStore.read(*uid)
return contents, err
}
func (rs *resourceStore) deleteFile(key string, uid string) error {
resp, err := rs.KeysAPI.Get(context.Background(), key, &client.GetOptions{Sort: true})
if err != nil {
return err
}
for _, u := range resp.Node.Nodes {
if u.Value == uid {
err = rs.fileStore.delete(u.Value)
return err
}
}
return errors.New("won't delete unreferenced file")
}