You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by cr...@apache.org on 2025/09/02 02:40:20 UTC

(pulsar-client-go) branch master updated: Add max topics per namespace methods (#1413)

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

crossoverjie 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 230d11b8 Add max topics per namespace methods (#1413)
230d11b8 is described below

commit 230d11b82ba8b60c971013516c4922afea4a022d
Author: Collignon-Ducret Rémi <mi...@users.noreply.github.com>
AuthorDate: Tue Sep 2 04:40:14 2025 +0200

    Add max topics per namespace methods (#1413)
---
 pulsaradmin/pkg/admin/namespace.go      | 27 ++++++++++++
 pulsaradmin/pkg/admin/namespace_test.go | 76 +++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+)

diff --git a/pulsaradmin/pkg/admin/namespace.go b/pulsaradmin/pkg/admin/namespace.go
index bd4dc88c..9a5cb361 100644
--- a/pulsaradmin/pkg/admin/namespace.go
+++ b/pulsaradmin/pkg/admin/namespace.go
@@ -154,6 +154,16 @@ type Namespaces interface {
 	// GetMaxProducersPerTopic returns the maxProducersPerTopic for a namespace.
 	GetMaxProducersPerTopic(namespace utils.NameSpaceName) (int, error)
 
+	// SetMaxTopicsPerNamespace sets maxTopicsPerNamespace for a namespace.
+	SetMaxTopicsPerNamespace(namespace utils.NameSpaceName, max int) error
+
+	// GetMaxTopicsPerNamespace returns the maxTopicsPerNamespace for a namespace.
+	GetMaxTopicsPerNamespace(namespace utils.NameSpaceName) (int, error)
+
+	// RemoveMaxTopicsPerNamespace removes maxTopicsPerNamespace configuration for a namespace,
+	// defaulting to broker settings
+	RemoveMaxTopicsPerNamespace(namespace utils.NameSpaceName) error
+
 	// GetNamespaceReplicationClusters returns the replication clusters for a namespace
 	GetNamespaceReplicationClusters(namespace string) ([]string, error)
 
@@ -996,3 +1006,20 @@ func (n *namespaces) RemoveProperties(namespace utils.NameSpaceName) error {
 	endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "properties")
 	return n.pulsar.Client.Delete(endpoint)
 }
+
+func (n *namespaces) SetMaxTopicsPerNamespace(namespace utils.NameSpaceName, max int) error {
+	endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "maxTopicsPerNamespace")
+	return n.pulsar.Client.Post(endpoint, max)
+}
+
+func (n *namespaces) GetMaxTopicsPerNamespace(namespace utils.NameSpaceName) (int, error) {
+	var result int
+	endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "maxTopicsPerNamespace")
+	err := n.pulsar.Client.Get(endpoint, &result)
+	return result, err
+}
+
+func (n *namespaces) RemoveMaxTopicsPerNamespace(namespace utils.NameSpaceName) error {
+	endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "maxTopicsPerNamespace")
+	return n.pulsar.Client.Delete(endpoint)
+}
diff --git a/pulsaradmin/pkg/admin/namespace_test.go b/pulsaradmin/pkg/admin/namespace_test.go
index 4917e94b..6600cfaf 100644
--- a/pulsaradmin/pkg/admin/namespace_test.go
+++ b/pulsaradmin/pkg/admin/namespace_test.go
@@ -523,3 +523,79 @@ func TestNamespaces_Properties(t *testing.T) {
 	assert.Equal(t, err, nil)
 	assert.Equal(t, actualPropertiesAfterRemoveCall, map[string]string{})
 }
+
+func TestNamespaces_SetMaxTopicsPerNamespace(t *testing.T) {
+	config := &config.Config{}
+	admin, err := New(config)
+	require.NoError(t, err)
+	require.NotNil(t, admin)
+
+	tests := []struct {
+		name      string
+		namespace string
+		maxTopics int
+		errReason string
+	}{
+		{
+			name:      "Set valid max topics per namespace",
+			namespace: "public/default",
+			maxTopics: 100,
+			errReason: "",
+		},
+		{
+			name:      "Set invalid max topics per namespace",
+			namespace: "public/default",
+			maxTopics: -1,
+			errReason: "maxTopicsPerNamespace must be 0 or more",
+		},
+		{
+			name:      "Set valid max topics per namespace: 0",
+			namespace: "public/default",
+			maxTopics: 0,
+			errReason: "",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			namespace, _ := utils.GetNamespaceName(tt.namespace)
+			err := admin.Namespaces().SetMaxTopicsPerNamespace(*namespace, tt.maxTopics)
+			if tt.errReason == "" {
+				assert.Equal(t, nil, err)
+
+				err = admin.Namespaces().RemoveMaxTopicsPerNamespace(*namespace)
+				assert.Equal(t, nil, err)
+			}
+			if err != nil {
+				restError := err.(rest.Error)
+				assert.Equal(t, tt.errReason, restError.Reason)
+			}
+		})
+	}
+}
+
+func TestNamespaces_GetMaxTopicsPerNamespace(t *testing.T) {
+
+	config := &config.Config{}
+	admin, err := New(config)
+	require.NoError(t, err)
+	require.NotNil(t, admin)
+
+	namespace, _ := utils.GetNamespaceName("public/default")
+
+	// set the max topics per namespace and get it
+	err = admin.Namespaces().SetMaxTopicsPerNamespace(*namespace, 100)
+	assert.Equal(t, nil, err)
+	maxTopics, err := admin.Namespaces().GetMaxTopicsPerNamespace(*namespace)
+	assert.Equal(t, nil, err)
+	expected := 100
+	assert.Equal(t, expected, maxTopics)
+
+	// remove the max topics per namespace and get it
+	err = admin.Namespaces().RemoveMaxTopicsPerNamespace(*namespace)
+	assert.Equal(t, nil, err)
+
+	maxTopics, err = admin.Namespaces().GetMaxTopicsPerNamespace(*namespace)
+	assert.Equal(t, nil, err)
+	expected = 0
+	assert.Equal(t, expected, maxTopics)
+}