Skip to content

Commit

Permalink
Merge pull request #38 from Nepomuceno/azurerm_recovery_services_vault
Browse files Browse the repository at this point in the history
Added azurerm_recovery_services_vault
  • Loading branch information
Nepomuceno authored Sep 16, 2020
2 parents 374fa8b + 34d679f commit b68a353
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v2
if: startsWith(github.ref, 'refs/tags/')
env:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}

Expand Down
5 changes: 4 additions & 1 deletion azurecaf/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ var (

// Generate a random value to add to the resource names
func randSeq(length int, seed *int64) string {
if length == 0 {
return ""
}
// initialize random seed
if seed == nil {
value := time.Now().UnixNano()
Expand All @@ -83,7 +86,7 @@ var Resources = map[string]ResourceStructure{
"acr": {"azure container registry", "acr", 5, 50, true, alphanum, "^[0-9A-Za-z]{5,50}$", true, "resourceGroup"},
"afw": {"azure firewall", "afw", 1, 80, false, alphanumhup, "^[a-zA-Z][0-9A-Za-z_.-]{0,79}$", true, "resourceGroup"},
"agw": {"application gateway", "agw", 1, 80, false, alphanumhup, "^[0-9a-zA-Z][0-9A-Za-z_.-]{0,78}[0-9a-zA-Z_]$", true, "resourceGroup"},
"aks": {"azure kubernetes service", "aks", 1, 63, false, alphanumu, "^[0-9a-zA-Z][0-9A-Za-z_.-]{0,61}[0-9a-zA-Z]$", true, "resourceGroup"},
"aks": {"azure kubernetes service", "aks", 1, 63, false, alphanumhu, "^[0-9a-zA-Z][0-9A-Za-z_.-]{0,61}[0-9a-zA-Z]$", true, "resourceGroup"},
"aksdns": {"aksdns prefix", "aksdns", 3, 45, false, alphanumh, "^[a-zA-Z][0-9A-Za-z-]{0,43}[0-9a-zA-Z]$", true, "resourceGroup"},
"aksnpl": {"aks node pool for Linux", "aksnpl", 2, 12, true, alphanum, "^[a-zA-Z][0-9a-z]{0,11}$", true, "resourceGroup"},
"aksnpw": {"aks node pool for Windows", "aksnpw", 2, 6, true, alphanum, "^[a-zA-Z][0-9a-z]{0,5}$", true, "resourceGroup"},
Expand Down
26 changes: 22 additions & 4 deletions azurecaf/models_generated.go

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

20 changes: 16 additions & 4 deletions azurecaf/resource_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func resourceName() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(1),
ValidateFunc: validation.IntAtLeast(0),
Default: 0,
},
"result": {
Expand Down Expand Up @@ -102,6 +102,12 @@ func resourceName() *schema.Resource {
Optional: true,
ForceNew: true,
},
"use_slug": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: true,
},
},
}
}
Expand Down Expand Up @@ -283,6 +289,7 @@ func getResourceName(resourceTypeName string, separator string,
convention string,
cleanInput bool,
passthrough bool,
useSlug bool,
namePrecedence []string) (string, error) {

resource, err := getResource(resourceTypeName)
Expand All @@ -294,7 +301,11 @@ func getResourceName(resourceTypeName string, separator string,
return "", err
}

slug := getSlug(resourceTypeName, convention)
slug := ""
if useSlug {
slug = getSlug(resourceTypeName, convention)
}

if cleanInput {
prefixes = cleanSlice(prefixes, resource)
suffixes = cleanSlice(suffixes, resource)
Expand Down Expand Up @@ -331,6 +342,7 @@ func getNameResult(d *schema.ResourceData, meta interface{}) error {
resourceTypes := convertInterfaceToString(d.Get("resource_types").([]interface{}))
cleanInput := d.Get("clean_input").(bool)
passthrough := d.Get("passthrough").(bool)
useSlug := d.Get("use_slug").(bool)
randomLength := d.Get("random_length").(int)
randomSeed := int64(d.Get("random_seed").(int))

Expand All @@ -345,7 +357,7 @@ func getNameResult(d *schema.ResourceData, meta interface{}) error {
}

if len(resourceType) > 0 {
resourceName, err := getResourceName(resourceType, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, namePrecedence)
resourceName, err := getResourceName(resourceType, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, useSlug, namePrecedence)
if err != nil {
return err
}
Expand All @@ -354,7 +366,7 @@ func getNameResult(d *schema.ResourceData, meta interface{}) error {
resourceNames := make(map[string]string, len(resourceTypes))
for _, resourceTypeName := range resourceTypes {
var err error
resourceNames[resourceTypeName], err = getResourceName(resourceTypeName, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, namePrecedence)
resourceNames[resourceTypeName], err = getResourceName(resourceTypeName, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, useSlug, namePrecedence)
if err != nil {
return err
}
Expand Down
22 changes: 19 additions & 3 deletions azurecaf/resource_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestValidResourceType_invalidParameters(t *testing.T) {

func TestGetResourceNameValid(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, namePrecedence)
resourceName, err := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, true, namePrecedence)
expected := "a-b-rg-myrg-1234"

if err != nil {
Expand All @@ -224,9 +224,25 @@ func TestGetResourceNameValid(t *testing.T) {
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)
expected := "a-b-myrg-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 TestGetResourceNameInvalidResourceType(t *testing.T) {
namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"}
resourceName, err := getResourceName("azurerm_invalid", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, namePrecedence)
resourceName, err := getResourceName("azurerm_invalid", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, true, namePrecedence)
expected := "a-b-rg-myrg-1234"

if err == nil {
Expand All @@ -241,7 +257,7 @@ func TestGetResourceNameInvalidResourceType(t *testing.T) {

func TestGetResourceNamePassthrough(t *testing.T) {
namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"}
resourceName, _ := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, true, namePrecedence)
resourceName, _ := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, true, true, namePrecedence)
expected := "myrg"

if expected != resourceName {
Expand Down
2 changes: 1 addition & 1 deletion azurecaf/resource_naming_convention_cafclassic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestAccCafNamingConvention_Classic(t *testing.T) {
testAccCafNamingValidation(
"azurecaf_naming_convention.classic_aks",
"kubedemo",
11,
12,
"aks"),
regexMatch("azurecaf_naming_convention.classic_aks", regexp.MustCompile(Resources["aks"].ValidationRegExp), 1),
testAccCafNamingValidation(
Expand Down
Loading

0 comments on commit b68a353

Please sign in to comment.