Skip to content
This repository was archived by the owner on Jun 12, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/functions/semver_check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
page_title: "semver_check function - terraform-provider-assert"
subcategory: "SemVer Functions"
description: |-
Check if a semver matches a constraint
---

# function: semver_check





## Variable Validation Example

```terraform
variable "version" {
type = string
validation {
condition = provider::assert::semver_check("~> 1.0", var.version)
error_message = "The provided version is not supported"
}
}
```

## Signature

<!-- signature generated by tfplugindocs -->
```text
semver_check(constraint string, semver string) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `constraint` (String) The constraint to check against
1. `semver` (String) The version to check


## Return Type

The return type of `semver_check` is a boolean.
41 changes: 41 additions & 0 deletions docs/functions/semver_constraint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
page_title: "semver_constraint function - terraform-provider-assert"
subcategory: "SemVer Functions"
description: |-
Check if a semver constraint is valid
---

# function: semver_constraint





## Variable Validation Example

```terraform
variable "version_constraint" {
type = string
validation {
condition = provider::assert::semver_constraint("~> 1.0", var.version_constraint)
error_message = "The provided version constraint is not valid"
}
}
```

## Signature

<!-- signature generated by tfplugindocs -->
```text
semver_constraint(constraint string) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `constraint` (String) The constraint to validate


## Return Type

The return type of `semver_constraint` is a boolean.
41 changes: 41 additions & 0 deletions docs/functions/semver_version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
page_title: "semver_version function - terraform-provider-assert"
subcategory: "SemVer Functions"
description: |-
Check if a semver version is valid
---

# function: semver_version





## Variable Validation Example

```terraform
variable "version" {
type = string
validation {
condition = provider::assert::semver_version(var.version)
error_message = "The provided version is not a valid SemVer version"
}
}
```

## Signature

<!-- signature generated by tfplugindocs -->
```text
semver_version(version string) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `version` (String) The version to validate


## Return Type

The return type of `semver_version` is a boolean.
7 changes: 7 additions & 0 deletions examples/functions/semver_check/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "version" {
type = string
validation {
condition = provider::assert::semver_check("~> 1.0", var.version)
error_message = "The provided version is not supported"
}
}
7 changes: 7 additions & 0 deletions examples/functions/semver_constraint/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "version_constraint" {
type = string
validation {
condition = provider::assert::semver_constraint("~> 1.0", var.version_constraint)
error_message = "The provided version constraint is not valid"
}
}
7 changes: 7 additions & 0 deletions examples/functions/semver_version/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "version" {
type = string
validation {
condition = provider::assert::semver_version(var.version)
error_message = "The provided version is not a valid SemVer version"
}
}
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (p *AssertProvider) Functions(ctx context.Context) []func() function.Functi
NewEmptyFunction,
NewNotEmptyFunction,
NewRegexMatchesFunction,
NewSemVerCheckFunction,
NewSemVerVersionFunction,
NewSemVerConstraintFunction,
}
}

Expand Down
69 changes: 69 additions & 0 deletions internal/provider/semver_check_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-framework/function"
)

var (
_ function.Function = SemVerCheckFunction{}
)

func NewSemVerCheckFunction() function.Function {
return SemVerCheckFunction{}
}

type SemVerCheckFunction struct{}

func (r SemVerCheckFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "semver_check"
}

func (r SemVerCheckFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Check if a semver matches a constraint",
Parameters: []function.Parameter{
function.StringParameter{
AllowNullValue: false,
AllowUnknownValues: false,
Description: "The constraint to check against",
Name: "constraint",
},
function.StringParameter{
AllowNullValue: false,
AllowUnknownValues: false,
Description: "The version to check",
Name: "semver",
},
},
Return: function.BoolReturn{},
}
}

func (r SemVerCheckFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var c, v string

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &c, &v))
if resp.Error != nil {
return
}

semver, err := version.NewVersion(v)
if err != nil {
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, false))
return
}

constraints, err := version.NewConstraint(c)
if err != nil {
resp.Error = function.NewFuncError(err.Error())
return
}

resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, constraints.Check(semver)))
}
78 changes: 78 additions & 0 deletions internal/provider/semver_check_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"testing"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestSemVerCheckFunction_trueCase(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::semver_check("~> 1.0", "1.1.5")
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestSemVerCheckFunction_falseCase(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::semver_check("< 1.0, < 1.2", "1.1.5")
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
},
})
}

func TestSemVerCheckFunction_invalidSemverCase(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::semver_check("< 1.0, < 1.2", "foobar")
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
},
})
}
52 changes: 52 additions & 0 deletions internal/provider/semver_constraint_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-framework/function"
)

var (
_ function.Function = SemVerConstraintFunction{}
)

func NewSemVerConstraintFunction() function.Function {
return SemVerConstraintFunction{}
}

type SemVerConstraintFunction struct{}

func (r SemVerConstraintFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "semver_constraint"
}

func (r SemVerConstraintFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Check if a semver constraint is valid",
Parameters: []function.Parameter{
function.StringParameter{
AllowNullValue: false,
AllowUnknownValues: false,
Description: "The constraint to validate",
Name: "constraint",
},
},
Return: function.BoolReturn{},
}
}

func (r SemVerConstraintFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var constraint string

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &constraint))
if resp.Error != nil {
return
}

_, err := version.NewConstraint(constraint)
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, err == nil))
}
Loading
Loading