-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new tool versions datasource
- Loading branch information
1 parent
ab02d41
commit 4671ecd
Showing
6 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "spacelift_tool_versions Data Source - terraform-provider-spacelift" | ||
subcategory: "" | ||
description: |- | ||
Lists supported versions for a given tool. | ||
--- | ||
|
||
# spacelift_tool_versions (Data Source) | ||
|
||
Lists supported versions for a given tool. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "spacelift_tool_versions" "terraform" { | ||
tool = "TERRAFORM_FOSS" | ||
} | ||
output "terraform" { | ||
value = data.spacelift_tool_versions.terraform.versions | ||
} | ||
data "spacelift_tool_versions" "open_tofu" { | ||
tool = "OPEN_TOFU" | ||
} | ||
output "open_tofu" { | ||
value = data.spacelift_tool_versions.open_tofu.versions | ||
} | ||
data "spacelift_tool_versions" "kubectl" { | ||
tool = "KUBECTL" | ||
} | ||
output "kubectl" { | ||
value = data.spacelift_tool_versions.kubectl.versions | ||
} | ||
data "spacelift_tool_versions" "terragrunt" { | ||
tool = "TERRAGRUNT" | ||
} | ||
output "terragrunt" { | ||
value = data.spacelift_tool_versions.terragrunt.versions | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `tool` (String) The tool to get a list of supported versions for. This can be one of `KUBECTL`, `OPEN_TOFU`, `TERRAFORM_FOSS`, or `TERRAGRUNT`. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `versions` (List of String) Supported versions of the given tool. |
31 changes: 31 additions & 0 deletions
31
examples/data-sources/spacelift_tool_versions/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
data "spacelift_tool_versions" "terraform" { | ||
tool = "TERRAFORM_FOSS" | ||
} | ||
|
||
output "terraform" { | ||
value = data.spacelift_tool_versions.terraform.versions | ||
} | ||
|
||
data "spacelift_tool_versions" "open_tofu" { | ||
tool = "OPEN_TOFU" | ||
} | ||
|
||
output "open_tofu" { | ||
value = data.spacelift_tool_versions.open_tofu.versions | ||
} | ||
|
||
data "spacelift_tool_versions" "kubectl" { | ||
tool = "KUBECTL" | ||
} | ||
|
||
output "kubectl" { | ||
value = data.spacelift_tool_versions.kubectl.versions | ||
} | ||
|
||
data "spacelift_tool_versions" "terragrunt" { | ||
tool = "TERRAGRUNT" | ||
} | ||
|
||
output "terragrunt" { | ||
value = data.spacelift_tool_versions.terragrunt.versions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package spacelift | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
|
||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal" | ||
) | ||
|
||
func dataToolVersions() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Lists supported versions for a given tool.", | ||
ReadContext: dataToolVersionsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"tool": { | ||
Type: schema.TypeString, | ||
Description: "The tool to get a list of supported versions for. This can be one of `KUBECTL`, `OPEN_TOFU`, `TERRAFORM_FOSS`, or `TERRAGRUNT`.", | ||
ValidateDiagFunc: dataToolVersionsValidateInput, | ||
Required: true, | ||
}, | ||
"versions": { | ||
Type: schema.TypeList, | ||
Description: "Supported versions of the given tool.", | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataToolVersionsValidateInput(i interface{}, p cty.Path) diag.Diagnostics { | ||
tool := i.(string) | ||
|
||
validTools := []string{ | ||
"KUBECTL", | ||
"OPEN_TOFU", | ||
"TERRAFORM_FOSS", | ||
"TERRAGRUNT", | ||
} | ||
|
||
if !slices.Contains(validTools, tool) { | ||
return diag.Errorf("tool must be one of %v", validTools) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func dataToolVersionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
d.SetId("spacelift-versions") | ||
tool := d.Get("tool").(string) | ||
switch tool { | ||
case "KUBECTL": | ||
var query struct { | ||
KubectlVersions []string `graphql:"kubectlVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "ReadKubectlVersions", &query, nil); err != nil { | ||
d.SetId("") | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.KubectlVersions) | ||
case "OPEN_TOFU": | ||
var query struct { | ||
OpenTofuVersions []string `graphql:"openTofuVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "ReadOpenTofuVersions", &query, nil); err != nil { | ||
d.SetId("") | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.OpenTofuVersions) | ||
case "TERRAFORM_FOSS": | ||
var query struct { | ||
TerraformVersions []string `graphql:"terraformVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "ReadTerraformVersions", &query, nil); err != nil { | ||
d.SetId("") | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.TerraformVersions) | ||
case "TERRAGRUNT": | ||
var query struct { | ||
TerragruntVersions []string `graphql:"terragruntVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "ReadTerragruntVersions", &query, nil); err != nil { | ||
d.SetId("") | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.TerragruntVersions) | ||
default: | ||
return diag.Errorf("unsupported tool: %s", tool) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package spacelift | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
. "github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/testhelpers" | ||
) | ||
|
||
func TestToolVersionsData(t *testing.T) { | ||
testSteps(t, []resource.TestStep{ | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "kubectl" { | ||
tool = "KUBECTL" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.kubectl", | ||
Attribute("id", Contains("spacelift-versions")), | ||
Attribute("tool", Equals("KUBECTL")), | ||
SetLengthGreaterThanZero("versions"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "open_tofu" { | ||
tool = "OPEN_TOFU" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.open_tofu", | ||
Attribute("id", Contains("spacelift-versions")), | ||
Attribute("tool", Equals("OPEN_TOFU")), | ||
SetLengthGreaterThanZero("versions"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "terraform_foss" { | ||
tool = "TERRAFORM_FOSS" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.terraform_foss", | ||
Attribute("id", Contains("spacelift-versions")), | ||
Attribute("tool", Equals("TERRAFORM_FOSS")), | ||
SetLengthGreaterThanZero("versions"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "terragrunt" { | ||
tool = "TERRAGRUNT" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.terragrunt", | ||
Attribute("id", Contains("spacelift-versions")), | ||
Attribute("tool", Equals("TERRAGRUNT")), | ||
SetLengthGreaterThanZero("versions"), | ||
), | ||
}, | ||
}) | ||
|
||
t.Run("only allows specific tools", func(t *testing.T) { | ||
re, err := regexp.Compile(`tool must be one of \[.*]`) | ||
if err != nil { | ||
t.Fatalf("could not compile regexp: %v", err) | ||
} | ||
testSteps(t, []resource.TestStep{ | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "test" { | ||
tool = "this-tool-should-error" | ||
} | ||
`, | ||
ExpectError: re, | ||
}, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters