Skip to content

Commit

Permalink
adding constrained function
Browse files Browse the repository at this point in the history
  • Loading branch information
anapsix committed Dec 12, 2024
1 parent 83768eb commit 21535b2
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 10 deletions.
16 changes: 12 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Unreleased

## 0.6.0

### Added
- The `provider::semvers::constrained` function for checking semver constrains

### Changed
- updating google.golang.org/grpc v1.68.1 -> v1.69.0

## 0.5.5

### Changed
Expand Down Expand Up @@ -28,7 +36,7 @@
## 0.5.1

### Added
- The `provider::semvers::equals` function for checking equality of two semver
- The `provider::semvers::equals` function for checking equality of two semver

### Fixed
- spacing consistency
Expand All @@ -38,7 +46,7 @@
## 0.5.0

### Added
- The `provider::semvers::compare` function for comparing semver strings
- The `provider::semvers::compare` function for comparing semver strings
- Tests covering invalid values

## 0.4.2
Expand All @@ -54,7 +62,7 @@
## 0.4.0

### Added
- The `provider::semvers::pick` function which takes list of semver strings,
- The `provider::semvers::pick` function which takes list of semver strings,
and semver constraint, and returns a list of filtered semver strings, sorted and deduped,
matching the constraint. See [Masterminds/semver](https://github.com/Masterminds/semver/tree/master?tab=readme-ov-file#checking-version-constraints) for constraint syntax.

Expand All @@ -69,7 +77,7 @@
## 0.3.0

### Added
- The `provider::semvers::sort` function which takes list of semver strings,
- The `provider::semvers::sort` function which takes list of semver strings,
and returns a list of semver strings, sorted and deduped

## 0.2.1
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Implements a data-source `semvers_list` to make semver sorting easy in TF, and
provider functions (TF >= 1.8 is required):
- `data.semvers_list`: sorts a list of semver strings
- `provider::semvers::compare`: compares two semver strings
- `provider::semvers::constrained`: checks whether semver is within constrains
- `provider::semvers::equals`: checks two semver strings for equality
- `provider::semvers::pick`: filters a list of semver strings by constraint
- `provider::semvers::sort`: sorts a list of semver strings, returns sorted one
Expand Down
69 changes: 69 additions & 0 deletions docs/functions/constrained.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "constrained function - semvers"
subcategory: ""
description: |-
Checks whether semver is within constrain, returns a boolean
---

# function: constrained

Checks whether semver is within constrain, returns a boolean

## Example Usage

```terraform
terraform {
required_providers {
semvers = {
source = "anapsix/semvers"
}
}
}
provider "semvers" {}
output "semvers_constrained_results" {
value = [
{
expected = true
arguments = "0.1.1, >= 0.1"
result = provider::semvers::constrained("0.1.1", ">= 0.1")
},
{
expected = false
arguments = "0.1, >= 0.1.1"
result = provider::semvers::constrained("0.1", ">= 0.1.1")
},
{
expected = true
arguments = "0.1.2, ~> 0.1"
result = provider::semvers::constrained("0.1.2", "~> 0.1")
},
{
expected = false
arguments = "0.2, ~> 0.1"
result = provider::semvers::constrained("0.2", "~> 0.1")
},
{
expected = true
arguments = "0.2, 0.1.1 - 0.2.0"
result = provider::semvers::constrained("0.2", "0.1.1 - 0.2.0")
},
]
}
```

## Signature

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

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `version` (String) Semver string version used as base for comparison
1. `constraint` (String) Constraint string

4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "semvers Provider"
subcategory: ""
description: |-
Implements semvers_list data source, sort and pick provider function. All functionality is based on github.com/Masterminds/semver/v3. Usage of provider functions requires Terraform version 1.8 and above.
Implements semvers_list data source, compare, constrained, equals, sort and pick provider function. All functionality is based on github.com/Masterminds/semver/v3. Usage of provider functions requires Terraform version 1.8 and above.
---

# semvers Provider

Implements `semvers_list` data source, `sort` and `pick` provider function. All functionality is based on `github.com/Masterminds/semver/v3`. Usage of provider functions requires Terraform version 1.8 and above.
Implements `semvers_list` data source, `compare`, `constrained`, `equals`, `sort` and `pick` provider function. All functionality is based on `github.com/Masterminds/semver/v3`. Usage of provider functions requires Terraform version 1.8 and above.

## Example Usage

Expand Down
39 changes: 39 additions & 0 deletions examples/functions/constrained/function.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
terraform {
required_providers {
semvers = {
source = "anapsix/semvers"
}
}
}

provider "semvers" {}

output "semvers_constrained_results" {
value = [
{
expected = true
arguments = "0.1.1, >= 0.1"
result = provider::semvers::constrained("0.1.1", ">= 0.1")
},
{
expected = false
arguments = "0.1, >= 0.1.1"
result = provider::semvers::constrained("0.1", ">= 0.1.1")
},
{
expected = true
arguments = "0.1.2, ~> 0.1"
result = provider::semvers::constrained("0.1.2", "~> 0.1")
},
{
expected = false
arguments = "0.2, ~> 0.1"
result = provider::semvers::constrained("0.2", "~> 0.1")
},
{
expected = true
arguments = "0.2, 0.1.1 - 0.2.0"
result = provider::semvers::constrained("0.2", "0.1.1 - 0.2.0")
},
]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ require (
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
google.golang.org/grpc v1.68.1 // indirect
google.golang.org/grpc v1.69.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
18 changes: 16 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+
github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow=
github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys=
github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
Expand Down Expand Up @@ -206,6 +210,16 @@ github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
go.abhg.dev/goldmark/frontmatter v0.2.0 h1:P8kPG0YkL12+aYk2yU3xHv4tcXzeVnN+gU0tJ5JnxRw=
go.abhg.dev/goldmark/frontmatter v0.2.0/go.mod h1:XqrEkZuM57djk7zrlRUB02x8I5J0px76YjkOzhB4YlU=
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
Expand Down Expand Up @@ -266,8 +280,8 @@ google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAs
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 h1:8ZmaLZE4XWrtU3MyClkYqqtl6Oegr3235h7jxsDyqCY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU=
google.golang.org/grpc v1.68.1 h1:oI5oTa11+ng8r8XMMN7jAOmWfPZWbYpCFaMUTACxkM0=
google.golang.org/grpc v1.68.1/go.mod h1:+q1XYFJjShcqn0QZHvCyeR4CXPA+llXIeUIfIe00waw=
google.golang.org/grpc v1.69.0 h1:quSiOM1GJPmPH5XtU+BCoVXcDVJJAzNcoyfC2cCjGkI=
google.golang.org/grpc v1.69.0/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
Expand Down
14 changes: 14 additions & 0 deletions internal/helpers/shelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,17 @@ func PickFromSemverStrings(list []string, contraint string) ([]string, error) {

return semvers_filtered, nil
}

func Constrained(version string, constraint string) (bool, error) {
c, err := semver.NewConstraint(constraint)
if err != nil {
return false, err
}

v, err := semver.NewVersion(version)
if err != nil {
return false, err
}

return c.Check(v), nil
}
68 changes: 68 additions & 0 deletions internal/provider/function_constrained.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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"
)

var (
_ function.Function = SemversConstrainedFunction{}
)

func NewSemversConstrainedFunction() function.Function {
return SemversConstrainedFunction{}
}

type SemversConstrainedFunction struct{}

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

func (r SemversConstrainedFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Checks whether semver is within constrain, returns a boolean",
MarkdownDescription: "Checks whether semver is within constrain, returns a boolean",
Parameters: []function.Parameter{
function.StringParameter{
AllowNullValue: false,
AllowUnknownValues: false,
Name: "version",
MarkdownDescription: "Semver string version used as base for comparison",
},
function.StringParameter{
AllowNullValue: false,
AllowUnknownValues: false,
Name: "constraint",
MarkdownDescription: "Constraint string",
},
},
Return: function.BoolReturn{},
}
}

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

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

compare_results, err := shelper.Constrained(version, constraint)

if err != nil {
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError("Error performing operation: "+err.Error()))
return
}

resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, compare_results))
}
Loading

0 comments on commit 21535b2

Please sign in to comment.