forked from cfergeau/quill
/
submission.go
150 lines (123 loc) · 3.11 KB
/
submission.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
package notary
import (
"context"
"fmt"
"math/rand"
"path/filepath"
"time"
"github.com/dustin-decker/quill/internal/log"
)
type SubmissionStatus string
const (
AcceptedStatus = "Accepted"
PendingStatus = "Pending"
InvalidStatus = "Invalid"
RejectedStatus = "Rejected"
TimeoutStatus = "Timeout"
)
func (s SubmissionStatus) isCompleted() bool {
switch s {
case AcceptedStatus, RejectedStatus, InvalidStatus, TimeoutStatus:
return true
default:
return false
}
}
func (s SubmissionStatus) isSuccessful() bool {
return s == AcceptedStatus
}
type Submission struct {
api api
binary *Payload
name string
id string
}
type SubmissionList struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
CreatedDate string `json:"createdDate"`
}
func NewSubmission(a api, bin *Payload) *Submission {
return &Submission{
name: filepath.Base(bin.Path + "-" + bin.Digest + "-" + randomString(8)),
binary: bin,
api: a,
}
}
func ExistingSubmission(a api, id string) *Submission {
return &Submission{
id: id,
api: a,
}
}
func (s Submission) ID() string {
return s.id
}
func (s *Submission) Start(ctx context.Context) error {
if s.id != "" {
return fmt.Errorf("submission already started")
}
log.WithFields("name", s.name).Debug("starting submission")
if s.binary == nil {
return fmt.Errorf("unable to start Submission without a binary")
}
response, err := s.api.submissionRequest(
ctx,
submissionRequest{
Sha256: s.binary.Digest,
SubmissionName: s.name,
},
)
if err != nil {
return err
}
s.id = response.Data.ID
log.WithFields("id", s.id, "name", s.name).Trace("received submission id")
return s.api.uploadBinary(ctx, *response, *s.binary)
}
func (s Submission) Status(ctx context.Context) (SubmissionStatus, error) {
log.WithFields("id", s.id).Trace("checking submission status")
response, err := s.api.submissionStatusRequest(ctx, s.id)
if err != nil {
return "", err
}
log.WithFields("status", fmt.Sprintf("%q", response.Data.Attributes.Status), "id", s.id).Debug("submission status")
switch response.Data.Attributes.Status {
case "In Progress":
return PendingStatus, nil
case "Accepted":
return AcceptedStatus, nil
case "Invalid":
return InvalidStatus, nil
case "Rejected":
return RejectedStatus, nil
default:
return "", fmt.Errorf("unexpected status: %s", response.Data.Attributes.Status)
}
}
func (s Submission) Logs(ctx context.Context) (string, error) {
return s.api.submissionLogs(ctx, s.id)
}
func (s Submission) List(ctx context.Context) ([]SubmissionList, error) {
resp, err := s.api.submissionList(ctx)
if err != nil {
return nil, err
}
var results []SubmissionList
for _, item := range resp.Data {
results = append(results, SubmissionList{
ID: item.ID,
Name: item.Attributes.Name,
Status: item.Attributes.Status,
CreatedDate: item.Attributes.CreatedDate,
})
}
return results, nil
}
func randomString(length int) string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, length)
rand.Read(b) //nolint:gosec
return fmt.Sprintf("%x", b)[:length]
}