/
storageconfiguration.go
179 lines (147 loc) · 4.11 KB
/
storageconfiguration.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
package azure
import (
"fmt"
"github.com/tkennes/openghg/pkg/cloud/config"
"github.com/tkennes/openghg/pkg/util/json"
)
type StorageConfiguration struct {
SubscriptionID string `json:"subscriptionID"`
Account string `json:"account"`
Container string `json:"container"`
Path string `json:"path"`
Cloud string `json:"cloud"`
Authorizer Authorizer `json:"authorizer"`
}
// Check ensures that all required fields are set, and throws an error if they are not
func (sc *StorageConfiguration) Validate() error {
if sc.Authorizer == nil {
return fmt.Errorf("StorageConfiguration: missing authorizer")
}
err := sc.Authorizer.Validate()
if err != nil {
return err
}
if sc.SubscriptionID == "" {
return fmt.Errorf("StorageConfiguration: missing Subcription ID")
}
if sc.Account == "" {
return fmt.Errorf("StorageConfiguration: missing Account")
}
if sc.Container == "" {
return fmt.Errorf("StorageConfiguration: missing Container")
}
return nil
}
func (sc *StorageConfiguration) Equals(config config.Config) bool {
if config == nil {
return false
}
thatConfig, ok := config.(*StorageConfiguration)
if !ok {
return false
}
if sc.Authorizer != nil {
if !sc.Authorizer.Equals(thatConfig.Authorizer) {
return false
}
} else {
if thatConfig.Authorizer != nil {
return false
}
}
if sc.SubscriptionID != thatConfig.SubscriptionID {
return false
}
if sc.Account != thatConfig.Account {
return false
}
if sc.Container != thatConfig.Container {
return false
}
if sc.Path != thatConfig.Path {
return false
}
if sc.Cloud != thatConfig.Cloud {
return false
}
return true
}
func (sc *StorageConfiguration) Sanitize() config.Config {
return &StorageConfiguration{
SubscriptionID: sc.SubscriptionID,
Account: sc.Account,
Container: sc.Container,
Path: sc.Path,
Cloud: sc.Cloud,
Authorizer: sc.Authorizer.Sanitize().(Authorizer),
}
}
func (sc *StorageConfiguration) Key() string {
key := fmt.Sprintf("%s/%s", sc.SubscriptionID, sc.Container)
// append container path to key if it exists
if sc.Path != "" {
key = fmt.Sprintf("%s/%s", key, sc.Path)
}
return key
}
func (sc *StorageConfiguration) UnmarshalJSON(b []byte) error {
var f interface{}
err := json.Unmarshal(b, &f)
if err != nil {
return err
}
fmap := f.(map[string]interface{})
subscriptionID, err := config.GetInterfaceValue[string](fmap, "subscriptionID")
if err != nil {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
}
sc.SubscriptionID = subscriptionID
account, err := config.GetInterfaceValue[string](fmap, "account")
if err != nil {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
}
sc.Account = account
container, err := config.GetInterfaceValue[string](fmap, "container")
if err != nil {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
}
sc.Container = container
path, err := config.GetInterfaceValue[string](fmap, "path")
if err != nil {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
}
sc.Path = path
cloud, err := config.GetInterfaceValue[string](fmap, "cloud")
if err != nil {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
}
sc.Cloud = cloud
authAny, ok := fmap["authorizer"]
if !ok {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: missing authorizer")
}
authorizer, err := config.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
if err != nil {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: %s", err.Error())
}
sc.Authorizer = authorizer
return nil
}
func ConvertAzureStorageConfigToConfig(asc AzureStorageConfig) config.KeyedConfig {
if asc.IsEmpty() {
return nil
}
var authorizer Authorizer
authorizer = &AccessKey{
AccessKey: asc.AccessKey,
Account: asc.AccountName,
}
return &StorageConfiguration{
SubscriptionID: asc.SubscriptionId,
Account: asc.AccountName,
Container: asc.ContainerName,
Path: asc.ContainerPath,
Cloud: asc.AzureCloud,
Authorizer: authorizer,
}
}