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 ("").
This commit is contained in:
Joe Topjian 2017-04-20 11:10:20 -06:00 committed by Paul Stack
parent 1157967fbe
commit 6262a73de7
3 changed files with 128 additions and 12 deletions

View File

@ -35,7 +35,7 @@ func resourceUser() *schema.Resource {
"tags": &schema.Schema{ "tags": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Required: true, Optional: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
}, },
}, },
@ -49,13 +49,17 @@ func CreateUser(d *schema.ResourceData, meta interface{}) error {
var tagList []string var tagList []string
for _, v := range d.Get("tags").([]interface{}) { 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{ userSettings := rabbithole.UserSettings{
Password: d.Get("password").(string), 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) 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) d.Set("name", user.Name)
tags := strings.Split(user.Tags, ",") if len(user.Tags) > 0 {
d.Set("tags", tags) tags := strings.Split(user.Tags, ",")
d.Set("tags", tags)
}
return nil return nil
} }
@ -124,12 +130,14 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error {
var tagList []string var tagList []string
for _, v := range newTags.([]interface{}) { 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{ userSettings := rabbithole.UserSettings{}
Tags: tags, if len(tagList) > 0 {
userSettings.Tags = strings.Join(tagList, ",")
} }
log.Printf("[DEBUG] RabbitMQ: Attempting to update tags for %s", name) log.Printf("[DEBUG] RabbitMQ: Attempting to update tags for %s", name)

View File

@ -2,6 +2,7 @@ package rabbitmq
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
"github.com/michaelklishin/rabbit-hole" "github.com/michaelklishin/rabbit-hole"
@ -10,7 +11,7 @@ import (
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
func TestAccUser(t *testing.T) { func TestAccUser_basic(t *testing.T) {
var user string var user string
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, 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 { func testAccUserCheck(rn string, name *string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn] 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 { func testAccUserCheckDestroy(name string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rmqc := testAccProvider.Meta().(*rabbithole.Client) rmqc := testAccProvider.Meta().(*rabbithole.Client)
@ -92,3 +173,30 @@ resource "rabbitmq_user" "test" {
password = "foobarry" password = "foobarry"
tags = ["management"] 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"]
}`

View File

@ -32,7 +32,7 @@ The following arguments are supported:
* `password` - (Required) The password of the user. The value of this argument * `password` - (Required) The password of the user. The value of this argument
is plain-text so make sure to secure where this is defined. 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. options are: management, policymaker, monitoring, and administrator.
## Attributes Reference ## Attributes Reference