mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Datastore validator (#565)
* add datastore validator; adapt mock and tests * adapt bolt datastore to common validator * adapt postgres datastore to validator * adapt redis datastore to common validator
This commit is contained in:
committed by
Travis Reeder
parent
5803886a06
commit
06171800e2
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/datastore/internal/datastoreutil"
|
||||
)
|
||||
|
||||
type RedisDataStore struct {
|
||||
@@ -43,7 +44,7 @@ func New(url *url.URL) (models.Datastore, error) {
|
||||
ds := &RedisDataStore{
|
||||
conn: conn,
|
||||
}
|
||||
return ds, nil
|
||||
return datastoreutil.NewValidator(ds), nil
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) setApp(app *models.App) (*models.App, error) {
|
||||
@@ -59,13 +60,6 @@ func (ds *RedisDataStore) setApp(app *models.App) (*models.App, error) {
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) InsertApp(ctx context.Context, app *models.App) (*models.App, error) {
|
||||
if app == nil {
|
||||
return nil, models.ErrDatastoreEmptyApp
|
||||
}
|
||||
if app.Name == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
reply, err := ds.conn.Do("HEXISTS", "apps", app.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -80,13 +74,6 @@ func (ds *RedisDataStore) InsertApp(ctx context.Context, app *models.App) (*mode
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) UpdateApp(ctx context.Context, newapp *models.App) (*models.App, error) {
|
||||
if newapp == nil {
|
||||
return nil, models.ErrDatastoreEmptyApp
|
||||
}
|
||||
if newapp.Name == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
app, err := ds.GetApp(ctx, newapp.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -98,10 +85,6 @@ func (ds *RedisDataStore) UpdateApp(ctx context.Context, newapp *models.App) (*m
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) RemoveApp(ctx context.Context, appName string) error {
|
||||
if appName == "" {
|
||||
return models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
if _, err := ds.conn.Do("HDEL", "apps", appName); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -110,10 +93,6 @@ func (ds *RedisDataStore) RemoveApp(ctx context.Context, appName string) error {
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) GetApp(ctx context.Context, name string) (*models.App, error) {
|
||||
if name == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
reply, err := ds.conn.Do("HGET", "apps", name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -168,18 +147,6 @@ func (ds *RedisDataStore) setRoute(set string, route *models.Route) (*models.Rou
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) InsertRoute(ctx context.Context, route *models.Route) (*models.Route, error) {
|
||||
if route == nil {
|
||||
return nil, models.ErrDatastoreEmptyRoute
|
||||
}
|
||||
|
||||
if route.AppName == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
if route.Path == "" {
|
||||
return nil, models.ErrDatastoreEmptyRoutePath
|
||||
}
|
||||
|
||||
reply, err := ds.conn.Do("HEXISTS", "apps", route.AppName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -207,18 +174,6 @@ func (ds *RedisDataStore) InsertRoute(ctx context.Context, route *models.Route)
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) UpdateRoute(ctx context.Context, newroute *models.Route) (*models.Route, error) {
|
||||
if newroute == nil {
|
||||
return nil, models.ErrDatastoreEmptyRoute
|
||||
}
|
||||
|
||||
if newroute.AppName == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
if newroute.Path == "" {
|
||||
return nil, models.ErrDatastoreEmptyRoutePath
|
||||
}
|
||||
|
||||
route, err := ds.GetRoute(ctx, newroute.AppName, newroute.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -233,14 +188,6 @@ func (ds *RedisDataStore) UpdateRoute(ctx context.Context, newroute *models.Rout
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) RemoveRoute(ctx context.Context, appName, routePath string) error {
|
||||
if appName == "" {
|
||||
return models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
if routePath == "" {
|
||||
return models.ErrDatastoreEmptyRoutePath
|
||||
}
|
||||
|
||||
hset := fmt.Sprintf("routes:%s", appName)
|
||||
if n, err := ds.conn.Do("HDEL", hset, routePath); err != nil {
|
||||
return err
|
||||
@@ -252,14 +199,6 @@ func (ds *RedisDataStore) RemoveRoute(ctx context.Context, appName, routePath st
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {
|
||||
if appName == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
|
||||
if routePath == "" {
|
||||
return nil, models.ErrDatastoreEmptyRoutePath
|
||||
}
|
||||
|
||||
hset := fmt.Sprintf("routes:%s", appName)
|
||||
reply, err := ds.conn.Do("HGET", hset, routePath)
|
||||
if err != nil {
|
||||
@@ -312,9 +251,6 @@ func (ds *RedisDataStore) GetRoutes(ctx context.Context, filter *models.RouteFil
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) GetRoutesByApp(ctx context.Context, appName string, filter *models.RouteFilter) ([]*models.Route, error) {
|
||||
if appName == "" {
|
||||
return nil, models.ErrDatastoreEmptyAppName
|
||||
}
|
||||
if filter == nil {
|
||||
filter = new(models.RouteFilter)
|
||||
}
|
||||
@@ -344,10 +280,6 @@ func (ds *RedisDataStore) GetRoutesByApp(ctx context.Context, appName string, fi
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) Put(ctx context.Context, key, value []byte) error {
|
||||
if key == nil || len(key) == 0 {
|
||||
return models.ErrDatastoreEmptyKey
|
||||
}
|
||||
|
||||
if _, err := ds.conn.Do("HSET", "extras", key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -356,10 +288,6 @@ func (ds *RedisDataStore) Put(ctx context.Context, key, value []byte) error {
|
||||
}
|
||||
|
||||
func (ds *RedisDataStore) Get(ctx context.Context, key []byte) ([]byte, error) {
|
||||
if key == nil || len(key) == 0 {
|
||||
return nil, models.ErrDatastoreEmptyKey
|
||||
}
|
||||
|
||||
value, err := ds.conn.Do("HGET", "extras", key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user