/
types_instanceid.go
246 lines (200 loc) · 6.11 KB
/
types_instanceid.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package types
import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"
"github.com/akutz/goof"
)
// InstanceIDMap is a map of InstanceID objects.
type InstanceIDMap map[string]*InstanceID
// InstanceID identifies a host to a remote storage platform.
type InstanceID struct {
// ID is the simple part of the InstanceID.
ID string `json:"id" yaml:"id"`
// Driver is the name of the StorageExecutor that created the InstanceID
// as well as the name of the StorageDriver for which the InstanceID is
// valid.
Driver string `json:"driver"`
// Service is the name of the libStorage service for which the InstanceID
// is valid.
Service string `json:"service"`
// Fields is additional, driver specific data about the Instance ID.
Fields map[string]string `json:"fields"`
metadata json.RawMessage
}
var (
// ErrIIDMetadataNil is returned by *InstanceID.UnmarshalMetadata when
// the InstanceID's metadata is empty or nil.
ErrIIDMetadataNil = goof.New("cannot unmarshal nil metadata")
// ErrIIDMetadataNilData is returned by *InstanceID.MarshalMetadata when
// the provided object to marshal is nil.
ErrIIDMetadataNilData = goof.New("cannot marshal nil metadata")
// ErrIIDMetadataNilDest is returned by *InstanceID.UnmarshalMetadata when
// the provided destination into which the metadata should be unmarshaled
// is nil.
ErrIIDMetadataNilDest = goof.New("cannot unmarshal into nil receiver")
)
// HasMetadata returns a flag indicating whether or not the instance ID has any
// associated metadata.
func (i *InstanceID) HasMetadata() bool {
return len(i.metadata) > 0
}
// String returns the string representation of an InstanceID object.
func (i *InstanceID) String() string {
buf, err := i.MarshalText()
if err != nil {
panic(err)
}
return string(buf)
}
// DeleteMetadata deletes the metadata from the InstanceID.
func (i *InstanceID) DeleteMetadata() {
i.metadata = json.RawMessage{}
}
// MarshalMetadata encodes the provided object to JSON and assigns the result
// to the InstanceID's metadata field.
func (i *InstanceID) MarshalMetadata(data interface{}) error {
if fmt.Sprintf("%v", data) == "<nil>" {
return ErrIIDMetadataNilData
}
var err error
if i.metadata, err = json.Marshal(data); err != nil {
return err
}
return nil
}
// UnmarshalMetadata decodes the InstanceID's metadata into the provided object.
func (i *InstanceID) UnmarshalMetadata(dest interface{}) error {
if fmt.Sprintf("%v", dest) == "<nil>" {
return ErrIIDMetadataNilDest
}
if len(i.metadata) == 0 {
return ErrIIDMetadataNil
}
if err := json.Unmarshal(i.metadata, dest); err != nil {
return err
}
return nil
}
// MarshalText marshals InstanceID to a text string that adheres to the format
// `DRIVER[:SERVICE]=ID,FIELDS,[,METADATA]`. If metadata is present it is
// encoded as a base64 string.
func (i *InstanceID) MarshalText() ([]byte, error) {
t := &bytes.Buffer{}
fmt.Fprint(t, i.Driver)
if len(i.Service) > 0 {
fmt.Fprintf(t, ":%s", i.Service)
}
fmt.Fprintf(t, "=%s", i.ID)
if len(i.Fields) == 0 && len(i.metadata) == 0 {
return t.Bytes(), nil
}
t.WriteByte(',')
if len(i.Fields) > 0 {
vals := url.Values{}
for k, v := range i.Fields {
vals.Add(k, v)
}
t.WriteString(vals.Encode())
}
if len(i.metadata) == 0 {
return t.Bytes(), nil
}
t.WriteByte(',')
enc := b64.NewEncoder(b64.StdEncoding, t)
if _, err := enc.Write(i.metadata); err != nil {
return nil, err
}
if err := enc.Close(); err != nil {
return nil, err
}
return t.Bytes(), nil
}
var iidRX = regexp.MustCompile(
`(?i)^([^=]+)=([^,]*)(?:,(.*?))?(?:,(.+))?$`)
// UnmarshalText unmarshals the data into a an InstanceID provided the data
// adheres to the format described in the MarshalText function.
func (i *InstanceID) UnmarshalText(value []byte) error {
m := iidRX.FindSubmatch(value)
lm := len(m)
if lm < 3 {
return goof.WithField("value", string(value), "invalid InstanceID")
}
dos := strings.Split(string(m[1]), ":")
i.Driver = dos[0]
if len(dos) > 1 {
i.Service = dos[1]
}
i.ID = string(m[2])
if lm > 3 && len(m[3]) > 0 {
qs, err := url.ParseQuery(string(m[3]))
if err != nil {
return err
}
i.Fields = map[string]string{}
for k := range qs {
i.Fields[k] = qs.Get(k)
}
}
if lm > 4 && len(m[4]) > 0 {
enc := m[4]
dec := b64.NewDecoder(b64.StdEncoding, bytes.NewReader(enc))
i.metadata = make([]byte, b64.StdEncoding.DecodedLen(len(enc)))
n, err := dec.Read(i.metadata)
if err != nil {
return err
}
i.metadata = i.metadata[:n]
}
return nil
}
// MarshalJSON marshals the InstanceID to JSON.
func (i *InstanceID) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
ID string `json:"id"`
Driver string `json:"driver"`
Service string `json:"service"`
Fields map[string]string `json:"fields,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
}{i.ID, i.Driver, i.Service, i.Fields, i.metadata})
}
// UnmarshalJSON marshals the InstanceID to JSON.
func (i *InstanceID) UnmarshalJSON(data []byte) error {
iid := &struct {
ID string `json:"id"`
Driver string `json:"driver"`
Service string `json:"service"`
Fields map[string]string `json:"fields,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
}{}
if err := json.Unmarshal(data, iid); err != nil {
return err
}
i.ID = iid.ID
i.Driver = iid.Driver
i.Service = iid.Service
i.Fields = iid.Fields
i.metadata = iid.Metadata
return nil
}
// MarshalYAML returns the object to marshal to the YAML representation of the
// InstanceID.
func (i *InstanceID) MarshalYAML() (interface{}, error) {
var metadata map[string]interface{}
if len(i.metadata) > 0 {
if err := json.Unmarshal(i.metadata, &metadata); err != nil {
return nil, err
}
}
return &struct {
ID string `yaml:"id"`
Driver string `yaml:"driver"`
Service string `yaml:"service"`
Fields map[string]string `yaml:"fields,omitempty"`
Metadata map[string]interface{} `yaml:"metadata,omitempty"`
}{i.ID, i.Driver, i.Service, i.Fields, metadata}, nil
}