Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-291622: Add delete command for Atlas Stream Processing PrivateLinks #3671

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions docs/command/atlas-streams-privateLinks-delete.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
.. _atlas-streams-privateLinks-delete:

=================================
atlas streams privateLinks delete
=================================

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Deletes an Atlas Stream Processing PrivateLink endpoint.

To use this command, you must authenticate with a user account or an API key with any of the following roles: Project Owner, Project Stream Processing Owner.

Syntax
------

.. code-block::
:caption: Command Syntax

atlas streams privateLinks delete <connectionID> [options]

.. Code end marker, please don't delete this comment

Arguments
---------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - connectionID
- string
- true
- ID of the PrivateLink endpoint.

Options
-------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - --force
-
- false
- Flag that indicates whether to skip the confirmation prompt before proceeding with the requested action.
* - -h, --help
-
- false
- help for delete
* - --projectId
- string
- false
- Hexadecimal string that identifies the project to use. This option overrides the settings in the configuration file or environment variable.

Inherited Options
-----------------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - -P, --profile
- string
- false
- Name of the profile to use from your configuration file. To learn about profiles for the Atlas CLI, see https://dochub.mongodb.org/core/atlas-cli-save-connection-settings.

Output
------

If the command succeeds, the CLI returns output similar to the following sample. Values in brackets represent your values.

.. code-block::

Atlas Stream Processing PrivateLink endpoint '<Name>' deleted.


Examples
--------

.. code-block::
:copyable: false

# delete an Atlas Stream Processing PrivateLink endpoint:
atlas streams privateLink delete 5e2211c17a3e5a48f5497de3

2 changes: 2 additions & 0 deletions docs/command/atlas-streams-privateLinks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Related Commands
----------------

* :ref:`atlas-streams-privateLinks-create` - Creates a PrivateLink endpoint that can be used as an Atlas Stream Processor connection.
* :ref:`atlas-streams-privateLinks-delete` - Deletes an Atlas Stream Processing PrivateLink endpoint.
* :ref:`atlas-streams-privateLinks-describe` - Describes a PrivateLink endpoint that can be used as an Atlas Stream Processor connection.
* :ref:`atlas-streams-privateLinks-list` - Lists the PrivateLink endpoints in the project that can be used as Atlas Stream Processor connections.

Expand All @@ -60,6 +61,7 @@ Related Commands
:titlesonly:

create </command/atlas-streams-privateLinks-create>
delete </command/atlas-streams-privateLinks-delete>
describe </command/atlas-streams-privateLinks-describe>
list </command/atlas-streams-privateLinks-list>

90 changes: 90 additions & 0 deletions internal/cli/streams/privatelink/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2025 MongoDB Inc
//
// Licensed 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 privatelink

import (
"context"
"fmt"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
)

var successDeleteTemplate = "Atlas Stream Processing PrivateLink endpoint '%s' deleted.\n"
var failDeleteTemplate = "Atlas Stream Processing PrivateLink endpoint not deleted"

type DeleteOpts struct {
cli.ProjectOpts
*cli.DeleteOpts
store store.PrivateLinkDeleter
}

func (opts *DeleteOpts) Run() error {
return opts.Delete(opts.store.DeletePrivateLinkEndpoint, opts.ConfigProjectID())
}

func (opts *DeleteOpts) initStore(ctx context.Context) func() error {
return func() error {
var err error
opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx))
return err
}
}

// atlas streams privateLink deleted <connectionID>
// Deletes a PrivateLink endpoint.
func DeleteBuilder() *cobra.Command {
opts := &DeleteOpts{
DeleteOpts: cli.NewDeleteOpts(successDeleteTemplate, failDeleteTemplate),
}
cmd := &cobra.Command{
Use: "delete <connectionID>",
Aliases: []string{"rm"},
Short: "Deletes an Atlas Stream Processing PrivateLink endpoint.",
Long: fmt.Sprintf(usage.RequiredOneOfRoles, commandRoles),
Args: require.ExactArgs(1),
Annotations: map[string]string{
"connectionIDDesc": "ID of the PrivateLink endpoint.",
"output": successDeleteTemplate,
},
Example: `# delete an Atlas Stream Processing PrivateLink endpoint:
atlas streams privateLink delete 5e2211c17a3e5a48f5497de3
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := opts.PreRunE(
opts.ValidateProjectID,
opts.initStore(cmd.Context()),
); err != nil {
return err
}
opts.Entry = args[0]
return opts.Prompt()
},
RunE: func(_ *cobra.Command, _ []string) error {
return opts.Run()
},
}

cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)

opts.AddProjectOptsFlags(cmd)

return cmd
}
97 changes: 97 additions & 0 deletions internal/cli/streams/privatelink/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2025 MongoDB Inc
//
// Licensed 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 privatelink

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/stretchr/testify/require"
)

func TestDeleteOpts_Run(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to have tests asserting the success and failure templates? I can see an argument that is validating the cli delete helper so feel free to tell me "no" 🙃

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found a clean way to check this in unit tests and we aren't verifying the output for other delete tests.

I added an additional test for the non-confirm delete case and we have an e2e test that verifies output for a successful delete.

t.Run("should call the store delete privateLink method with the correct parameters", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockPrivateLinkDeleter(ctrl)

const projectID = "a-project-id"
const connectionID = "the-connection-id"

deleteOpts := &DeleteOpts{
store: mockStore,
ProjectOpts: cli.ProjectOpts{
ProjectID: projectID,
},
DeleteOpts: &cli.DeleteOpts{
Confirm: true,
Entry: connectionID,
},
}

mockStore.
EXPECT().
DeletePrivateLinkEndpoint(gomock.Eq(projectID), gomock.Eq(connectionID)).
Times(1)

require.NoError(t, deleteOpts.Run())
})

t.Run("should delete without error", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockPrivateLinkDeleter(ctrl)

deleteOpts := &DeleteOpts{
DeleteOpts: &cli.DeleteOpts{
Entry: "some-connection-id",
Confirm: true,
},
store: mockStore,
}

mockStore.
EXPECT().
DeletePrivateLinkEndpoint(gomock.Any(), gomock.Any()).
Return(nil).
Times(1)

err := deleteOpts.Run()

require.NoError(t, err)
})

t.Run("should not call delete if confirm is false", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockPrivateLinkDeleter(ctrl)

deleteOpts := &DeleteOpts{
DeleteOpts: &cli.DeleteOpts{
Entry: "another-connection-id",
Confirm: false,
},
store: mockStore,
}

mockStore.
EXPECT().
DeletePrivateLinkEndpoint(gomock.Any(), gomock.Any()).
Times(0)

err := deleteOpts.Run()

require.NoError(t, err)
})
}
1 change: 1 addition & 0 deletions internal/cli/streams/privatelink/privatelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Builder() *cobra.Command {
CreateBuilder(),
ListBuilder(),
DescribeBuilder(),
DeleteBuilder(),
)

return cmd
Expand Down
39 changes: 38 additions & 1 deletion internal/mocks/mock_streams.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion internal/store/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
atlasv2 "go.mongodb.org/atlas-sdk/v20241113005/admin"
)

//go:generate mockgen -destination=../mocks/mock_streams.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store StreamsLister,StreamsDescriber,StreamsCreator,StreamsDeleter,StreamsUpdater,StreamsDownloader,ConnectionCreator,ConnectionDeleter,ConnectionUpdater,StreamsConnectionDescriber,StreamsConnectionLister,PrivateLinkCreator,PrivateLinkLister,PrivateLinkDescriber
//go:generate mockgen -destination=../mocks/mock_streams.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store StreamsLister,StreamsDescriber,StreamsCreator,StreamsDeleter,StreamsUpdater,StreamsDownloader,ConnectionCreator,ConnectionDeleter,ConnectionUpdater,StreamsConnectionDescriber,StreamsConnectionLister,PrivateLinkCreator,PrivateLinkLister,PrivateLinkDescriber,PrivateLinkDeleter

type StreamsLister interface {
ProjectStreams(*atlasv2.ListStreamInstancesApiParams) (*atlasv2.PaginatedApiStreamsTenant, error)
Expand Down Expand Up @@ -79,6 +79,10 @@ type PrivateLinkDescriber interface {
DescribePrivateLinkEndpoint(projectID, connectionID string) (*atlasv2.StreamsPrivateLinkConnection, error)
}

type PrivateLinkDeleter interface {
DeletePrivateLinkEndpoint(projectID, connectionID string) error
}

func (s *Store) ProjectStreams(opts *atlasv2.ListStreamInstancesApiParams) (*atlasv2.PaginatedApiStreamsTenant, error) {
result, _, err := s.clientv2.StreamsApi.ListStreamInstancesWithParams(s.ctx, opts).Execute()
return result, err
Expand Down Expand Up @@ -159,3 +163,8 @@ func (s *Store) DescribePrivateLinkEndpoint(projectID, connectionID string) (*at
result, _, err := s.clientv2.StreamsApi.GetPrivateLinkConnection(s.ctx, projectID, connectionID).Execute()
return result, err
}

func (s *Store) DeletePrivateLinkEndpoint(projectID, connectionID string) error {
_, _, err := s.clientv2.StreamsApi.DeletePrivateLinkConnection(s.ctx, projectID, connectionID).Execute()
return err
}
Loading
Loading