From 21a9e871ecc4e5901b4b65e9cc069862fc238f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Laforet?= Date: Fri, 21 Jul 2017 09:12:30 +0200 Subject: [PATCH] - Add support to manipulate cluster name GetClusterName() to fetch cluster name SetClusterName() to rename the cluster --- README.md | 14 +++++++++++++- bin/ci/before_build.sh | 3 +++ cluster.go | 38 ++++++++++++++++++++++++++++++++++++++ doc.go | 9 +++++++++ rabbithole_test.go | 21 +++++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 cluster.go diff --git a/README.md b/README.md index 473c072..a7ff0ea 100644 --- a/README.md +++ b/README.md @@ -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"}) +// => *http.Response, err + +``` + ### HTTPS Connections ``` go @@ -302,7 +314,7 @@ rmqc, err := NewTLSClient("https://127.0.0.1:15672", "guest", "guest", transport ``` go var transport *http.Transport -... +... rmqc.SetTransport(transport) ``` diff --git a/bin/ci/before_build.sh b/bin/ci/before_build.sh index 60be3b2..bd940c5 100755 --- a/bin/ci/before_build.sh +++ b/bin/ci/before_build.sh @@ -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 diff --git a/cluster.go b/cluster.go new file mode 100644 index 0000000..f655bdb --- /dev/null +++ b/cluster.go @@ -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 +} diff --git a/doc.go b/doc.go index 549be70..4abb7db 100644 --- a/doc.go +++ b/doc.go @@ -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 diff --git a/rabbithole_test.go b/rabbithole_test.go index bddaa8e..7598309 100644 --- a/rabbithole_test.go +++ b/rabbithole_test.go @@ -265,6 +265,27 @@ var _ = Describe("Rabbithole", func() { }) }) + Context("PUT /cluster-name", func() { + It("Set cluster name", func() { + 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.