Skip to content

Commit

Permalink
Merge pull request #44 from Nepomuceno/41-fix-rsv
Browse files Browse the repository at this point in the history
Correcting regex on name cleaning
  • Loading branch information
Nepomuceno authored Sep 17, 2020
2 parents 31a78f0 + c7c52da commit 9f88d3e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"request": "launch",
"mode": "test",
//"program": "${workspaceFolder}/main.go"
"args": [
"check-go-version",
"false"
],
"program": "${file}"
},
{
Expand Down
12 changes: 6 additions & 6 deletions azurecaf/models_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions azurecaf/models_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ func TestCompileRegexValidation(t *testing.T) {
}
}

func TestStrimingNameRegexValidation(t *testing.T) {
for _, resource := range ResourceDefinitions {
reg, err := regexp.Compile(resource.RegEx)
if err != nil {
t.Logf("Error on the regex %s for the resource %s error %v", resource.RegEx, resource.ResourceTypeName, err.Error())
t.Fail()
}
content := "abcde"
result := reg.ReplaceAllString(content, "")
if len(result) != 5 {
t.Logf("%s : expected not be clear anything startd with %s end with %s", resource.ResourceTypeName, content, result)
t.Fail()
}
}
}

func TestRegexValidationMinLength(t *testing.T) {
content := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
contentBase := []rune(content)
Expand Down
3 changes: 2 additions & 1 deletion azurecaf/resource_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func getResourceName(resourceTypeName string, separator string,
separator = cleanString(separator, resource)
randomSuffix = cleanString(randomSuffix, resource)
}

var resourceName string

if passthrough {
Expand All @@ -431,7 +432,7 @@ func getResourceName(resourceTypeName string, separator string,
}

if !validationRegEx.MatchString(resourceName) {
return "", fmt.Errorf("Invalid name for Random CAF naming %s %s, the pattern %s doesn't match %s", resource.ResourceTypeName, name, resource.ValidationRegExp, resourceName)
return "", fmt.Errorf("Invalid name for CAF naming %s %s, the pattern %s doesn't match %s", resource.ResourceTypeName, name, resource.ValidationRegExp, resourceName)
}

return resourceName, nil
Expand Down
56 changes: 56 additions & 0 deletions azurecaf/resource_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ func TestAccResourceName_CafClassic(t *testing.T) {
})
}

func TestAccResourceNameRsv_CafClassic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccResourceNameCafClassicConfigRsv,
Check: resource.ComposeTestCheckFunc(
testAccCafNamingValidation(
"azurecaf_name.rsv",
"pr1-rsv-test-gm-su1",
19,
""),
regexMatch("azurecaf_name.rsv", regexp.MustCompile(ResourceDefinitions["azurerm_recovery_services_vault"].ValidationRegExp), 1),
),
},
},
})
}

func TestComposeName(t *testing.T) {
namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"}
prefixes := []string{"a", "b"}
Expand Down Expand Up @@ -226,6 +247,21 @@ func TestGetResourceNameValid(t *testing.T) {
}
}

func TestGetResourceNameValidRsv(t *testing.T) {
namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"}
resourceName, err := getResourceName("azurerm_recovery_services_vault", "-", []string{"a", "b"}, "test", nil, "1234", "cafclassic", true, false, true, namePrecedence)
expected := "a-b-rsv-test-1234"

if err != nil {
t.Logf("getResource Name generated an error %s", err.Error())
t.Fail()
}
if expected != resourceName {
t.Logf("invalid name, expected %s got %s", expected, resourceName)
t.Fail()
}
}

func TestGetResourceNameValidNoSlug(t *testing.T) {
namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"}
resourceName, err := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, false, namePrecedence)
Expand Down Expand Up @@ -298,6 +334,7 @@ resource "azurecaf_name" "classic_rg" {
resource_type = "azurerm_resource_group"
prefixes = ["pr1", "pr2"]
suffixes = ["su1", "su2"]
random_seed = 1
random_length = 5
clean_input = true
}
Expand All @@ -307,6 +344,7 @@ resource "azurecaf_name" "classic_acr_invalid" {
resource_type = "azurerm_container_registry"
prefixes = ["pr1", "pr2"]
suffixes = ["su1", "su2"]
random_seed = 1
random_length = 5
clean_input = true
}
Expand All @@ -316,8 +354,26 @@ resource "azurecaf_name" "passthrough" {
resource_type = "azurerm_container_registry"
prefixes = ["pr1", "pr2"]
suffixes = ["su1", "su2"]
random_seed = 1
random_length = 5
clean_input = true
passthrough = true
}
`

const testAccResourceNameCafClassicConfigRsv = `
# Resource Group
resource "azurecaf_name" "rsv" {
name = "test"
resource_type = "azurerm_recovery_services_vault"
prefixes = ["pr1"]
suffixes = ["su1"]
random_length = 2
random_seed = 1
clean_input = true
passthrough = false
}
`
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ go 1.13

require (
cloud.google.com/go v0.61.0 // indirect
github.com/go-delve/delve v1.5.0 // indirect
github.com/google/go-dap v0.3.0 // indirect
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 // indirect
github.com/hashicorp/go-plugin v1.3.0 // indirect
github.com/hashicorp/hcl/v2 v2.3.0 // indirect
github.com/hashicorp/terraform-json v0.5.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.9.0
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-testing-interface v1.0.4 // indirect
github.com/peterh/liner v1.2.0 // indirect
github.com/spf13/cobra v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.7 // indirect
go.starlark.net v0.0.0-20200901195727-6e684ef5eeee // indirect
golang.org/x/arch v0.0.0-20200826200359-b19915210f00 // indirect
golang.org/x/sys v0.0.0-20200917073148-efd3b9a0ff20 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
12 changes: 6 additions & 6 deletions resourceDefinition.json
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,12 @@
"name": "azurerm_recovery_services_vault",
"min_length": 2,
"max_length": 50,
"validation_regex": "\"^[a-zA-Z][a-zA-Z0-9\\\\-]{1,49}$\"",
"validation_regex": "\"^[a-zA-Z][a-zA-Z0-9-]{1,49}$\"",
"scope": "resourceGroup",
"slug": "rsv",
"dashes": true,
"lowercase": false,
"regex": "\"[a-zA-Z0-9\\\\-]\""
"regex": "`[^a-zA-Z0-9-]`"
},
{
"name": "azurerm_recovery_services_vault_backup_police",
Expand All @@ -780,7 +780,7 @@
"slug": "rsvbp",
"dashes": true,
"lowercase": false,
"regex": "\"[a-zA-Z0-9\\\\-]\""
"regex": "`[^a-zA-Z0-9-]`"
},
{
"name": "azurerm_data_factory_linked_service_mysql",
Expand Down Expand Up @@ -1744,7 +1744,7 @@
"slug": "sqlfw",
"dashes": true,
"lowercase": false,
"regex": "\"[^<>*%:?\\\\+\\\\/]\""
"regex": "`[<>*%:?\\\\+\\\\/]`"
},
{
"name": "azurerm_log_analytics_workspace",
Expand Down Expand Up @@ -2064,7 +2064,7 @@
"slug": "egt",
"dashes": true,
"lowercase": false,
"regex": "\"[^0-9A-Za-]\""
"regex": "`[^0-9A-Za-z-]`"
},
{
"name": "azurerm_relay_namespace",
Expand Down Expand Up @@ -2185,6 +2185,6 @@
"slug": "syfw",
"dashes": true,
"lowercase": false,
"regex": "\"[^<>*%:?\\\\+\\\\/]\""
"regex": "`[<>*%:?\\\\+\\\\/]`"
}
]

0 comments on commit 9f88d3e

Please sign in to comment.