forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_run.go
182 lines (154 loc) · 5.36 KB
/
admin_run.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
package tfe
import (
"context"
"fmt"
"net/url"
"strings"
"time"
)
// Compile-time proof of interface implementation.
var _ AdminRuns = (*adminRuns)(nil)
// AdminRuns describes all the admin run related methods that the Terraform
// Enterprise API supports.
// It contains endpoints to help site administrators manage their runs.
//
// TFE API docs: https://www.terraform.io/docs/cloud/api/admin/runs.html
type AdminRuns interface {
// List all the runs of the given installation.
List(ctx context.Context, options *AdminRunsListOptions) (*AdminRunsList, error)
// Force-cancel a run by its ID.
ForceCancel(ctx context.Context, runID string, options AdminRunForceCancelOptions) error
}
// AdminRun represents AdminRuns interface.
type AdminRun struct {
ID string `jsonapi:"primary,runs"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
HasChanges bool `jsonapi:"attr,has-changes"`
Status RunStatus `jsonapi:"attr,status"`
StatusTimestamps *RunStatusTimestamps `jsonapi:"attr,status-timestamps"`
// Relations
Workspace *AdminWorkspace `jsonapi:"relation,workspace"`
Organization *AdminOrganization `jsonapi:"relation,workspace.organization"`
}
// AdminRunsList represents a list of runs.
type AdminRunsList struct {
*Pagination
Items []*AdminRun
}
// AdminRunIncludeOpt represents the available options for include query params.
// https://www.terraform.io/cloud-docs/api-docs/admin/runs#available-related-resources
type AdminRunIncludeOpt string
const (
AdminRunWorkspace AdminRunIncludeOpt = "workspace"
AdminRunWorkspaceOrg AdminRunIncludeOpt = "workspace.organization"
AdminRunWorkspaceOrgOwners AdminRunIncludeOpt = "workspace.organization.owners"
)
// AdminRunsListOptions represents the options for listing runs.
// https://www.terraform.io/docs/cloud/api/admin/runs.html#query-parameters
type AdminRunsListOptions struct {
ListOptions
RunStatus string `url:"filter[status],omitempty"`
Query string `url:"q,omitempty"`
// Optional: A list of relations to include. See available resources
// https://www.terraform.io/cloud-docs/api-docs/admin/runs#available-related-resources
Include []AdminRunIncludeOpt `url:"include,omitempty"`
}
// adminRuns implements the AdminRuns interface.
type adminRuns struct {
client *Client
}
// List all the runs of the terraform enterprise installation.
// https://www.terraform.io/docs/cloud/api/admin/runs.html#list-all-runs
func (s *adminRuns) List(ctx context.Context, options *AdminRunsListOptions) (*AdminRunsList, error) {
if err := options.valid(); err != nil {
return nil, err
}
u := "admin/runs"
req, err := s.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}
rl := &AdminRunsList{}
err = req.Do(ctx, rl)
if err != nil {
return nil, err
}
return rl, nil
}
// AdminRunForceCancelOptions represents the options for force-canceling a run.
type AdminRunForceCancelOptions struct {
// An optional comment explaining the reason for the force-cancel.
// https://www.terraform.io/docs/cloud/api/admin/runs.html#request-body
Comment *string `json:"comment,omitempty"`
}
// ForceCancel is used to forcefully cancel a run by its ID.
// https://www.terraform.io/docs/cloud/api/admin/runs.html#force-a-run-into-the-quot-cancelled-quot-state
func (s *adminRuns) ForceCancel(ctx context.Context, runID string, options AdminRunForceCancelOptions) error {
if !validStringID(&runID) {
return ErrInvalidRunID
}
u := fmt.Sprintf("admin/runs/%s/actions/force-cancel", url.QueryEscape(runID))
req, err := s.client.NewRequest("POST", u, &options)
if err != nil {
return err
}
return req.Do(ctx, nil)
}
func (o *AdminRunsListOptions) valid() error {
if o == nil { // nothing to validate
return nil
}
if err := validateAdminRunFilterParams(o.RunStatus); err != nil {
return err
}
if err := validateAdminRunIncludeParams(o.Include); err != nil {
return err
}
return nil
}
func validateAdminRunFilterParams(runStatus string) error {
// For the platform, an invalid filter value is a semantically understood query that returns an empty set, no error, no warning. But for go-tfe, an invalid value is good enough reason to error prior to a network call to the platform:
if validString(&runStatus) {
sanitizedRunstatus := strings.TrimSpace(runStatus)
runStatuses := strings.Split(sanitizedRunstatus, ",")
// iterate over our statuses, and ensure it is valid.
for _, status := range runStatuses {
switch status {
case string(RunApplied),
string(RunApplyQueued),
string(RunApplying),
string(RunCanceled),
string(RunConfirmed),
string(RunCostEstimate),
string(RunCostEstimating),
string(RunDiscarded),
string(RunErrored),
string(RunPending),
string(RunPlanQueued),
string(RunPlanned),
string(RunPlannedAndFinished),
string(RunPlanning),
string(RunPolicyChecked),
string(RunPolicyChecking),
string(RunPolicyOverride),
string(RunPolicySoftFailed),
"":
// do nothing
default:
return fmt.Errorf(`invalid value "%s" for run status`, status)
}
}
}
return nil
}
func validateAdminRunIncludeParams(params []AdminRunIncludeOpt) error {
for _, p := range params {
switch p {
case AdminRunWorkspace, AdminRunWorkspaceOrg, AdminRunWorkspaceOrgOwners:
// do nothing
default:
return ErrInvalidIncludeValue
}
}
return nil
}