-
Notifications
You must be signed in to change notification settings - Fork 8
/
meta.go
77 lines (64 loc) · 1.57 KB
/
meta.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
package zebra
import (
"errors"
"time"
"github.com/google/uuid"
)
const (
ShortIDSize = 7
DefaultMaxDuration = 4
)
var (
ErrNameEmpty = errors.New("name is empty")
ErrIDEmpty = errors.New("id is empty")
ErrIDShort = errors.New("id must be at least 7 characters long")
)
// Meta defines the resource data that is required to be maintained by all the
// resource types.
type Meta struct {
ID string `json:"id"`
Name string `json:"name"`
Type Type `json:"type"`
Owner string `json:"owner"`
CreationTime time.Time `json:"creationTime"`
ModificationTime time.Time `json:"modificationTime"`
Labels Labels `json:"labels"`
}
func NewMeta(resType Type, name string, group string, owner string) Meta {
t := time.Now()
labels := Labels{}
id := uuid.New().String()
labels.Add("system.group", group)
if name == "" {
// Make the short ID as the default name
name = id[:7]
}
return Meta{
ID: id,
Name: name,
Type: resType,
Owner: owner,
CreationTime: t,
ModificationTime: t,
Labels: labels,
}
}
// Validate returns an error if the given BaseResource object has incorrect values.
// Else, it returns nil.
func (r Meta) Validate() error {
switch {
case r.ID == "":
return ErrIDEmpty
case len(r.ID) < ShortIDSize:
return ErrIDShort
case r.Name == "":
return ErrNameEmpty
}
if err := r.Type.Validate(); err != nil {
return err
}
if err := r.Labels.Validate(); err != nil {
return err
}
return nil
}