Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to manipulate cluster name #104

Merged
merged 1 commit into from
Jul 21, 2017
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ resp, err := rmqc.DeleteShovel("/", "a.shovel")

```

### Operations on cluster name
``` go
// Get cluster name
cn, err := rmqc.GetClusterName()
// => ClusterName, err

// Rename cluster
resp, err := rmqc.SetClusterName(ClusterName{Name: "rabbitmq@rabbit-hole"})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should take a string… or reusing the same struct is actually OK. In most cases I've seen a cluster name is not set to a previously fetched value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially chose to use a string as parameter, but I though passing a struct is more consistent with the API, since cluster name is set via the request body and not as part of path. I've have no problem to switch back to string.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gerhard and I discussed this and perhaps using a struct is not too odd. It would be more flexible should this part of the API change or require validation and it's not too weird for Go libraries to do something like this.

So no need for changing it. Thank you for responding so quickly!

// => *http.Response, err

```

### HTTPS Connections

``` go
Expand All @@ -302,7 +314,7 @@ rmqc, err := NewTLSClient("https://127.0.0.1:15672", "guest", "guest", transport
``` go
var transport *http.Transport

...
...

rmqc.SetTransport(transport)
```
Expand Down
3 changes: 3 additions & 0 deletions bin/ci/before_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ $CTL eval 'supervisor2:terminate_child(rabbit_mgmt_agent_sup_sup, rabbit_mgmt_ag
$CTL add_vhost "rabbit/hole"
$CTL set_permissions -p "rabbit/hole" guest ".*" ".*" ".*"

# set cluster name
$CTL set_cluster_name rabbitmq@localhost

# Enable shovel plugin
$PLUGINS enable rabbitmq_shovel
$PLUGINS enable rabbitmq_shovel_management
38 changes: 38 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package rabbithole

import (
"encoding/json"
"net/http"
)

type ClusterName struct {
Name string `json:"name"`
}

func (c *Client) GetClusterName() (rec *ClusterName, err error) {
req, err := newGETRequest(c, "cluster-name/")
if err != nil {
return nil, err
}

if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}

return rec, nil
}

func (c *Client) SetClusterName(cn ClusterName) (res *http.Response, err error) {
body, err := json.Marshal(cn)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "PUT", "cluster-name", body)
if err != nil {
return nil, err
}

res, err = executeRequest(c, req)

return res, nil
}
9 changes: 9 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,14 @@ Managing Permissions
// revokes permissions in vhost
resp, err := rmqc.ClearPermissionsIn("/", "my.user")
// => *http.Response, err

Operations on cluster name
// Get cluster name
cn, err := rmqc.GetClusterName()
// => ClusterName, err

// Rename cluster
resp, err := rmqc.SetClusterName(ClusterName{Name: "rabbitmq@rabbit-hole"})
// => *http.Response, err
*/
package rabbithole
21 changes: 21 additions & 0 deletions rabbithole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,27 @@ var _ = Describe("Rabbithole", func() {
})
})

Context("PUT /cluster-name", func() {
It("Set cluster name", func() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not restore cluster name to its original value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, no problem. BTW, since cluster name is defined at run time and is hostname based, it may be more elegant and predictable to set arbitrarily a value in ./bin/ci/before_build.sh. Are you ok with that ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. I don't think we depend on cluster name anywhere. rabbit@localhost is the value I'd use.

previousClusterName, err := rmqc.GetClusterName()
Ω(err).Should(BeNil())
Ω(previousClusterName.Name).Should(Equal("rabbitmq@localhost"))
cnStr := "rabbitmq@rabbit-hole-test"
cn := ClusterName{Name: cnStr}
resp, err := rmqc.SetClusterName(cn)
Ω(err).Should(BeNil())
Ω(resp.Status).Should(Equal("204 No Content"))
awaitEventPropagation()
cn2, err := rmqc.GetClusterName()
Ω(err).Should(BeNil())
Ω(cn2.Name).Should(Equal(cnStr))
// Restore cluster name
rmqc.SetClusterName(*previousClusterName)
Ω(err).Should(BeNil())
Ω(resp.Status).Should(Equal("204 No Content"))
})
})

Context("GET /connections when there are active connections", func() {
It("returns decoded response", func() {
// this really should be tested with > 1 connection and channel. MK.
Expand Down