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

Support for source-name and source-url on tfe_workspace #392

Closed
lucymhdavies opened this issue Nov 12, 2021 · 6 comments
Closed

Support for source-name and source-url on tfe_workspace #392

lucymhdavies opened this issue Nov 12, 2021 · 6 comments

Comments

@lucymhdavies
Copy link
Contributor

lucymhdavies commented Nov 12, 2021

Use-cases

source-name and source-url are currently listed as beta
https://www.terraform.io/docs/cloud/api/workspaces.html

These result in "Created via <SOURCE NAME>" to be displayed on the workspace.

Attempted Solutions

N/A as this is not yet implemented

Tempted to do a short-term workaround using a provisioner to update the workspace with a PATCH though:
https://www.terraform.io/docs/cloud/api/workspaces.html#update-a-workspace

Proposal

Add the source-name and source-url parameters to the tfe_workspace resource.

I appreciate we may not want to actually do this while those parameters are listed as "beta" though

lucymhdavies added a commit to hashi-strawb/demo-tfcb-seed that referenced this issue Nov 15, 2021
lucymhdavies added a commit to hashi-strawb/demo-tfcb-seed that referenced this issue Nov 15, 2021
@lucymhdavies
Copy link
Contributor Author

lucymhdavies commented Nov 15, 2021

Update on my workaround idea, for the curious...

I have been successful in using a curl provisioner as a workaround in the past, see: #383

However, in this case, source-name and source-url are only available on workspace creation.
Doesn't look like these attributes can be used when updating a workspace.

Ah well. It was at least worth checking, just in case.

@lucymhdavies
Copy link
Contributor Author

The TFE SDK does actually contain SourceName and SourceURL fields already:
https://github.com/hashicorp/go-tfe/blob/main/workspace.go#L129-L130

So I've added this:
main...lucymhdavies:source-name-url

Not had a chance to test it yet though

@lucymhdavies
Copy link
Contributor Author

With a source set...

terraform {
  required_providers {
    tfe = {
      source  = "localhost/providers/tfe"
      version = "0.0.1"
    }
  }
}

resource "tfe_workspace" "test" {
  name         = "test-source"
  organization = "lmhd"
  source_name  = "TFE Provider"
  source_url   = "https://github.com/hashicorp/terraform-provider-tfe/issues/392"
}

behaves as expected
image

$ terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
  + create

Terraform will perform the following actions:

  # tfe_workspace.test will be created
  + resource "tfe_workspace" "test" {
      + allow_destroy_plan            = true
      + auto_apply                    = false
      + execution_mode                = (known after apply)
      + file_triggers_enabled         = true
      + global_remote_state           = (known after apply)
      + id                            = (known after apply)
      + name                          = "test-source"
      + operations                    = (known after apply)
      + organization                  = "lmhd"
      + queue_all_runs                = true
      + remote_state_consumer_ids     = (known after apply)
      + source_name                   = "TFE Provider"
      + source_url                    = "https://github.com/hashicorp/terraform-provider-tfe/issues/392"
      + speculative_enabled           = true
      + structured_run_output_enabled = true
      + tag_names                     = (known after apply)
      + terraform_version             = (known after apply)
      + trigger_prefixes              = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

tfe_workspace.test: Creating...
tfe_workspace.test: Creation complete after 0s [id=ws-NmSpLA8E8KXE1ph1]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

@lucymhdavies
Copy link
Contributor Author

Updating the attributes, behaviour is as expected. Terraform destroys then recreates the workspace. This makes sense, as these source parameters can only be set at workspace creation.

$ terraform apply
tfe_workspace.test: Refreshing state... [id=ws-NxpxtDpKbpZTx5Wu]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # tfe_workspace.test must be replaced
-/+ resource "tfe_workspace" "test" {
      ~ execution_mode                = "remote" -> (known after apply)
      ~ global_remote_state           = false -> (known after apply)
      ~ id                            = "ws-NxpxtDpKbpZTx5Wu" -> (known after apply)
        name                          = "test-source"
      ~ operations                    = true -> (known after apply)
      ~ remote_state_consumer_ids     = [] -> (known after apply)
      ~ source_url                    = "https://github.com/hashicorp/terraform-provider-tfe/issues/392" -> "https://github.com/hashicorp/terraform-provider-tfe/issues" # forces replacement
      ~ tag_names                     = [] -> (known after apply)
      ~ terraform_version             = "1.2.3" -> (known after apply)
      ~ trigger_prefixes              = [] -> (known after apply)
        # (8 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

tfe_workspace.test: Destroying... [id=ws-NxpxtDpKbpZTx5Wu]
tfe_workspace.test: Destruction complete after 0s
tfe_workspace.test: Creating...
tfe_workspace.test: Creation complete after 1s [id=ws-btNn3eSGnbWHnj7j]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

However, when it comes to removing the attributes...

$ terraform apply
tfe_workspace.test: Refreshing state... [id=ws-NmSpLA8E8KXE1ph1]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # tfe_workspace.test must be replaced
-/+ resource "tfe_workspace" "test" {
      ~ execution_mode                = "remote" -> (known after apply)
      ~ global_remote_state           = false -> (known after apply)
      ~ id                            = "ws-NmSpLA8E8KXE1ph1" -> (known after apply)
        name                          = "test-source"
      ~ operations                    = true -> (known after apply)
      ~ remote_state_consumer_ids     = [] -> (known after apply)
      - source_name                   = "TFE Provider" -> null # forces replacement
      - source_url                    = "https://github.com/hashicorp/terraform-provider-tfe/issues/392" -> null # forces replacement
      ~ tag_names                     = [] -> (known after apply)
      ~ terraform_version             = "1.2.3" -> (known after apply)
      ~ trigger_prefixes              = [] -> (known after apply)
        # (7 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

tfe_workspace.test: Destroying... [id=ws-NmSpLA8E8KXE1ph1]
tfe_workspace.test: Destruction complete after 1s
tfe_workspace.test: Creating...
╷
│ Error: Error creating workspace test-source for organization lmhd: invalid attribute
│
│ Source name must have at least 1 character
│ invalid attribute
│
│ Source url has no hostname
│ invalid attribute
│
│ Source url does not have a valid protocol
│
│   with tfe_workspace.test,
│   on main.tf line 10, in resource "tfe_workspace" "test":
│   10: resource "tfe_workspace" "test" {
│
╵

TF tries to set the parameter regardless, and as such it fails validation.
So I'll need to add a check here to only set the parameters if they are not the empty string.

I don't have time to do that today, but it shouldn't be a big change.

@lucymhdavies
Copy link
Contributor Author

lucymhdavies commented Jun 16, 2022

Turns out I did, in fact, have time.

Fixed, and PR incoming (probably tomorrow on that one though).

lucymhdavies added a commit to lucymhdavies/terraform-provider-tfe that referenced this issue Jun 21, 2022
Addresses hashicorp#392

The TFE SDK does actually contain `SourceName` and `SourceURL` fields already:
https://github.com/hashicorp/go-tfe/blob/main/workspace.go#L129-L130

This is currently untested
@annawinkler
Copy link
Contributor

PR merged 🥳 & closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants