-
Notifications
You must be signed in to change notification settings - Fork 67
/
namespaces.go
111 lines (95 loc) · 3.45 KB
/
namespaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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 brokers
import (
"errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/pulsar"
)
func getOwnedNamespacesCmd(vc *cmdutils.VerbCmd) {
desc := pulsar.LongDescription{}
desc.CommandUsedFor = "List namespaces owned by the broker"
desc.CommandPermission = "This command requires super-user permissions."
var examples []pulsar.Example
list := pulsar.Example{
Desc: "List namespaces owned by the broker",
Command: "pulsarctl brokers namespaces (cluster-name) --url (eg:127.0.0.1:8080)",
}
examples = append(examples, list)
desc.CommandExamples = examples
var out []pulsar.Output
successOut := pulsar.Output{
Desc: "normal output",
Out: "{\n" +
" \"public/functions/0x40000000_0x80000000\": {\n" +
" \"broker_assignment\": \"shared\",\n" +
" \"is_controlled\": false,\n" +
" \"is_active\": true\n" +
" },\n" +
" \"pulsar/standalone/127.0.0.1:8080/0x00000000_0xffffffff\": {\n" +
" \"broker_assignment\": \"shared\",\n" +
" \"is_controlled\": false,\n" +
" \"is_active\": true\n" +
" }\n" +
"}",
}
var argsError = pulsar.Output{
Desc: "the cluster name is not specified or the cluster name is specified more than one",
Out: "[✖] the cluster name is not specified or the cluster name is specified more than one",
}
var urlError = pulsar.Output{
Desc: "The correct url is not provided, please check the `--url` arg.",
Out: "[✖] Get (broker url)/admin/v2/brokers/standalone/127.0.0.1:6650/ownedNamespaces: EOF",
}
out = append(out, successOut, argsError, urlError)
desc.CommandOutput = out
vc.SetDescription(
"namespaces",
"List namespaces owned by the broker",
desc.ToString(),
desc.ExampleToString(),
"namespaces")
brokerData := &pulsar.BrokerData{}
vc.SetRunFuncWithNameArg(func() error {
return doOwnedNamespaces(vc, brokerData)
}, "the cluster name is not specified or the cluster name is specified more than one")
// register the params
vc.FlagSetGroup.InFlagSet("BrokerData", func(flagSet *pflag.FlagSet) {
flagSet.StringVar(
&brokerData.URL,
"url",
"",
"broker-url")
cobra.MarkFlagRequired(flagSet, "url")
})
}
func doOwnedNamespaces(vc *cmdutils.VerbCmd, brokerData *pulsar.BrokerData) error {
clusterName := vc.NameArg
if clusterName == "" {
return errors.New("should specified a cluster name")
}
admin := cmdutils.NewPulsarClient()
namespaces, err := admin.Brokers().GetOwnedNamespaces(clusterName, brokerData.URL)
if err != nil {
cmdutils.PrintError(vc.Command.OutOrStderr(), err)
} else {
cmdutils.PrintJSON(vc.Command.OutOrStdout(), namespaces)
}
return err
}