Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
resource_user: Fix tags/password update.
Browse files Browse the repository at this point in the history
Updating tags was resetting password (set to empty)

I had to update the rabbit-hole library which was in a very old version.
So it's quite a big change but this version did not check correctly RMQ API errors.
  • Loading branch information
cyrilgdn committed May 7, 2019
1 parent d397672 commit c66a5a1
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 38 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-rabbitmq
require (
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/hashicorp/terraform v0.11.14-0.20190411132215-4966ece93f85
github.com/michaelklishin/rabbit-hole v0.0.0-20160706111056-88550829bcdc
github.com/michaelklishin/rabbit-hole v1.5.0
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/onsi/ginkgo v1.7.0 // indirect
github.com/onsi/gomega v1.4.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ github.com/mattn/go-shellwords v1.0.1/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vq
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/michaelklishin/rabbit-hole v0.0.0-20160706111056-88550829bcdc h1:wJwJP2S2NVamJQivFhhpV8Wp7vUczR7PKPazc/dJ48g=
github.com/michaelklishin/rabbit-hole v0.0.0-20160706111056-88550829bcdc/go.mod h1:vvI1uOitYZi0O5HEGXhaWC1XT80Gy+HvFheJ+5Krlhk=
github.com/michaelklishin/rabbit-hole v1.5.0 h1:Bex27BiFDsijCM9D0ezSHqyy0kehpYHuNKaPqq/a4RM=
github.com/michaelklishin/rabbit-hole v1.5.0/go.mod h1:vvI1uOitYZi0O5HEGXhaWC1XT80Gy+HvFheJ+5Krlhk=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v0.0.0-20171129193617-33edc47170b5 h1:OYr3N2fY3e3kP/x/d81CJXlcZrIV2hH8gPnuRLpiME4=
github.com/mitchellh/cli v0.0.0-20171129193617-33edc47170b5/go.mod h1:oGumspjLm2kTyiT1QMGpFqRlmxnKHfCvhZEVnx+5UeE=
Expand Down
1 change: 1 addition & 0 deletions rabbitmq/import_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestAccQueue_importBasic(t *testing.T) {

resourceName := "rabbitmq_queue.test"
var queue rabbithole.QueueInfo

Expand Down
4 changes: 2 additions & 2 deletions rabbitmq/resource_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func testAccQueueCheckDestroy(queueInfo *rabbithole.QueueInfo) resource.TestChec
rmqc := testAccProvider.Meta().(*rabbithole.Client)

queues, err := rmqc.ListQueuesIn(queueInfo.Vhost)
if err != nil && !strings.Contains(err.Error(), "not found") {
return fmt.Errorf("Error retrieving queue: %s", err)
if err != nil && !strings.Contains(strings.ToLower(err.Error()), "not found") {
return fmt.Errorf("Error retrieving queues: %s", err)
}

for _, queue := range queues {
Expand Down
48 changes: 13 additions & 35 deletions rabbitmq/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,46 +93,24 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error {
rmqc := meta.(*rabbithole.Client)

name := d.Id()
tags := userTagsToString(d)
password := d.Get("password").(string)

if d.HasChange("password") {
tags := userTagsToString(d)
password := d.Get("password").(string)

userSettings := rabbithole.UserSettings{
Password: password,
Tags: tags,
}

log.Printf("[DEBUG] RabbitMQ: Attempting to update password for %s", name)

resp, err := rmqc.PutUser(name, userSettings)
log.Printf("[DEBUG] RabbitMQ: Password update response: %#v", resp)
if err != nil {
return err
}

if resp.StatusCode >= 400 {
return fmt.Errorf("Error updating RabbitMQ user: %s", resp.Status)
}

userSettings := rabbithole.UserSettings{
Password: password,
Tags: tags,
}

if d.HasChange("tags") {
userSettings := rabbithole.UserSettings{}
userSettings.Tags = userTagsToString(d)
log.Printf("[DEBUG] RabbitMQ: Attempting to update user %s", name)

log.Printf("[DEBUG] RabbitMQ: Attempting to update tags for %s", name)

resp, err := rmqc.PutUser(name, userSettings)
log.Printf("[DEBUG] RabbitMQ: Tags update response: %#v", resp)
if err != nil {
return err
}

if resp.StatusCode >= 400 {
return fmt.Errorf("Error updating RabbitMQ user: %s", resp.Status)
}
resp, err := rmqc.PutUser(name, userSettings)
log.Printf("[DEBUG] RabbitMQ: User update response: %#v", resp)
if err != nil {
return err
}

if resp.StatusCode >= 400 {
return fmt.Errorf("Error updating RabbitMQ user: %s", resp.Status)
}

return ReadUser(d, meta)
Expand Down
58 changes: 58 additions & 0 deletions rabbitmq/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ func TestAccUser_basic(t *testing.T) {
})
}

func TestUpdateTags_password(t *testing.T) {
var user string
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccUserCheckDestroy(user),
Steps: []resource.TestStep{
{
Config: testUpdateTagsCreate,
Check: resource.ComposeTestCheckFunc(
testAccUserCheck(
"rabbitmq_user.test", &user,
),
testAccUserConnect("mctest", "foobar"),
),
},
{
Config: testUpdateTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccUserCheck(
"rabbitmq_user.test", &user,
),
testAccUserConnect("mctest", "foobar"),
),
},
},
})
}

func TestAccUser_emptyTag(t *testing.T) {
var user string
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -144,6 +173,21 @@ func testAccUserCheck(rn string, name *string) resource.TestCheckFunc {
}
}

func testAccUserConnect(username, password string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client, err := rabbithole.NewClient("http://localhost:15672", username, password)
if err != nil {
return fmt.Errorf("could not create rmq client: %v", err)
}

_, err = client.Whoami()
if err != nil {
return fmt.Errorf("could not call whoami with username %s: %v", username, err)
}
return nil
}
}

func testAccUserCheckTagCount(name *string, tagCount int) resource.TestCheckFunc {
return func(s *terraform.State) error {
rmqc := testAccProvider.Meta().(*rabbithole.Client)
Expand Down Expand Up @@ -199,6 +243,20 @@ resource "rabbitmq_user" "test" {
tags = ["management"]
}`

const testUpdateTagsCreate = `
resource "rabbitmq_user" "test" {
name = "mctest"
password = "foobar"
tags = ["management"]
}`

const testUpdateTagsUpdate = `
resource "rabbitmq_user" "test" {
name = "mctest"
password = "foobar"
tags = ["monitoring"]
}`

const testAccUserConfig_emptyTag_1 = `
resource "rabbitmq_user" "test" {
name = "mctest"
Expand Down

0 comments on commit c66a5a1

Please sign in to comment.