-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
repo_options.go
212 lines (170 loc) · 5.9 KB
/
repo_options.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package udmrepo
import (
"os"
"path/filepath"
"strings"
)
const (
StorageTypeS3 = "s3"
StorageTypeAzure = "azure"
StorageTypeFs = "filesystem"
StorageTypeGcs = "gcs"
GenOptionMaintainMode = "mode"
GenOptionMaintainFull = "full"
GenOptionMaintainQuick = "quick"
GenOptionOwnerName = "username"
GenOptionOwnerDomain = "domainname"
StoreOptionS3KeyID = "accessKeyID"
StoreOptionS3Provider = "providerName"
StoreOptionS3SecretKey = "secretAccessKey"
StoreOptionS3Token = "sessionToken"
StoreOptionS3Endpoint = "endpoint"
StoreOptionS3DisableTLS = "doNotUseTLS"
StoreOptionS3DisableTLSVerify = "skipTLSVerify"
StoreOptionS3CustomCA = "customCA"
StoreOptionAzureKey = "storageKey"
StoreOptionAzureDomain = "storageDomain"
StoreOptionAzureStorageAccount = "storageAccount"
StoreOptionAzureToken = "sasToken"
StoreOptionFsPath = "fspath"
StoreOptionGcsReadonly = "readonly"
StoreOptionOssBucket = "bucket"
StoreOptionOssRegion = "region"
StoreOptionCredentialFile = "credFile"
StoreOptionPrefix = "prefix"
StoreOptionPrefixName = "unified-repo"
StoreOptionGenHashAlgo = "hashAlgo"
StoreOptionGenEncryptAlgo = "encryptAlgo"
StoreOptionGenSplitAlgo = "splitAlgo"
StoreOptionGenRetentionMode = "retentionMode"
StoreOptionGenRetentionPeriod = "retentionPeriod"
StoreOptionGenReadOnly = "readOnly"
ThrottleOptionReadOps = "readOPS"
ThrottleOptionWriteOps = "writeOPS"
ThrottleOptionListOps = "listOPS"
ThrottleOptionUploadBytes = "uploadBytes"
ThrottleOptionDownloadBytes = "downloadBytes"
)
const (
defaultUsername = "default"
defaultDomain = "default"
)
type RepoOptions struct {
// StorageType is a repository specific string to identify a backup storage, i.e., "s3", "filesystem"
StorageType string
// RepoPassword is the backup repository's password, if any
RepoPassword string
// ConfigFilePath is a custom path to save the repository's configuration, if any
ConfigFilePath string
// GeneralOptions takes other repository specific options
GeneralOptions map[string]string
// StorageOptions takes storage specific options
StorageOptions map[string]string
// Description is a description of the backup repository/backup repository operation.
// It is for logging/debugging purpose only and doesn't control any behavior of the backup repository.
Description string
}
// PasswordGetter defines the method to get a repository password.
type PasswordGetter interface {
GetPassword(param interface{}) (string, error)
}
// StoreOptionsGetter defines the methods to get the storage related options.
type StoreOptionsGetter interface {
GetStoreType(param interface{}) (string, error)
GetStoreOptions(param interface{}) (map[string]string, error)
}
// NewRepoOptions creates a new RepoOptions for different purpose
func NewRepoOptions(optionFuncs ...func(*RepoOptions) error) (*RepoOptions, error) {
options := &RepoOptions{
GeneralOptions: make(map[string]string),
StorageOptions: make(map[string]string),
}
for _, optionFunc := range optionFuncs {
err := optionFunc(options)
if err != nil {
return nil, err
}
}
return options, nil
}
// WithPassword sets the RepoPassword to RepoOptions, the password is acquired through
// the provided interface
func WithPassword(getter PasswordGetter, param interface{}) func(*RepoOptions) error {
return func(options *RepoOptions) error {
password, err := getter.GetPassword(param)
if err != nil {
return err
}
options.RepoPassword = password
return nil
}
}
// WithConfigFile sets the ConfigFilePath to RepoOptions
func WithConfigFile(workPath string, repoID string) func(*RepoOptions) error {
return func(options *RepoOptions) error {
options.ConfigFilePath = getRepoConfigFile(workPath, repoID)
return nil
}
}
// WithGenOptions sets the GeneralOptions to RepoOptions
func WithGenOptions(genOptions map[string]string) func(*RepoOptions) error {
return func(options *RepoOptions) error {
for k, v := range genOptions {
options.GeneralOptions[k] = v
}
return nil
}
}
// WithStoreOptions sets the StorageOptions to RepoOptions, the store options are acquired through
// the provided interface
func WithStoreOptions(getter StoreOptionsGetter, param interface{}) func(*RepoOptions) error {
return func(options *RepoOptions) error {
storeType, err := getter.GetStoreType(param)
if err != nil {
return err
}
storeOptions, err := getter.GetStoreOptions(param)
if err != nil {
return err
}
options.StorageType = storeType
for k, v := range storeOptions {
options.StorageOptions[k] = v
}
return nil
}
}
// WithDescription sets the Description to RepoOptions
func WithDescription(desc string) func(*RepoOptions) error {
return func(options *RepoOptions) error {
options.Description = desc
return nil
}
}
// GetRepoUser returns the default username that is used to manipulate the Unified Repo
func GetRepoUser() string {
return defaultUsername
}
// GetRepoDomain returns the default user domain that is used to manipulate the Unified Repo
func GetRepoDomain() string {
return defaultDomain
}
func getRepoConfigFile(workPath string, repoID string) string {
if workPath == "" {
workPath = filepath.Join(os.Getenv("HOME"), "udmrepo")
}
name := "repo-" + strings.ToLower(repoID) + ".conf"
return filepath.Join(workPath, name)
}