Add json storage for container storage
This is just a temporary storage solution to get containers running on the new code. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
@@ -2,6 +2,7 @@ package containers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/pkg/errors"
|
||||
@@ -37,21 +38,109 @@ func NewStore(tx *bolt.Tx) Store {
|
||||
}
|
||||
|
||||
func (s *storage) Get(ctx context.Context, id string) (Container, error) {
|
||||
panic("not implemented")
|
||||
bkt := s.bucket()
|
||||
if bkt == nil {
|
||||
return Container{}, errors.Wrap(ErrNotFound, "bucket does not exist")
|
||||
}
|
||||
|
||||
cb := bkt.Get([]byte(id))
|
||||
if len(cb) == 0 {
|
||||
return Container{}, errors.Wrap(ErrNotFound, "no content for id")
|
||||
}
|
||||
|
||||
var container Container
|
||||
if err := json.Unmarshal(cb, &container); err != nil {
|
||||
return Container{}, errors.Wrap(err, "failed to unmarshal container")
|
||||
}
|
||||
|
||||
return container, nil
|
||||
}
|
||||
|
||||
func (s *storage) List(ctx context.Context, filter string) ([]Container, error) {
|
||||
panic("not implemented")
|
||||
containers := []Container{}
|
||||
bkt := s.bucket()
|
||||
if bkt == nil {
|
||||
return containers, nil
|
||||
}
|
||||
err := bkt.ForEach(func(k, v []byte) error {
|
||||
container := Container{ID: string(k)}
|
||||
if err := json.Unmarshal(v, &container); err != nil {
|
||||
return errors.Wrap(err, "failed to unmarshal container")
|
||||
}
|
||||
containers = append(containers, container)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return containers, nil
|
||||
}
|
||||
|
||||
func (s *storage) Create(ctx context.Context, container Container) (Container, error) {
|
||||
panic("not implemented")
|
||||
bkt, err := s.createBucket()
|
||||
if err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
b := bkt.Get([]byte(container.ID))
|
||||
if len(b) > 0 {
|
||||
return Container{}, errors.Wrap(ErrExists, "content for id already exists")
|
||||
}
|
||||
|
||||
b, err = json.Marshal(container)
|
||||
if err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
return container, bkt.Put([]byte(container.ID), b)
|
||||
}
|
||||
|
||||
func (s *storage) Update(ctx context.Context, container Container) (Container, error) {
|
||||
panic("not implemented")
|
||||
bkt, err := s.createBucket()
|
||||
if err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
b := bkt.Get([]byte(container.ID))
|
||||
if len(b) == 0 {
|
||||
return Container{}, errors.Wrap(ErrNotFound, "no content for id")
|
||||
}
|
||||
|
||||
b, err = json.Marshal(container)
|
||||
if err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
return container, bkt.Put([]byte(container.ID), b)
|
||||
}
|
||||
|
||||
func (s *storage) Delete(ctx context.Context, id string) error {
|
||||
panic("not implemented")
|
||||
bkt, err := s.createBucket()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b := bkt.Get([]byte(id))
|
||||
if len(b) == 0 {
|
||||
return errors.Wrap(ErrNotFound, "no content for id")
|
||||
}
|
||||
|
||||
return bkt.Delete([]byte(id))
|
||||
}
|
||||
|
||||
func (s *storage) bucket() *bolt.Bucket {
|
||||
bkt := s.tx.Bucket(bucketKeyStorageVersion)
|
||||
if bkt == nil {
|
||||
return nil
|
||||
}
|
||||
return bkt.Bucket(bucketKeyContainers)
|
||||
}
|
||||
|
||||
func (s *storage) createBucket() (*bolt.Bucket, error) {
|
||||
bkt, err := s.tx.CreateBucketIfNotExists(bucketKeyStorageVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bkt.CreateBucketIfNotExists(bucketKeyContainers)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user