-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding
provider::semvers::pick
function
The `pick` function takes a list of semver strings, and a constraint as a string (according to [docs][1]), and returns a list of matched semver strings. An empty list is returned if no matches found. [1]: https://github.com/Masterminds/semver/tree/master?tab=readme-ov-file#checking-version-constraints
- Loading branch information
Showing
15 changed files
with
286 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "pick function - semvers" | ||
subcategory: "" | ||
description: |- | ||
Returns semver from list of semvers according to contraint | ||
--- | ||
|
||
# function: pick | ||
|
||
Returns semver from list of semvers according to contraint | ||
|
||
|
||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
pick(versions list of string, constraint string) list of string | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `versions` (List of String) List of semver strigs | ||
1. `constraint` (String) Semver constraint |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// Copyright (c) Anastas Dancha | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
|
||
shelper "github.com/anapsix/terraform-provider-semvers/internal/helpers" | ||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
var ( | ||
_ function.Function = SemversPickFunction{} | ||
) | ||
|
||
func NewSemversPickFunction() function.Function { | ||
return SemversPickFunction{} | ||
} | ||
|
||
type SemversPickFunction struct{} | ||
|
||
func (r SemversPickFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "pick" | ||
} | ||
|
||
func (r SemversPickFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Returns semver from list of semvers according to contraint", | ||
MarkdownDescription: "Returns semver from list of semvers according to contraint", | ||
Parameters: []function.Parameter{ | ||
function.ListParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: false, | ||
ElementType: types.StringType, | ||
Name: "versions", | ||
MarkdownDescription: "List of semver strigs", | ||
}, | ||
function.StringParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: false, | ||
Name: "constraint", | ||
MarkdownDescription: "Semver constraint", | ||
}, | ||
}, | ||
Return: function.ListReturn{ | ||
ElementType: types.StringType, | ||
}, | ||
} | ||
} | ||
|
||
func (r SemversPickFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var versions []string | ||
var constraint string | ||
|
||
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &versions, &constraint)) | ||
if resp.Error != nil { | ||
return | ||
} | ||
|
||
filtered_semvers, err := shelper.PickFromSemverStrings(versions, constraint) | ||
|
||
if err != nil { | ||
tflog.Error(ctx, "Error in shelper.PickFromSemverStrings()") | ||
} | ||
|
||
if len(filtered_semvers) == 0 { | ||
empty_list := make([]string, 0) | ||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, empty_list)) | ||
} else { | ||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, filtered_semvers)) | ||
} | ||
} |
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,124 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// Copyright (c) Anastas Dancha | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
"github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestSemversPickFunction_Known(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_8_0), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
output "semvers_filtered" { | ||
value = provider::semvers::pick( | ||
["0.1.1-rc1+a231f59", "0.1.1", "0.1.10", "0.1.2-rc1", "0.2.1"], | ||
"~> 0.2" | ||
) | ||
} | ||
`, | ||
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownOutputValue( | ||
"semvers_filtered", | ||
knownvalue.ListExact([]knownvalue.Check{ | ||
knownvalue.StringExact("0.2.1"), | ||
}), | ||
), | ||
}, | ||
}, | ||
{ | ||
Config: ` | ||
output "semvers_filtered" { | ||
value = provider::semvers::pick( | ||
["0.1.0", "0.1.1-rc1+a231f59", "0.1.1", "0.1.10", "0.1.2-rc1", "0.2.1"], | ||
">= 0.1.1" | ||
) | ||
} | ||
`, | ||
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownOutputValue( | ||
"semvers_filtered", | ||
knownvalue.ListExact([]knownvalue.Check{ | ||
knownvalue.StringExact("0.1.1"), | ||
knownvalue.StringExact("0.1.10"), | ||
knownvalue.StringExact("0.2.1"), | ||
}), | ||
), | ||
}, | ||
}, | ||
{ | ||
Config: ` | ||
output "semvers_filtered" { | ||
value = provider::semvers::pick( | ||
["0.1.0", "0.1.1-rc1+a231f59", "0.1.1", "0.1.10", "0.1.2-rc1", "0.2.1"], | ||
">= 3.0" | ||
) | ||
} | ||
`, | ||
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownOutputValue( | ||
"semvers_filtered", | ||
knownvalue.ListSizeExact(0), | ||
), | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// func TPick_Null(t *testing.T) { | ||
// resource.UnitTest(t, resource.TestCase{ | ||
// TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
// tfversion.SkipBelow(tfversion.Version1_8_0), | ||
// }, | ||
// ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
// Steps: []resource.TestStep{ | ||
// { | ||
// Config: ` | ||
// output "test" { | ||
// value = provider::semvers::sort_semvers(null) | ||
// } | ||
// `, | ||
// // The parameter does not enable AllowNullValue | ||
// ExpectError: regexp.MustCompile(`argument must not be null`), | ||
// }, | ||
// }, | ||
// }) | ||
// } | ||
|
||
// func TPick_Unknown(t *testing.T) { | ||
// resource.UnitTest(t, resource.TestCase{ | ||
// TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
// tfversion.SkipBelow(tfversion.Version1_8_0), | ||
// }, | ||
// ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
// Steps: []resource.TestStep{ | ||
// { | ||
// Config: ` | ||
// resource "terraform_data" "test" { | ||
// input = "testvalue" | ||
// } | ||
|
||
// output "test" { | ||
// value = provider::semvers::sort_semvers(terraform_data.test.output) | ||
// } | ||
// `, | ||
// Check: resource.ComposeAggregateTestCheckFunc( | ||
// resource.TestCheckOutput("test", "testvalue"), | ||
// ), | ||
// }, | ||
// }, | ||
// }) | ||
// } |
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
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
Oops, something went wrong.