Skip to content

Commit 3388eae

Browse files
[Improve] Implement GetTopicAutoCreation (#1151)
### Motivation The `GetTopicAutoCreation` endpoint is missed, needs to add it ### Modifications Implement the `GetTopicAutoCreation` method
1 parent ad7887e commit 3388eae

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

pulsaradmin/pkg/admin/namespace.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ type Namespaces interface {
7575
// RemoveBacklogQuota removes a backlog quota policy from a namespace
7676
RemoveBacklogQuota(namespace string) error
7777

78+
// GetTopicAutoCreation returns the topic auto-creation config for a namespace
79+
GetTopicAutoCreation(namespace utils.NameSpaceName) (*utils.TopicAutoCreationConfig, error)
80+
7881
// SetTopicAutoCreation sets topic auto-creation config for a namespace, overriding broker settings
7982
SetTopicAutoCreation(namespace utils.NameSpaceName, config utils.TopicAutoCreationConfig) error
8083

@@ -445,6 +448,13 @@ func (n *namespaces) RemoveBacklogQuota(namespace string) error {
445448
return n.pulsar.Client.DeleteWithQueryParams(endpoint, params)
446449
}
447450

451+
func (n *namespaces) GetTopicAutoCreation(namespace utils.NameSpaceName) (*utils.TopicAutoCreationConfig, error) {
452+
var topicAutoCreation utils.TopicAutoCreationConfig
453+
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "autoTopicCreation")
454+
err := n.pulsar.Client.Get(endpoint, &topicAutoCreation)
455+
return &topicAutoCreation, err
456+
}
457+
448458
func (n *namespaces) SetTopicAutoCreation(namespace utils.NameSpaceName, config utils.TopicAutoCreationConfig) error {
449459
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "autoTopicCreation")
450460
return n.pulsar.Client.Post(endpoint, &config)
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package admin
19+
20+
import (
21+
"testing"
22+
23+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config"
24+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest"
25+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils"
26+
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
28+
)
29+
30+
func ptr(n int) *int {
31+
return &n
32+
}
33+
34+
func TestSetTopicAutoCreation(t *testing.T) {
35+
config := &config.Config{}
36+
admin, err := New(config)
37+
require.NoError(t, err)
38+
require.NotNil(t, admin)
39+
40+
tests := []struct {
41+
name string
42+
namespace string
43+
config utils.TopicAutoCreationConfig
44+
errReason string
45+
}{
46+
{
47+
name: "Set partitioned type topic auto creation",
48+
namespace: "public/default",
49+
config: utils.TopicAutoCreationConfig{
50+
Allow: true,
51+
Type: utils.Partitioned,
52+
Partitions: ptr(3),
53+
},
54+
errReason: "",
55+
},
56+
{
57+
name: "Set partitioned type topic auto creation without partitions",
58+
namespace: "public/default",
59+
config: utils.TopicAutoCreationConfig{
60+
Allow: true,
61+
Type: utils.Partitioned,
62+
},
63+
errReason: "Invalid configuration for autoTopicCreationOverride. the detail is [defaultNumPartitions] " +
64+
"cannot be null when the type is partitioned.",
65+
},
66+
{
67+
name: "Set partitioned type topic auto creation with partitions < 1",
68+
namespace: "public/default",
69+
config: utils.TopicAutoCreationConfig{
70+
Allow: true,
71+
Type: utils.Partitioned,
72+
Partitions: ptr(-1),
73+
},
74+
errReason: "Invalid configuration for autoTopicCreationOverride. the detail is [defaultNumPartitions] " +
75+
"cannot be less than 1 for partition type.",
76+
},
77+
{
78+
name: "Set non-partitioned type topic auto creation",
79+
namespace: "public/default",
80+
config: utils.TopicAutoCreationConfig{
81+
Allow: true,
82+
Type: utils.NonPartitioned,
83+
},
84+
errReason: "",
85+
},
86+
{
87+
name: "Set non-partitioned type topic auto creation with partitions",
88+
namespace: "public/default",
89+
config: utils.TopicAutoCreationConfig{
90+
Allow: true,
91+
Type: utils.NonPartitioned,
92+
Partitions: ptr(3),
93+
},
94+
errReason: "Invalid configuration for autoTopicCreationOverride. the detail is [defaultNumPartitions] is " +
95+
"not allowed to be set when the type is non-partition.",
96+
},
97+
{
98+
name: "Disable topic auto creation",
99+
namespace: "public/default",
100+
config: utils.TopicAutoCreationConfig{
101+
Allow: false,
102+
},
103+
errReason: "",
104+
},
105+
{
106+
name: "Set topic auto creation on a non-exist namespace",
107+
namespace: "public/nonexist",
108+
config: utils.TopicAutoCreationConfig{
109+
Allow: true,
110+
Type: utils.NonPartitioned,
111+
},
112+
errReason: "Namespace does not exist",
113+
},
114+
{
115+
name: "Set topic auto creation on a non-exist tenant",
116+
namespace: "non-exist/default",
117+
config: utils.TopicAutoCreationConfig{
118+
Allow: true,
119+
Type: utils.NonPartitioned,
120+
},
121+
errReason: "Tenant does not exist",
122+
},
123+
}
124+
for _, tt := range tests {
125+
t.Run(tt.name, func(t *testing.T) {
126+
namespace, _ := utils.GetNamespaceName(tt.namespace)
127+
err := admin.Namespaces().SetTopicAutoCreation(*namespace, tt.config)
128+
if tt.errReason == "" {
129+
assert.Equal(t, nil, err)
130+
131+
err = admin.Namespaces().RemoveTopicAutoCreation(*namespace)
132+
assert.Equal(t, nil, err)
133+
}
134+
if err != nil {
135+
restError := err.(rest.Error)
136+
assert.Equal(t, tt.errReason, restError.Reason)
137+
}
138+
})
139+
}
140+
}
141+
142+
func TestGetTopicAutoCreation(t *testing.T) {
143+
config := &config.Config{}
144+
admin, err := New(config)
145+
require.NoError(t, err)
146+
require.NotNil(t, admin)
147+
148+
namespace, _ := utils.GetNamespaceName("public/default")
149+
150+
// set the topic auto creation config and get it
151+
err = admin.Namespaces().SetTopicAutoCreation(*namespace, utils.TopicAutoCreationConfig{
152+
Allow: true,
153+
Type: utils.NonPartitioned,
154+
})
155+
assert.Equal(t, nil, err)
156+
topicAutoCreation, err := admin.Namespaces().GetTopicAutoCreation(*namespace)
157+
assert.Equal(t, nil, err)
158+
expected := utils.TopicAutoCreationConfig{
159+
Allow: true,
160+
Type: utils.NonPartitioned,
161+
}
162+
assert.Equal(t, expected, *topicAutoCreation)
163+
164+
// remove the topic auto creation config and get it
165+
err = admin.Namespaces().RemoveTopicAutoCreation(*namespace)
166+
assert.Equal(t, nil, err)
167+
168+
topicAutoCreation, err = admin.Namespaces().GetTopicAutoCreation(*namespace)
169+
assert.Equal(t, nil, err)
170+
expected = utils.TopicAutoCreationConfig{
171+
Allow: false,
172+
Type: "",
173+
}
174+
assert.Equal(t, expected, *topicAutoCreation)
175+
}

0 commit comments

Comments
 (0)