Skip to content

Commit

Permalink
tfe_workspace: add structured_run_output_enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
radditude committed Jun 23, 2021
1 parent c2fa147 commit 72381ae
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 54 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/go-querystring v1.1.0 // 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.15.0
github.com/hashicorp/go-tfe v0.17.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.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -174,8 +174,8 @@ github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoD
github.com/hashicorp/go-slug v0.4.1/go.mod h1:I5tq5Lv0E2xcNXNkmx7BSfzi1PsJ2cNjs3cC3LwyhK8=
github.com/hashicorp/go-slug v0.7.0 h1:8HIi6oreWPtnhpYd8lIGQBgp4rXzDWQTOhfILZm+nok=
github.com/hashicorp/go-slug v0.7.0/go.mod h1:Ib+IWBYfEfJGI1ZyXMGNbu2BU+aa3Dzu41RKLH301v4=
github.com/hashicorp/go-tfe v0.15.0 h1:vdnz1NjOhvmap+cj8iPsL8SbS4iFFVuNYFkGpF5SdoA=
github.com/hashicorp/go-tfe v0.15.0/go.mod h1:c8glB5p6XzocEWLNkuy5RxcjqN5X2PpY6NF3f2W6nIo=
github.com/hashicorp/go-tfe v0.17.0 h1:COB2c5MeQU/NjR8EQFfUrcF7b3ZX7kogc59A/eu8zX0=
github.com/hashicorp/go-tfe v0.17.0/go.mod h1:c8glB5p6XzocEWLNkuy5RxcjqN5X2PpY6NF3f2W6nIo=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down
4 changes: 4 additions & 0 deletions tfe/client_mock.go
Expand Up @@ -53,6 +53,10 @@ func (m *mockWorkspaces) Read(ctx context.Context, organization string, workspac
return w, nil
}

func (m *mockWorkspaces) ReadByIDWithOptions(ctx context.Context, workspaceID string, options *tfe.WorkspaceReadOptions) (*tfe.Workspace, error) {
panic("not implemented")
}

func (m *mockWorkspaces) Readme(ctx context.Context, workspaceID string) (io.Reader, error) {
panic("not implemented")
}
Expand Down
54 changes: 30 additions & 24 deletions tfe/data_source_workspace.go
Expand Up @@ -59,11 +59,31 @@ func dataSourceTFEWorkspace() *schema.Resource {
Computed: true,
},

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

"queue_all_runs": {
Type: schema.TypeBool,
Computed: true,
},

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

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

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

"speculative_enabled": {
Type: schema.TypeBool,
Computed: true,
Expand All @@ -74,6 +94,11 @@ func dataSourceTFEWorkspace() *schema.Resource {
Computed: true,
},

"structured_run_output_enabled": {
Type: schema.TypeBool,
Computed: true,
},

"terraform_version": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -117,26 +142,6 @@ 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,
},
},
}
}
Expand Down Expand Up @@ -170,15 +175,16 @@ func dataSourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error
d.Set("global_remote_state", globalRemoteState)
d.Set("remote_state_consumer_ids", remoteStateConsumerIDs)
d.Set("operations", workspace.Operations)
d.Set("policy_check_failures", workspace.PolicyCheckFailures)
d.Set("queue_all_runs", workspace.QueueAllRuns)
d.Set("resource_count", workspace.ResourceCount)
d.Set("run_failures", workspace.RunFailures)
d.Set("runs_count", workspace.RunsCount)
d.Set("speculative_enabled", workspace.SpeculativeEnabled)
d.Set("structured_run_output_enabled", workspace.StructuredRunOutputEnabled)
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)

if workspace.SSHKey != nil {
d.Set("ssh_key_id", workspace.SSHKey.ID)
Expand Down
15 changes: 10 additions & 5 deletions tfe/data_source_workspace_test.go
Expand Up @@ -33,10 +33,20 @@ func TestAccTFEWorkspaceDataSource_basic(t *testing.T) {
"data.tfe_workspace.foobar", "auto_apply", "true"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "file_triggers_enabled", "true"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "policy_check_failures", "0"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "queue_all_runs", "false"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "resource_count", "0"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "run_failures", "0"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "runs_count", "0"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "speculative_enabled", "true"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "structured_run_output_enabled", "true"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "terraform_version", "0.11.1"),
resource.TestCheckResourceAttr(
Expand All @@ -47,11 +57,6 @@ func TestAccTFEWorkspaceDataSource_basic(t *testing.T) {
"data.tfe_workspace.foobar", "trigger_prefixes.1", "/shared"),
resource.TestCheckResourceAttr(
"data.tfe_workspace.foobar", "working_directory", "terraform/test"),

resource.TestCheckResourceAttr("data.tfe_workspace.foobar", "resource_count", "0"),
resource.TestCheckResourceAttr("data.tfe_workspace.foobar", "policy_check_failures", "0"),
resource.TestCheckResourceAttr("data.tfe_workspace.foobar", "run_failures", "0"),
resource.TestCheckResourceAttr("data.tfe_workspace.foobar", "runs_count", "0"),
),
},
},
Expand Down
45 changes: 27 additions & 18 deletions tfe/resource_tfe_workspace.go
Expand Up @@ -139,6 +139,12 @@ func resourceTFEWorkspace() *schema.Resource {
Default: "",
},

"structured_run_output_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"terraform_version": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -200,14 +206,15 @@ func resourceTFEWorkspaceCreate(d *schema.ResourceData, meta interface{}) error

// Create a new options struct.
options := tfe.WorkspaceCreateOptions{
Name: tfe.String(name),
AllowDestroyPlan: tfe.Bool(d.Get("allow_destroy_plan").(bool)),
AutoApply: tfe.Bool(d.Get("auto_apply").(bool)),
Description: tfe.String(d.Get("description").(string)),
FileTriggersEnabled: tfe.Bool(d.Get("file_triggers_enabled").(bool)),
QueueAllRuns: tfe.Bool(d.Get("queue_all_runs").(bool)),
SpeculativeEnabled: tfe.Bool(d.Get("speculative_enabled").(bool)),
WorkingDirectory: tfe.String(d.Get("working_directory").(string)),
Name: tfe.String(name),
AllowDestroyPlan: tfe.Bool(d.Get("allow_destroy_plan").(bool)),
AutoApply: tfe.Bool(d.Get("auto_apply").(bool)),
Description: tfe.String(d.Get("description").(string)),
FileTriggersEnabled: tfe.Bool(d.Get("file_triggers_enabled").(bool)),
QueueAllRuns: tfe.Bool(d.Get("queue_all_runs").(bool)),
SpeculativeEnabled: tfe.Bool(d.Get("speculative_enabled").(bool)),
StructuredRunOutputEnabled: tfe.Bool(d.Get("structured_run_output_enabled").(bool)),
WorkingDirectory: tfe.String(d.Get("working_directory").(string)),
}

// Send global_remote_state if it's set; otherwise, let it be computed.
Expand Down Expand Up @@ -315,6 +322,7 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("execution_mode", workspace.ExecutionMode)
d.Set("queue_all_runs", workspace.QueueAllRuns)
d.Set("speculative_enabled", workspace.SpeculativeEnabled)
d.Set("structured_run_output_enabled", workspace.StructuredRunOutputEnabled)
d.Set("terraform_version", workspace.TerraformVersion)
d.Set("trigger_prefixes", workspace.TriggerPrefixes)
d.Set("working_directory", workspace.WorkingDirectory)
Expand Down Expand Up @@ -371,19 +379,20 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
d.HasChange("allow_destroy_plan") || d.HasChange("speculative_enabled") ||
d.HasChange("operations") || d.HasChange("execution_mode") ||
d.HasChange("description") || d.HasChange("agent_pool_id") ||
d.HasChange("global_remote_state") {
d.HasChange("global_remote_state") || d.HasChange("structured_run_output_enabled") {

// Create a new options struct.
options := tfe.WorkspaceUpdateOptions{
Name: tfe.String(d.Get("name").(string)),
AllowDestroyPlan: tfe.Bool(d.Get("allow_destroy_plan").(bool)),
AutoApply: tfe.Bool(d.Get("auto_apply").(bool)),
Description: tfe.String(d.Get("description").(string)),
FileTriggersEnabled: tfe.Bool(d.Get("file_triggers_enabled").(bool)),
GlobalRemoteState: tfe.Bool(d.Get("global_remote_state").(bool)),
QueueAllRuns: tfe.Bool(d.Get("queue_all_runs").(bool)),
SpeculativeEnabled: tfe.Bool(d.Get("speculative_enabled").(bool)),
WorkingDirectory: tfe.String(d.Get("working_directory").(string)),
Name: tfe.String(d.Get("name").(string)),
AllowDestroyPlan: tfe.Bool(d.Get("allow_destroy_plan").(bool)),
AutoApply: tfe.Bool(d.Get("auto_apply").(bool)),
Description: tfe.String(d.Get("description").(string)),
FileTriggersEnabled: tfe.Bool(d.Get("file_triggers_enabled").(bool)),
GlobalRemoteState: tfe.Bool(d.Get("global_remote_state").(bool)),
QueueAllRuns: tfe.Bool(d.Get("queue_all_runs").(bool)),
SpeculativeEnabled: tfe.Bool(d.Get("speculative_enabled").(bool)),
StructuredRunOutputEnabled: tfe.Bool(d.Get("structured_run_output_enabled").(bool)),
WorkingDirectory: tfe.String(d.Get("working_directory").(string)),
}

if d.HasChange("agent_pool_id") {
Expand Down
49 changes: 49 additions & 0 deletions tfe/resource_tfe_workspace_test.go
Expand Up @@ -46,6 +46,8 @@ func TestAccTFEWorkspace_basic(t *testing.T) {
"tfe_workspace.foobar", "queue_all_runs", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "speculative_enabled", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "structured_run_output_enabled", "true"),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "trigger_prefixes.#", "0"),
resource.TestCheckResourceAttr(
Expand Down Expand Up @@ -392,6 +394,38 @@ func TestAccTFEWorkspace_updateSpeculative(t *testing.T) {
})
}

func TestAccTFEWorkspace_updateStructuredRunOutput(t *testing.T) {
workspace := &tfe.Workspace{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "structured_run_output_enabled", "true"),
),
},

{
Config: testAccTFEWorkspace_updateStructuredRunOutput(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace),
resource.TestCheckResourceAttr(
"tfe_workspace.foobar", "structured_run_output_enabled", "false"),
),
},
},
})
}

func TestAccTFEWorkspace_updateVCSRepo(t *testing.T) {
workspace := &tfe.Workspace{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
Expand Down Expand Up @@ -1564,3 +1598,18 @@ resource "tfe_workspace" "foobar_two" {
organization = tfe_organization.foobar.id
}`, rInt)
}

func testAccTFEWorkspace_updateStructuredRunOutput(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_workspace" "foobar" {
name = "workspace-test"
organization = tfe_organization.foobar.id
auto_apply = true
structured_run_output_enabled = false
}`, rInt)
}
9 changes: 5 additions & 4 deletions website/docs/d/workspace.html.markdown
Expand Up @@ -39,19 +39,20 @@ In addition to all arguments above, the following attributes are exported:
* `global_remote_state` - (Optional) Whether the workspace should allow all workspaces in the organization to access its state data during runs. If false, then only specifically approved workspaces can access its state (determined by the `remote_state_consumer_ids` argument).
* `remote_state_consumer_ids` - (Optional) A set of workspace IDs that will be set as the remote state consumers for the given workspace. Cannot be used if `global_remote_state` is set to `true`.
* `operations` - Indicates whether the workspace is using remote execution mode. Set to `false` to switch execution mode to local. `true` by default.
* `policy_check_failures` - The number of policy check failures from the latest run.
* `queue_all_runs` - Indicates whether the workspace will automatically perform runs
in response to webhooks immediately after its creation. If `false`, an initial run must
be manually queued to enable future automatic runs.
* `resource_count` - The number of resources managed by the workspace.
* `run_failures` - The number of run failures on the workspace.
* `runs_count` - The number of runs on the workspace.
* `speculative_enabled` - Indicates whether this workspace allows speculative plans.
* `ssh_key_id` - The ID of an SSH key assigned to the workspace.
* `structured_run_output_enabled` - Indicated whether runs in this workspace use the enhanced output UI.
* `terraform_version` - The version of Terraform used for this workspace.
* `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
1 change: 1 addition & 0 deletions website/docs/r/workspace.html.markdown
Expand Up @@ -86,6 +86,7 @@ The following arguments are supported:
Enterprise instance from running plans on pull requests, which can improve
security if the VCS repository is public or includes untrusted contributors.
Defaults to `true`.
* `structured_run_output_enabled` - (Optional) Whether this workspace should show output from Terraform runs using the enhanced UI when available. Setting this to `false` ensures that all runs in this workspace will display their output as text logs. Defaults to `true`.
* `ssh_key_id` - (Optional) The ID of an SSH key to assign to the workspace.
* `terraform_version` - (Optional) The version of Terraform to use for this workspace. Defaults to
the latest available version.
Expand Down

0 comments on commit 72381ae

Please sign in to comment.