spacelift-io / terraform-provider-spacelift

Terraform provider to interact with Spacelift
MIT License
76 stars 29 forks source link

feat: add new tool versions datasource #580

Closed Apollorion closed 2 weeks ago

Apollorion commented 2 weeks ago

Description of the change

Introduce new tool versions datasource, will list out supported tool versions for KUBECTL, OPEN_TOFU, TERRAFORM_FOSS, and TERRAGRUNT.

data "spacelift_tool_versions" "test" {
    tool = "TERRAFORM_FOSS"
}

Type of change

Checklists

Development

Code review

Apollorion commented 2 weeks ago

Im not entirely sure why the test is failing, are these queries gated in some fashion that I cannot see? I cant imagine it, all these queries work even anonymously.

I thought this might have been a difference in the way the provider is authenticated to spacelift in CI vs my local, but the tests pass locally no matter how I authenticate (generated token vs api key and id).

Built the plugin and ran it with tofu:

$ tofu apply
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - spacelift.io/spacelift-io/spacelift in /Users/apollorion/projects/src/github.com/spacelift-io/terraform-provider-spacelift/dist/terraform-provider-spacelift_darwin_arm64
│
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
data.spacelift_tool_versions.kubectl: Reading...
data.spacelift_tool_versions.terragrunt: Reading...
data.spacelift_tool_versions.terraform: Reading...
data.spacelift_tool_versions.opentofu: Reading...
data.spacelift_tool_versions.terragrunt: Read complete after 0s [id=spacelift-versions]
data.spacelift_tool_versions.opentofu: Read complete after 1s [id=spacelift-versions]
data.spacelift_tool_versions.kubectl: Read complete after 1s [id=spacelift-versions]
data.spacelift_tool_versions.terraform: Read complete after 1s [id=spacelift-versions]

Changes to Outputs:
  + kubectl    = {
      + id       = "spacelift-versions"
      + tool     = "KUBECTL"
      + versions = [
          + "1.31.1",
          + "1.31.0",
          + "1.30.5",
          ...(edited for brevity)
        ]
    }
  + terraform  = {
      + id       = "spacelift-versions"
      + tool     = "TERRAFORM_FOSS"
      + versions = [
          + "1.5.7",
          + "1.5.6",
          + "1.5.5",
          ...(edited for brevity)
        ]
    }
  + terragrunt = {
      + id       = "spacelift-versions"
      + tool     = "TERRAGRUNT"
      + versions = [
          + "0.67.2",
          + "0.67.1",
          + "0.67.0",
          ...(edited for brevity)
        ]
    }
  + tofu       = {
      + id       = "spacelift-versions"
      + tool     = "OPEN_TOFU"
      + versions = [
          + "1.8.4",
          + "1.8.3",
          + "1.8.2",
          ...(edited for brevity)
        ]
    }

I can also run the tests with no problem locally:

$ go test ./... -run 'TestToolVersionsData'
?       github.com/spacelift-io/terraform-provider-spacelift    [no test files]
?       github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal [no test files]
?       github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs [no test files]
?       github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs/search  [no test files]
?       github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs/search/predicates   [no test files]
?       github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs/vcs [no test files]
?       github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/testhelpers [no test files]
?       github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/validations [no test files]
ok      github.com/spacelift-io/terraform-provider-spacelift/spacelift  32.917s

The error says Error running pre-apply refresh, but Im able to also run tofu refresh with no problems:

$ tofu refresh
data.spacelift_tool_versions.kubectl: Reading...
data.spacelift_tool_versions.opentofu: Reading...
data.spacelift_tool_versions.terragrunt: Reading...
data.spacelift_tool_versions.terraform: Reading...
data.spacelift_tool_versions.kubectl: Read complete after 0s [id=spacelift-versions]
data.spacelift_tool_versions.terragrunt: Read complete after 0s [id=spacelift-versions]
data.spacelift_tool_versions.terraform: Read complete after 0s [id=spacelift-versions]
data.spacelift_tool_versions.opentofu: Read complete after 0s [id=spacelift-versions]
╷
│ Warning: Empty or non-existent state
│
│ There are currently no remote objects tracked in the state, so there is nothing to refresh.
╵

I dont see what the difference is between CI and my local.

adamconnelly commented 2 weeks ago

@Apollorion the reason the tests are failing in CI is because of a bug in the Spacelift backend code. Here's the relevant error in the logs:

image

The problem is that the API key you'll have generated for local testing isn't quite the same as a Spacelift run token, and the GitHub Actions tests use a token that simulates a Spacelift run. I'll fix the backend issue just now.

Apollorion commented 2 weeks ago

Pushed up the ID change, once that backend fix is in place tests should pass 👍 @adamconnelly @peterdeme need reapproval :)