From 47697acff7e507e41441220eeaf9efbb5bc906f4 Mon Sep 17 00:00:00 2001 From: Neng Lu Date: Fri, 13 Mar 2026 10:17:47 -0700 Subject: [PATCH] add namespace get-topic-auto-creation cmd --- pkg/ctl/namespace/get_topic_auto_creation.go | 78 +++++++++++++++++++ pkg/ctl/namespace/namespace.go | 1 + .../namespace_command_registration_test.go | 37 +++++++++ pkg/ctl/namespace/topic_auto_creation_test.go | 64 +++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 pkg/ctl/namespace/get_topic_auto_creation.go create mode 100644 pkg/ctl/namespace/namespace_command_registration_test.go create mode 100644 pkg/ctl/namespace/topic_auto_creation_test.go diff --git a/pkg/ctl/namespace/get_topic_auto_creation.go b/pkg/ctl/namespace/get_topic_auto_creation.go new file mode 100644 index 00000000..387bb01b --- /dev/null +++ b/pkg/ctl/namespace/get_topic_auto_creation.go @@ -0,0 +1,78 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package namespace + +import ( + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" +) + +func GetTopicAutoCreationCmd(vc *cmdutils.VerbCmd) { + var desc cmdutils.LongDescription + desc.CommandUsedFor = "Get topic auto-creation config for a namespace" + desc.CommandPermission = "This command requires tenant admin permissions." + + var examples []cmdutils.Example + examples = append(examples, cmdutils.Example{ + Desc: desc.CommandUsedFor, + Command: "pulsarctl namespaces get-topic-auto-creation tenant/namespace", + }) + desc.CommandExamples = examples + + var out []cmdutils.Output + successOut := cmdutils.Output{ + Desc: "normal output", + Out: "{\n" + + " \"allowAutoTopicCreation\": true,\n" + + " \"topicType\": \"partitioned\",\n" + + " \"defaultNumPartitions\": 2\n" + + "}", + } + + out = append(out, successOut) + out = append(out, NsErrors...) + desc.CommandOutput = out + + vc.SetDescription( + "get-topic-auto-creation", + desc.CommandUsedFor, + desc.ToString(), + desc.ExampleToString()) + + vc.EnableOutputFlagSet() + + vc.SetRunFuncWithNameArg(func() error { + return doGetTopicAutoCreation(vc) + }, "the namespace name is not specified or the namespace name is specified more than one") +} + +func doGetTopicAutoCreation(vc *cmdutils.VerbCmd) error { + ns, err := utils.GetNamespaceName(vc.NameArg) + if err != nil { + return err + } + + admin := cmdutils.NewPulsarClient() + config, err := admin.Namespaces().GetTopicAutoCreation(*ns) + if err == nil { + cmdutils.PrintJSON(vc.Command.OutOrStdout(), config) + } + + return err +} diff --git a/pkg/ctl/namespace/namespace.go b/pkg/ctl/namespace/namespace.go index 116f260f..7698fa4e 100644 --- a/pkg/ctl/namespace/namespace.go +++ b/pkg/ctl/namespace/namespace.go @@ -43,6 +43,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { cmdutils.AddVerbCmd(flagGrouping, resourceCmd, getBacklogQuota) cmdutils.AddVerbCmd(flagGrouping, resourceCmd, setBacklogQuota) cmdutils.AddVerbCmd(flagGrouping, resourceCmd, removeBacklogQuota) + cmdutils.AddVerbCmd(flagGrouping, resourceCmd, GetTopicAutoCreationCmd) cmdutils.AddVerbCmd(flagGrouping, resourceCmd, setTopicAutoCreation) cmdutils.AddVerbCmd(flagGrouping, resourceCmd, removeTopicAutoCreation) cmdutils.AddVerbCmd(flagGrouping, resourceCmd, SetSchemaValidationEnforcedCmd) diff --git a/pkg/ctl/namespace/namespace_command_registration_test.go b/pkg/ctl/namespace/namespace_command_registration_test.go new file mode 100644 index 00000000..1c34ed82 --- /dev/null +++ b/pkg/ctl/namespace/namespace_command_registration_test.go @@ -0,0 +1,37 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package namespace + +import ( + "testing" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" +) + +func TestNamespaceCommandRegistersGetTopicAutoCreation(t *testing.T) { + cmd := Command(cmdutils.NewGrouping()) + + subCmd, _, err := cmd.Find([]string{"get-topic-auto-creation"}) + if err != nil { + t.Fatalf("expected get-topic-auto-creation to be registered: %v", err) + } + + if subCmd == nil || subCmd.Name() != "get-topic-auto-creation" { + t.Fatalf("expected get-topic-auto-creation command, got %v", subCmd) + } +} diff --git a/pkg/ctl/namespace/topic_auto_creation_test.go b/pkg/ctl/namespace/topic_auto_creation_test.go new file mode 100644 index 00000000..d0f09ff9 --- /dev/null +++ b/pkg/ctl/namespace/topic_auto_creation_test.go @@ -0,0 +1,64 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package namespace + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" + "github.com/onsi/gomega" + + "github.com/streamnative/pulsarctl/pkg/test" +) + +func TestTopicAutoCreationCmd(t *testing.T) { + g := gomega.NewWithT(t) + + nsName := fmt.Sprintf("public/test-topic-auto-creation-ns-%s", test.RandomSuffix()) + createArgs := []string{"create", nsName} + _, execErr, _, _ := TestNamespaceCommands(createNs, createArgs) + g.Expect(execErr).Should(gomega.BeNil()) + + setArgs := []string{"set-topic-auto-creation", nsName, "--type", "partitioned", "--partitions", "2"} + out, execErr, _, _ := TestNamespaceCommands(setTopicAutoCreation, setArgs) + g.Expect(execErr).Should(gomega.BeNil()) + g.Expect(out.String()).Should(gomega.Equal( + fmt.Sprintf("Set topic auto-creation config successfully for [%s]\n", nsName))) + + getArgs := []string{"get-topic-auto-creation", nsName} + g.Eventually(func(g gomega.Gomega) { + out, execErr, _, _ = TestNamespaceCommands(GetTopicAutoCreationCmd, getArgs) + g.Expect(execErr).Should(gomega.BeNil()) + + var cfg utils.TopicAutoCreationConfig + err := json.Unmarshal(out.Bytes(), &cfg) + g.Expect(err).Should(gomega.BeNil()) + g.Expect(cfg.Allow).Should(gomega.BeTrue()) + g.Expect(cfg.Type.String()).Should(gomega.Equal("partitioned")) + g.Expect(cfg.Partitions).ShouldNot(gomega.BeNil()) + g.Expect(*cfg.Partitions).Should(gomega.Equal(2)) + }).Should(gomega.Succeed()) + + removeArgs := []string{"remove-topic-auto-creation", nsName} + out, execErr, _, _ = TestNamespaceCommands(removeTopicAutoCreation, removeArgs) + g.Expect(execErr).Should(gomega.BeNil()) + g.Expect(out.String()).Should(gomega.Equal( + fmt.Sprintf("Removed topic auto-creation config successfully for [%s]\n", nsName))) +}