From 6262a73de79758a68d2065c46ee0f0de8712a3f6 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Thu, 20 Apr 2017 11:10:20 -0600 Subject: [PATCH] provider/rabbitmq: Allow users without tags (#13798) This commit makes the tags attribute optional for users. It also handles cases when a user defines a tag as an empty string (""). --- builtin/providers/rabbitmq/resource_user.go | 28 +++-- .../providers/rabbitmq/resource_user_test.go | 110 +++++++++++++++++- .../providers/rabbitmq/r/user.html.markdown | 2 +- 3 files changed, 128 insertions(+), 12 deletions(-) diff --git a/builtin/providers/rabbitmq/resource_user.go b/builtin/providers/rabbitmq/resource_user.go index 2177182ef..e67e60e99 100644 --- a/builtin/providers/rabbitmq/resource_user.go +++ b/builtin/providers/rabbitmq/resource_user.go @@ -35,7 +35,7 @@ func resourceUser() *schema.Resource { "tags": &schema.Schema{ Type: schema.TypeList, - Required: true, + Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, }, }, @@ -49,13 +49,17 @@ func CreateUser(d *schema.ResourceData, meta interface{}) error { var tagList []string for _, v := range d.Get("tags").([]interface{}) { - tagList = append(tagList, v.(string)) + if v, ok := v.(string); ok { + tagList = append(tagList, v) + } } - tags := strings.Join(tagList, ",") userSettings := rabbithole.UserSettings{ Password: d.Get("password").(string), - Tags: tags, + } + + if len(tagList) > 0 { + userSettings.Tags = strings.Join(tagList, ",") } log.Printf("[DEBUG] RabbitMQ: Attempting to create user %s", name) @@ -87,8 +91,10 @@ func ReadUser(d *schema.ResourceData, meta interface{}) error { d.Set("name", user.Name) - tags := strings.Split(user.Tags, ",") - d.Set("tags", tags) + if len(user.Tags) > 0 { + tags := strings.Split(user.Tags, ",") + d.Set("tags", tags) + } return nil } @@ -124,12 +130,14 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error { var tagList []string for _, v := range newTags.([]interface{}) { - tagList = append(tagList, v.(string)) + if v, ok := v.(string); ok { + tagList = append(tagList, v) + } } - tags := strings.Join(tagList, ",") - userSettings := rabbithole.UserSettings{ - Tags: tags, + userSettings := rabbithole.UserSettings{} + if len(tagList) > 0 { + userSettings.Tags = strings.Join(tagList, ",") } log.Printf("[DEBUG] RabbitMQ: Attempting to update tags for %s", name) diff --git a/builtin/providers/rabbitmq/resource_user_test.go b/builtin/providers/rabbitmq/resource_user_test.go index 6876134bf..35a3ccd12 100644 --- a/builtin/providers/rabbitmq/resource_user_test.go +++ b/builtin/providers/rabbitmq/resource_user_test.go @@ -2,6 +2,7 @@ package rabbitmq import ( "fmt" + "strings" "testing" "github.com/michaelklishin/rabbit-hole" @@ -10,7 +11,7 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccUser(t *testing.T) { +func TestAccUser_basic(t *testing.T) { var user string resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -33,6 +34,63 @@ func TestAccUser(t *testing.T) { }) } +func TestAccUser_emptyTag(t *testing.T) { + var user string + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccUserCheckDestroy(user), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccUserConfig_emptyTag_1, + Check: resource.ComposeTestCheckFunc( + testAccUserCheck("rabbitmq_user.test", &user), + testAccUserCheckTagCount(&user, 0), + ), + }, + resource.TestStep{ + Config: testAccUserConfig_emptyTag_2, + Check: resource.ComposeTestCheckFunc( + testAccUserCheck("rabbitmq_user.test", &user), + testAccUserCheckTagCount(&user, 1), + ), + }, + resource.TestStep{ + Config: testAccUserConfig_emptyTag_1, + Check: resource.ComposeTestCheckFunc( + testAccUserCheck("rabbitmq_user.test", &user), + testAccUserCheckTagCount(&user, 0), + ), + }, + }, + }) +} + +func TestAccUser_noTags(t *testing.T) { + var user string + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccUserCheckDestroy(user), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccUserConfig_noTags_1, + Check: resource.ComposeTestCheckFunc( + testAccUserCheck("rabbitmq_user.test", &user), + testAccUserCheckTagCount(&user, 0), + ), + }, + resource.TestStep{ + Config: testAccUserConfig_noTags_2, + Check: resource.ComposeTestCheckFunc( + testAccUserCheck("rabbitmq_user.test", &user), + testAccUserCheckTagCount(&user, 1), + ), + }, + }, + }) +} + func testAccUserCheck(rn string, name *string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] @@ -61,6 +119,29 @@ func testAccUserCheck(rn string, name *string) resource.TestCheckFunc { } } +func testAccUserCheckTagCount(name *string, tagCount int) resource.TestCheckFunc { + return func(s *terraform.State) error { + rmqc := testAccProvider.Meta().(*rabbithole.Client) + user, err := rmqc.GetUser(*name) + if err != nil { + return fmt.Errorf("Error retrieving user: %s", err) + } + + var tagList []string + for _, v := range strings.Split(user.Tags, ",") { + if v != "" { + tagList = append(tagList, v) + } + } + + if len(tagList) != tagCount { + return fmt.Errorf("Expected %d tags, user has %d", tagCount, len(tagList)) + } + + return nil + } +} + func testAccUserCheckDestroy(name string) resource.TestCheckFunc { return func(s *terraform.State) error { rmqc := testAccProvider.Meta().(*rabbithole.Client) @@ -92,3 +173,30 @@ resource "rabbitmq_user" "test" { password = "foobarry" tags = ["management"] }` + +const testAccUserConfig_emptyTag_1 = ` +resource "rabbitmq_user" "test" { + name = "mctest" + password = "foobar" + tags = [""] +}` + +const testAccUserConfig_emptyTag_2 = ` +resource "rabbitmq_user" "test" { + name = "mctest" + password = "foobar" + tags = ["administrator"] +}` + +const testAccUserConfig_noTags_1 = ` +resource "rabbitmq_user" "test" { + name = "mctest" + password = "foobar" +}` + +const testAccUserConfig_noTags_2 = ` +resource "rabbitmq_user" "test" { + name = "mctest" + password = "foobar" + tags = ["administrator"] +}` diff --git a/website/source/docs/providers/rabbitmq/r/user.html.markdown b/website/source/docs/providers/rabbitmq/r/user.html.markdown index e9e6e3183..f62db774e 100644 --- a/website/source/docs/providers/rabbitmq/r/user.html.markdown +++ b/website/source/docs/providers/rabbitmq/r/user.html.markdown @@ -32,7 +32,7 @@ The following arguments are supported: * `password` - (Required) The password of the user. The value of this argument is plain-text so make sure to secure where this is defined. -* `tags` - (Required) Which permission model to apply to the user. Valid +* `tags` - (Optional) Which permission model to apply to the user. Valid options are: management, policymaker, monitoring, and administrator. ## Attributes Reference