|
| 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/spf13/cobra" |
| 23 | + "github.com/spf13/pflag" |
| 24 | + |
| 25 | + "github.com/streamnative/pulsarctl/pkg/cmdutils" |
| 26 | + ctlutils "github.com/streamnative/pulsarctl/pkg/ctl/utils" |
| 27 | +) |
| 28 | + |
| 29 | +type setInactiveTopicPoliciesArgs struct { |
| 30 | + deleteWhileInactive bool |
| 31 | + deleteInactiveTopicsMaxInactiveDuration string |
| 32 | + inactiveTopicDeleteMode string |
| 33 | +} |
| 34 | + |
| 35 | +func SetInactiveTopicCmd(vc *cmdutils.VerbCmd) { |
| 36 | + var desc cmdutils.LongDescription |
| 37 | + desc.CommandUsedFor = "Set the inactive topic policies on a namespace" |
| 38 | + desc.CommandPermission = "This command requires tenant admin permissions." |
| 39 | + |
| 40 | + var examples []cmdutils.Example |
| 41 | + set := cmdutils.Example{ |
| 42 | + Desc: desc.CommandUsedFor, |
| 43 | + Command: "pulsarctl namespaces set-inactive-topic-policies tenant/namespace \n" + |
| 44 | + "\t--enable-delete-while-inactive true \n" + |
| 45 | + "\t--max-inactive-duration 1h \n" + |
| 46 | + "\t--delete-mode delete_when_no_subscriptions", |
| 47 | + } |
| 48 | + |
| 49 | + examples = append(examples, set) |
| 50 | + desc.CommandExamples = examples |
| 51 | + |
| 52 | + var out []cmdutils.Output |
| 53 | + out = append(out, NsErrors...) |
| 54 | + desc.CommandOutput = out |
| 55 | + |
| 56 | + vc.SetDescription( |
| 57 | + "set-inactive-topic-policies", |
| 58 | + desc.CommandUsedFor, |
| 59 | + desc.ToString(), |
| 60 | + desc.ExampleToString()) |
| 61 | + |
| 62 | + args := setInactiveTopicPoliciesArgs{} |
| 63 | + |
| 64 | + vc.FlagSetGroup.InFlagSet("Set Inactive Topic", func(flagSet *pflag.FlagSet) { |
| 65 | + flagSet.BoolVarP(&args.deleteWhileInactive, |
| 66 | + "enable-delete-while-inactive", |
| 67 | + "e", |
| 68 | + false, |
| 69 | + "Control whether deletion is enabled while inactive") |
| 70 | + |
| 71 | + flagSet.StringVarP(&args.deleteInactiveTopicsMaxInactiveDuration, |
| 72 | + "max-inactive-duration", |
| 73 | + "t", |
| 74 | + "", |
| 75 | + "Max duration of topic inactivity in seconds, "+ |
| 76 | + "topics that are inactive for longer than this value will be deleted (eg: 1s, 10s, 1m, 5h, 3d)") |
| 77 | + flagSet.StringVarP(&args.inactiveTopicDeleteMode, |
| 78 | + "delete-mode", |
| 79 | + "m", |
| 80 | + "", |
| 81 | + "Mode of delete inactive topic, "+ |
| 82 | + "Valid options are: [delete_when_no_subscriptions, delete_when_subscriptions_caught_up]") |
| 83 | + |
| 84 | + _ = cobra.MarkFlagRequired(flagSet, "delete-mode") |
| 85 | + _ = cobra.MarkFlagRequired(flagSet, "max-inactive-duration") |
| 86 | + }) |
| 87 | + vc.EnableOutputFlagSet() |
| 88 | + |
| 89 | + vc.SetRunFuncWithNameArg(func() error { |
| 90 | + return doSetInactiveTopicPolicies(vc, args) |
| 91 | + }, "the namespace name is not specified or the namespace name is specified more than one") |
| 92 | +} |
| 93 | + |
| 94 | +func doSetInactiveTopicPolicies(vc *cmdutils.VerbCmd, args setInactiveTopicPoliciesArgs) error { |
| 95 | + inactiveTopicDeleteMode, err := utils.ParseInactiveTopicDeleteMode(args.inactiveTopicDeleteMode) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + maxInactiveDuration, err := ctlutils.ParseRelativeTimeInSeconds(args.deleteInactiveTopicsMaxInactiveDuration) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + body := utils.NewInactiveTopicPolicies( |
| 106 | + &inactiveTopicDeleteMode, |
| 107 | + int(maxInactiveDuration.Seconds()), |
| 108 | + args.deleteWhileInactive) |
| 109 | + |
| 110 | + ns, err := utils.GetNamespaceName(vc.NameArg) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + admin := cmdutils.NewPulsarClient() |
| 116 | + err = admin.Namespaces().SetInactiveTopicPolicies(*ns, body) |
| 117 | + if err == nil { |
| 118 | + vc.Command.Printf("Set inactive topic policies successfully for [%s]", ns.String()) |
| 119 | + } |
| 120 | + |
| 121 | + return err |
| 122 | +} |
0 commit comments