Skip to content

Commit

Permalink
provider/rabbitmq: Allow users without tags (hashicorp#13798)
Browse files Browse the repository at this point in the history
This commit makes the tags attribute optional for users. It also
handles cases when a user defines a tag as an empty string ("").
  • Loading branch information
jtopjian authored and stack72 committed Apr 20, 2017
1 parent 1157967 commit 6262a73
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 12 deletions.
28 changes: 18 additions & 10 deletions builtin/providers/rabbitmq/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func resourceUser() *schema.Resource {

"tags": &schema.Schema{
Type: schema.TypeList,
Required: true,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
110 changes: 109 additions & 1 deletion builtin/providers/rabbitmq/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rabbitmq

import (
"fmt"
"strings"
"testing"

"github.com/michaelklishin/rabbit-hole"
Expand All @@ -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) },
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"]
}`
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6262a73

Please sign in to comment.