Skip to content

Commit 11c1eea

Browse files
committed
chore: update changelog
1 parent 60906a8 commit 11c1eea

8 files changed

+520
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Unreleased
22

33
## Enhancements
4-
* Exports the StackConfiguration UploadTarGzip receiver function[#1219](https://github.com/hashicorp/go-tfe/pull/1219)
4+
* Exports the StackConfiguration UploadTarGzip receiver function [#1219](https://github.com/hashicorp/go-tfe/pull/1219)
5+
* Updates BETA stacks resource schemas to match latest API spec by @ctrombley [#1220](https://github.com/hashicorp/go-tfe/pull/1220)
56

67
## Deprecations
78

stack.go

Lines changed: 114 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type Stacks interface {
2626
// Update updates a stack.
2727
Update(ctx context.Context, stackID string, options StackUpdateOptions) (*Stack, error)
2828

29+
// Update updates the agent pool in use by a stack.
30+
UpdateAgentPool(ctx context.Context, stackID string, agentPoolID string) (*Stack, error)
31+
2932
// Delete deletes a stack.
3033
Delete(ctx context.Context, stackID string) error
3134

@@ -34,7 +37,14 @@ type Stacks interface {
3437

3538
// FetchLatestFromVcs updates the configuration of a stack, triggering stack preparation.
3639
FetchLatestFromVcs(ctx context.Context, stackID string) (*Stack, error)
37-
}
40+
41+
// ListDeployments returns a list of deployments for the given stack.
42+
ListDeployments(ctx context.Context, stackID string, options *StackListDeploymentsOptions) (*StackDeploymentList, error)
43+
44+
// ListDeploymentRunsForDeployment returns a list of deployments for the given
45+
// stack and deployment.
46+
ListDeploymentRunsForDeployment(ctx context.Context, stackID string, deployment string, options *StackListDeploymentRunsForDeploymentOptions) (*StackDeploymentRunList, error)
47+
3848

3949
// stacks implements Stacks.
4050
type stacks struct {
@@ -86,6 +96,7 @@ type StackVCSRepoOptions struct {
8696
// Stack represents a stack.
8797
type Stack struct {
8898
ID string `jsonapi:"primary,stacks"`
99+
89100
Name string `jsonapi:"attr,name"`
90101
Description string `jsonapi:"attr,description"`
91102
VCSRepo *StackVCSRepo `jsonapi:"attr,vcs-repo"`
@@ -138,23 +149,6 @@ type StackConfiguration struct {
138149
IngressAttributes *IngressAttributes `jsonapi:"relation,ingress-attributes"`
139150
}
140151

141-
// StackState represents a stack state
142-
type StackState struct {
143-
// Attributes
144-
ID string `jsonapi:"primary,stack-states"`
145-
Description string `jsonapi:"attr,description"`
146-
Generation int `jsonapi:"attr,generation"`
147-
Status string `jsonapi:"attr,status"`
148-
Deployment string `jsonapi:"attr,deployment"`
149-
Components string `jsonapi:"attr,components"`
150-
IsCurrent bool `jsonapi:"attr,is-current"`
151-
ResourceInstanceCount int `jsonapi:"attr,resource-instance-count"`
152-
153-
// Relationships
154-
Stack *Stack `jsonapi:"relation,stack"`
155-
StackDeploymentRun *StackDeploymentRun `jsonapi:"relation,stack-deployment-run"`
156-
}
157-
158152
// StackListOptions represents the options for listing stacks.
159153
type StackListOptions struct {
160154
ListOptions
@@ -163,6 +157,10 @@ type StackListOptions struct {
163157
SearchByName string `url:"search[name],omitempty"`
164158
}
165159

160+
func (s *StackListOptions) valid() error {
161+
return nil
162+
}
163+
166164
// StackCreateOptions represents the options for creating a stack. The project
167165
// relation is required.
168166
type StackCreateOptions struct {
@@ -174,6 +172,18 @@ type StackCreateOptions struct {
174172
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`
175173
}
176174

175+
func (s StackCreateOptions) valid() error {
176+
if s.Name == "" {
177+
return ErrRequiredName
178+
}
179+
180+
if s.Project.ID == "" {
181+
return ErrRequiredProject
182+
}
183+
184+
return nil
185+
}
186+
177187
// StackUpdateOptions represents the options for updating a stack.
178188
type StackUpdateOptions struct {
179189
Name *string `jsonapi:"attr,name,omitempty"`
@@ -286,6 +296,27 @@ func (s stacks) Update(ctx context.Context, stackID string, options StackUpdateO
286296
return stack, nil
287297
}
288298

299+
// UpdateAgentPool updates the agent pool in use by a stack.
300+
func (s stacks) UpdateAgentPool(ctx context.Context, stackID string, agentPoolID string) (*Stack, error) {
301+
options := StackUpdateOptions{
302+
AgentPool: &AgentPool{
303+
ID: agentPoolID,
304+
},
305+
}
306+
307+
req, err := s.client.NewRequest("PATCH", fmt.Sprintf("stacks/%s", url.PathEscape(stackID)), &options)
308+
if err != nil {
309+
return nil, err
310+
}
311+
312+
stack := &Stack{}
313+
if err = req.Do(ctx, stack); err != nil {
314+
return nil, err
315+
}
316+
317+
return stack, nil
318+
}
319+
289320
// Delete deletes a stack.
290321
func (s stacks) Delete(ctx context.Context, stackID string) error {
291322
req, err := s.client.NewRequest("DELETE", fmt.Sprintf("stacks/%s", url.PathEscape(stackID)), nil)
@@ -306,22 +337,81 @@ func (s stacks) ForceDelete(ctx context.Context, stackID string) error {
306337
return req.Do(ctx, nil)
307338
}
308339

309-
func (s *StackListOptions) valid() error {
340+
// StackListDeploymentsOptions represents the options for listing stack
341+
// deployments.
342+
type StackListDeploymentsOptions struct {
343+
ListOptions
344+
}
345+
346+
// valid validates the StackListDeploymentsOptions values.
347+
func (s *StackListDeploymentsOptions) valid() error {
310348
return nil
311349
}
312350

313-
func (s StackCreateOptions) valid() error {
314-
if s.Name == "" {
315-
return ErrRequiredName
351+
// StackDeploymentList represents a list of stack deployments.
352+
type StackDeploymentList struct {
353+
*Pagination
354+
Items []*StackDeployment
355+
}
356+
357+
// StackDeployment represents a stack deployment.
358+
type StackDeployment struct {
359+
// Attributes
360+
ID string `jsonapi:"primary,stack-states"`
361+
Name string `jsonapi:"attr,name"`
362+
363+
// Relationships
364+
Stack *Stack `jsonapi:"relation,stack"`
365+
LatestDeploymentRun *StackDeploymentRun `jsonapi:"relation,latest-deployment-run"`
366+
}
367+
368+
// ListDeployments returns a list of deployments for the given stack.
369+
func (s stacks) ListDeployments(ctx context.Context, stackID string, options *StackListDeploymentsOptions) (*StackDeploymentList, error) {
370+
if err := options.valid(); err != nil {
371+
return nil, err
316372
}
317373

318-
if s.Project.ID == "" {
319-
return ErrRequiredProject
374+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stacks/%s/deployments", url.PathEscape(stackID)), options)
375+
if err != nil {
376+
return nil, err
320377
}
321378

379+
sdl := &StackDeploymentList{}
380+
if err = req.Do(ctx, sdl); err != nil {
381+
return nil, err
382+
}
383+
384+
return sdl, nil
385+
}
386+
387+
type StackListDeploymentRunsForDeploymentOptions struct {
388+
ListOptions
389+
}
390+
391+
func (s *StackListDeploymentRunsForDeploymentOptions) valid() error {
322392
return nil
323393
}
324394

395+
// ListDeploymentRunsForDeployment returns a list of deployment runs for the
396+
// given stack and deployment.
397+
func (s stacks) ListDeploymentRunsForDeployment(ctx context.Context, stackID, deployment string, options *StackListDeploymentRunsForDeploymentOptions) (*StackDeploymentRunList, error) {
398+
if err := options.valid(); err != nil {
399+
return nil, err
400+
}
401+
402+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stacks/%s/stack-deployments/%s/stack-deployment-runs", url.PathEscape(stackID), url.PathEscape(deployment)), options)
403+
if err != nil {
404+
return nil, err
405+
}
406+
407+
sdl := &StackDeploymentRunList{}
408+
if err := req.Do(ctx, sdl); err != nil {
409+
return nil, err
410+
}
411+
412+
return sdl, nil
413+
}
414+
325415
// awaitPoll is a helper function that uses a callback to read a status, then
326416
// waits for a terminal status or an error. The callback should return the
327417
// current status, or an error. For each time the status changes, the channel

stack_configuration.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ type StackConfigurations interface {
2424
// Upload a tar gzip archive to the specified stack configuration upload URL.
2525
UploadTarGzip(ctx context.Context, url string, archive io.Reader) error
2626

27-
// ReadConfiguration returns a stack configuration by its ID.
27+
// Read returns a stack configuration by its ID.
2828
Read(ctx context.Context, id string) (*StackConfiguration, error)
2929

30-
// ListStackConfigurations returns a list of stack configurations for a stack.
30+
// List returns a list of stack configurations for a stack.
3131
List(ctx context.Context, stackID string, options *StackConfigurationListOptions) (*StackConfigurationList, error)
3232

3333
// JSONSchemas returns a byte slice of the JSON schema for the stack configuration.
@@ -42,6 +42,11 @@ type StackConfigurations interface {
4242
// stack configuration as it progresses, until that status is "<status>",
4343
// "errored", "canceled".
4444
AwaitStatus(ctx context.Context, stackConfigurationID string, status StackConfigurationStatus) <-chan WaitForStatusResult
45+
46+
ListDeploymentRuns(ctx context.Context, stackConfigurationID string, options *StackDeploymentRunListOptions) (*StackDeploymentRunList, error)
47+
48+
// Diagnostics retrieves stack diagnostics for a given stack configuration.
49+
Diagnostics(ctx context.Context, stackConfigurationID string) ([]*StackDiagnostic, error)
4550
}
4651

4752
type StackConfigurationStatus string
@@ -204,6 +209,36 @@ func (s stackConfigurations) CreateAndUpload(ctx context.Context, stackID, path
204209
return sc, nil
205210
}
206211

212+
func (s stackConfigurations) ListDeploymentRuns(ctx context.Context, stackConfigurationID string, options *StackDeploymentRunListOptions) (*StackDeploymentRunList, error) {
213+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-configurations/%s/stack-deployment-runs", url.PathEscape(stackConfigurationID)), options)
214+
if err != nil {
215+
return nil, err
216+
}
217+
218+
sdrl := &StackDeploymentRunList{}
219+
err = req.Do(ctx, sdrl)
220+
if err != nil {
221+
return nil, err
222+
}
223+
224+
return sdrl, nil
225+
}
226+
227+
func (s stackConfigurations) Diagnostics(ctx context.Context, stackConfigurationID string) ([]*StackDiagnostic, error) {
228+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-configurations/%s/stack-diagnostics", url.PathEscape(stackConfigurationID)), nil)
229+
if err != nil {
230+
return nil, err
231+
}
232+
233+
var diagnostics []*StackDiagnostic
234+
err = req.Do(ctx, &diagnostics)
235+
if err != nil {
236+
return nil, err
237+
}
238+
239+
return diagnostics, nil
240+
}
241+
207242
// PollForUploadURL polls for the upload URL of a stack configuration until it becomes available.
208243
// It makes a request every 2 seconds until the upload URL is present in the response.
209244
// It will timeout after 10 seconds.

stack_configuration_integration_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,61 @@ func TestStackConfigurationCreateUploadAndRead(t *testing.T) {
122122
require.Fail(t, "timed out waiting for stack configuration to be processed")
123123
}
124124
}
125+
126+
func TestStackConfigurationListDeploymentRuns(t *testing.T) {
127+
skipUnlessBeta(t)
128+
129+
client := testClient(t)
130+
ctx := context.Background()
131+
132+
orgTest, orgTestCleanup := createOrganization(t, client)
133+
t.Cleanup(orgTestCleanup)
134+
135+
oauthClient, cleanup := createOAuthClient(t, client, orgTest, nil)
136+
t.Cleanup(cleanup)
137+
138+
stack, err := client.Stacks.Create(ctx, StackCreateOptions{
139+
Name: "test-stack-list",
140+
VCSRepo: &StackVCSRepoOptions{
141+
Identifier: "ctrombley/linked-stacks-demo-network",
142+
OAuthTokenID: oauthClient.OAuthTokens[0].ID,
143+
},
144+
Project: &Project{
145+
ID: orgTest.DefaultProject.ID,
146+
},
147+
})
148+
require.NoError(t, err)
149+
150+
// Trigger stack configuration with a fetch from VCS
151+
_, err = client.Stacks.FetchLatestFromVcs(ctx, stack.ID)
152+
require.NoError(t, err)
153+
154+
list, err := client.StackConfigurations.List(ctx, stack.ID, nil)
155+
require.NoError(t, err)
156+
require.NotNil(t, list)
157+
assert.Equal(t, len(list.Items), 1)
158+
159+
cfg := list.Items[0]
160+
161+
// List deployment runs for the configuration
162+
listRuns, err := client.StackConfigurations.ListDeploymentRuns(ctx, cfg.ID, nil)
163+
require.NoError(t, err)
164+
require.NotNil(t, listRuns)
165+
assert.Equal(t, len(listRuns.Items), 1)
166+
167+
// Test with pagination options
168+
t.Run("with pagination options", func(t *testing.T) {
169+
options := &StackDeploymentRunListOptions{
170+
ListOptions: ListOptions{
171+
PageNumber: 1,
172+
PageSize: 10,
173+
},
174+
}
175+
176+
// List deployment runs for the configuration
177+
listRuns, err := client.StackConfigurations.ListDeploymentRuns(ctx, cfg.ID, options)
178+
require.NoError(t, err)
179+
require.NotNil(t, listRuns)
180+
assert.Equal(t, len(listRuns.Items), 1)
181+
})
182+
}

stack_deployment_steps.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ type StackDeploymentSteps interface {
2121
Read(ctx context.Context, stackDeploymentStepID string) (*StackDeploymentStep, error)
2222
// Advance advances the stack deployment step when in the "pending_operator" state.
2323
Advance(ctx context.Context, stackDeploymentStepID string) error
24+
// Fail fails the deployment step.
25+
Fail(ctx context.Context, stackDeploymentStepID string) error
26+
// Diagnostics retrieves stack diagnostics for a given step.
27+
Diagnostics(ctx context.Context, stackDeploymentStepID string) ([]*StackDiagnostic, error)
28+
// Artifacts retrieves artifacts for a given step.
29+
Artifacts(ctx context.Context, stackDeploymentStepID string) ([]*StackDiagnostic, error)
2430
}
2531

2632
// StackDeploymentStep represents a step from a stack deployment
@@ -94,3 +100,43 @@ func (s stackDeploymentSteps) Advance(ctx context.Context, stackDeploymentStepID
94100

95101
return req.Do(ctx, nil)
96102
}
103+
104+
func (s stackDeploymentSteps) Fail(ctx context.Context, stackDeploymentStepID string) error {
105+
req, err := s.client.NewRequest("POST", fmt.Sprintf("stack-deployment-steps/%s/fail", url.PathEscape(stackDeploymentStepID)), nil)
106+
if err != nil {
107+
return err
108+
}
109+
110+
return req.Do(ctx, nil)
111+
}
112+
113+
func (s stackDeploymentSteps) Diagnostics(ctx context.Context, stackDeploymentStepID string) ([]*StackDiagnostic, error) {
114+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-deployment-steps/%s/stack-diagnostics", url.PathEscape(stackDeploymentStepID)), nil)
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
err = req.Do(ctx, nil)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
diagnostics := []*StackDiagnostic{}
125+
return diagnostics, nil
126+
}
127+
128+
// Artifacts retrieves artifacts for a given step.
129+
func (s stackDeploymentSteps) Artifacts(ctx context.Context, stackDeploymentStepID string) ([]*StackDiagnostic, error) {
130+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-deployment-steps/%s/artifacts", url.PathEscape(stackDeploymentStepID)), nil)
131+
if err != nil {
132+
return nil, err
133+
}
134+
135+
err = req.Do(ctx, nil)
136+
if err != nil {
137+
return nil, err
138+
}
139+
140+
artifacts := []*StackDiagnostic{}
141+
return artifacts, nil
142+
}

0 commit comments

Comments
 (0)