Skip to content

Commit 4df1be7

Browse files
Implement endpoint for deployment-run list (#1134)
* initial commit * change * change * Update CHANGELOG.md * remove comments * Update stack_deployment_run.go * change file name * also change test file name --------- Co-authored-by: Chris Trombley <[email protected]>
1 parent 7d68fed commit 4df1be7

File tree

5 files changed

+149
-7
lines changed

5 files changed

+149
-7
lines changed

CHANGELOG.md

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

55
## Enhancements
66

7+
* Adds BETA support for listing `StackDeploymentRuns`, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @shwetamurali [#1134](https://github.com/hashicorp/go-tfe/pull/1134)
78
* 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)
89
* 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)
910
* 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)

stack_deployment_groups.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/url"
7+
"time"
78
)
89

910
// StackDeploymentGroups describes all the stack-deployment-groups related methods that the HCP Terraform API supports.
@@ -34,14 +35,14 @@ var _ StackDeploymentGroups = &stackDeploymentGroups{}
3435

3536
// StackDeploymentGroup represents a stack deployment group.
3637
type StackDeploymentGroup struct {
37-
ID string `jsonapi:"primary,stacks-deployment-groups"`
38-
Name string `jsonapi:"attr,name"`
39-
Status DeploymentGroupStatus `jsonapi:"attr,status"`
40-
CreatedAt string `jsonapi:"attr,created-at"`
41-
UpdatedAt string `jsonapi:"attr,updated-at"`
38+
ID string `jsonapi:"primary,stacks-deployment-groups"`
39+
Name string `jsonapi:"attr,name"`
40+
Status string `jsonapi:"attr,status"`
41+
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
42+
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
4243

4344
// Relationships
44-
StackConfiguration StackConfiguration `jsonapi:"relation,stack-configurations"`
45+
StackConfiguration *StackConfiguration `jsonapi:"relation,stack-configuration"`
4546
}
4647

4748
// StackDeploymentGroupList represents a list of stack deployment groups.

stack_deployment_runs.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfe
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"net/url"
10+
"time"
11+
)
12+
13+
// StackDeploymentRuns describes all the stack deployment runs-related methods that the HCP Terraform API supports.
14+
type StackDeploymentRuns interface {
15+
// List returns a list of stack deployment runs for a given deployment group.
16+
List(ctx context.Context, deploymentGroupID string, options *StackDeploymentRunListOptions) (*StackDeploymentRunList, error)
17+
}
18+
19+
// stackDeploymentRuns implements StackDeploymentRuns.
20+
type stackDeploymentRuns struct {
21+
client *Client
22+
}
23+
24+
var _ StackDeploymentRuns = &stackDeploymentRuns{}
25+
26+
// StackDeploymentRun represents a stack deployment run.
27+
type StackDeploymentRun struct {
28+
ID string `jsonapi:"primary,stacks-deployment-runs"`
29+
Status string `jsonapi:"attr,status"`
30+
StartedAt time.Time `jsonapi:"attr,started-at,iso8601"`
31+
CompletedAt time.Time `jsonapi:"attr,completed-at,iso8601"`
32+
33+
// Relationships
34+
StackDeploymentGroup *StackDeploymentGroup `jsonapi:"relation,stack-deployment-group"`
35+
}
36+
37+
// StackDeploymentRunList represents a list of stack deployment runs.
38+
type StackDeploymentRunList struct {
39+
*Pagination
40+
Items []*StackDeploymentRun
41+
}
42+
43+
// StackDeploymentRunListOptions represents the options for listing stack deployment runs.
44+
type StackDeploymentRunListOptions struct {
45+
ListOptions
46+
}
47+
48+
// List returns a list of stack deployment runs for a given deployment group.
49+
func (s *stackDeploymentRuns) List(ctx context.Context, deploymentGroupID string, options *StackDeploymentRunListOptions) (*StackDeploymentRunList, error) {
50+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-deployment-groups/%s/stack-deployment-runs", url.PathEscape(deploymentGroupID)), options)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
sdrl := &StackDeploymentRunList{}
56+
err = req.Do(ctx, sdrl)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return sdrl, nil
62+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfe
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestStackDeploymentRunList(t *testing.T) {
15+
skipUnlessBeta(t)
16+
17+
client := testClient(t)
18+
ctx := context.Background()
19+
20+
orgTest, orgTestCleanup := createOrganization(t, client)
21+
t.Cleanup(orgTestCleanup)
22+
23+
oauthClient, cleanup := createOAuthClient(t, client, orgTest, nil)
24+
t.Cleanup(cleanup)
25+
26+
stack, err := client.Stacks.Create(ctx, StackCreateOptions{
27+
Name: "test-stack",
28+
VCSRepo: &StackVCSRepoOptions{
29+
Identifier: "hashicorp-guides/pet-nulls-stack",
30+
OAuthTokenID: oauthClient.OAuthTokens[0].ID,
31+
Branch: "main",
32+
},
33+
Project: &Project{
34+
ID: orgTest.DefaultProject.ID,
35+
},
36+
})
37+
require.NoError(t, err)
38+
require.NotNil(t, stack)
39+
40+
stackUpdated, err := client.Stacks.UpdateConfiguration(ctx, stack.ID)
41+
require.NoError(t, err)
42+
require.NotNil(t, stackUpdated)
43+
44+
stack = pollStackDeployments(t, ctx, client, stackUpdated.ID)
45+
require.NotNil(t, stack.LatestStackConfiguration)
46+
47+
// Get the deployment group ID from the stack configuration
48+
deploymentGroups, err := client.StackDeploymentGroups.List(ctx, stack.LatestStackConfiguration.ID, nil)
49+
require.NoError(t, err)
50+
require.NotNil(t, deploymentGroups)
51+
require.NotEmpty(t, deploymentGroups.Items)
52+
deploymentGroupID := deploymentGroups.Items[0].ID
53+
54+
t.Run("List without options", func(t *testing.T) {
55+
t.Parallel()
56+
57+
runList, err := client.StackDeploymentRuns.List(ctx, deploymentGroupID, nil)
58+
require.NoError(t, err)
59+
assert.NotNil(t, runList)
60+
})
61+
62+
t.Run("List with pagination", func(t *testing.T) {
63+
t.Parallel()
64+
65+
runList, err := client.StackDeploymentRuns.List(ctx, deploymentGroupID, &StackDeploymentRunListOptions{
66+
ListOptions: ListOptions{
67+
PageNumber: 1,
68+
PageSize: 10,
69+
},
70+
})
71+
require.NoError(t, err)
72+
assert.NotNil(t, runList)
73+
})
74+
}

tfe.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ type Client struct {
190190
Projects Projects
191191

192192
Meta Meta
193+
194+
StackDeploymentRuns StackDeploymentRuns
193195
}
194196

195197
// Admin is the the Terraform Enterprise Admin API. It provides access to site
@@ -459,8 +461,8 @@ func NewClient(cfg *Config) (*Client, error) {
459461
client.AuditTrails = &auditTrails{client: client}
460462
client.Comments = &comments{client: client}
461463
client.ConfigurationVersions = &configurationVersions{client: client}
462-
client.GHAInstallations = &gHAInstallations{client: client}
463464
client.CostEstimates = &costEstimates{client: client}
465+
client.GHAInstallations = &gHAInstallations{client: client}
464466
client.GPGKeys = &gpgKeys{client: client}
465467
client.RegistryNoCodeModules = &registryNoCodeModules{client: client}
466468
client.NotificationConfigurations = &notificationConfigurations{client: client}
@@ -521,6 +523,8 @@ func NewClient(cfg *Config) (*Client, error) {
521523
IPRanges: &ipRanges{client: client},
522524
}
523525

526+
client.StackDeploymentRuns = &stackDeploymentRuns{client: client}
527+
524528
return client, nil
525529
}
526530

0 commit comments

Comments
 (0)