From c66a5a14883ee50a90ea0df0a698a22389eda173 Mon Sep 17 00:00:00 2001 From: Cyril Gaudin Date: Tue, 7 May 2019 10:21:42 +0200 Subject: [PATCH] resource_user: Fix tags/password update. 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. --- go.mod | 2 +- go.sum | 2 ++ rabbitmq/import_queue_test.go | 1 + rabbitmq/resource_queue_test.go | 4 +-- rabbitmq/resource_user.go | 48 ++++++++------------------- rabbitmq/resource_user_test.go | 58 +++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index af3e9be9..035f1de2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d6d6907b..14e0eb82 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/rabbitmq/import_queue_test.go b/rabbitmq/import_queue_test.go index e673dffa..1e7543eb 100644 --- a/rabbitmq/import_queue_test.go +++ b/rabbitmq/import_queue_test.go @@ -9,6 +9,7 @@ import ( ) func TestAccQueue_importBasic(t *testing.T) { + resourceName := "rabbitmq_queue.test" var queue rabbithole.QueueInfo diff --git a/rabbitmq/resource_queue_test.go b/rabbitmq/resource_queue_test.go index dc81967c..daff40b8 100644 --- a/rabbitmq/resource_queue_test.go +++ b/rabbitmq/resource_queue_test.go @@ -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 { diff --git a/rabbitmq/resource_user.go b/rabbitmq/resource_user.go index b7c4283e..e72e7d44 100644 --- a/rabbitmq/resource_user.go +++ b/rabbitmq/resource_user.go @@ -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) diff --git a/rabbitmq/resource_user_test.go b/rabbitmq/resource_user_test.go index e6e780df..8bcb8397 100644 --- a/rabbitmq/resource_user_test.go +++ b/rabbitmq/resource_user_test.go @@ -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{ @@ -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) @@ -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"