Skip to content

Commit 230d11b

Browse files
authored
Add max topics per namespace methods (#1413)
1 parent a5fb2fc commit 230d11b

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

pulsaradmin/pkg/admin/namespace.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ type Namespaces interface {
154154
// GetMaxProducersPerTopic returns the maxProducersPerTopic for a namespace.
155155
GetMaxProducersPerTopic(namespace utils.NameSpaceName) (int, error)
156156

157+
// SetMaxTopicsPerNamespace sets maxTopicsPerNamespace for a namespace.
158+
SetMaxTopicsPerNamespace(namespace utils.NameSpaceName, max int) error
159+
160+
// GetMaxTopicsPerNamespace returns the maxTopicsPerNamespace for a namespace.
161+
GetMaxTopicsPerNamespace(namespace utils.NameSpaceName) (int, error)
162+
163+
// RemoveMaxTopicsPerNamespace removes maxTopicsPerNamespace configuration for a namespace,
164+
// defaulting to broker settings
165+
RemoveMaxTopicsPerNamespace(namespace utils.NameSpaceName) error
166+
157167
// GetNamespaceReplicationClusters returns the replication clusters for a namespace
158168
GetNamespaceReplicationClusters(namespace string) ([]string, error)
159169

@@ -996,3 +1006,20 @@ func (n *namespaces) RemoveProperties(namespace utils.NameSpaceName) error {
9961006
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "properties")
9971007
return n.pulsar.Client.Delete(endpoint)
9981008
}
1009+
1010+
func (n *namespaces) SetMaxTopicsPerNamespace(namespace utils.NameSpaceName, max int) error {
1011+
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "maxTopicsPerNamespace")
1012+
return n.pulsar.Client.Post(endpoint, max)
1013+
}
1014+
1015+
func (n *namespaces) GetMaxTopicsPerNamespace(namespace utils.NameSpaceName) (int, error) {
1016+
var result int
1017+
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "maxTopicsPerNamespace")
1018+
err := n.pulsar.Client.Get(endpoint, &result)
1019+
return result, err
1020+
}
1021+
1022+
func (n *namespaces) RemoveMaxTopicsPerNamespace(namespace utils.NameSpaceName) error {
1023+
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "maxTopicsPerNamespace")
1024+
return n.pulsar.Client.Delete(endpoint)
1025+
}

pulsaradmin/pkg/admin/namespace_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,79 @@ func TestNamespaces_Properties(t *testing.T) {
523523
assert.Equal(t, err, nil)
524524
assert.Equal(t, actualPropertiesAfterRemoveCall, map[string]string{})
525525
}
526+
527+
func TestNamespaces_SetMaxTopicsPerNamespace(t *testing.T) {
528+
config := &config.Config{}
529+
admin, err := New(config)
530+
require.NoError(t, err)
531+
require.NotNil(t, admin)
532+
533+
tests := []struct {
534+
name string
535+
namespace string
536+
maxTopics int
537+
errReason string
538+
}{
539+
{
540+
name: "Set valid max topics per namespace",
541+
namespace: "public/default",
542+
maxTopics: 100,
543+
errReason: "",
544+
},
545+
{
546+
name: "Set invalid max topics per namespace",
547+
namespace: "public/default",
548+
maxTopics: -1,
549+
errReason: "maxTopicsPerNamespace must be 0 or more",
550+
},
551+
{
552+
name: "Set valid max topics per namespace: 0",
553+
namespace: "public/default",
554+
maxTopics: 0,
555+
errReason: "",
556+
},
557+
}
558+
for _, tt := range tests {
559+
t.Run(tt.name, func(t *testing.T) {
560+
namespace, _ := utils.GetNamespaceName(tt.namespace)
561+
err := admin.Namespaces().SetMaxTopicsPerNamespace(*namespace, tt.maxTopics)
562+
if tt.errReason == "" {
563+
assert.Equal(t, nil, err)
564+
565+
err = admin.Namespaces().RemoveMaxTopicsPerNamespace(*namespace)
566+
assert.Equal(t, nil, err)
567+
}
568+
if err != nil {
569+
restError := err.(rest.Error)
570+
assert.Equal(t, tt.errReason, restError.Reason)
571+
}
572+
})
573+
}
574+
}
575+
576+
func TestNamespaces_GetMaxTopicsPerNamespace(t *testing.T) {
577+
578+
config := &config.Config{}
579+
admin, err := New(config)
580+
require.NoError(t, err)
581+
require.NotNil(t, admin)
582+
583+
namespace, _ := utils.GetNamespaceName("public/default")
584+
585+
// set the max topics per namespace and get it
586+
err = admin.Namespaces().SetMaxTopicsPerNamespace(*namespace, 100)
587+
assert.Equal(t, nil, err)
588+
maxTopics, err := admin.Namespaces().GetMaxTopicsPerNamespace(*namespace)
589+
assert.Equal(t, nil, err)
590+
expected := 100
591+
assert.Equal(t, expected, maxTopics)
592+
593+
// remove the max topics per namespace and get it
594+
err = admin.Namespaces().RemoveMaxTopicsPerNamespace(*namespace)
595+
assert.Equal(t, nil, err)
596+
597+
maxTopics, err = admin.Namespaces().GetMaxTopicsPerNamespace(*namespace)
598+
assert.Equal(t, nil, err)
599+
expected = 0
600+
assert.Equal(t, expected, maxTopics)
601+
}

0 commit comments

Comments
 (0)