Skip to content

Commit d484ca0

Browse files
Merge pull request #1204 from hashicorp/TF-27756-go-tfe-list-stack-configuration-group-summaries
Added support for listing `StackConfigurationSummaries`
2 parents 7aad28f + ab400d5 commit d484ca0

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
* Fixes timestamp attribute mapping for deployment runs to use correct API field names (`created-at`/`updated-at` instead of `started-at`/`completed-at`) by @shwetamurali [#1199](https://github.com/hashicorp/go-tfe/pull/1199)
88

9+
## Enhancements
10+
11+
* Adds support for listing `StackConfigurationSummaries` by @hwatkins05-hashicorp [#1204](https://github.com/hashicorp/go-tfe/pull/1204)
12+
913
# v1.91.0
1014

1115
## Enhancements

stack_configuration_summary.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package tfe
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
)
8+
9+
type StackConfigurationSummaries interface {
10+
// List lists all the stack configuration summaries for a stack.
11+
List(ctx context.Context, stackID string, options *StackConfigurationSummaryListOptions) (*StackConfigurationSummaryList, error)
12+
}
13+
14+
type stackConfigurationSummaries struct {
15+
client *Client
16+
}
17+
18+
var _ StackConfigurationSummaries = &stackConfigurationSummaries{}
19+
20+
type StackConfigurationSummaryList struct {
21+
*Pagination
22+
Items []*StackConfigurationSummary
23+
}
24+
25+
type StackConfigurationSummaryListOptions struct {
26+
ListOptions
27+
}
28+
29+
type StackConfigurationSummary struct {
30+
ID string `jsonapi:"primary,stack-configuration-summaries"`
31+
Status string `jsonapi:"attr,status"`
32+
SequenceNumber int `jsonapi:"attr,sequence-number"`
33+
}
34+
35+
func (s stackConfigurationSummaries) List(ctx context.Context, stackID string, options *StackConfigurationSummaryListOptions) (*StackConfigurationSummaryList, error) {
36+
if !validStringID(&stackID) {
37+
return nil, fmt.Errorf("invalid stack ID: %s", stackID)
38+
}
39+
40+
if options == nil {
41+
options = &StackConfigurationSummaryListOptions{}
42+
}
43+
44+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stacks/%s/stack-configuration-summaries", url.PathEscape(stackID)), options)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
scl := &StackConfigurationSummaryList{}
50+
err = req.Do(ctx, scl)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
return scl, nil
56+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 TestStackConfigurationSummaryList(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: "aa-test-stack",
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+
require.NotNil(t, stack)
39+
stack2, err := client.Stacks.Create(ctx, StackCreateOptions{
40+
Name: "bb-test-stack",
41+
VCSRepo: &StackVCSRepoOptions{
42+
Identifier: "hashicorp-guides/pet-nulls-stack",
43+
OAuthTokenID: oauthClient.OAuthTokens[0].ID,
44+
},
45+
Project: &Project{
46+
ID: orgTest.DefaultProject.ID,
47+
},
48+
})
49+
require.NoError(t, err)
50+
require.NotNil(t, stack2)
51+
52+
// Trigger first stack configuration by updating configuration
53+
_, err = client.Stacks.UpdateConfiguration(ctx, stack2.ID)
54+
require.NoError(t, err)
55+
56+
// Wait a bit and trigger second stack configuration
57+
time.Sleep(2 * time.Second)
58+
_, err = client.Stacks.UpdateConfiguration(ctx, stack2.ID)
59+
require.NoError(t, err)
60+
61+
t.Run("Successful empty list", func(t *testing.T) {
62+
stackConfigSummaryList, err := client.StackConfigurationSummaries.List(ctx, stack.ID, nil)
63+
require.NoError(t, err)
64+
65+
assert.Len(t, stackConfigSummaryList.Items, 0)
66+
})
67+
68+
t.Run("Successful multiple config summary list", func(t *testing.T) {
69+
stackConfigSummaryList, err := client.StackConfigurationSummaries.List(ctx, stack2.ID, nil)
70+
require.NoError(t, err)
71+
72+
assert.Len(t, stackConfigSummaryList.Items, 2)
73+
})
74+
75+
t.Run("Unsuccessful list", func(t *testing.T) {
76+
_, err := client.StackConfigurationSummaries.List(ctx, "", nil)
77+
require.Error(t, err)
78+
})
79+
}

tfe.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ type Client struct {
166166
SSHKeys SSHKeys
167167
Stacks Stacks
168168
StackConfigurations StackConfigurations
169+
StackConfigurationSummaries StackConfigurationSummaries
169170
StackDeployments StackDeployments
170171
StackDeploymentGroups StackDeploymentGroups
171172
StackDeploymentRuns StackDeploymentRuns
@@ -500,6 +501,7 @@ func NewClient(cfg *Config) (*Client, error) {
500501
client.SSHKeys = &sshKeys{client: client}
501502
client.Stacks = &stacks{client: client}
502503
client.StackConfigurations = &stackConfigurations{client: client}
504+
client.StackConfigurationSummaries = &stackConfigurationSummaries{client: client}
503505
client.StackDeployments = &stackDeployments{client: client}
504506
client.StackDeploymentGroups = &stackDeploymentGroups{client: client}
505507
client.StackDeploymentRuns = &stackDeploymentRuns{client: client}

0 commit comments

Comments
 (0)