-
Notifications
You must be signed in to change notification settings - Fork 0
/
registries.go
87 lines (75 loc) · 2.61 KB
/
registries.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storage
import (
"sort"
"github.com/juju/errors"
)
// ChainedProviderRegistry is storage provider registry that combines
// multiple storage provider registries, chaining their results. Registries
// earlier in the chain take precedence.
type ChainedProviderRegistry []ProviderRegistry
// StorageProviderTypes implements ProviderRegistry.
func (r ChainedProviderRegistry) StorageProviderTypes() ([]ProviderType, error) {
var result []ProviderType
for _, r := range r {
types, err := r.StorageProviderTypes()
if err != nil {
return nil, errors.Trace(err)
}
result = append(result, types...)
}
return result, nil
}
// StorageProvider implements ProviderRegistry.
func (r ChainedProviderRegistry) StorageProvider(t ProviderType) (Provider, error) {
for _, r := range r {
p, err := r.StorageProvider(t)
if err == nil {
return p, nil
}
if errors.Is(err, errors.NotFound) {
continue
}
return nil, errors.Annotatef(err, "getting storage provider %q", t)
}
return nil, errors.NotFoundf("storage provider %q", t)
}
// StaticProviderRegistry is a storage provider registry with a statically
// defined set of providers.
type StaticProviderRegistry struct {
// Providers contains the storage providers for this registry.
Providers map[ProviderType]Provider
}
// StorageProviderTypes implements ProviderRegistry.
func (r StaticProviderRegistry) StorageProviderTypes() ([]ProviderType, error) {
typeStrings := make([]string, 0, len(r.Providers))
for t := range r.Providers {
typeStrings = append(typeStrings, string(t))
}
sort.Strings(typeStrings)
types := make([]ProviderType, len(typeStrings))
for i, s := range typeStrings {
types[i] = ProviderType(s)
}
return types, nil
}
// StorageProvider implements ProviderRegistry.
func (r StaticProviderRegistry) StorageProvider(t ProviderType) (Provider, error) {
p, ok := r.Providers[t]
if ok {
return p, nil
}
return nil, errors.NotFoundf("storage provider %q", t)
}
// NotImplementedProviderRegistry is a storage provider registry that
// returns an error satisfying [errors.NotImplemented] for any method call.
type NotImplementedProviderRegistry struct{}
// StorageProviderTypes implements ProviderRegistry.
func (r NotImplementedProviderRegistry) StorageProviderTypes() ([]ProviderType, error) {
return nil, errors.NotImplementedf(`"StorageProviderTypes"`)
}
// StorageProvider implements ProviderRegistry.
func (r NotImplementedProviderRegistry) StorageProvider(t ProviderType) (Provider, error) {
return nil, errors.NotImplementedf(`"StorageProvider"`)
}