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.
Merge pull request #13 from datadrivers/fix/name-generation
fix(name): Fix validation and handling of name variable
- Loading branch information
Showing
5 changed files
with
215 additions
and
159 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,68 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"k8s.io/apimachinery/pkg/util/rand" | ||
) | ||
|
||
// validateInputs checks if all needed inputs are set | ||
func validateInputs(inputs map[string]string, variables []Variable) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
|
||
missingInputs := []string{} | ||
for _, variable := range variables { | ||
if variable.Default.IsNull() && (variable.Generated.IsNull() || !variable.Generated.Value) { | ||
if _, ok := inputs[variable.Name.Value]; !ok && variable.Name.Value != "name" { | ||
missingInputs = append(missingInputs, variable.Name.Value) | ||
} | ||
} | ||
|
||
} | ||
if len(missingInputs) > 0 { | ||
diags.AddError( | ||
"Convention Usage Error", | ||
fmt.Sprintf("All convention variables that are not generated or have a default must be present. Missing inputs: %s", strings.Join(missingInputs, ", ")), | ||
) | ||
} | ||
|
||
return diags | ||
} | ||
|
||
// generateName generates the name with the inputs, variables and definition | ||
func generateName(name string, inputs map[string]string, convention Convention) (types.String, diag.Diagnostics) { | ||
var diags diag.Diagnostics | ||
|
||
result := convention.Definition | ||
|
||
for _, variable := range convention.Variables { | ||
block := fmt.Sprintf("(%s)", variable.Name.Value) | ||
replacement := inputs[variable.Name.Value] | ||
if variable.Name.Value == "name" { | ||
replacement = name | ||
} | ||
length := 0 | ||
|
||
if !variable.MaxLength.IsNull() && variable.MaxLength.Value > int64(0) { | ||
length = int(variable.MaxLength.Value) | ||
} | ||
|
||
if !variable.Generated.IsNull() && variable.Generated.Value { | ||
replacement = rand.String(length) | ||
} | ||
|
||
if replacement == "" { | ||
replacement = variable.Default.Value | ||
} | ||
|
||
if length > 0 && len(replacement) > length { | ||
replacement = replacement[0:length] | ||
} | ||
result = strings.Replace(result, block, replacement, -1) | ||
} | ||
|
||
return types.String{Value: result}, diags | ||
} |
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,145 @@ | ||
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: "name"}, | ||
Generated: types.Bool{ | ||
Null: true, | ||
}, | ||
Default: types.String{ | ||
Null: true, | ||
}, | ||
MaxLength: types.Int64{ | ||
Null: false, | ||
Value: int64(8), | ||
}, | ||
}, | ||
{ | ||
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()) | ||
} | ||
|
||
func TestGenerateName(t *testing.T) { | ||
name := "foobar" | ||
convention := Convention{ | ||
Definition: "(name)-(not-generated)-(default)-(generated)", | ||
Variables: []Variable{ | ||
{ | ||
Name: types.String{Value: "name"}, | ||
Generated: types.Bool{ | ||
Null: true, | ||
}, | ||
Default: types.String{ | ||
Null: true, | ||
}, | ||
MaxLength: types.Int64{ | ||
Null: false, | ||
Value: int64(3), | ||
}, | ||
}, | ||
{ | ||
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, | ||
Null: false, | ||
}, | ||
MaxLength: types.Int64{ | ||
Null: false, | ||
Value: int64(4), | ||
}, | ||
}, | ||
{ | ||
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": "bar", | ||
} | ||
|
||
result, diags := generateName(name, inputs, convention) | ||
assert.False(t, diags.HasError()) | ||
assert.Contains(t, result.Value, "foo-bar-default-") | ||
assert.Len(t, result.Value, 20) | ||
} |
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.