Skip to content

Commit 28963ec

Browse files
committed
add namespace get-topic-auto-creation cmd
1 parent 6c4f231 commit 28963ec

4 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 namespace
19+
20+
import (
21+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils"
22+
23+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
24+
)
25+
26+
func GetTopicAutoCreationCmd(vc *cmdutils.VerbCmd) {
27+
var desc cmdutils.LongDescription
28+
desc.CommandUsedFor = "Get topic auto-creation config for a namespace"
29+
desc.CommandPermission = "This command requires tenant admin permissions."
30+
31+
var examples []cmdutils.Example
32+
examples = append(examples, cmdutils.Example{
33+
Desc: desc.CommandUsedFor,
34+
Command: "pulsarctl namespaces get-topic-auto-creation tenant/namespace",
35+
})
36+
desc.CommandExamples = examples
37+
38+
var out []cmdutils.Output
39+
successOut := cmdutils.Output{
40+
Desc: "normal output",
41+
Out: "{\n" +
42+
" \"allowAutoTopicCreation\": true,\n" +
43+
" \"topicType\": \"partitioned\",\n" +
44+
" \"defaultNumPartitions\": 2\n" +
45+
"}",
46+
}
47+
48+
out = append(out, successOut)
49+
out = append(out, NsErrors...)
50+
desc.CommandOutput = out
51+
52+
vc.SetDescription(
53+
"get-topic-auto-creation",
54+
desc.CommandUsedFor,
55+
desc.ToString(),
56+
desc.ExampleToString())
57+
58+
vc.EnableOutputFlagSet()
59+
60+
vc.SetRunFuncWithNameArg(func() error {
61+
return doGetTopicAutoCreation(vc)
62+
}, "the namespace name is not specified or the namespace name is specified more than one")
63+
}
64+
65+
func doGetTopicAutoCreation(vc *cmdutils.VerbCmd) error {
66+
ns, err := utils.GetNamespaceName(vc.NameArg)
67+
if err != nil {
68+
return err
69+
}
70+
71+
admin := cmdutils.NewPulsarClient()
72+
config, err := admin.Namespaces().GetTopicAutoCreation(*ns)
73+
if err == nil {
74+
cmdutils.PrintJSON(vc.Command.OutOrStdout(), config)
75+
}
76+
77+
return err
78+
}

pkg/ctl/namespace/namespace.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
4343
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, getBacklogQuota)
4444
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, setBacklogQuota)
4545
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, removeBacklogQuota)
46+
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, GetTopicAutoCreationCmd)
4647
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, setTopicAutoCreation)
4748
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, removeTopicAutoCreation)
4849
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, SetSchemaValidationEnforcedCmd)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 namespace
19+
20+
import (
21+
"testing"
22+
23+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
24+
)
25+
26+
func TestNamespaceCommandRegistersGetTopicAutoCreation(t *testing.T) {
27+
cmd := Command(cmdutils.NewGrouping())
28+
29+
subCmd, _, err := cmd.Find([]string{"get-topic-auto-creation"})
30+
if err != nil {
31+
t.Fatalf("expected get-topic-auto-creation to be registered: %v", err)
32+
}
33+
34+
if subCmd == nil || subCmd.Name() != "get-topic-auto-creation" {
35+
t.Fatalf("expected get-topic-auto-creation command, got %v", subCmd)
36+
}
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 namespace
19+
20+
import (
21+
"encoding/json"
22+
"fmt"
23+
"testing"
24+
25+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils"
26+
"github.com/onsi/gomega"
27+
28+
"github.com/streamnative/pulsarctl/pkg/test"
29+
)
30+
31+
func TestTopicAutoCreationCmd(t *testing.T) {
32+
g := gomega.NewWithT(t)
33+
34+
nsName := fmt.Sprintf("public/test-topic-auto-creation-ns-%s", test.RandomSuffix())
35+
createArgs := []string{"create", nsName}
36+
_, execErr, _, _ := TestNamespaceCommands(createNs, createArgs)
37+
g.Expect(execErr).Should(gomega.BeNil())
38+
39+
setArgs := []string{"set-topic-auto-creation", nsName, "--type", "partitioned", "--partitions", "2"}
40+
out, execErr, _, _ := TestNamespaceCommands(setTopicAutoCreation, setArgs)
41+
g.Expect(execErr).Should(gomega.BeNil())
42+
g.Expect(out.String()).Should(gomega.Equal(
43+
fmt.Sprintf("Set topic auto-creation config successfully for [%s]\n", nsName)))
44+
45+
getArgs := []string{"get-topic-auto-creation", nsName}
46+
g.Eventually(func(g gomega.Gomega) {
47+
out, execErr, _, _ = TestNamespaceCommands(GetTopicAutoCreationCmd, getArgs)
48+
g.Expect(execErr).Should(gomega.BeNil())
49+
50+
var cfg utils.TopicAutoCreationConfig
51+
err := json.Unmarshal(out.Bytes(), &cfg)
52+
g.Expect(err).Should(gomega.BeNil())
53+
g.Expect(cfg.Allow).Should(gomega.BeTrue())
54+
g.Expect(cfg.Type.String()).Should(gomega.Equal("partitioned"))
55+
g.Expect(cfg.Partitions).ShouldNot(gomega.BeNil())
56+
g.Expect(*cfg.Partitions).Should(gomega.Equal(2))
57+
}).Should(gomega.Succeed())
58+
59+
removeArgs := []string{"remove-topic-auto-creation", nsName}
60+
out, execErr, _, _ = TestNamespaceCommands(removeTopicAutoCreation, removeArgs)
61+
g.Expect(execErr).Should(gomega.BeNil())
62+
g.Expect(out.String()).Should(gomega.Equal(
63+
fmt.Sprintf("Removed topic auto-creation config successfully for [%s]\n", nsName)))
64+
}

0 commit comments

Comments
 (0)