forked from cnabio/cnab-go
/
claim.go
98 lines (85 loc) · 2.74 KB
/
claim.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
package claim
import (
"fmt"
"math/rand"
"regexp"
"time"
"github.com/oklog/ulid"
"github.com/deislabs/cnab-go/bundle"
)
// Status constants define the CNAB status fields on a Result.
const (
StatusSuccess = "success"
StatusFailure = "failure"
StatusUnderway = "underway"
StatusUnknown = "unknown"
)
// Action constants define the CNAB action to be taken
const (
ActionInstall = "install"
ActionUpgrade = "upgrade"
ActionDowngrade = "downgrade"
ActionUninstall = "uninstall"
ActionStatus = "status"
ActionUnknown = "unknown"
)
// Claim is an installation claim receipt.
//
// Claims reprsent information about a particular installation, and
// provide the necessary data to upgrade, uninstall, and downgrade
// a CNAB package.
type Claim struct {
Name string `json:"name"`
Revision string `json:"revision"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Bundle *bundle.Bundle `json:"bundle"`
Result Result `json:"result,omitempty"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
// Outputs is a map from the names of outputs (defined in the bundle) to the contents of the files.
Outputs map[string]interface{} `json:"outputs,omitempty"`
Custom interface{} `json:"custom,omitempty"`
}
// ValidName is a regular expression that indicates whether a name is a valid claim name.
var ValidName = regexp.MustCompile("^[a-zA-Z0-9_-]+$")
// New creates a new Claim initialized for an installation operation.
func New(name string) (*Claim, error) {
if !ValidName.MatchString(name) {
return nil, fmt.Errorf("invalid name %q. Names must be [a-zA-Z0-9-_]+", name)
}
now := time.Now()
return &Claim{
Name: name,
Revision: ULID(),
Created: now,
Modified: now,
Result: Result{
Action: ActionUnknown,
Status: StatusUnknown,
},
Parameters: map[string]interface{}{},
Outputs: map[string]interface{}{},
}, nil
}
// Update is a convenience for modifying the necessary fields on a Claim.
//
// Per spec, when a claim is updated, the action, status, revision, and modified fields all change.
// All but status and action can be computed.
func (c *Claim) Update(action, status string) {
c.Result.Action = action
c.Result.Status = status
c.Modified = time.Now()
c.Revision = ULID()
}
// Result tracks the result of a Duffle operation on a CNAB installation
type Result struct {
Message string `json:"message"`
Action string `json:"action"`
Status string `json:"status"`
}
// ULID generates a string representation of a ULID.
func ULID() string {
now := time.Now()
entropy := rand.New(rand.NewSource(now.UnixNano()))
return ulid.MustNew(ulid.Timestamp(now), entropy).String()
}