Skip to content

Commit bc90873

Browse files
authored
Merge pull request #823 from hashicorp/ISSUE-754-add-workspaceresources-interface
Issue 754 add workspaceresources interface
2 parents c990c76 + b3df155 commit bc90873

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# UNRELEASED
22

3+
## Enhancements
4+
* Adds a missing interface `WorkspaceResources` and the `List` method by @stefan-kiss [Issue#754](https://github.com/hashicorp/go-tfe/issues/754)
5+
36
## Bug Fixes
47
* Removed unused field `AgentPoolID` from the Workspace model. (Callers should be using the `AgentPool` relation instead) by @brandonc [#815](https://github.com/hashicorp/go-tfe/pull/815)
58

generate_mocks.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ mockgen -source=project.go -destination=mocks/project_mocks.go -package=mocks
7272
mockgen -source=registry_no_code_module.go -destination=mocks/registry_no_code_module_mocks.go -package=mocks
7373
mockgen -source=registry_module.go -destination=mocks/registry_module_mocks.go -package=mocks
7474
mockgen -source=registry_module.go -destination=mocks/registry_module_mocks.go -package=mocks
75+
mockgen -source=workspace_resources.go -destination=mocks/workspace_resources.go -package=mocks

mocks/workspace_resources.go

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tfe.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ type Client struct {
177177
VariableSets VariableSets
178178
VariableSetVariables VariableSetVariables
179179
Workspaces Workspaces
180+
WorkspaceResources WorkspaceResources
180181
WorkspaceRunTasks WorkspaceRunTasks
181182
Projects Projects
182183

@@ -473,6 +474,7 @@ func NewClient(cfg *Config) (*Client, error) {
473474
client.VariableSetVariables = &variableSetVariables{client: client}
474475
client.WorkspaceRunTasks = &workspaceRunTasks{client: client}
475476
client.Workspaces = &workspaces{client: client}
477+
client.WorkspaceResources = &workspaceResources{client: client}
476478

477479
client.Meta = Meta{
478480
IPRanges: &ipRanges{client: client},

workspace_resources.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
)
11+
12+
// Compile-time proof of interface implementation.
13+
var _ WorkspaceResources = (*workspaceResources)(nil)
14+
15+
// WorkspaceResources describes all the workspace resources related methods that the Terraform
16+
// Enterprise API supports.
17+
//
18+
// TFE API docs: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/workspace-resources
19+
type WorkspaceResources interface {
20+
// List all the workspaces resources within a workspace
21+
List(ctx context.Context, workspaceID string, options *WorkspaceResourceListOptions) (*WorkspaceResourcesList, error)
22+
}
23+
24+
// workspaceResources implements WorkspaceResources.
25+
type workspaceResources struct {
26+
client *Client
27+
}
28+
29+
// WorkspaceResourcesList represents a list of workspace resources.
30+
type WorkspaceResourcesList struct {
31+
*Pagination
32+
Items []*WorkspaceResource
33+
}
34+
35+
// WorkspaceResource represents a Terraform Enterprise workspace resource.
36+
type WorkspaceResource struct {
37+
ID string `jsonapi:"primary,resources"`
38+
Address string `jsonapi:"attr,address"`
39+
Name string `jsonapi:"attr,name"`
40+
CreatedAt string `jsonapi:"attr,created-at"`
41+
UpdatedAt string `jsonapi:"attr,updated-at"`
42+
Module string `jsonapi:"attr,module"`
43+
Provider string `jsonapi:"attr,provider"`
44+
ProviderType string `jsonapi:"attr,provider-type"`
45+
ModifiedByStateVersionID string `jsonapi:"attr,modified-by-state-version-id"`
46+
NameIndex *string `jsonapi:"attr,name-index"`
47+
}
48+
49+
// WorkspaceResourceListOptions represents the options for listing workspace resources.
50+
type WorkspaceResourceListOptions struct {
51+
ListOptions
52+
}
53+
54+
// List all the workspaces resources within a workspace
55+
func (s *workspaceResources) List(ctx context.Context, workspaceID string, options *WorkspaceResourceListOptions) (*WorkspaceResourcesList, error) {
56+
if !validStringID(&workspaceID) {
57+
return nil, ErrInvalidWorkspaceID
58+
}
59+
if err := options.valid(); err != nil {
60+
return nil, err
61+
}
62+
63+
u := fmt.Sprintf("workspaces/%s/resources", url.QueryEscape(workspaceID))
64+
req, err := s.client.NewRequest("GET", u, options)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
wl := &WorkspaceResourcesList{}
70+
err = req.Do(ctx, wl)
71+
if err != nil {
72+
return nil, err
73+
}
74+
return wl, nil
75+
}
76+
77+
func (o *WorkspaceResourceListOptions) valid() error {
78+
return nil
79+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfe
5+
6+
import (
7+
"context"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
"testing"
11+
)
12+
13+
func TestWorkspaceResourcesList(t *testing.T) {
14+
client := testClient(t)
15+
ctx := context.Background()
16+
17+
orgTest, orgTestCleanup := createOrganization(t, client)
18+
t.Cleanup(orgTestCleanup)
19+
20+
wTest, wTestCleanup := createWorkspace(t, client, orgTest)
21+
t.Cleanup(wTestCleanup)
22+
23+
svTest, svTestCleanup := createStateVersion(t, client, 0, wTest)
24+
t.Cleanup(svTestCleanup)
25+
26+
// give TFC some time to process the statefile and extract the outputs.
27+
waitForSVOutputs(t, client, svTest.ID)
28+
29+
t.Run("without list options", func(t *testing.T) {
30+
rs, err := client.WorkspaceResources.List(ctx, wTest.ID, nil)
31+
require.NoError(t, err)
32+
assert.Equal(t, 1, len(rs.Items))
33+
assert.Equal(t, 1, rs.CurrentPage)
34+
assert.Equal(t, 1, rs.TotalCount)
35+
36+
assert.Equal(t, "null_resource.test", rs.Items[0].Address)
37+
assert.Equal(t, "test", rs.Items[0].Name)
38+
assert.Equal(t, "root", rs.Items[0].Module)
39+
assert.Equal(t, "null", rs.Items[0].Provider)
40+
})
41+
t.Run("with list options", func(t *testing.T) {
42+
rs, err := client.WorkspaceResources.List(ctx, wTest.ID, &WorkspaceResourceListOptions{
43+
ListOptions: ListOptions{
44+
PageNumber: 999,
45+
PageSize: 100,
46+
},
47+
})
48+
require.NoError(t, err)
49+
assert.Empty(t, rs.Items)
50+
assert.Equal(t, 999, rs.CurrentPage)
51+
assert.Equal(t, 1, rs.TotalCount)
52+
})
53+
}

0 commit comments

Comments
 (0)