Skip to content

Commit e2310e5

Browse files
authored
Merge pull request #1181 from hashicorp/fix-deployment-group-itemss
Fix `StackDeploymentGroup` Rerun, add `ReadByName`
2 parents 8cde4d3 + 72fff5a commit e2310e5

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## Enhancements
44
* Add support for querying and filtering private registry modules based on a search query, `provider`, `registry_name` and `organization_name`, by @gautambaghel [#1179](https://github.com/hashicorp/go-tfe/pull/1179)
55
* Adds support for `RegistryModule` VCS source_directory and tag_prefix options, by @jillrami [#1154] (https://github.com/hashicorp/go-tfe/pull/1154)
6-
* Adds `GroupName` filter option for `StackDeploymentGroup` by @Maed223 [#1175](https://github.com/hashicorp/go-tfe/pull/1175)
76
* Adds endpoint for reruning a stack deployment by @hwatkins05-hashicorp/@Maed223 [#1176](https://github.com/hashicorp/go-tfe/pull/1176)
7+
* Adds `ReadByName` for `StackDeploymentGroup` by @Maed223 [#1181](https://github.com/hashicorp/go-tfe/pull/1181)
88

99

1010
## Bug Fixes

stack_deployment_groups.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"net/url"
10+
"strings"
1011
"time"
1112
)
1213

@@ -18,6 +19,9 @@ type StackDeploymentGroups interface {
1819
// Read retrieves a stack deployment group by its ID.
1920
Read(ctx context.Context, stackDeploymentGroupID string) (*StackDeploymentGroup, error)
2021

22+
// ReadByName retrieves a stack deployment group by its Name.
23+
ReadByName(ctx context.Context, stackConfigurationID, stackDeploymentName string) (*StackDeploymentGroup, error)
24+
2125
// ApproveAllPlans approves all pending plans in a stack deployment group.
2226
ApproveAllPlans(ctx context.Context, stackDeploymentGroupID string) error
2327

@@ -64,14 +68,12 @@ type StackDeploymentGroupList struct {
6468
// StackDeploymentGroupListOptions represents additional options when listing stack deployment groups.
6569
type StackDeploymentGroupListOptions struct {
6670
ListOptions
67-
// A query string used to filter by deployment group name.
68-
GroupName string `url:"group_name,omitempty"`
6971
}
7072

7173
// StackDeploymentGroupRerunOptions represents options for rerunning deployments in a stack deployment group.
7274
type StackDeploymentGroupRerunOptions struct {
7375
// Required query parameter: A list of deployment run IDs to rerun.
74-
Deployments []string `url:"deployments"`
76+
Deployments []string
7577
}
7678

7779
// List returns a list of Deployment Groups in a stack, optionally filtered by additional parameters.
@@ -80,12 +82,11 @@ func (s stackDeploymentGroups) List(ctx context.Context, stackConfigID string, o
8082
return nil, fmt.Errorf("invalid stack configuration ID: %s", stackConfigID)
8183
}
8284

83-
u := fmt.Sprintf("stack-configurations/%s/stack-deployment-groups/", url.PathEscape(stackConfigID))
84-
qp, err := decodeQueryParams(options)
85-
if err != nil {
86-
return nil, err
85+
if options == nil {
86+
options = &StackDeploymentGroupListOptions{}
8787
}
88-
req, err := s.client.NewRequestWithAdditionalQueryParams("GET", u, nil, qp)
88+
89+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-configurations/%s/stack-deployment-groups", url.PathEscape(stackConfigID)), options)
8990
if err != nil {
9091
return nil, err
9192
}
@@ -99,6 +100,29 @@ func (s stackDeploymentGroups) List(ctx context.Context, stackConfigID string, o
99100
return sdgl, nil
100101
}
101102

103+
// ReadByName retrieves a stack deployment group by its Name.
104+
func (s stackDeploymentGroups) ReadByName(ctx context.Context, stackConfigurationID, stackDeploymentName string) (*StackDeploymentGroup, error) {
105+
if !validStringID(&stackConfigurationID) {
106+
return nil, fmt.Errorf("invalid stack configuration id: %s", stackConfigurationID)
107+
}
108+
if !validStringID(&stackDeploymentName) {
109+
return nil, fmt.Errorf("invalid stack deployment group name: %s", stackDeploymentName)
110+
}
111+
112+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-configurations/%s/stack-deployment-groups/%s", url.PathEscape(stackConfigurationID), url.PathEscape(stackDeploymentName)), nil)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
sdg := &StackDeploymentGroup{}
118+
err = req.Do(ctx, sdg)
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
return sdg, nil
124+
}
125+
102126
// Read retrieves a stack deployment group by its ID.
103127
func (s stackDeploymentGroups) Read(ctx context.Context, stackDeploymentGroupID string) (*StackDeploymentGroup, error) {
104128
if !validStringID(&stackDeploymentGroupID) {
@@ -143,7 +167,19 @@ func (s stackDeploymentGroups) Rerun(ctx context.Context, stackDeploymentGroupID
143167
return fmt.Errorf("no deployments specified for rerun")
144168
}
145169

146-
req, err := s.client.NewRequest("POST", fmt.Sprintf("stack-deployment-groups/%s/rerun", url.PathEscape(stackDeploymentGroupID)), options)
170+
u := fmt.Sprintf("stack-deployment-groups/%s/rerun", url.PathEscape(stackDeploymentGroupID))
171+
172+
type DeploymentQueryParams struct {
173+
Deployments string `url:"deployments"`
174+
}
175+
176+
qp, err := decodeQueryParams(&DeploymentQueryParams{
177+
Deployments: strings.Join(options.Deployments, ","),
178+
})
179+
if err != nil {
180+
return err
181+
}
182+
req, err := s.client.NewRequestWithAdditionalQueryParams("POST", u, nil, qp)
147183
if err != nil {
148184
return err
149185
}

stack_deployment_groups_integration_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,6 @@ func TestStackDeploymentGroupsList(t *testing.T) {
7272
require.NotNil(t, sdgl)
7373
require.Len(t, sdgl.Items, 1)
7474
})
75-
76-
t.Run("List with filtering by group name", func(t *testing.T) {
77-
noOptionSdg, err := client.StackDeploymentGroups.List(ctx, stackUpdated.LatestStackConfiguration.ID, nil)
78-
require.NoError(t, err)
79-
require.NotNil(t, noOptionSdg)
80-
require.GreaterOrEqual(t, len(noOptionSdg.Items), 1)
81-
options := &StackDeploymentGroupListOptions{
82-
GroupName: noOptionSdg.Items[0].Name,
83-
}
84-
sdgl, err := client.StackDeploymentGroups.List(ctx, stackUpdated.LatestStackConfiguration.ID, options)
85-
require.NoError(t, err)
86-
require.NotNil(t, sdgl)
87-
require.Equal(t, 1, len(sdgl.Items))
88-
require.Equal(t, noOptionSdg.Items[0].Name, sdgl.Items[0].Name)
89-
})
9075
}
9176

9277
func TestStackDeploymentGroupsRead(t *testing.T) {

0 commit comments

Comments
 (0)