|
| 1 | +package topic |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" |
| 5 | + "github.com/pkg/errors" |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "github.com/spf13/pflag" |
| 8 | + |
| 9 | + "github.com/streamnative/pulsarctl/pkg/cmdutils" |
| 10 | +) |
| 11 | + |
| 12 | +func SetReplicationClustersCmd(vc *cmdutils.VerbCmd) { |
| 13 | + desc := cmdutils.LongDescription{} |
| 14 | + desc.CommandUsedFor = "Set the replication clusters for a topic" |
| 15 | + desc.CommandPermission = "This command requires tenant admin permissions." |
| 16 | + |
| 17 | + var examples []cmdutils.Example |
| 18 | + setReplication := cmdutils.Example{ |
| 19 | + Desc: "Set the replication clusters for a topic", |
| 20 | + Command: "pulsarctl topics set-replication-clusters tenant/namespace/topic --clusters cluster1,cluster2", |
| 21 | + } |
| 22 | + examples = append(examples, setReplication) |
| 23 | + desc.CommandExamples = examples |
| 24 | + |
| 25 | + var out []cmdutils.Output |
| 26 | + successOut := cmdutils.Output{ |
| 27 | + Desc: "normal output", |
| 28 | + Out: "Set the replication clusters for [topic] successfully", |
| 29 | + } |
| 30 | + |
| 31 | + noTopicName := cmdutils.Output{ |
| 32 | + Desc: "you must specify a tenant/namespace/topic name, please check if the tenant/namespace/topic name is provided", |
| 33 | + Out: "[✖] the topic name is not specified or the topic name is specified more than one", |
| 34 | + } |
| 35 | + |
| 36 | + tenantNotExistError := cmdutils.Output{ |
| 37 | + Desc: "the tenant does not exist", |
| 38 | + Out: "[✖] code: 404 reason: Tenant does not exist", |
| 39 | + } |
| 40 | + |
| 41 | + nsNotExistError := cmdutils.Output{ |
| 42 | + Desc: "the namespace does not exist", |
| 43 | + Out: "[✖] code: 404 reason: Namespace (tenant/namespace) does not exist", |
| 44 | + } |
| 45 | + |
| 46 | + out = append(out, successOut, noTopicName, tenantNotExistError, nsNotExistError) |
| 47 | + desc.CommandOutput = out |
| 48 | + |
| 49 | + vc.SetDescription( |
| 50 | + "set-replication-clusters", |
| 51 | + "Set the replication clusters for a topic", |
| 52 | + desc.ToString(), |
| 53 | + desc.ExampleToString(), |
| 54 | + "set-replication-clusters", |
| 55 | + ) |
| 56 | + |
| 57 | + var clusters []string |
| 58 | + |
| 59 | + vc.FlagSetGroup.InFlagSet("Set replication clusters", func(flagSet *pflag.FlagSet) { |
| 60 | + flagSet.StringSliceVarP(&clusters, "clusters", "c", nil, |
| 61 | + "Replication cluster ids.") |
| 62 | + _ = cobra.MarkFlagRequired(flagSet, "clusters") |
| 63 | + }) |
| 64 | + vc.EnableOutputFlagSet() |
| 65 | + |
| 66 | + vc.SetRunFuncWithNameArg(func() error { |
| 67 | + return doSetReplicationClusters(vc, clusters) |
| 68 | + }, "the topic name is not specified or the topic name is specified more than one") |
| 69 | +} |
| 70 | + |
| 71 | +func doSetReplicationClusters(vc *cmdutils.VerbCmd, clusters []string) error { |
| 72 | + topic := vc.NameArg |
| 73 | + |
| 74 | + if len(clusters) == 0 { |
| 75 | + return errors.New("clusters cannot be empty") |
| 76 | + } |
| 77 | + |
| 78 | + admin := cmdutils.NewPulsarClient() |
| 79 | + topicName, err := utils.GetTopicName(topic) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + err = admin.Topics().SetReplicationClusters(*topicName, clusters) |
| 85 | + if err == nil { |
| 86 | + vc.Command.Printf("Set the replication clusters successfully on [%s]\n", topic) |
| 87 | + } |
| 88 | + |
| 89 | + return err |
| 90 | +} |
0 commit comments