Skip to content

Commit eae164f

Browse files
authored
Merge pull request #1176 from hashicorp/TF-25667-milestone-3-go-tfe-deployment-group-rerun-endpoint
Rerun deployment group
2 parents 4a98dad + 26e2ddf commit eae164f

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## Enhancements
44
* Adds support for `RegistryModule` VCS source_directory and tag_prefix options, by @jillrami [#1154] (https://github.com/hashicorp/go-tfe/pull/1154)
55
* Adds `GroupName` filter option for `StackDeploymentGroup` by @Maed223 [#1175](https://github.com/hashicorp/go-tfe/pull/1175)
6+
* Adds endpoint for reruning a stack deployment by @hwatkins05-hashicorp/@Maed223 [#1176](https://github.com/hashicorp/go-tfe/pull/1176)
7+
68

79
## Bug Fixes
810
* Fixes issue [1061](https://github.com/hashicorp/go-tfe/issues/1061), validation accepts all RunStatus including `"cost_estimated"` by @KenCox-Hashicorp [#1171](https://github.com/hashicorp/go-tfe/pull/1171)

stack_deployment_groups.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type StackDeploymentGroups interface {
2020

2121
// ApproveAllPlans approves all pending plans in a stack deployment group.
2222
ApproveAllPlans(ctx context.Context, stackDeploymentGroupID string) error
23+
24+
// Rerun re-runs all the stack deployment runs in a deployment group.
25+
Rerun(ctx context.Context, stackDeploymentGroupID string, options *StackDeploymentGroupRerunOptions) error
2326
}
2427

2528
type DeploymentGroupStatus string
@@ -65,6 +68,12 @@ type StackDeploymentGroupListOptions struct {
6568
GroupName string `url:"group_name,omitempty"`
6669
}
6770

71+
// StackDeploymentGroupRerunOptions represents options for rerunning deployments in a stack deployment group.
72+
type StackDeploymentGroupRerunOptions struct {
73+
// Required query parameter: A list of deployment run IDs to rerun.
74+
Deployments []string `url:"deployments"`
75+
}
76+
6877
// List returns a list of Deployment Groups in a stack, optionally filtered by additional parameters.
6978
func (s stackDeploymentGroups) List(ctx context.Context, stackConfigID string, options *StackDeploymentGroupListOptions) (*StackDeploymentGroupList, error) {
7079
if !validStringID(&stackConfigID) {
@@ -123,3 +132,21 @@ func (s stackDeploymentGroups) ApproveAllPlans(ctx context.Context, stackDeploym
123132

124133
return req.Do(ctx, nil)
125134
}
135+
136+
// Rerun re-runs all the stack deployment runs in a deployment group.
137+
func (s stackDeploymentGroups) Rerun(ctx context.Context, stackDeploymentGroupID string, options *StackDeploymentGroupRerunOptions) error {
138+
if !validStringID(&stackDeploymentGroupID) {
139+
return fmt.Errorf("invalid stack deployment group ID: %s", stackDeploymentGroupID)
140+
}
141+
142+
if options == nil || len(options.Deployments) == 0 {
143+
return fmt.Errorf("no deployments specified for rerun")
144+
}
145+
146+
req, err := s.client.NewRequest("POST", fmt.Sprintf("stack-deployment-groups/%s/rerun", url.PathEscape(stackDeploymentGroupID)), options)
147+
if err != nil {
148+
return err
149+
}
150+
151+
return req.Do(ctx, nil)
152+
}

stack_deployment_groups_integration_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,73 @@ func TestStackDeploymentGroupsApproveAllPlans(t *testing.T) {
186186
require.NoError(t, err)
187187
})
188188
}
189+
190+
func TestStackDeploymentGroupsRerun(t *testing.T) {
191+
skipUnlessBeta(t)
192+
193+
client := testClient(t)
194+
ctx := context.Background()
195+
196+
orgTest, orgTestCleanup := createOrganization(t, client)
197+
t.Cleanup(orgTestCleanup)
198+
199+
oauthClient, cleanup := createOAuthClient(t, client, orgTest, nil)
200+
t.Cleanup(cleanup)
201+
202+
stack, err := client.Stacks.Create(ctx, StackCreateOptions{
203+
Name: "test-stack",
204+
VCSRepo: &StackVCSRepoOptions{
205+
Identifier: "hashicorp-guides/pet-nulls-stack",
206+
OAuthTokenID: oauthClient.OAuthTokens[0].ID,
207+
Branch: "main",
208+
},
209+
Project: &Project{
210+
ID: orgTest.DefaultProject.ID,
211+
},
212+
})
213+
require.NoError(t, err)
214+
require.NotNil(t, stack)
215+
216+
stackUpdated, err := client.Stacks.UpdateConfiguration(ctx, stack.ID)
217+
require.NoError(t, err)
218+
require.NotNil(t, stackUpdated)
219+
220+
stack = pollStackDeployments(t, ctx, client, stackUpdated.ID)
221+
require.NotNil(t, stack.LatestStackConfiguration)
222+
223+
deploymentGroups, err := client.StackDeploymentGroups.List(ctx, stack.LatestStackConfiguration.ID, nil)
224+
require.NoError(t, err)
225+
require.NotNil(t, deploymentGroups)
226+
require.NotEmpty(t, deploymentGroups.Items)
227+
228+
deploymentGroupID := deploymentGroups.Items[0].ID
229+
230+
deploymentRuns, err := client.StackDeploymentRuns.List(ctx, deploymentGroupID, nil)
231+
require.NoError(t, err)
232+
require.NotNil(t, deploymentRuns)
233+
require.NotEmpty(t, deploymentRuns.Items)
234+
235+
err = client.StackDeploymentGroups.ApproveAllPlans(ctx, deploymentGroupID)
236+
require.NoError(t, err)
237+
238+
pollStackDeploymentRunForDeployingStatus(t, ctx, client, deploymentRuns.Items[0].ID)
239+
240+
deploymentRunIds := []string{deploymentRuns.Items[0].ID}
241+
for _, dr := range deploymentRuns.Items {
242+
deploymentRunIds = append(deploymentRunIds, dr.ID)
243+
}
244+
245+
t.Run("No deployments specified for rerun", func(t *testing.T) {
246+
err := client.StackDeploymentGroups.Rerun(ctx, deploymentGroupID, nil)
247+
require.Error(t, err)
248+
assert.Contains(t, err.Error(), "no deployments specified for rerun")
249+
})
250+
251+
t.Run("Rerun with invalid ID", func(t *testing.T) {
252+
err := client.StackDeploymentGroups.Rerun(ctx, "", &StackDeploymentGroupRerunOptions{
253+
Deployments: deploymentRunIds,
254+
})
255+
require.Error(t, err)
256+
assert.Contains(t, err.Error(), "invalid stack deployment group ID")
257+
})
258+
}

0 commit comments

Comments
 (0)