Skip to content

Commit 08c00dd

Browse files
committed
feat: add multiple admin getter commands for pulsarctl
1 parent 66ae3ef commit 08c00dd

23 files changed

Lines changed: 1952 additions & 7 deletions
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
23+
)
24+
25+
func getTopicAutoCreation(vc *cmdutils.VerbCmd) {
26+
desc := cmdutils.LongDescription{}
27+
desc.CommandUsedFor = "Get topic auto-creation config for a namespace"
28+
desc.CommandPermission = "This command requires tenant admin permissions."
29+
30+
desc.CommandExamples = []cmdutils.Example{
31+
{
32+
Desc: "Get topic auto-creation config for a namespace",
33+
Command: "pulsarctl namespaces get-auto-topic-creation tenant/namespace",
34+
},
35+
}
36+
37+
vc.SetDescription(
38+
"get-auto-topic-creation",
39+
"Get topic auto-creation config for a namespace",
40+
desc.ToString(),
41+
desc.ExampleToString(),
42+
"get-auto-topic-creation",
43+
)
44+
45+
vc.SetRunFuncWithNameArg(func() error {
46+
return doGetTopicAutoCreation(vc)
47+
}, "the namespace name is not specified or the namespace name is specified more than one")
48+
vc.EnableOutputFlagSet()
49+
}
50+
51+
func doGetTopicAutoCreation(vc *cmdutils.VerbCmd) error {
52+
ns, err := utils.GetNamespaceName(vc.NameArg)
53+
if err != nil {
54+
return err
55+
}
56+
57+
admin := cmdutils.NewPulsarClient()
58+
config, err := admin.Namespaces().GetTopicAutoCreation(*ns)
59+
if err == nil {
60+
err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), cmdutils.NewOutputContent().WithObject(config))
61+
}
62+
return err
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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/stretchr/testify/assert"
24+
)
25+
26+
func TestGetTopicAutoCreationNameError(t *testing.T) {
27+
_, _, nameErr, _ := TestNamespaceCommands(getTopicAutoCreation, []string{"get-auto-topic-creation"})
28+
assert.NotNil(t, nameErr)
29+
assert.Equal(t, "the namespace name is not specified or the namespace name is specified more than one", nameErr.Error())
30+
}

pkg/ctl/namespace/namespace.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
4646
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, setBacklogQuota)
4747
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, removeBacklogQuota)
4848
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, setTopicAutoCreation)
49+
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, getTopicAutoCreation)
4950
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, removeTopicAutoCreation)
5051
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, SetSchemaValidationEnforcedCmd)
5152
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, GetSchemaValidationEnforcedCmd)
@@ -84,6 +85,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
8485
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, RevokePermissionsCmd)
8586
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, GrantSubPermissionsCmd)
8687
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, RevokeSubPermissionsCmd)
88+
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, GetSubPermissionsCmd)
8789
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, ClearBacklogCmd)
8890
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, GetDispatchRateCmd)
8991
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, SetDispatchRateCmd)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
23+
)
24+
25+
func GetSubPermissionsCmd(vc *cmdutils.VerbCmd) {
26+
desc := cmdutils.LongDescription{}
27+
desc.CommandUsedFor = "Get permissions to access subscription admin API"
28+
desc.CommandPermission = "This command requires tenant admin permissions."
29+
30+
desc.CommandExamples = []cmdutils.Example{
31+
{
32+
Desc: "Get permissions to access subscription admin API",
33+
Command: "pulsarctl namespaces subscription-permission tenant/namespace",
34+
},
35+
}
36+
37+
vc.SetDescription(
38+
"subscription-permission",
39+
"Get permissions to access subscription admin API",
40+
desc.ToString(),
41+
desc.ExampleToString(),
42+
"subscription-permission",
43+
)
44+
45+
vc.SetRunFuncWithNameArg(func() error {
46+
return doGetSubPermissions(vc)
47+
}, "the namespace name is not specified or the namespace name is specified more than one")
48+
vc.EnableOutputFlagSet()
49+
}
50+
51+
func doGetSubPermissions(vc *cmdutils.VerbCmd) error {
52+
ns, err := utils.GetNamespaceName(vc.NameArg)
53+
if err != nil {
54+
return err
55+
}
56+
57+
admin := cmdutils.NewPulsarClient()
58+
permissions, err := admin.Namespaces().GetSubPermissions(*ns)
59+
if err == nil {
60+
err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), cmdutils.NewOutputContent().WithObject(permissions))
61+
}
62+
return err
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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/stretchr/testify/assert"
24+
)
25+
26+
func TestSubscriptionPermissionNameError(t *testing.T) {
27+
_, _, nameErr, _ := TestNamespaceCommands(GetSubPermissionsCmd, []string{"subscription-permission"})
28+
assert.NotNil(t, nameErr)
29+
assert.Equal(t, "the namespace name is not specified or the namespace name is specified more than one", nameErr.Error())
30+
}

pkg/ctl/sinks/reload.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 sinks
19+
20+
import (
21+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config"
22+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
23+
)
24+
25+
func reloadSinksCmd(vc *cmdutils.VerbCmd) {
26+
desc := cmdutils.LongDescription{}
27+
desc.CommandUsedFor = "Reload built-in Pulsar IO sinks"
28+
desc.CommandPermission = "This command requires tenant admin permissions."
29+
30+
desc.CommandExamples = []cmdutils.Example{
31+
{
32+
Desc: "Reload built-in Pulsar IO sinks",
33+
Command: "pulsarctl sinks reload",
34+
},
35+
}
36+
37+
vc.SetDescription("reload", "Reload built-in Pulsar IO sinks", desc.ToString(), desc.ExampleToString(), "reload")
38+
vc.SetRunFunc(func() error {
39+
return doReloadSinks(vc)
40+
})
41+
}
42+
43+
func doReloadSinks(vc *cmdutils.VerbCmd) error {
44+
admin := cmdutils.NewPulsarClientWithAPIVersion(config.V3)
45+
err := admin.Sinks().ReloadBuiltInSinks()
46+
if err == nil {
47+
vc.Command.Println("Reloaded built-in sinks successfully")
48+
}
49+
return err
50+
}

pkg/ctl/sinks/sinks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
4141
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, restartSinksCmd)
4242
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, statusSinksCmd)
4343
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, listBuiltInSinksCmd)
44+
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, reloadSinksCmd)
4445

4546
return resourceCmd
4647
}

pkg/ctl/sources/reload.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 sources
19+
20+
import (
21+
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config"
22+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
23+
)
24+
25+
func reloadSourcesCmd(vc *cmdutils.VerbCmd) {
26+
desc := cmdutils.LongDescription{}
27+
desc.CommandUsedFor = "Reload built-in Pulsar IO sources"
28+
desc.CommandPermission = "This command requires tenant admin permissions."
29+
30+
desc.CommandExamples = []cmdutils.Example{
31+
{
32+
Desc: "Reload built-in Pulsar IO sources",
33+
Command: "pulsarctl sources reload",
34+
},
35+
}
36+
37+
vc.SetDescription("reload", "Reload built-in Pulsar IO sources", desc.ToString(), desc.ExampleToString(), "reload")
38+
vc.SetRunFunc(func() error {
39+
return doReloadSources(vc)
40+
})
41+
}
42+
43+
func doReloadSources(vc *cmdutils.VerbCmd) error {
44+
admin := cmdutils.NewPulsarClientWithAPIVersion(config.V3)
45+
err := admin.Sources().ReloadBuiltInSources()
46+
if err == nil {
47+
vc.Command.Println("Reloaded built-in sources successfully")
48+
}
49+
return err
50+
}

pkg/ctl/sources/sources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
4141
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, restartSourcesCmd)
4242
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, statusSourcesCmd)
4343
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, listBuiltInSourcesCmd)
44+
cmdutils.AddVerbCmd(flagGrouping, resourceCmd, reloadSourcesCmd)
4445

4546
return resourceCmd
4647
}

0 commit comments

Comments
 (0)