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

resource_user: Fix tags/password update. #31

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.exe
.DS_Store
example.tf
terraform-provider-rabbitmq
terraform.tfplan
terraform.tfstate
bin/
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/terraform-providers/terraform-provider-rabbitmq

require (
github.com/hashicorp/terraform v0.12.2
github.com/michaelklishin/rabbit-hole v0.0.0-20160706111056-88550829bcdc
github.com/michaelklishin/rabbit-hole v1.5.0
github.com/streadway/amqp v0.0.0-20190214183023-884228600bc9 // indirect
)

go 1.13
55 changes: 2 additions & 53 deletions go.sum

Large diffs are not rendered by default.

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