Skip to content

Commit b07f2ae

Browse files
authored
Merge pull request #1152 from hashicorp/TF-27398/includes-stack-deployment-runs
`Includes` option for stack deployment runs Read/List
2 parents b24090e + d52f5c3 commit b07f2ae

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
## Enhancements
4+
5+
* Adds option for `Includes` for `StackDeploymentRuns` Read/List, by @Maed223 [#1152](https://github.com/hashicorp/go-tfe/pull/1152)
6+
37
# v1.85.0
48

59
## Bug Fixes

stack_deployment_runs.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type StackDeploymentRuns interface {
1515
// List returns a list of stack deployment runs for a given deployment group.
1616
List(ctx context.Context, deploymentGroupID string, options *StackDeploymentRunListOptions) (*StackDeploymentRunList, error)
1717
Read(ctx context.Context, stackDeploymentRunID string) (*StackDeploymentRun, error)
18+
ReadWithOptions(ctx context.Context, stackDeploymentRunID string, options *StackDeploymentRunReadOptions) (*StackDeploymentRun, error)
1819
ApproveAllPlans(ctx context.Context, deploymentRunID string) error
1920
}
2021

@@ -36,15 +37,28 @@ type StackDeploymentRun struct {
3637
StackDeploymentGroup *StackDeploymentGroup `jsonapi:"relation,stack-deployment-group"`
3738
}
3839

40+
type SDRIncludeOpt string
41+
42+
const (
43+
SDRDeploymentGroup SDRIncludeOpt = "stack-deployment-group"
44+
)
45+
3946
// StackDeploymentRunList represents a list of stack deployment runs.
4047
type StackDeploymentRunList struct {
4148
*Pagination
4249
Items []*StackDeploymentRun
4350
}
4451

52+
type StackDeploymentRunReadOptions struct {
53+
// Optional: A list of relations to include.
54+
Include []SDRIncludeOpt `url:"include,omitempty"`
55+
}
56+
4557
// StackDeploymentRunListOptions represents the options for listing stack deployment runs.
4658
type StackDeploymentRunListOptions struct {
4759
ListOptions
60+
// Optional: A list of relations to include.
61+
Include []SDRIncludeOpt `url:"include,omitempty"`
4862
}
4963

5064
// List returns a list of stack deployment runs for a given deployment group.
@@ -78,6 +92,25 @@ func (s stackDeploymentRuns) Read(ctx context.Context, stackDeploymentRunID stri
7892
return &run, nil
7993
}
8094

95+
func (s stackDeploymentRuns) ReadWithOptions(ctx context.Context, stackDeploymentRunID string, options *StackDeploymentRunReadOptions) (*StackDeploymentRun, error) {
96+
if err := options.valid(); err != nil {
97+
return nil, err
98+
}
99+
100+
req, err := s.client.NewRequest("GET", fmt.Sprintf("stack-deployment-runs/%s", url.PathEscape(stackDeploymentRunID)), options)
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
run := StackDeploymentRun{}
106+
err = req.Do(ctx, &run)
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
return &run, nil
112+
}
113+
81114
func (s stackDeploymentRuns) ApproveAllPlans(ctx context.Context, stackDeploymentRunID string) error {
82115
req, err := s.client.NewRequest("POST", fmt.Sprintf("stack-deployment-runs/%s/approve-all-plans", url.PathEscape(stackDeploymentRunID)), nil)
83116
if err != nil {
@@ -86,3 +119,15 @@ func (s stackDeploymentRuns) ApproveAllPlans(ctx context.Context, stackDeploymen
86119

87120
return req.Do(ctx, nil)
88121
}
122+
123+
func (o *StackDeploymentRunReadOptions) valid() error {
124+
for _, include := range o.Include {
125+
switch include {
126+
case SDRDeploymentGroup:
127+
// Valid option, do nothing.
128+
default:
129+
return fmt.Errorf("invalid include option: %s", include)
130+
}
131+
}
132+
return nil
133+
}

stack_deployment_runs_integration_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ func TestStackDeploymentRunsList(t *testing.T) {
7171
require.NoError(t, err)
7272
assert.NotNil(t, runList)
7373
})
74+
75+
t.Run("With include option", func(t *testing.T) {
76+
t.Parallel()
77+
78+
runList, err := client.StackDeploymentRuns.List(ctx, deploymentGroupID, &StackDeploymentRunListOptions{
79+
Include: []SDRIncludeOpt{"stack-deployment-group"},
80+
})
81+
assert.NoError(t, err)
82+
assert.NotNil(t, runList)
83+
for _, run := range runList.Items {
84+
assert.NotNil(t, run.StackDeploymentGroup.ID)
85+
}
86+
})
87+
88+
t.Run("With invalid include option", func(t *testing.T) {
89+
t.Parallel()
90+
91+
_, err := client.StackDeploymentRuns.List(ctx, deploymentGroupID, &StackDeploymentRunListOptions{
92+
Include: []SDRIncludeOpt{"invalid-option"},
93+
})
94+
assert.Error(t, err)
95+
})
7496
}
7597

7698
func TestStackDeploymentRunsRead(t *testing.T) {
@@ -126,6 +148,20 @@ func TestStackDeploymentRunsRead(t *testing.T) {
126148
_, err := client.StackDeploymentRuns.Read(ctx, "")
127149
assert.Error(t, err)
128150
})
151+
t.Run("Read with options", func(t *testing.T) {
152+
run, err := client.StackDeploymentRuns.ReadWithOptions(ctx, sdr.ID, &StackDeploymentRunReadOptions{
153+
Include: []SDRIncludeOpt{"stack-deployment-group"},
154+
})
155+
assert.NoError(t, err)
156+
assert.NotNil(t, run)
157+
assert.NotNil(t, run.StackDeploymentGroup.ID)
158+
})
159+
t.Run("Read with invalid options", func(t *testing.T) {
160+
_, err := client.StackDeploymentRuns.ReadWithOptions(ctx, sdr.ID, &StackDeploymentRunReadOptions{
161+
Include: []SDRIncludeOpt{"invalid-option"},
162+
})
163+
assert.Error(t, err)
164+
})
129165
}
130166

131167
func TestStackDeploymentRunsApproveAllPlans(t *testing.T) {

0 commit comments

Comments
 (0)