mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Vast commit, includes: * Introduces the Trigger domain entity. * Introduces the Fns domain entity. * V2 of the API for interacting with the new entities in swaggerv2.yml * Adds v2 end points for Apps to support PUT updates. * Rewrites the datastore level tests into a new pattern. * V2 routes use entity ID over name as the path parameter.
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package datastore
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
|
|
"fmt"
|
|
|
|
"github.com/fnproject/fn/api/common"
|
|
"github.com/fnproject/fn/api/datastore/internal/datastoreutil"
|
|
"github.com/fnproject/fn/api/models"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// New creates a DataStore from the specified URL
|
|
func New(ctx context.Context, dbURL string) (models.Datastore, error) {
|
|
log := common.Logger(ctx)
|
|
u, err := url.Parse(dbURL)
|
|
if err != nil {
|
|
log.WithError(err).WithFields(logrus.Fields{"url": dbURL}).Fatal("bad DB URL")
|
|
}
|
|
log.WithFields(logrus.Fields{"db": u.Scheme}).Debug("creating new datastore")
|
|
|
|
for _, provider := range providers {
|
|
if provider.Supports(u) {
|
|
return provider.New(ctx, u)
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("no data store provider found for storage url %s", u)
|
|
}
|
|
|
|
func Wrap(ds models.Datastore) models.Datastore {
|
|
return datastoreutil.MetricDS(datastoreutil.NewValidator(ds))
|
|
}
|
|
|
|
// Provider is a datastore provider
|
|
type Provider interface {
|
|
fmt.Stringer
|
|
// Supports indicates if this provider can handle a given data store.
|
|
Supports(url *url.URL) bool
|
|
// New creates a new data store from the specified URL
|
|
New(ctx context.Context, url *url.URL) (models.Datastore, error)
|
|
}
|
|
|
|
var providers []Provider
|
|
|
|
// AddProvider globally registers a data store provider
|
|
func AddProvider(provider Provider) {
|
|
logrus.Infof("Adding DataStore provider %s", provider)
|
|
providers = append(providers, provider)
|
|
}
|