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

Workspace Source Name and URL #527

Merged
merged 14 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

FEATURES:
* **New Resource**: `tfe_workspace_variable_set` ([#537](https://github.com/hashicorp/terraform-provider-tfe/pull/537)) adds the ability to assign a variable set to a workspace in a single, flexible resource.
* r/workspace, d/workspace: Add `source_name` and `source_url` to workspaces ([#527](https://github.com/hashicorp/terraform-provider-tfe/pull/527))

DEPRECATION NOTICE: The `workspace_ids` argument on `tfe_variable_set` has been labelled as deprecated and should not be used in conjunction with `tfe_workspace_variable_set`.

Expand Down
12 changes: 12 additions & 0 deletions tfe/data_source_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ func dataSourceTFEWorkspace() *schema.Resource {
Computed: true,
},

"source_name": {
Type: schema.TypeString,
Computed: true,
},

"source_url": {
Type: schema.TypeString,
Computed: true,
},

"speculative_enabled": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -179,6 +189,8 @@ func dataSourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error
d.Set("resource_count", workspace.ResourceCount)
d.Set("run_failures", workspace.RunFailures)
d.Set("runs_count", workspace.RunsCount)
d.Set("source_name", workspace.SourceName)
d.Set("source_url", workspace.SourceURL)
d.Set("speculative_enabled", workspace.SpeculativeEnabled)
d.Set("structured_run_output_enabled", workspace.StructuredRunOutputEnabled)
d.Set("terraform_version", workspace.TerraformVersion)
Expand Down
24 changes: 24 additions & 0 deletions tfe/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ func resourceTFEWorkspace() *schema.Resource {
Default: true,
},

"source_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
RequiredWith: []string{"source_url"},
},

"source_url": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
RequiredWith: []string{"source_name"},
},

"speculative_enabled": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -245,6 +260,13 @@ func resourceTFEWorkspaceCreate(d *schema.ResourceData, meta interface{}) error
options.Operations = tfe.Bool(v.(bool))
}

if v, ok := d.GetOk("source_url"); ok {
options.SourceURL = tfe.String(v.(string))
}
if v, ok := d.GetOk("source_name"); ok {
options.SourceName = tfe.String(v.(string))
}

// Process all configured options.
if tfVersion, ok := d.GetOk("terraform_version"); ok {
options.TerraformVersion = tfe.String(tfVersion.(string))
Expand Down Expand Up @@ -341,6 +363,8 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("operations", workspace.Operations)
d.Set("execution_mode", workspace.ExecutionMode)
d.Set("queue_all_runs", workspace.QueueAllRuns)
d.Set("source_name", workspace.SourceName)
d.Set("source_url", workspace.SourceURL)
d.Set("speculative_enabled", workspace.SpeculativeEnabled)
d.Set("structured_run_output_enabled", workspace.StructuredRunOutputEnabled)
d.Set("terraform_version", workspace.TerraformVersion)
Expand Down
110 changes: 110 additions & 0 deletions tfe/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"math/rand"
"regexp"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -937,6 +938,60 @@ func TestAccTFEWorkspace_createWithRemoteStateConsumers(t *testing.T) {
})
}

func TestAccTFEWorkspace_createWithSourceURL(t *testing.T) {
sudomateo marked this conversation as resolved.
Show resolved Hide resolved
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
sudomateo marked this conversation as resolved.
Show resolved Hide resolved

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEWorkspace_basicWithSourceURL(rInt),
ExpectError: regexp.MustCompile(`Missing required argument`),
},
},
})
}

func TestAccTFEWorkspace_createWithSourceName(t *testing.T) {
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_basicWithSourceName(rInt),
ExpectError: regexp.MustCompile(`Missing required argument`),
},
},
})
}

func TestAccTFEWorkspace_createWithSourceURLAndName(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_basicWithSourceURLAndName(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEWorkspaceExists(
"tfe_workspace.foobar", workspace),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "source_url", "https://example.com"),
resource.TestCheckResourceAttr("tfe_workspace.foobar", "source_name", "Example Source"),
),
},
},
})
}

// Test pagination works for remote state consumers. Adding over 100 consumers should result in a
// subsequent empty plan if pagination works correctly. The client fetches the maximum results per
// page (100) by default.
Expand Down Expand Up @@ -1395,6 +1450,61 @@ resource "tfe_workspace" "foobar" {
}`, rInt)
}

func testAccTFEWorkspace_basicWithSourceURL(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
description = "My favorite workspace!"
allow_destroy_plan = false
auto_apply = true
tag_names = ["fav", "test"]
source_url = "https://example.com"
}`, rInt)
}

func testAccTFEWorkspace_basicWithSourceName(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
description = "My favorite workspace!"
allow_destroy_plan = false
auto_apply = true
tag_names = ["fav", "test"]
source_name = "Example Source"
}`, rInt)
}

func testAccTFEWorkspace_basicWithSourceURLAndName(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
description = "My favorite workspace!"
allow_destroy_plan = false
auto_apply = true
tag_names = ["fav", "test"]
source_url = "https://example.com"
source_name = "Example Source"
}`, rInt)
}

func testAccTFEWorkspace_operationsTrue(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ In addition to all arguments above, the following attributes are exported:
* `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.
* `source_name` - The name of the workspace creation source, if set.
* `source_url` - The URL of the workspace creation source, if set.
* `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` - Indicates whether runs in this workspace use the enhanced apply UI.
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ The following arguments are supported:
is `false`. The provider uses `true` as any workspace provisioned with
`false` would need to then have a run manually queued out-of-band before
accepting webhooks.
* `source_name` - (Optional) A friendly name for the application or client
creating this workspace. If set, this will be displayed on the workspace as
"Created via <SOURCE NAME>".
Requires `source_url` to also be set.
* `source_url` - (Optional) A URL for the application or client creating this
workspace. This can be the URL of a related resource in another app, or a
link to documentation or other info about the client.
Requires `source_name` to also be set.
**Note:** The API does not (currently) allow this to be updated after a
workspace has been created, so modifying this value will result in the
workspace being replaced.
* `speculative_enabled` - (Optional) Whether this workspace allows speculative
plans. Defaults to `true`. Setting this to `false` prevents Terraform Cloud
or the Terraform Enterprise instance from running plans on pull requests,
Expand Down