generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add negative and positive functions
- Loading branch information
1 parent
baaf5d8
commit c9d3b75
Showing
11 changed files
with
488 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,39 @@ | ||
--- | ||
page_title: "negative function - terraform-provider-assert" | ||
subcategory: "Numeric Functions" | ||
description: |- | ||
Checks whether a number is negative | ||
--- | ||
|
||
# function: negative | ||
|
||
|
||
|
||
## Variable Validation Example | ||
|
||
```terraform | ||
variable "example" { | ||
type = number | ||
validation { | ||
condition = provider::assert::negative(var.example) | ||
error_message = "Expected ${var.example} to be negative" | ||
} | ||
} | ||
``` | ||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
negative(number number) bool | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `number` (Number) The number to check | ||
|
||
|
||
## Return Type | ||
|
||
The return type of `negative` is a boolean. |
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,39 @@ | ||
--- | ||
page_title: "positive function - terraform-provider-assert" | ||
subcategory: "Numeric Functions" | ||
description: |- | ||
Checks whether a number is positive | ||
--- | ||
|
||
# function: positive | ||
|
||
|
||
|
||
## Variable Validation Example | ||
|
||
```terraform | ||
variable "example" { | ||
type = number | ||
validation { | ||
condition = provider::assert::positive(var.example) | ||
error_message = "Expected ${var.example} to be positive" | ||
} | ||
} | ||
``` | ||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
positive(number number) bool | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `number` (Number) The number to check | ||
|
||
|
||
## Return Type | ||
|
||
The return type of `positive` is a boolean. |
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,7 @@ | ||
variable "example" { | ||
type = number | ||
validation { | ||
condition = provider::assert::negative(var.example) | ||
error_message = "Expected ${var.example} to be negative" | ||
} | ||
} |
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,7 @@ | ||
variable "example" { | ||
type = number | ||
validation { | ||
condition = provider::assert::positive(var.example) | ||
error_message = "Expected ${var.example} to be positive" | ||
} | ||
} |
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,50 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
) | ||
|
||
var ( | ||
_ function.Function = NegativeFunction{} | ||
) | ||
|
||
func NewNegativeFunction() function.Function { | ||
return NegativeFunction{} | ||
} | ||
|
||
type NegativeFunction struct{} | ||
|
||
func (r NegativeFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "negative" | ||
} | ||
|
||
func (r NegativeFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Checks whether a number is negative", | ||
Parameters: []function.Parameter{ | ||
function.NumberParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: true, | ||
Description: "The number to check", | ||
Name: "number", | ||
}, | ||
}, | ||
Return: function.BoolReturn{}, | ||
} | ||
} | ||
|
||
func (r NegativeFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var number *big.Float | ||
|
||
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &number)) | ||
if resp.Error != nil { | ||
return | ||
} | ||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, number.Cmp(big.NewFloat(0)) == -1)) | ||
} |
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,118 @@ | ||
// 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 TestNegativeFunction(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::negative(-5) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestNegativeFunction_float(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::negative(-50.32132) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestNegativeFunction_falseCases(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::negative(0) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "false"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::assert::negative(0.0) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "false"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::assert::negative(1) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "false"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::assert::negative(5) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "false"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::assert::negative(50.32132) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "false"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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,50 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
) | ||
|
||
var ( | ||
_ function.Function = PositiveFunction{} | ||
) | ||
|
||
func NewPositiveFunction() function.Function { | ||
return PositiveFunction{} | ||
} | ||
|
||
type PositiveFunction struct{} | ||
|
||
func (r PositiveFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "positive" | ||
} | ||
|
||
func (r PositiveFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Checks whether a number is positive", | ||
Parameters: []function.Parameter{ | ||
function.NumberParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: true, | ||
Description: "The number to check", | ||
Name: "number", | ||
}, | ||
}, | ||
Return: function.BoolReturn{}, | ||
} | ||
} | ||
|
||
func (r PositiveFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var number *big.Float | ||
|
||
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &number)) | ||
if resp.Error != nil { | ||
return | ||
} | ||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, number.Cmp(big.NewFloat(0)) == 1)) | ||
} |
Oops, something went wrong.