-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into Uk1288-v-1-40-0
- Loading branch information
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |