forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_trigger.go
229 lines (187 loc) · 6.03 KB
/
run_trigger.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package tfe
import (
"context"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ RunTriggers = (*runTriggers)(nil)
// RunTriggers describes all the Run Trigger
// related methods that the Terraform Cloud API supports.
//
// TFE API docs:
// https://www.terraform.io/docs/cloud/api/run-triggers.html
type RunTriggers interface {
// List all the run triggers within a workspace.
List(ctx context.Context, workspaceID string, options *RunTriggerListOptions) (*RunTriggerList, error)
// Create a new run trigger with the given options.
Create(ctx context.Context, workspaceID string, options RunTriggerCreateOptions) (*RunTrigger, error)
// Read a run trigger by its ID.
Read(ctx context.Context, RunTriggerID string) (*RunTrigger, error)
// Delete a run trigger by its ID.
Delete(ctx context.Context, RunTriggerID string) error
}
// runTriggers implements RunTriggers.
type runTriggers struct {
client *Client
}
// RunTriggerList represents a list of Run Triggers
type RunTriggerList struct {
*Pagination
Items []*RunTrigger
}
// RunTrigger represents a run trigger.
type RunTrigger struct {
ID string `jsonapi:"primary,run-triggers"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
SourceableName string `jsonapi:"attr,sourceable-name"`
WorkspaceName string `jsonapi:"attr,workspace-name"`
// Relations
// TODO: this will eventually need to be polymorphic
Sourceable *Workspace `jsonapi:"relation,sourceable"`
Workspace *Workspace `jsonapi:"relation,workspace"`
}
// https://www.terraform.io/cloud-docs/api-docs/run-triggers#query-parameters
type RunTriggerFilterOp string
const (
RunTriggerOutbound RunTriggerFilterOp = "outbound" // create runs in other workspaces.
RunTriggerInbound RunTriggerFilterOp = "inbound" // create runs in the specified workspace
)
// A list of relations to include
// https://www.terraform.io/cloud-docs/api-docs/run-triggers#available-related-resources
type RunTriggerIncludeOpt string
const (
RunTriggerWorkspace RunTriggerIncludeOpt = "workspace"
RunTriggerSourceable RunTriggerIncludeOpt = "sourceable"
)
// RunTriggerListOptions represents the options for listing
// run triggers.
type RunTriggerListOptions struct {
ListOptions
RunTriggerType RunTriggerFilterOp `url:"filter[run-trigger][type]"` // Required
Include []RunTriggerIncludeOpt `url:"include,omitempty"` // optional
}
// RunTriggerCreateOptions represents the options for
// creating a new run trigger.
type RunTriggerCreateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,run-triggers"`
// The source workspace
Sourceable *Workspace `jsonapi:"relation,sourceable"`
}
// List all the run triggers associated with a workspace.
func (s *runTriggers) List(ctx context.Context, workspaceID string, options *RunTriggerListOptions) (*RunTriggerList, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidWorkspaceID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("workspaces/%s/run-triggers", url.QueryEscape(workspaceID))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
rtl := &RunTriggerList{}
err = s.client.do(ctx, req, rtl)
if err != nil {
return nil, err
}
return rtl, nil
}
// Create a run trigger with the given options.
func (s *runTriggers) Create(ctx context.Context, workspaceID string, options RunTriggerCreateOptions) (*RunTrigger, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidWorkspaceID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("workspaces/%s/run-triggers", url.QueryEscape(workspaceID))
req, err := s.client.newRequest("POST", u, &options)
if err != nil {
return nil, err
}
rt := &RunTrigger{}
err = s.client.do(ctx, req, rt)
if err != nil {
return nil, err
}
return rt, nil
}
// Read a run trigger by its ID.
func (s *runTriggers) Read(ctx context.Context, runTriggerID string) (*RunTrigger, error) {
if !validStringID(&runTriggerID) {
return nil, ErrInvalidRunTriggerID
}
u := fmt.Sprintf("run-triggers/%s", url.QueryEscape(runTriggerID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
rt := &RunTrigger{}
err = s.client.do(ctx, req, rt)
if err != nil {
return nil, err
}
return rt, nil
}
// Delete a run trigger by its ID.
func (s *runTriggers) Delete(ctx context.Context, runTriggerID string) error {
if !validStringID(&runTriggerID) {
return ErrInvalidRunTriggerID
}
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)
}
func (o RunTriggerCreateOptions) valid() error {
if o.Sourceable == nil {
return ErrRequiredSourceable
}
return nil
}
func (o *RunTriggerListOptions) valid() error {
if o == nil {
return ErrRequiredRunTriggerListOps
}
if err := validateRunTriggerFilterParam(o.RunTriggerType, o.Include); err != nil {
return err
}
if err := validateRunTriggerIncludeParams(o.Include); err != nil {
return err
}
return nil
}
func validateRunTriggerFilterParam(filterParam RunTriggerFilterOp, includeParams []RunTriggerIncludeOpt) error {
switch filterParam {
case RunTriggerOutbound, RunTriggerInbound:
// Do nothing
default:
return ErrInvalidRunTriggerType // return an error even if string is empty because this a required field
}
if len(includeParams) > 0 {
if filterParam != RunTriggerInbound {
return ErrUnsupportedRunTriggerType // if user passes RunTriggerOutbound the platform will not return any "include" data
}
}
return nil
}
func validateRunTriggerIncludeParams(params []RunTriggerIncludeOpt) error {
for _, p := range params {
switch p {
case RunTriggerWorkspace, RunTriggerSourceable:
// Do nothing
default:
return ErrInvalidIncludeValue
}
}
return nil
}