Skip to content

Commit bb0ae4f

Browse files
Merge pull request #1132 from hashicorp/hw/get-deployment-group
Added support for reading a `StackDeploymentGroup` by its ID
2 parents d445cae + 846c395 commit bb0ae4f

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Add support for HCP Terraform `/api/v2/workspaces/{external_id}/all-vars` API endpoint to fetch the list of all variables available to a workspace (include inherited variables from varsets) by @debrin-hc [#1105](https://github.com/hashicorp/go-tfe/pull/1105)
66
* Adds BETA support for listing `StackDeploymentGroups`, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @hwatkins05-hashicorp [#1128](https://github.com/hashicorp/go-tfe/pull/1128)
77
* Adds BETA support for removing/adding VCS backing for a Stack, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @Maed223 [#1131](https://github.com/hashicorp/go-tfe/pull/1131)
8+
* Adds BETA support for reading a `StackDeploymentGroup`, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @hwatkins05-hashicorp [#1132](https://github.com/hashicorp-go-tfe/pull/1132)
89

910
## Enhancements
1011

stack_deployment_groups.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
type StackDeploymentGroups interface {
1111
// List returns a list of Deployment Groups in a stack.
1212
List(ctx context.Context, stackConfigID string, options *StackDeploymentGroupListOptions) (*StackDeploymentGroupList, error)
13+
14+
// Read retrieves a stack deployment group by its ID.
15+
Read(ctx context.Context, stackDeploymentGroupID string) (*StackDeploymentGroup, error)
1316
}
1417

1518
type DeploymentGroupStatus string
@@ -75,3 +78,23 @@ func (s stackDeploymentGroups) List(ctx context.Context, stackConfigID string, o
7578

7679
return sdgl, nil
7780
}
81+
82+
// Read retrieves a stack deployment group by its ID.
83+
func (s stackDeploymentGroups) Read(ctx context.Context, stackDeploymentGroupID string) (*StackDeploymentGroup, error) {
84+
if !validStringID(&stackDeploymentGroupID) {
85+
return nil, fmt.Errorf("invalid stack deployment group ID: %s", stackDeploymentGroupID)
86+
}
87+
88+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-deployment-groups/%s", url.PathEscape(stackDeploymentGroupID)), nil)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
sdg := &StackDeploymentGroup{}
94+
err = req.Do(ctx, sdg)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
return sdg, nil
100+
}

stack_deployment_groups_integration_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,54 @@ func TestStackDeploymentGroupsList(t *testing.T) {
7373
require.Len(t, sdgl.Items, 1)
7474
})
7575
}
76+
77+
func TestStackDeploymentGroupsRead(t *testing.T) {
78+
skipUnlessBeta(t)
79+
client := testClient(t)
80+
ctx := context.Background()
81+
82+
orgTest, orgTestCleanup := createOrganization(t, client)
83+
t.Cleanup(orgTestCleanup)
84+
85+
oauthClient, cleanup := createOAuthClient(t, client, orgTest, nil)
86+
t.Cleanup(cleanup)
87+
88+
stack, err := client.Stacks.Create(ctx, StackCreateOptions{
89+
Name: "test-stack",
90+
VCSRepo: &StackVCSRepoOptions{
91+
Identifier: "hashicorp-guides/pet-nulls-stack",
92+
OAuthTokenID: oauthClient.OAuthTokens[0].ID,
93+
},
94+
Project: &Project{
95+
ID: orgTest.DefaultProject.ID,
96+
},
97+
})
98+
99+
require.NoError(t, err)
100+
require.NotNil(t, stack)
101+
102+
stackUpdated, err := client.Stacks.UpdateConfiguration(ctx, stack.ID)
103+
require.NoError(t, err)
104+
require.NotNil(t, stackUpdated)
105+
106+
stackUpdated = pollStackDeployments(t, ctx, client, stackUpdated.ID)
107+
require.NotNil(t, stackUpdated.LatestStackConfiguration)
108+
109+
sdgl, err := client.StackDeploymentGroups.List(ctx, stackUpdated.LatestStackConfiguration.ID, nil)
110+
require.NoError(t, err)
111+
require.NotNil(t, sdgl)
112+
require.Len(t, sdgl.Items, 2)
113+
114+
t.Run("Read with valid ID", func(t *testing.T) {
115+
sdgRead, err := client.StackDeploymentGroups.Read(ctx, sdgl.Items[0].ID)
116+
require.NoError(t, err)
117+
assert.Equal(t, sdgl.Items[0].ID, sdgRead.ID)
118+
assert.Equal(t, sdgl.Items[0].Name, sdgRead.Name)
119+
assert.Equal(t, sdgl.Items[0].Status, sdgRead.Status)
120+
})
121+
122+
t.Run("Read with invalid ID", func(t *testing.T) {
123+
_, err := client.StackDeploymentGroups.Read(ctx, "")
124+
require.Error(t, err)
125+
})
126+
}

0 commit comments

Comments
 (0)