forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_plan.go
263 lines (216 loc) · 9.63 KB
/
stack_plan.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
)
// StackPlans describes all the stacks plans-related methods that the HCP Terraform API supports.
// NOTE WELL: This is a beta feature and is subject to change until noted otherwise in the
// release notes.
type StackPlans interface {
// Read returns a stack plan by its ID.
Read(ctx context.Context, stackPlanID string) (*StackPlan, error)
// ListByConfiguration returns a list of stack plans for a given stack configuration.
ListByConfiguration(ctx context.Context, stackConfigurationID string, options *StackPlansListOptions) (*StackPlanList, error)
// Approve approves a stack plan.
Approve(ctx context.Context, stackPlanID string) error
// Cancel cancels a stack plan.
Cancel(ctx context.Context, stackPlanID string) error
// Discard discards a stack plan.
Discard(ctx context.Context, stackPlanID string) error
// PlanDescription returns the plan description for a stack plan.
PlanDescription(ctx context.Context, stackPlanID string) (*JSONChangeDesc, error)
}
type StackPlansStatusFilter string
const (
StackPlansStatusFilterCreated StackPlansStatusFilter = "created"
StackPlansStatusFilterRunning StackPlansStatusFilter = "running"
StackPlansStatusFilterPaused StackPlansStatusFilter = "paused"
StackPlansStatusFilterFinished StackPlansStatusFilter = "finished"
StackPlansStatusFilterDiscarded StackPlansStatusFilter = "discarded"
StackPlansStatusFilterErrored StackPlansStatusFilter = "errored"
StackPlansStatusFilterCanceled StackPlansStatusFilter = "canceled"
)
type StackPlansIncludeOpt string
const (
StackPlansIncludeOperations StackPlansIncludeOpt = "stack_plan_operations"
)
type StackPlansListOptions struct {
ListOptions
// Optional: A query string to filter plans by status.
Status StackPlansStatusFilter `url:"filter[status],omitempty"`
// Optional: A query string to filter plans by deployment.
Deployment string `url:"filter[deployment],omitempty"`
Include []StackPlansIncludeOpt `url:"include,omitempty"`
}
type StackPlanList struct {
*Pagination
Items []*StackPlan
}
// stackPlans implements StackPlans.
type stackPlans struct {
client *Client
}
var _ StackPlans = &stackPlans{}
// StackPlanStatusTimestamps are the timestamps of the status changes for a stack
type StackPlanStatusTimestamps struct {
CreatedAt time.Time `jsonapi:"attr,created-at,rfc3339"`
RunningAt time.Time `jsonapi:"attr,running-at,rfc3339"`
PausedAt time.Time `jsonapi:"attr,paused-at,rfc3339"`
FinishedAt time.Time `jsonapi:"attr,finished-at,rfc3339"`
}
// PlanChanges is the summary of the planned changes
type PlanChanges struct {
Add int `jsonapi:"attr,add"`
Total int `jsonapi:"attr,total"`
Change int `jsonapi:"attr,change"`
Import int `jsonapi:"attr,import"`
Remove int `jsonapi:"attr,remove"`
}
// StackPlan represents a plan for a stack.
type StackPlan struct {
ID string `jsonapi:"primary,stack-plans"`
PlanMode string `jsonapi:"attr,plan-mode"`
PlanNumber string `jsonapi:"attr,plan-number"`
Status string `jsonapi:"attr,status"`
StatusTimestamps *StackPlanStatusTimestamps `jsonapi:"attr,status-timestamps"`
IsPlanned bool `jsonapi:"attr,is-planned"`
Changes *PlanChanges `jsonapi:"attr,changes"`
Deployment string `jsonapi:"attr,deployment"`
// Relationships
StackConfiguration *StackConfiguration `jsonapi:"relation,stack-configuration"`
Stack *Stack `jsonapi:"relation,stack"`
StackPlanOperations []*StackPlanOperation `jsonapi:"relation,stack-plan-operations"`
}
// JSONChangeDesc represents a change description of a stack plan / apply operation.
type JSONChangeDesc struct {
FormatVersion uint64 `json:"terraform_stack_change_description"`
Interim bool `json:"interim,omitempty"`
Applyable bool `json:"applyable"`
PlanMode string `json:"plan_mode"`
Components []JSONComponent `json:"components"`
ResourceInstances []JSONResourceInstance `json:"resource_instances"`
DeferredResourceInstances []JSONResourceInstanceDeferral `json:"deferred_resource_instances"`
Outputs map[string]JSONOutput `json:"outputs"`
}
// JSONComponent represents a change description of a single component in a plan.
type JSONComponent struct {
Address string `json:"address"`
ComponentAddress string `json:"component_address"`
InstanceCorrelator string `json:"instance_correlator"`
ComponentCorrelator string `json:"component_correlator"`
Actions []ChangeAction `json:"actions"`
Complete bool `json:"complete"`
}
// ChangeAction are the actions a change can have: no-op, create, read, update, delte, forget.
type ChangeAction string
// JSONResourceInstance is the change description of a single resource instance in a plan.
type JSONResourceInstance struct {
ComponentInstanceCorrelator string `json:"component_instance_correlator"`
ComponentInstanceAddress string `json:"component_instance_address"`
Address string `json:"address"`
PreviousComponentInstanceAddress string `json:"previous_component_instance_address,omitempty"`
PreviousAddress string `json:"previous_address,omitempty"`
DeposedKey string `json:"deposed,omitempty"`
ResourceMode string `json:"mode,omitempty"`
ResourceType string `json:"type"`
ProviderAddr string `json:"provider_name"`
Change Change `json:"change"`
ResourceName string `json:"resource_name"`
Index json.RawMessage `json:"index"`
IndexUnknown bool `json:"index_unknown"`
ModuleAddr string `json:"module_address"`
ActionReason string `json:"action_reason,omitempty"`
}
// JSONResourceInstanceDeferral is the change description of a single resource instance that is deferred.
type JSONResourceInstanceDeferral struct {
ResourceInstance JSONResourceInstance `json:"resource_instance"`
Deferred JSONDeferred `json:"deferred"`
}
// JSONDeferred contains the reason why a resource instance is deferred: instance_count_unknown, resource_config_unknown, provider_config_unknown, provider_config_unknown, or deferred_prereq.
type JSONDeferred struct {
Reason string `json:"reason"`
}
// JSONOutput is the value of a single output in a plan.
type JSONOutput struct {
Change json.RawMessage `json:"change"`
}
// Change represents the change of a resource instance in a plan.
type Change struct {
Actions []ChangeAction `json:"actions"`
After json.RawMessage `json:"after"`
Before json.RawMessage `json:"before"`
AfterUnknown json.RawMessage `json:"after_unknown"`
BeforeSensitive json.RawMessage `json:"before_sensitive"`
AfterSensitive json.RawMessage `json:"after_sensitive"`
Importing *JsonImporting `json:"importing,omitempty"`
ReplacePaths json.RawMessage `json:"replace_paths,omitempty"`
}
// JsonImporting represents the import status of a resource instance in a plan.
type JsonImporting struct {
// True within a deferred instance
Unknown bool `json:"unknown"`
ID string `json:"id"`
GeneratedConfig string `json:"generated_config"`
}
func (s stackPlans) Read(ctx context.Context, stackPlanID string) (*StackPlan, error) {
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-plans/%s", url.PathEscape(stackPlanID)), nil)
if err != nil {
return nil, err
}
sp := &StackPlan{}
err = req.Do(ctx, sp)
if err != nil {
return nil, err
}
return sp, nil
}
func (s stackPlans) ListByConfiguration(ctx context.Context, stackConfigurationID string, options *StackPlansListOptions) (*StackPlanList, error) {
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-configurations/%s/stack-plans", url.PathEscape(stackConfigurationID)), options)
if err != nil {
return nil, err
}
sl := &StackPlanList{}
err = req.Do(ctx, sl)
if err != nil {
return nil, err
}
return sl, nil
}
func (s stackPlans) Approve(ctx context.Context, stackPlanID string) error {
req, err := s.client.NewRequest("POST", fmt.Sprintf("stack-plans/%s/approve", url.PathEscape(stackPlanID)), nil)
if err != nil {
return err
}
return req.Do(ctx, nil)
}
func (s stackPlans) Discard(ctx context.Context, stackPlanID string) error {
req, err := s.client.NewRequest("POST", fmt.Sprintf("stack-plans/%s/discard", url.PathEscape(stackPlanID)), nil)
if err != nil {
return err
}
return req.Do(ctx, nil)
}
func (s stackPlans) Cancel(ctx context.Context, stackPlanID string) error {
req, err := s.client.NewRequest("POST", fmt.Sprintf("stack-plans/%s/cancel", url.PathEscape(stackPlanID)), nil)
if err != nil {
return err
}
return req.Do(ctx, nil)
}
func (s stackPlans) PlanDescription(ctx context.Context, stackPlanID string) (*JSONChangeDesc, error) {
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-plans/%s/plan-description", url.PathEscape(stackPlanID)), nil)
if err != nil {
return nil, err
}
jd := &JSONChangeDesc{}
err = req.DoJSON(ctx, jd)
if err != nil {
return nil, err
}
return jd, nil
}