Skip to content

Commit

Permalink
Merge pull request #12 from datadrivers/fix/input-validation
Browse files Browse the repository at this point in the history
fix(name): Fix input validation of resource_name and add tests
  • Loading branch information
anmoel authored Jun 27, 2022
2 parents d5973a8 + b3ea683 commit 8a4a27c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
### Feat

- Rename provider
- Rename provider
- Move convention definition into own resource
- Move convention definition into own resource

## v0.2.0 (2022-06-01)

### Feat

- Improve handling with max_length and add descriptions
- Improve handling with max_length and add descriptions

## v0.1.0 (2022-06-01)

Expand Down
3 changes: 3 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ default: testacc
build: fmt
go build -v .

test:
go test ./... -v $(TESTARGS)

testacc:
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m

Expand Down
10 changes: 0 additions & 10 deletions examples/development/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
terraform {
required_version = ">= 0.14"
required_providers {
designation = {
source = "datadrivers/designation"
}
}
}
provider "designation" {}

resource "designation_convention" "this" {
definition = "(region)-(stage)-(name)-(random)"
variables = [
Expand Down
8 changes: 8 additions & 0 deletions examples/development/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_version = ">= 0.3"
required_providers {
designation = {
source = "datadrivers/designation"
}
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/hashicorp/terraform-plugin-docs v0.10.1
github.com/hashicorp/terraform-plugin-framework v0.9.0
github.com/stretchr/testify v1.7.2
k8s.io/apimachinery v0.24.2
)

Expand All @@ -14,6 +15,7 @@ require (
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
Expand Down Expand Up @@ -43,6 +45,7 @@ require (
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand All @@ -58,4 +61,5 @@ require (
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
google.golang.org/grpc v1.46.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 3 additions & 3 deletions internal/provider/resource_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ func validateInputs(inputs map[string]string, variables []Variable) diag.Diagnos

missingInputs := []string{}
for _, variable := range variables {
if variable.Default.Null && !variable.Generated.Value {
if inputs[variable.Name.Value] != "" {
if variable.Default.IsNull() && (variable.Generated.IsNull() || !variable.Generated.Value) {
if _, ok := inputs[variable.Name.Value]; !ok {
missingInputs = append(missingInputs, variable.Name.Value)
}
}
Expand All @@ -209,7 +209,7 @@ func validateInputs(inputs map[string]string, variables []Variable) diag.Diagnos
if len(missingInputs) > 0 {
diags.AddError(
"Convention Usage Error",
fmt.Sprintf("All provider variables that are not generated or have a default must be present. Missing inputs: %s", strings.Join(missingInputs, ", ")),
fmt.Sprintf("All convention variables that are not generated or have a default must be present. Missing inputs: %s", strings.Join(missingInputs, ", ")),
)
}

Expand Down
63 changes: 63 additions & 0 deletions internal/provider/resource_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package provider

import (
"testing"

// "github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stretchr/testify/assert"
)

func TestValidateInputs(t *testing.T) {
variables := []Variable{
{
Name: types.String{Value: "not-generated"},
Generated: types.Bool{
Null: true,
Value: false},
Default: types.String{
Null: true,
},
MaxLength: types.Int64{
Null: true,
},
},
{
Name: types.String{Value: "generated"},
Generated: types.Bool{Value: true},
Default: types.String{
Null: true,
},
MaxLength: types.Int64{
Null: true,
},
},
{
Name: types.String{Value: "default"},
Generated: types.Bool{
Null: true,
},
Default: types.String{
Null: false,
Value: "default",
},
MaxLength: types.Int64{
Null: true,
},
},
}

inputs := map[string]string{
"not-generated": "test",
}

diags := validateInputs(inputs, variables)
assert.False(t, diags.HasError())

inputs2 := map[string]string{
"generated": "test",
}
diags2 := validateInputs(inputs2, variables)

assert.True(t, diags2.HasError())
}

0 comments on commit 8a4a27c

Please sign in to comment.