Skip to content

Commit

Permalink
Merge pull request #164 from Cox-Automotive/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
americk0 committed May 4, 2022
2 parents 1146926 + 6a9272e commit 5a6f343
Show file tree
Hide file tree
Showing 22 changed files with 1,300 additions and 0 deletions.
16 changes: 16 additions & 0 deletions naming.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import (
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

const MaxRoleLen = 64

// NameWithPrefix returns in order:
// the name if non-empty,
// a prefix generated name if non-empty,
Expand Down Expand Up @@ -31,3 +36,14 @@ func NamePrefixFromName(name string) *string {

return &namePrefix
}

// Validate Role name based on https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html
var ValidRoleName = validation.All(
validation.StringLenBetween(1, MaxRoleLen),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]+$`), "must match [\\w+=,.@-]"),
)

var ValidRolePrefix = validation.All(
validation.StringLenBetween(1, MaxRoleLen-resource.UniqueIDSuffixLength),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]+$`), "must match [\\w+=,.@-]"),
)
64 changes: 64 additions & 0 deletions naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,67 @@ func TestNamePrefixFromName_InvalidPrefixedName(t *testing.T) {
t.Fatal("expected prefix to be nil")
}
}

func TestValidRoleName_NameTooShort(t *testing.T) {
name := ""
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 2 {
t.Fatalf("expected 2 validation errors")
}
}

func TestValidRoleName_NameMaxLength(t *testing.T) {
name := "0123456789012345678901234567890123456789012345678901234567890123"
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 0 {
t.Fatalf("expected no validation errors")
}
}

func TestValidRoleName_NameTooLong(t *testing.T) {
name := "01234567890123456789012345678901234567890123456789012345678901234"
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}

func TestValidRoleName_NameInvalidChar(t *testing.T) {
name := "name!!!!@#$%^&*()-="
warn, err := ValidRoleName(name, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}

func TestValidRolePrefix_NamePrefixTooShort(t *testing.T) {
prefix := ""
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 2 {
t.Fatalf("expected 2 validation errors")
}
}

func TestValidRolePrefix_NamePrefixMaxLength(t *testing.T) {
prefix := "01234567890123456789012345678901234567"
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 0 {
t.Fatalf("expected no validation errors")
}
}

func TestValidRolePrefix_NamePrefixTooLong(t *testing.T) {
prefix := "012345678901234567890123456789012345678"
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}

func TestValidRolePrefix_NamePrefixInvalidChar(t *testing.T) {
prefix := "name!!!!@#$%^&*()-="
warn, err := ValidRolePrefix(prefix, "")
if len(warn) != 0 || len(err) != 1 {
t.Fatalf("expected 1 validation error")
}
}
2 changes: 2 additions & 0 deletions resource_alks_iamrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ func resourceAlksIamRole() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: ValidRoleName,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: ValidRolePrefix,
},
"type": {
Type: schema.TypeString,
Expand Down
24 changes: 24 additions & 0 deletions resource_alks_iamrole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ func TestAccIAMRole_NameAndNamePrefixConflict(t *testing.T) {
})
}

func TestAccIAMRole_NameTooLong(t *testing.T) {
var resp alks.IamRoleResponse

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAlksIamRoleDestroy(&resp),
Steps: []resource.TestStep{
{
Config: testAccCheckAlksIamRoleConfigNameTooLong,
ExpectError: regexp.MustCompile(".* expected length of name to be in the range \\(1 - 64\\).*"),
},
},
})
}

func testAccCheckAlksIamRoleDestroy(role *alks.IamRoleResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*alks.Client)
Expand Down Expand Up @@ -205,3 +221,11 @@ const testAccCheckAlksIamRoleConfigNameAndNamePrefixConflict = `
include_default_policies = false
}
`

const testAccCheckAlksIamRoleConfigNameTooLong = `
resource "alks_iamrole" "nametoolong" {
name = "nameandnametoolongggggggggggggggggggggggggggggggggggggggggggggggg"
type = "Amazon EC2"
include_default_policies = false
}
`
2 changes: 2 additions & 0 deletions resource_alks_iamtrustrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ func resourceAlksIamTrustRole() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: ValidRoleName,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: ValidRolePrefix,
},
"type": {
Type: schema.TypeString,
Expand Down
30 changes: 30 additions & 0 deletions resource_alks_iamtrustrole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func TestAccAlksIamTrustRole_NameAndNamePrefixConflict(t *testing.T) {
})
}

func TestAccAlksIamTrustRole_NameTooLong(t *testing.T) {
var resp alks.IamRoleResponse

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAlksIamRoleDestroy(&resp),
Steps: []resource.TestStep{
{
Config: testAccCheckAlksIamTrustRoleConfigNameTooLong,
ExpectError: regexp.MustCompile(".* expected length of name to be in the range \\(1 - 64\\).*"),
},
},
})
}

const testAccCheckAlksIamTrustRoleConfigBasic = `
resource "alks_iamrole" "foo" {
name = "foo"
Expand Down Expand Up @@ -137,3 +153,17 @@ const testAccCheckAlksIamTrustRoleConfigNameAndNamePrefixConflict = `
trust_arn = "${alks_iamrole.nameprefixconflict_role.arn}"
}
`

const testAccCheckAlksIamTrustRoleConfigNameTooLong= `
resource "alks_iamrole" "nametoolong_role" {
name_prefix = "alks_test_acc_"
type = "Amazon EC2"
include_default_policies = false
}
resource "alks_iamtrustrole" "nametoolong_trustrole" {
name = "nameandnametoolongggggggggggggggggggggggggggggggggggggggggggggggg"
type = "Inner Account"
trust_arn = "${alks_iamrole.nametoolong_role.arn}"
}
`

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

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

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

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

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

Loading

0 comments on commit 5a6f343

Please sign in to comment.