Skip to content

Commit

Permalink
feat: WorkspaceResources interface and List method
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kiss committed Nov 18, 2023
1 parent 976b812 commit 19f4398
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# UNRELEASED

## Enhancements
* Adds a missing interface `WorkspaceResources` and the `List` method by @stefan-kiss [Issue#754](https://github.com/hashicorp/go-tfe/issues/754)

## Bug Fixes
* 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)

Expand Down
1 change: 1 addition & 0 deletions generate_mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ mockgen -source=project.go -destination=mocks/project_mocks.go -package=mocks
mockgen -source=registry_no_code_module.go -destination=mocks/registry_no_code_module_mocks.go -package=mocks
mockgen -source=registry_module.go -destination=mocks/registry_module_mocks.go -package=mocks
mockgen -source=registry_module.go -destination=mocks/registry_module_mocks.go -package=mocks
mockgen -source=workspace_resources.go -destination=mocks/workspace_resources.go -package=mocks
51 changes: 51 additions & 0 deletions mocks/workspace_resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type Client struct {
VariableSets VariableSets
VariableSetVariables VariableSetVariables
Workspaces Workspaces
WorkspaceResources WorkspaceResources
WorkspaceRunTasks WorkspaceRunTasks
Projects Projects

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

client.Meta = Meta{
IPRanges: &ipRanges{client: client},
Expand Down
79 changes: 79 additions & 0 deletions workspace_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tfe

import (
"context"
"fmt"
"net/url"
)

// Compile-time proof of interface implementation.
var _ WorkspaceResources = (*workspaceResources)(nil)

// WorkspaceResources describes all the workspace resources related methods that the Terraform
// Enterprise API supports.
//
// TFE API docs: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/workspace-resources
type WorkspaceResources interface {
// List all the workspaces resources within a workspace
List(ctx context.Context, workspaceID string, options *WorkspaceResourceListOptions) (*WorkspaceResourcesList, error)
}

// workspaceResources implements WorkspaceResources.
type workspaceResources struct {
client *Client
}

// WorkspaceResourcesList represents a list of workspace resources.
type WorkspaceResourcesList struct {
*Pagination
Items []*WorkspaceResource
}

// WorkspaceResource represents a Terraform Enterprise workspace resource.
type WorkspaceResource struct {
ID string `jsonapi:"primary,resources"`
Address string `jsonapi:"attr,address"`
Name string `jsonapi:"attr,name"`
CreatedAt string `jsonapi:"attr,created-at"`
UpdatedAt string `jsonapi:"attr,updated-at"`
Module string `jsonapi:"attr,module"`
Provider string `jsonapi:"attr,provider"`
ProviderType string `jsonapi:"attr,provider-type"`
ModifiedByStateVersionID string `jsonapi:"attr,modified-by-state-version-id"`
NameIndex string `jsonapi:"attr,name-index"`
}

// WorkspaceResourceListOptions represents the options for listing workspace resources.
type WorkspaceResourceListOptions struct {
ListOptions
}

// List all the workspaces resources within a workspace
func (s *workspaceResources) List(ctx context.Context, workspaceID string, options *WorkspaceResourceListOptions) (*WorkspaceResourcesList, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidWorkspaceID
}
if err := options.valid(); err != nil {
return nil, err
}

u := fmt.Sprintf("workspaces/%s/resources", url.QueryEscape(workspaceID))
req, err := s.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}

wl := &WorkspaceResourcesList{}
err = req.Do(ctx, wl)
if err != nil {
return nil, err
}
return wl, nil
}

func (o *WorkspaceResourceListOptions) valid() error {
return nil
}
53 changes: 53 additions & 0 deletions workspace_resources_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tfe

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestWorkspaceResourcesList(t *testing.T) {
client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)

wTest, wTestCleanup := createWorkspace(t, client, orgTest)
t.Cleanup(wTestCleanup)

svTest, svTestCleanup := createStateVersion(t, client, 0, wTest)
t.Cleanup(svTestCleanup)

// give TFC some time to process the statefile and extract the outputs.
waitForSVOutputs(t, client, svTest.ID)

t.Run("without list options", func(t *testing.T) {
rs, err := client.WorkspaceResources.List(ctx, wTest.ID, nil)
require.NoError(t, err)
assert.Equal(t, 1, len(rs.Items))
assert.Equal(t, 1, rs.CurrentPage)
assert.Equal(t, 1, rs.TotalCount)

assert.Equal(t, "null_resource.test", rs.Items[0].Address)
assert.Equal(t, "test", rs.Items[0].Name)
assert.Equal(t, "root", rs.Items[0].Module)
assert.Equal(t, "null", rs.Items[0].Provider)
})
t.Run("with list options", func(t *testing.T) {
rs, err := client.WorkspaceResources.List(ctx, wTest.ID, &WorkspaceResourceListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, rs.Items)
assert.Equal(t, 999, rs.CurrentPage)
assert.Equal(t, 1, rs.TotalCount)
})
}

0 comments on commit 19f4398

Please sign in to comment.