2019-05-24 22:03:06 +00:00
|
|
|
package storage
|
2015-07-06 19:41:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-24 22:03:06 +00:00
|
|
|
"net/url"
|
2015-07-06 19:41:21 +00:00
|
|
|
|
2019-05-24 22:03:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-07-06 19:41:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage is an interface to have all storage systems compatible to each other
|
|
|
|
type Storage interface {
|
|
|
|
Put(dashboardID string, data []byte) error
|
|
|
|
Get(dashboardID string) ([]byte, error)
|
|
|
|
Delete(dashboardID string) error
|
|
|
|
Exists(dashboardID string) (bool, error)
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:08:27 +00:00
|
|
|
// DashboardNotFoundError signalizes the requested dashboard could not be found
|
|
|
|
type DashboardNotFoundError struct {
|
|
|
|
DashboardID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e DashboardNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("Dashboard with ID '%s' was not found.", e.DashboardID)
|
|
|
|
}
|
|
|
|
|
2015-07-06 19:41:21 +00:00
|
|
|
// GetStorage acts as a storage factory providing the storage named by input
|
|
|
|
// name parameter
|
2019-05-24 22:03:06 +00:00
|
|
|
func GetStorage(uri string) (Storage, error) {
|
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Invalid storage URI")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch u.Scheme {
|
2015-07-06 19:41:21 +00:00
|
|
|
case "s3":
|
2019-05-24 22:03:06 +00:00
|
|
|
return NewS3Storage(u), nil
|
2015-07-06 20:08:27 +00:00
|
|
|
case "file":
|
2019-05-24 22:03:06 +00:00
|
|
|
return NewFileStorage(u), nil
|
2015-07-06 19:41:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 22:03:06 +00:00
|
|
|
return nil, errors.Errorf("Storage %q not found", u.Scheme)
|
2015-07-06 19:41:21 +00:00
|
|
|
}
|