Skip to content

Commit a9b2933

Browse files
authored
Stack configuration list endpoint (#1138)
* commit * Update stack_configuration_integration_test.go * Update CHANGELOG.md * Update stack_configuration_integration_test.go * add relationships to stackconfig * Update stack_configuration_integration_test.go * update relationships
1 parent 9217ab1 commit a9b2933

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Enhancements
44

5+
* Adds BETA support for listing `StackConfigurationList`, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @shwetamurali [#1138](https://github.com/hashicorp/go-tfe/pull/1138)
56
* Adds BETA support for approving all plans for a stack deployment run, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @ctrombley [#1136](https://github.com/hashicorp/go-tfe/pull/1136)
67
* Adds BETA support for listing and reading `StackDeploymentSteps`, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @ctrombley [#1133](https://github.com/hashicorp/go-tfe/pull/1133)
78
* Adds BETA support for approving all plans in `StackDeploymentGroups`, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @hwatkins05-hashicorp [#1137](https://github.com/hashicorp/go-tfe/pull/1137)

stack.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ type StackConfiguration struct {
131131
ErrorMessage *string `jsonapi:"attr,error-message"`
132132
EventStreamURL string `jsonapi:"attr,event-stream-url"`
133133
Diagnostics []*StackDiagnostic `jsonapi:"attr,diags"`
134+
135+
Stack *Stack `jsonapi:"relation,stack"`
134136
}
135137

136138
// StackDeployment represents a stack deployment, specified by configuration

stack_configuration.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type StackConfigurations interface {
1818
// ReadConfiguration returns a stack configuration by its ID.
1919
Read(ctx context.Context, id string) (*StackConfiguration, error)
2020

21+
// ListStackConfigurations returns a list of stack configurations for a stack.
22+
List(ctx context.Context, stackID string, options *StackConfigurationListOptions) (*StackConfigurationList, error)
23+
2124
// JSONSchemas returns a byte slice of the JSON schema for the stack configuration.
2225
JSONSchemas(ctx context.Context, stackConfigurationID string) ([]byte, error)
2326

@@ -121,3 +124,33 @@ func (s stackConfigurations) AwaitStatus(ctx context.Context, stackConfiguration
121124
return stackConfiguration.Status, nil
122125
}, []string{status.String(), StackConfigurationStatusErrored.String(), StackConfigurationStatusCanceled.String()})
123126
}
127+
128+
// StackConfigurationList represents a paginated list of stack configurations.
129+
type StackConfigurationList struct {
130+
Pagination *Pagination
131+
Items []*StackConfiguration
132+
}
133+
134+
// StackConfigurationListOptions represents the options for listing stack configurations.
135+
type StackConfigurationListOptions struct {
136+
ListOptions
137+
}
138+
139+
func (s stackConfigurations) List(ctx context.Context, stackID string, options *StackConfigurationListOptions) (*StackConfigurationList, error) {
140+
if options == nil {
141+
options = &StackConfigurationListOptions{}
142+
}
143+
144+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stacks/%s/stack-configurations", url.PathEscape(stackID)), options)
145+
if err != nil {
146+
return nil, err
147+
}
148+
149+
result := &StackConfigurationList{}
150+
err = req.Do(ctx, result)
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
return result, nil
156+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfe
5+
6+
import (
7+
"context"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestStackConfigurationList(t *testing.T) {
16+
skipUnlessBeta(t)
17+
18+
client := testClient(t)
19+
ctx := context.Background()
20+
21+
orgTest, orgTestCleanup := createOrganization(t, client)
22+
t.Cleanup(orgTestCleanup)
23+
24+
oauthClient, cleanup := createOAuthClient(t, client, orgTest, nil)
25+
t.Cleanup(cleanup)
26+
27+
stack, err := client.Stacks.Create(ctx, StackCreateOptions{
28+
Name: "test-stack-list",
29+
VCSRepo: &StackVCSRepoOptions{
30+
Identifier: "hashicorp-guides/pet-nulls-stack",
31+
OAuthTokenID: oauthClient.OAuthTokens[0].ID,
32+
},
33+
Project: &Project{
34+
ID: orgTest.DefaultProject.ID,
35+
},
36+
})
37+
require.NoError(t, err)
38+
39+
// Trigger first stack configuration by updating configuration
40+
_, err = client.Stacks.UpdateConfiguration(ctx, stack.ID)
41+
require.NoError(t, err)
42+
43+
// Wait a bit and trigger second stack configuration
44+
time.Sleep(2 * time.Second)
45+
_, err = client.Stacks.UpdateConfiguration(ctx, stack.ID)
46+
require.NoError(t, err)
47+
48+
list, err := client.StackConfigurations.List(ctx, stack.ID, nil)
49+
require.NoError(t, err)
50+
require.NotNil(t, list)
51+
assert.Equal(t, len(list.Items), 2)
52+
53+
// Assert attributes for each configuration
54+
for _, cfg := range list.Items {
55+
require.NotEmpty(t, cfg.ID)
56+
require.NotEmpty(t, cfg.Status)
57+
require.GreaterOrEqual(t, cfg.SequenceNumber, 1)
58+
59+
require.NotNil(t, cfg.Stack)
60+
require.NotEmpty(t, cfg.Stack.ID)
61+
}
62+
63+
// Test with pagination options
64+
t.Run("with pagination options", func(t *testing.T) {
65+
options := &StackConfigurationListOptions{
66+
ListOptions: ListOptions{
67+
PageNumber: 1,
68+
PageSize: 10,
69+
},
70+
}
71+
72+
listWithOptions, err := client.StackConfigurations.List(ctx, stack.ID, options)
73+
require.NoError(t, err)
74+
require.NotNil(t, listWithOptions)
75+
assert.GreaterOrEqual(t, len(listWithOptions.Items), 2)
76+
77+
require.NotNil(t, listWithOptions.Pagination)
78+
assert.GreaterOrEqual(t, listWithOptions.Pagination.TotalCount, 2)
79+
})
80+
}

0 commit comments

Comments
 (0)