aboutsummaryrefslogtreecommitdiff
path: root/storage/registry/registry.go
blob: 4b48ef4fa2df003f2e5cd5e550951c9686b087cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package registry

import (
	"code.laria.me/petrific/config"
	"code.laria.me/petrific/storage"
	"code.laria.me/petrific/storage/cloud"
	"code.laria.me/petrific/storage/local"
	"code.laria.me/petrific/storage/memory"
	"errors"
	"fmt"
)

// List af all available storage types
func getStorageTypes() map[string]storage.CreateStorageFromConfig {
	return map[string]storage.CreateStorageFromConfig{
		"local":           local.LocalStorageFromConfig,
		"memory":          memory.MemoryStorageFromConfig,
		"filter":          filterStorageFromConfig,
		"openstack-swift": cloud.SwiftStorageCreator(),
	}
}

var notFoundErr = errors.New("Storage not found")

type unknownMethodErr string

func (method unknownMethodErr) Error() string {
	return fmt.Sprintf("Method %s unknown", string(method))
}

type storageConfErr struct {
	name string
	err  error
}

func (e storageConfErr) Error() string {
	return fmt.Sprintf("Failed setting up storage %s: %s", e.name, e.err.Error())
}

func loadStorage(conf config.Config, storageName string) (storage.Storage, error) {
	method, err := conf.GetStorageMethod(storageName)

	if err != nil {
		return nil, err
	}

	st, ok := getStorageTypes()[method]
	if !ok {
		return nil, unknownMethodErr(method)
	}

	s, err := st(conf, storageName)
	if err != nil {
		return nil, err
	}

	return s, nil
}

func LoadStorage(conf config.Config, storageName string) (storage.Storage, error) {
	s, err := loadStorage(conf, storageName)
	if err != nil {
		return nil, storageConfErr{storageName, err}
	}
	return s, nil
}