|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package privatelink |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli" |
| 22 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require" |
| 23 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config" |
| 24 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" |
| 25 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" |
| 26 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +var successDeleteTemplate = "Atlas Stream Processing PrivateLink endpoint '%s' deleted.\n" |
| 31 | +var failDeleteTemplate = "Atlas Stream Processing PrivateLink endpoint not deleted" |
| 32 | + |
| 33 | +type DeleteOpts struct { |
| 34 | + cli.ProjectOpts |
| 35 | + *cli.DeleteOpts |
| 36 | + store store.PrivateLinkDeleter |
| 37 | +} |
| 38 | + |
| 39 | +func (opts *DeleteOpts) Run() error { |
| 40 | + return opts.Delete(opts.store.DeletePrivateLinkEndpoint, opts.ConfigProjectID()) |
| 41 | +} |
| 42 | + |
| 43 | +func (opts *DeleteOpts) initStore(ctx context.Context) func() error { |
| 44 | + return func() error { |
| 45 | + var err error |
| 46 | + opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx)) |
| 47 | + return err |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// atlas streams privateLink deleted <connectionID> |
| 52 | +// Deletes a PrivateLink endpoint. |
| 53 | +func DeleteBuilder() *cobra.Command { |
| 54 | + opts := &DeleteOpts{ |
| 55 | + DeleteOpts: cli.NewDeleteOpts(successDeleteTemplate, failDeleteTemplate), |
| 56 | + } |
| 57 | + cmd := &cobra.Command{ |
| 58 | + Use: "delete <connectionID>", |
| 59 | + Aliases: []string{"rm"}, |
| 60 | + Short: "Deletes an Atlas Stream Processing PrivateLink endpoint.", |
| 61 | + Long: fmt.Sprintf(usage.RequiredOneOfRoles, commandRoles), |
| 62 | + Args: require.ExactArgs(1), |
| 63 | + Annotations: map[string]string{ |
| 64 | + "connectionIDDesc": "ID of the PrivateLink endpoint.", |
| 65 | + "output": successDeleteTemplate, |
| 66 | + }, |
| 67 | + Example: `# delete an Atlas Stream Processing PrivateLink endpoint: |
| 68 | + atlas streams privateLink delete 5e2211c17a3e5a48f5497de3 |
| 69 | +`, |
| 70 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 71 | + if err := opts.PreRunE( |
| 72 | + opts.ValidateProjectID, |
| 73 | + opts.initStore(cmd.Context()), |
| 74 | + ); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + opts.Entry = args[0] |
| 78 | + return opts.Prompt() |
| 79 | + }, |
| 80 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 81 | + return opts.Run() |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force) |
| 86 | + |
| 87 | + opts.AddProjectOptsFlags(cmd) |
| 88 | + |
| 89 | + return cmd |
| 90 | +} |
0 commit comments