forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.go
68 lines (57 loc) · 1.8 KB
/
status.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
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package bundle
import (
"time"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/server/types"
"github.com/pkg/errors"
)
const (
errCode = "bundle_error"
)
// Status represents the status of processing a bundle.
type Status struct {
Name string `json:"name"`
ActiveRevision string `json:"active_revision,omitempty"`
LastSuccessfulActivation time.Time `json:"last_successful_activation,omitempty"`
LastSuccessfulDownload time.Time `json:"last_successful_download,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Errors []error `json:"errors,omitempty"`
}
// SetActivateSuccess updates the status object to reflect a successful
// activation.
func (s *Status) SetActivateSuccess(revision string) {
s.LastSuccessfulActivation = time.Now().UTC()
s.ActiveRevision = revision
}
// SetDownloadSuccess updates the status object to reflect a successful
// download.
func (s *Status) SetDownloadSuccess() {
s.LastSuccessfulDownload = time.Now().UTC()
}
// SetError updates the status object to reflect a failure to download or
// activate. If err is nil, the error status is cleared.
func (s *Status) SetError(err error) {
if err == nil {
s.Code = ""
s.Message = ""
s.Errors = nil
return
}
cause := errors.Cause(err)
if astErr, ok := cause.(ast.Errors); ok {
s.Code = errCode
s.Message = types.MsgCompileModuleError
s.Errors = make([]error, len(astErr))
for i := range astErr {
s.Errors[i] = astErr[i]
}
} else {
s.Code = errCode
s.Message = err.Error()
s.Errors = nil
}
}