Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional workspace fields #277

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## (Unreleased)

ENHANCEMENTS:
* d/tfe_workspace: Added new fields from the API ([#277](https://github.com/hashicorp/terraform-provider-tfe/pull/277))

## 0.24.0 (January 22, 2021)

BREAKING CHANGES:
Expand Down Expand Up @@ -80,7 +85,7 @@ ENHANCEMENTS:
* r/tfe_policy_set: Added a validation for the `name` attribute so that invalid policy set names are caught at plan time ([#168](https://github.com/hashicorp/terraform-provider-tfe/pull/168))

NOTES:
* This validation matches the requirements specified by the [Terraform Cloud API](https://www.terraform.io/docs/cloud/api/policy-sets.html#request-body). Policy set names can only include letters, numbers, -, and _.
* This validation matches the requirements specified by the [Terraform Cloud API](https://www.terraform.io/docs/cloud/api/policy-sets.html#request-body). Policy set names can only include letters, numbers, -, and \_.

## 0.20.0 (July 17, 2020)

Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ module github.com/hashicorp/terraform-provider-tfe
go 1.14

require (
github.com/hashicorp/go-tfe v0.12.0
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/hashicorp/go-slug v0.7.0 // indirect
github.com/hashicorp/go-tfe v0.13.0
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk/v2 v2.3.0
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
)
82 changes: 12 additions & 70 deletions go.sum

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions tfe/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tfe

import (
"context"
"io"

tfe "github.com/hashicorp/go-tfe"
)
Expand All @@ -11,11 +12,13 @@ type workspaceNamesKey struct {
}

type mockWorkspaces struct {
workspaceID string
workspaceNames map[workspaceNamesKey]*tfe.Workspace
}

func newMockWorkspaces() *mockWorkspaces {
func newMockWorkspaces(workspaceID string) *mockWorkspaces {
return &mockWorkspaces{
workspaceID: workspaceID,
workspaceNames: make(map[workspaceNamesKey]*tfe.Workspace),
}
}
Expand All @@ -26,7 +29,7 @@ func (m *mockWorkspaces) List(ctx context.Context, organization string, options

func (m *mockWorkspaces) Create(ctx context.Context, organization string, options tfe.WorkspaceCreateOptions) (*tfe.Workspace, error) {
ws := &tfe.Workspace{
ID: options.ID,
ID: m.workspaceID,
Name: *options.Name,
Organization: &tfe.Organization{
Name: organization,
Expand All @@ -47,6 +50,10 @@ func (m *mockWorkspaces) Read(ctx context.Context, organization string, workspac
return w, nil
}

func (m *mockWorkspaces) Readme(ctx context.Context, workspaceID string) (io.Reader, error) {
panic("not implemented")
}

func (m *mockWorkspaces) ReadByID(ctx context.Context, workspaceID string) (*tfe.Workspace, error) {
panic("not implemented")
}
Expand Down
24 changes: 24 additions & 0 deletions tfe/data_source_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ func dataSourceTFEWorkspace() *schema.Resource {
},
},

"resource_count": {
Type: schema.TypeInt,
Computed: true,
},

"policy_check_failures": {
Type: schema.TypeInt,
Computed: true,
},

"run_failures": {
Type: schema.TypeInt,
Computed: true,
},

"runs_count": {
Type: schema.TypeInt,
Computed: true,
},

"external_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -133,6 +153,10 @@ func dataSourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error
d.Set("terraform_version", workspace.TerraformVersion)
d.Set("trigger_prefixes", workspace.TriggerPrefixes)
d.Set("working_directory", workspace.WorkingDirectory)
d.Set("resource_count", workspace.ResourceCount)
d.Set("policy_check_failures", workspace.PolicyCheckFailures)
d.Set("run_failures", workspace.RunFailures)
d.Set("runs_count", workspace.RunsCount)
// TODO: remove when external_id is removed
d.Set("external_id", workspace.ID)

Expand Down
4 changes: 4 additions & 0 deletions tfe/data_source_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func TestAccTFEWorkspaceDataSource_basic(t *testing.T) {
"data.tfe_workspace.foobar", "working_directory", "terraform/test"),

resource.TestCheckResourceAttrSet("data.tfe_workspace.foobar", "external_id"),
resource.TestCheckResourceAttrSet("data.tfe_workspace.foobar", "resource_count"),
resource.TestCheckResourceAttrSet("data.tfe_workspace.foobar", "policy_check_failures"),
resource.TestCheckResourceAttrSet("data.tfe_workspace.foobar", "run_failures"),
resource.TestCheckResourceAttrSet("data.tfe_workspace.foobar", "runs_count"),
),
},
},
Expand Down
3 changes: 1 addition & 2 deletions tfe/resource_tfe_team_access_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ func testResourceTfeTeamAccessStateDataV1() map[string]interface{} {
}

func TestResourceTfeTeamAccessStateUpgradeV0(t *testing.T) {
client := testTfeClient(t)
client := testTfeClient(t, "ws-123")
name := "a-workspace"
client.Workspaces.Create(nil, "hashicorp", tfe.WorkspaceCreateOptions{
ID: "ws-123",
Name: &name,
})

Expand Down
4 changes: 2 additions & 2 deletions tfe/resource_tfe_variable_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func testResourceTfeVariableStateDataV1() map[string]interface{} {
}

func TestResourceTfeVariableStateUpgradeV0(t *testing.T) {
client := testTfeClient(t)
client := testTfeClient(t, "ws-123")
name := "a-workspace"

client.Workspaces.Create(nil, "hashicorp", tfe.WorkspaceCreateOptions{
ID: "ws-123",
Name: &name,
})

Expand Down
6 changes: 4 additions & 2 deletions tfe/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
tfe "github.com/hashicorp/go-tfe"
)

func testTfeClient(t *testing.T) *tfe.Client {
// testTfeClient creates a mock client that creates workspaces with their ID
// set to workspaceID.
func testTfeClient(t *testing.T, workspaceID string) *tfe.Client {
config := &tfe.Config{
Token: "not-a-token",
}
Expand All @@ -16,7 +18,7 @@ func testTfeClient(t *testing.T) *tfe.Client {
t.Fatalf("error creating tfe client: %v", err)
}

client.Workspaces = newMockWorkspaces()
client.Workspaces = newMockWorkspaces(workspaceID)

return client
}
3 changes: 1 addition & 2 deletions tfe/workspace_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ func TestFetchWorkspaceExternalID(t *testing.T) {
},
}

client := testTfeClient(t)
client := testTfeClient(t, "ws-123")
name := "a-workspace"
client.Workspaces.Create(nil, "hashicorp", tfe.WorkspaceCreateOptions{
ID: "ws-123",
Name: &name,
})

Expand Down
5 changes: 5 additions & 0 deletions website/docs/d/workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ In addition to all arguments above, the following attributes are exported:
* `trigger_prefixes` - List of repository-root-relative paths which describe all locations to be tracked for changes.
* `vcs_repo` - Settings for the workspace's VCS repository.
* `working_directory` - A relative path that Terraform will execute within.
* `resource_count` - The number of resources managed by the workspace.
* `policy_check_failures` - The number of policy check failures from the latest run.
* `run_failures` - The number of run failures on the workspace.
* `runs_count` - The number of runs on the workspace.


The `vcs_repo` block contains:

Expand Down