Skip to content

Commit dbb735a

Browse files
committed
Add support for run actions (apply, cancel, discard)
1 parent b79ba53 commit dbb735a

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

run.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ type Runs interface {
1818
Read(ctx context.Context, runID string) (*Run, error)
1919
// Create a new run with the given options.
2020
Create(ctx context.Context, options RunCreateOptions) (*Run, error)
21+
// Apply a run that is paused waiting for confirmation after a plan.
22+
Apply(ctx context.Context, runID string, options RunApplyOptions) error
23+
// Interrupt a run that is currently planning or applying.
24+
Cancel(ctx context.Context, runID string, options RunCancelOptions) error
25+
// Skip any remaining work on runs that are paused waiting for confirmation or priority.
26+
Discard(ctx context.Context, runID string, options RunDiscardOptions) error
2127
}
2228

2329
// runs implements Runs.
@@ -28,7 +34,7 @@ type runs struct {
2834
// RunStatus represents a run state.
2935
type RunStatus string
3036

31-
//List all available run statuses.
37+
// List all available run statuses.
3238
const (
3339
RunApplied RunStatus = "applied"
3440
RunApplyQueued RunStatus = "apply_queued"
@@ -157,3 +163,70 @@ func (s *runs) Read(ctx context.Context, runID string) (*Run, error) {
157163

158164
return r, nil
159165
}
166+
167+
// RunApplyOptions represents the options for applying a Run.
168+
type RunApplyOptions struct {
169+
Comment string `jsonapi:"attr,comment"`
170+
ApplyAt *time.Time `jsonapi:"attr,apply-at,iso8601"`
171+
}
172+
173+
// Apply a run that is paused waiting for confirmation after a plan.
174+
func (s *runs) Apply(ctx context.Context, runID string, options RunApplyOptions) error {
175+
if !validStringID(&runID) {
176+
return errors.New("invalid value for run ID")
177+
}
178+
179+
u := fmt.Sprintf("runs/%s/actions/apply", url.PathEscape(runID))
180+
req, err := s.client.newRequest("POST", u, &options)
181+
if err != nil {
182+
return err
183+
}
184+
185+
req.Header.Set("Content-Type", "application/json")
186+
187+
return s.client.do(ctx, req, nil)
188+
}
189+
190+
// RunCancelOptions represents the options for canceling a Run.
191+
type RunCancelOptions struct {
192+
Comment string `jsonapi:"attr,comment"`
193+
}
194+
195+
// Cancel interrupts a run that is currently planning or applying.
196+
func (s *runs) Cancel(ctx context.Context, runID string, options RunCancelOptions) error {
197+
if !validStringID(&runID) {
198+
return errors.New("invalid value for run ID")
199+
}
200+
201+
u := fmt.Sprintf("runs/%s/actions/cancel", url.PathEscape(runID))
202+
req, err := s.client.newRequest("POST", u, &options)
203+
if err != nil {
204+
return err
205+
}
206+
207+
req.Header.Set("Content-Type", "application/json")
208+
209+
return s.client.do(ctx, req, nil)
210+
}
211+
212+
// RunDiscardOptions represents the options for discarding a Run.
213+
type RunDiscardOptions struct {
214+
Comment string `jsonapi:"attr,comment"`
215+
}
216+
217+
// Discard skips any remaining work on runs that are paused waiting for confirmation or priority.
218+
func (s *runs) Discard(ctx context.Context, runID string, options RunDiscardOptions) error {
219+
if !validStringID(&runID) {
220+
return errors.New("invalid value for run ID")
221+
}
222+
223+
u := fmt.Sprintf("runs/%s/actions/discard", url.PathEscape(runID))
224+
req, err := s.client.newRequest("POST", u, &options)
225+
if err != nil {
226+
return err
227+
}
228+
229+
req.Header.Set("Content-Type", "application/json")
230+
231+
return s.client.do(ctx, req, nil)
232+
}

0 commit comments

Comments
 (0)