This is an automated email from the ASF dual-hosted git repository.

xyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new b4d45cd3 [Improve] Add admin api GetLeaderBroker (#1203)
b4d45cd3 is described below

commit b4d45cd360599b52e3f160db9d39d619889c556a
Author: crossoverJie <crossover...@gmail.com>
AuthorDate: Mon Apr 22 21:25:00 2024 +0800

    [Improve] Add admin api GetLeaderBroker (#1203)
    
    ### Motivation
    
    To keep consistent with the [Java 
client](https://github.com/apache/pulsar/pull/9799).
    
    ### Modifications
    
    Add `GetLeaderBroker` interface.
---
 pulsaradmin/pkg/admin/brokers.go      | 12 ++++++++++++
 pulsaradmin/pkg/admin/brokers_test.go | 16 ++++++++++++++++
 pulsaradmin/pkg/utils/data.go         |  5 +++++
 3 files changed, 33 insertions(+)

diff --git a/pulsaradmin/pkg/admin/brokers.go b/pulsaradmin/pkg/admin/brokers.go
index e178610c..650fab8e 100644
--- a/pulsaradmin/pkg/admin/brokers.go
+++ b/pulsaradmin/pkg/admin/brokers.go
@@ -58,6 +58,9 @@ type Brokers interface {
 
        // HealthCheckWithTopicVersion run a health check on the broker
        HealthCheckWithTopicVersion(utils.TopicVersion) error
+
+       // GetLeaderBroker get the information of the leader broker.
+       GetLeaderBroker() (utils.BrokerInfo, error)
 }
 
 type broker struct {
@@ -162,3 +165,12 @@ func (b *broker) HealthCheckWithTopicVersion(topicVersion 
utils.TopicVersion) er
        }
        return nil
 }
+func (b *broker) GetLeaderBroker() (utils.BrokerInfo, error) {
+       endpoint := b.pulsar.endpoint(b.basePath, "/leaderBroker")
+       var brokerInfo utils.BrokerInfo
+       err := b.pulsar.Client.Get(endpoint, &brokerInfo)
+       if err != nil {
+               return brokerInfo, err
+       }
+       return brokerInfo, nil
+}
diff --git a/pulsaradmin/pkg/admin/brokers_test.go 
b/pulsaradmin/pkg/admin/brokers_test.go
index d48ce7cb..97679759 100644
--- a/pulsaradmin/pkg/admin/brokers_test.go
+++ b/pulsaradmin/pkg/admin/brokers_test.go
@@ -42,3 +42,19 @@ func TestBrokerHealthCheckWithTopicVersion(t *testing.T) {
        err = admin.Brokers().HealthCheckWithTopicVersion(utils.TopicVersionV2)
        assert.NoError(t, err)
 }
+
+func TestGetLeaderBroker(t *testing.T) {
+       readFile, err := 
os.ReadFile("../../../integration-tests/tokens/admin-token")
+       assert.NoError(t, err)
+       cfg := &config.Config{
+               Token: string(readFile),
+       }
+       admin, err := New(cfg)
+       assert.NoError(t, err)
+       assert.NotNil(t, admin)
+       leaderBroker, err := admin.Brokers().GetLeaderBroker()
+       assert.NoError(t, err)
+       assert.NotNil(t, leaderBroker)
+       assert.NotEmpty(t, leaderBroker.ServiceURL)
+       assert.NotEmpty(t, leaderBroker.BrokerID)
+}
diff --git a/pulsaradmin/pkg/utils/data.go b/pulsaradmin/pkg/utils/data.go
index 1e67e3c7..61607912 100644
--- a/pulsaradmin/pkg/utils/data.go
+++ b/pulsaradmin/pkg/utils/data.go
@@ -477,6 +477,11 @@ type GetStatsOptions struct {
        ExcludeConsumers         bool `json:"exclude_consumers"`
 }
 
+type BrokerInfo struct {
+       BrokerID   string `json:"brokerId"`
+       ServiceURL string `json:"serviceUrl"`
+}
+
 type TopicVersion string
 
 const (

Reply via email to