-
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 490814f
Showing
7 changed files
with
301 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" | ||
} | ||
output "terraform" { | ||
value = data.spacelift_tool_versions.terraform.versions | ||
} | ||
data "spacelift_tool_versions" "open_tofu" { | ||
tool = "openTofu" | ||
} | ||
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 supported versions for. | ||
|
||
### 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" | ||
} | ||
|
||
output "terraform" { | ||
value = data.spacelift_tool_versions.terraform.versions | ||
} | ||
|
||
data "spacelift_tool_versions" "open_tofu" { | ||
tool = "openTofu" | ||
} | ||
|
||
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,95 @@ | ||
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 supported versions for.", | ||
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", | ||
"openTofu", | ||
"terraform", | ||
"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 { | ||
Versions []string `graphql:"kubectlVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "toolVersions", &query, map[string]interface{}{}); err != nil { | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.Versions) | ||
case "openTofu": | ||
var query struct { | ||
Versions []string `graphql:"openTofuVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "toolVersions", &query, map[string]interface{}{}); err != nil { | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.Versions) | ||
case "terraform": | ||
var query struct { | ||
Versions []string `graphql:"terraformVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "toolVersions", &query, map[string]interface{}{}); err != nil { | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.Versions) | ||
case "terragrunt": | ||
var query struct { | ||
Versions []string `graphql:"terragruntVersions"` | ||
} | ||
if err := meta.(*internal.Client).Query(ctx, "toolVersions", &query, map[string]interface{}{}); err != nil { | ||
return diag.Errorf("could not query for tool: %v", err) | ||
} | ||
d.Set("versions", query.Versions) | ||
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,96 @@ | ||
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) { | ||
t.Run("reads all tool versions", func(t *testing.T) { | ||
testSteps(t, []resource.TestStep{ | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "test" { | ||
tool = "kubectl" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.test", | ||
Attribute("id", Contains("spacelift-versions")), | ||
Attribute("tool", Equals("kubectl")), | ||
SetLengthGreaterThanZero("versions"), | ||
), | ||
//Check: func(state *terraform.State) error { | ||
// resource, ok := state.Modules[0].Resources["data.spacelift_tool_versions.test"] | ||
// if !ok { | ||
// return errors.Errorf("resource not found") | ||
// } | ||
// | ||
// fmt.Println(resource.Primary.Attributes) | ||
// | ||
// return nil | ||
//}, | ||
}, | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "test" { | ||
tool = "openTofu" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.test", | ||
Attribute("id", Contains("spacelift-versions")), | ||
Attribute("tool", Equals("openTofu")), | ||
SetLengthGreaterThanZero("versions"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "test" { | ||
tool = "terraform" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.test", | ||
Attribute("id", Contains("spacelift-versions")), | ||
Attribute("tool", Equals("terraform")), | ||
SetLengthGreaterThanZero("versions"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
data "spacelift_tool_versions" "test" { | ||
tool = "terragrunt" | ||
} | ||
`, | ||
Check: Resource( | ||
"data.spacelift_tool_versions.test", | ||
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
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