Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for run actions (apply, cancel, discard) #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type Runs interface {
Read(ctx context.Context, runID string) (*Run, error)
// Create a new run with the given options.
Create(ctx context.Context, options RunCreateOptions) (*Run, error)
// Apply a run that is paused waiting for confirmation after a plan.
Apply(ctx context.Context, runID string, options RunApplyOptions) error
// Interrupt a run that is currently planning or applying.
Cancel(ctx context.Context, runID string, options RunCancelOptions) error
// Skip any remaining work on runs that are paused waiting for confirmation or priority.
Discard(ctx context.Context, runID string, options RunDiscardOptions) error
}

// runs implements Runs.
Expand All @@ -28,7 +34,7 @@ type runs struct {
// RunStatus represents a run state.
type RunStatus string

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

return r, nil
}

// RunApplyOptions represents the options for applying a Run.
type RunApplyOptions struct {
Comment string `jsonapi:"attr,comment"`
ApplyAt *time.Time `jsonapi:"attr,apply-at,iso8601"`
}

// Apply a run that is paused waiting for confirmation after a plan.
func (s *runs) Apply(ctx context.Context, runID string, options RunApplyOptions) error {
if !validStringID(&runID) {
return errors.New("invalid value for run ID")
}

u := fmt.Sprintf("runs/%s/actions/apply", url.PathEscape(runID))
req, err := s.client.newRequest("POST", u, &options)
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")

return s.client.do(ctx, req, nil)
}

// RunCancelOptions represents the options for canceling a Run.
type RunCancelOptions struct {
Comment string `jsonapi:"attr,comment"`
}

// Cancel interrupts a run that is currently planning or applying.
func (s *runs) Cancel(ctx context.Context, runID string, options RunCancelOptions) error {
if !validStringID(&runID) {
return errors.New("invalid value for run ID")
}

u := fmt.Sprintf("runs/%s/actions/cancel", url.PathEscape(runID))
req, err := s.client.newRequest("POST", u, &options)
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")

return s.client.do(ctx, req, nil)
}

// RunDiscardOptions represents the options for discarding a Run.
type RunDiscardOptions struct {
Comment string `jsonapi:"attr,comment"`
}

// Discard skips any remaining work on runs that are paused waiting for confirmation or priority.
func (s *runs) Discard(ctx context.Context, runID string, options RunDiscardOptions) error {
if !validStringID(&runID) {
return errors.New("invalid value for run ID")
}

u := fmt.Sprintf("runs/%s/actions/discard", url.PathEscape(runID))
req, err := s.client.newRequest("POST", u, &options)
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")

return s.client.do(ctx, req, nil)
}