-
Notifications
You must be signed in to change notification settings - Fork 361
/
namespace.go
188 lines (165 loc) · 4.73 KB
/
namespace.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package block
import (
"errors"
"fmt"
"net/url"
"strings"
)
type StorageType int
const (
StorageTypeMem = iota
StorageTypeLocal
StorageTypeS3
StorageTypeGS
StorageTypeAzure
)
var ErrInvalidNamespace = errors.New("invalid namespace")
func (s StorageType) BlockstoreType() string {
switch s {
case StorageTypeAzure:
return "azure"
default:
return s.Scheme()
}
}
func (s StorageType) Scheme() string {
scheme := ""
switch s {
case StorageTypeMem:
scheme = "mem"
case StorageTypeLocal:
scheme = "local"
case StorageTypeGS:
scheme = "gs"
case StorageTypeS3:
scheme = "s3"
case StorageTypeAzure:
scheme = "https"
default:
panic("unknown storage type")
}
return scheme
}
type StorageNamespaceInfo struct {
ValidityRegex string // regex pattern that could be used to validate the namespace
Example string // example of a valid namespace
DefaultNamespacePrefix string // when a repo is created from the UI, suggest a default storage namespace under this prefix
PreSignSupport bool
ImportSupport bool
}
type QualifiedKey struct {
StorageType StorageType
StorageNamespace string
Key string
}
type QualifiedPrefix struct {
StorageType StorageType
StorageNamespace string
Prefix string
}
func (qk QualifiedKey) Format() string {
return qk.StorageType.Scheme() + "://" + formatPathWithNamespace(qk.StorageNamespace, qk.Key)
}
func GetStorageType(namespaceURL *url.URL) (StorageType, error) {
var st StorageType
switch namespaceURL.Scheme {
case "s3":
return StorageTypeS3, nil
case "mem", "memory":
return StorageTypeMem, nil
case "local":
return StorageTypeLocal, nil
case "gs":
return StorageTypeGS, nil
case "http", "https":
return StorageTypeAzure, nil
default:
return st, fmt.Errorf("%s: %w", namespaceURL.Scheme, ErrInvalidNamespace)
}
}
func formatPathWithNamespace(namespacePath, keyPath string) string {
namespacePath = strings.Trim(namespacePath, "/")
if len(namespacePath) == 0 {
return strings.TrimPrefix(keyPath, "/")
}
return namespacePath + "/" + keyPath
}
func ResolveNamespacePrefix(defaultNamespace, prefix string) (QualifiedPrefix, error) {
// behaviour for key and prefix is the same
key, err := resolveRelative(defaultNamespace, prefix)
if err != nil {
return QualifiedPrefix{}, fmt.Errorf("resolving namespace: %w", err)
}
return QualifiedPrefix{
StorageType: key.StorageType,
StorageNamespace: key.StorageNamespace,
Prefix: key.Key,
}, nil
}
func ResolveNamespace(defaultNamespace, key string, identifierType IdentifierType) (QualifiedKey, error) {
switch identifierType {
case IdentifierTypeUnknownDeprecated:
return resolveNamespaceUnknown(defaultNamespace, key)
case IdentifierTypeRelative:
return resolveRelative(defaultNamespace, key)
case IdentifierTypeFull:
return resolveFull(key)
default:
panic(fmt.Sprintf("unknown identifier type: %d", identifierType))
}
}
func resolveFull(key string) (QualifiedKey, error) {
parsedKey, err := url.ParseRequestURI(key)
if err != nil {
return QualifiedKey{}, fmt.Errorf("could not parse URI: %w", err)
}
// extract its scheme
storageType, err := GetStorageType(parsedKey)
if err != nil {
return QualifiedKey{}, err
}
return QualifiedKey{
StorageType: storageType,
StorageNamespace: parsedKey.Host,
Key: formatPathWithNamespace("", parsedKey.Path),
}, nil
}
func resolveRelative(defaultNamespace, key string) (QualifiedKey, error) {
// is not fully qualified, treat as key only
// if we don't have a trailing slash for the namespace, add it.
parsedNs, err := url.ParseRequestURI(defaultNamespace)
if err != nil {
return QualifiedKey{}, fmt.Errorf("default namespace %s: %w", defaultNamespace, ErrInvalidNamespace)
}
storageType, err := GetStorageType(parsedNs)
if err != nil {
return QualifiedKey{}, fmt.Errorf("no storage type for %s: %w", parsedNs, err)
}
return QualifiedKey{
StorageType: storageType,
StorageNamespace: parsedNs.Host,
Key: formatPathWithNamespace(parsedNs.Path, key),
}, nil
}
func resolveNamespaceUnknown(defaultNamespace, key string) (QualifiedKey, error) {
// first try to treat key as a full path
if qk, err := resolveFull(key); err == nil {
return qk, nil
}
// else, treat it as a relative path
return resolveRelative(defaultNamespace, key)
}
func DefaultExample(scheme string) string {
return scheme + "://example-bucket/"
}
func DefaultValidationRegex(scheme string) string {
return fmt.Sprintf("^%s://", scheme)
}
func DefaultStorageNamespaceInfo(scheme string) StorageNamespaceInfo {
return StorageNamespaceInfo{
ValidityRegex: DefaultValidationRegex(scheme),
Example: DefaultExample(scheme),
PreSignSupport: true,
ImportSupport: true,
}
}