-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_triggers.go
132 lines (107 loc) · 3.1 KB
/
run_triggers.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ RunTriggers = (*runTriggers)(nil)
type RunTriggers interface {
// Create is used to create a new run trigger.
Create(ctx context.Context, options RunTriggerCreateOptions) (*RunTrigger, error)
// Read RunTrigger by it's ID
Read(ctx context.Context, runTriggerID string) (*RunTrigger, error)
// Delete RunTrigger by it's ID
Delete(ctx context.Context, runTriggerID string) error
}
// runTriggers implements RunTriggers
type runTriggers struct {
client *Client
}
type RunTrigger struct {
ID string `jsonapi:"primary,run-triggers"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
// Relations
Upstream *Upstream `jsonapi:"relation,upstream"`
Downstream *Downstream `jsonapi:"relation,downstream"`
}
type RunTriggerCreateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,run-triggers"`
Downstream *Downstream `jsonapi:"relation,downstream"`
Upstream *Upstream `jsonapi:"relation,upstream"`
}
type Downstream struct {
ID string `jsonapi:"primary,workspaces"`
}
type Upstream struct {
ID string `jsonapi:"primary,workspaces"`
}
// Create is used to create a new runTrigger.
func (s *runTriggers) Create(ctx context.Context, options RunTriggerCreateOptions) (*RunTrigger, error) {
if err := options.valid(); err != nil {
return nil, err
}
// Make sure we don't send a user provided ID.
options.ID = ""
req, err := s.client.newRequest("POST", "run-triggers", &options)
if err != nil {
return nil, err
}
runTrigger := &RunTrigger{}
err = s.client.do(ctx, req, runTrigger)
if err != nil {
return nil, err
}
return runTrigger, nil
}
func (o RunTriggerCreateOptions) valid() error {
if o.Downstream == nil {
return errors.New("downstream ID is required")
}
if o.Upstream == nil {
return errors.New("upstream ID is required")
}
if !validString(&o.Downstream.ID) {
return errors.New("downstream ID is required")
}
if !validStringID(&o.Downstream.ID) {
return errors.New("invalid value for Downstream ID")
}
if !validString(&o.Upstream.ID) {
return errors.New("upstream ID is required")
}
if !validStringID(&o.Upstream.ID) {
return errors.New("invalid value for Upstream ID")
}
return nil
}
func (s *runTriggers) Read(ctx context.Context, runTriggerID string) (*RunTrigger, error) {
if !validStringID(&runTriggerID) {
return nil, errors.New("invalid value for RunTrigger ID")
}
u := fmt.Sprintf("run-triggers/%s", url.QueryEscape(runTriggerID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
runTrigger := &RunTrigger{}
err = s.client.do(ctx, req, runTrigger)
if err != nil {
return nil, err
}
return runTrigger, nil
}
func (s *runTriggers) Delete(ctx context.Context, runTriggerID string) error {
if !validStringID(&runTriggerID) {
return errors.New("invalid value for RunTrigger ID")
}
u := fmt.Sprintf("run-triggers/%s", url.QueryEscape(runTriggerID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}