Skip to content

Commit

Permalink
Merge branch 'master' into CLOUDP-202283
Browse files Browse the repository at this point in the history
  • Loading branch information
blva committed Oct 4, 2023
2 parents 6a65a9b + 94dc62a commit 8925e19
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 39 deletions.
42 changes: 42 additions & 0 deletions docs/atlascli/command/atlas-deployments-delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ atlas deployments delete

Delete a deployment.

The command prompts you to confirm the operation when you run the command without the --force option.

Deleting an Atlas deployment also deletes any backup snapshots for that cluster.
Deleting a Local deployment also deletes any local data volumes.

To use this command, you must authenticate with a user account or an API key that has the Project Owner role.

Syntax
------

Expand Down Expand Up @@ -59,6 +66,22 @@ Options
-
- 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.
* - --type
- string
- false
- Type of deployment that you want to create. Valid values are ATLAS or LOCAL.
* - -w, --watch
-
- false
- Watch the command until it completes its execution or the watch times out. To set the time that the watch times out, use the --watchTimeout option.
* - --watchTimeout
- uint
- false
- Time in seconds until a watch times out. After a watch times out, the CLI no longer watches the command.

Inherited Options
-----------------
Expand Down Expand Up @@ -86,3 +109,22 @@ If the command succeeds, the CLI returns output similar to the following sample.
Deployment '<Name>' deleted


Examples
--------

.. code-block::

# Remove an Atlas deployment named myDeployment after prompting for a confirmation:
atlas deployments delete myDeployment --type ATLAS


.. code-block::

# Remove an Atlas deployment named myDeployment without requiring confirmation:
atlas deployments delete myDeployment --type ATLAS --force


.. code-block::

# Remove an Local deployment named myDeployment without requiring confirmation:
atlas deployments delete myDeployment --type LOCAL --force
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/kms v1.24.5
github.com/briandowns/spinner v1.23.0
github.com/containers/common v0.56.0
github.com/containers/podman/v4 v4.6.2
github.com/containers/podman/v4 v4.7.0
github.com/creack/pty v1.1.18
github.com/denisbrodbeck/machineid v1.0.1
github.com/evergreen-ci/shrub v0.0.0-20230905131908-6b01f04bb1d2
Expand Down Expand Up @@ -95,7 +95,7 @@ require (
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.1.8 // indirect
github.com/containers/storage v1.50.2 // indirect
github.com/coreos/stream-metadata-go v0.4.2 // indirect
github.com/coreos/stream-metadata-go v0.4.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
Expand Down Expand Up @@ -147,9 +147,9 @@ require (
github.com/montanaflynn/stats v0.7.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/runc v1.1.9 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 // indirect
github.com/opencontainers/selinux v1.11.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 h1:Qzk5C6cYgle
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01/go.mod h1:9rfv8iPl1ZP7aqh9YA68wnZv2NUDbXdcdPHVz0pFbPY=
github.com/containers/ocicrypt v1.1.8 h1:saSBF0/8DyPUjzcxMVzL2OBUWCkvRvqIm75pu0ADSZk=
github.com/containers/ocicrypt v1.1.8/go.mod h1:jM362hyBtbwLMWzXQZTlkjKGAQf/BN/LFMtH0FIRt34=
github.com/containers/podman/v4 v4.6.2 h1:tUHH6RfavqH0QZJHA9JoiMtMatjUSe4LjkvQYD8AhMs=
github.com/containers/podman/v4 v4.6.2/go.mod h1:RszjaonNv/hLayX58T06xci2ixrcCoR5hfdfN6PQWmc=
github.com/containers/podman/v4 v4.7.0 h1:KjawmmazJHyQfck5sFLzQ/KI+JtsnnoGI+d+qfiPvaY=
github.com/containers/podman/v4 v4.7.0/go.mod h1:FJPqIhiwdklJenJskZyoNd1ZNin6kvY6zL9Rypaajxs=
github.com/containers/storage v1.50.2 h1:Fys4BjFUVNRBEXlO70hFI48VW4EXsgnGisTpk9tTMsE=
github.com/containers/storage v1.50.2/go.mod h1:dpspZsUrcKD8SpTofvKWhwPDHD0MkO4Q7VE+oYdWkiA=
github.com/coreos/stream-metadata-go v0.4.2 h1:A0Ug0Ecb6mlJ5X/tXpBZgZJsA/Ulhj1BR0rEZJMzN4I=
github.com/coreos/stream-metadata-go v0.4.2/go.mod h1:fMObQqQm8Ku91G04btKzEH3AsdP1mrAb986z9aaK0tE=
github.com/coreos/stream-metadata-go v0.4.3 h1:5GykJ8dtZSx1rdlzEAiDVzA73cwmUF3ceTuIP293L6E=
github.com/coreos/stream-metadata-go v0.4.3/go.mod h1:fMObQqQm8Ku91G04btKzEH3AsdP1mrAb986z9aaK0tE=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -425,12 +425,12 @@ github.com/onsi/ginkgo/v2 v2.12.0 h1:UIVDowFPwpg6yMUpPjGkYvf06K3RAiJXUhCxEwQVHRI
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc4 h1:oOxKUJWnFC4YGHCCMNql1x4YaDfYBTS5Y4x/Cgeo1E0=
github.com/opencontainers/image-spec v1.1.0-rc4/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI=
github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/opencontainers/runc v1.1.9 h1:XR0VIHTGce5eWPkaPesqTBrhW2yAcaraWfsEalNwQLM=
github.com/opencontainers/runc v1.1.9/go.mod h1:CbUumNnWCuTGFukNXahoo/RFBZvDAgRh/smNYNOhA50=
github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg=
github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 h1:EctkgBjZ1y4q+sibyuuIgiKpa0QSd2elFtSSdNvBVow=
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
Expand Down
131 changes: 118 additions & 13 deletions internal/cli/atlas/deployments/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,104 @@ package deployments

import (
"context"
"fmt"
"time"

"github.com/mongodb/mongodb-atlas-cli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/internal/cli/atlas/deployments/options"
"github.com/mongodb/mongodb-atlas-cli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/internal/log"
"github.com/mongodb/mongodb-atlas-cli/internal/podman"
"github.com/mongodb/mongodb-atlas-cli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/internal/telemetry"
"github.com/mongodb/mongodb-atlas-cli/internal/usage"
"github.com/mongodb/mongodb-atlas-cli/internal/watchers"
"github.com/spf13/cobra"
)

const (
deleteSuccessMessage = "Deployment '%s' deleted\n"
deleteFailMessage = "Deployment not deleted"
)

type DeleteOpts struct {
cli.OutputOpts
cli.GlobalOpts
*cli.DeleteOpts
cli.WatchOpts
options.DeploymentOpts
podmanClient podman.Client
atlasStore store.ClusterDeleter
}

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

func (opts *DeleteOpts) Run(ctx context.Context) error {
if err := opts.LocalDeploymentPreRun(ctx); err != nil {
if err := opts.validateAndPrompt(ctx); err != nil {
return err
}
opts.Entry = opts.DeploymentName

if err := opts.Prompt(); err != nil {
return err
}

if err := opts.podmanClient.Ready(ctx); err != nil {
if opts.IsAtlasDeploymentType() {
return opts.runAtlas()
}
return opts.runLocal(ctx)
}

func (opts *DeleteOpts) validateAndPrompt(ctx context.Context) error {
if err := opts.ValidateAndPromptDeploymentType(); err != nil {
return err
}
telemetry.AppendOption(telemetry.WithDeploymentType(options.LocalCluster))

if opts.IsAtlasDeploymentType() {
return opts.validateAndPromptAtlas()
}
return opts.validateAndPromptLocal(ctx)
}

func (opts *DeleteOpts) validateAndPromptAtlas() error {
if opts.DeploymentName == "" {
return ErrNoDeploymentName
}

return opts.ValidateProjectID()
}

func (opts *DeleteOpts) validateAndPromptLocal(ctx context.Context) error {
if err := opts.PodmanClient.Ready(ctx); err != nil {
return err
}

telemetry.AppendOption(telemetry.WithDeploymentType(options.LocalCluster)) // always local
if opts.DeploymentName == "" {
if err := opts.DeploymentOpts.Select(ctx); err != nil {
return err
}
}

opts.Entry = opts.DeploymentName
if err := opts.Prompt(); err != nil {
return nil
}

func (opts *DeleteOpts) runAtlas() error {
return opts.Delete(opts.atlasStore.DeleteCluster, opts.ConfigProjectID())
}

func (opts *DeleteOpts) runLocal(ctx context.Context) error {
err := opts.LocalDeploymentPreRun(ctx)
if err != nil {
return err
}

return opts.Delete(func() error {
_, _ = log.Warningln("deleting deployment...")
opts.StartSpinner()
Expand All @@ -65,16 +122,52 @@ func (opts *DeleteOpts) Run(ctx context.Context) error {
})
}

func (opts *DeleteOpts) PostRun() error {
if !opts.EnableWatch || !opts.IsAtlasDeploymentType() {
return nil
}

watcher := watchers.NewWatcher(
*watchers.ClusterDeleted,
watchers.NewAtlasClusterStateDescriber(
opts.atlasStore.(store.AtlasClusterDescriber),
opts.ProjectID,
opts.Entry,
),
)

watcher.Timeout = time.Duration(opts.Timeout)
if err := opts.WatchWatcher(watcher); err != nil {
return err
}

return opts.Print(nil)
}

// atlas deployments delete <clusterName>.
func DeleteBuilder() *cobra.Command {
opts := &DeleteOpts{
DeleteOpts: cli.NewDeleteOpts("Deployment '%s' deleted\n", "Deployment not deleted"),
DeleteOpts: cli.NewDeleteOpts(deleteSuccessMessage, deleteFailMessage),
}
cmd := &cobra.Command{
Use: "delete [deploymentName]",
Short: "Delete a deployment.",
Use: "delete [deploymentName]",
Short: "Delete a deployment.",
Long: `The command prompts you to confirm the operation when you run the command without the --force option.
Deleting an Atlas deployment also deletes any backup snapshots for that cluster.
Deleting a Local deployment also deletes any local data volumes.
` + fmt.Sprintf(usage.RequiredRole, "Project Owner"),
Example: fmt.Sprintf(` # Remove an Atlas deployment named myDeployment after prompting for a confirmation:
%[1]s deployments delete myDeployment --type ATLAS
# Remove an Atlas deployment named myDeployment without requiring confirmation:
%[1]s deployments delete myDeployment --type ATLAS --force
# Remove an Local deployment named myDeployment without requiring confirmation:
%[1]s deployments delete myDeployment --type LOCAL --force`, cli.ExampleAtlasEntryPoint()),
Aliases: []string{"rm"},
GroupID: "local",
GroupID: "all",
Args: require.MaximumNArgs(1),
Annotations: map[string]string{
"deploymentNameDesc": "Name of the deployment that you want to delete.",
Expand All @@ -84,15 +177,27 @@ func DeleteBuilder() *cobra.Command {
if len(args) == 1 {
opts.DeploymentName = args[0]
}
opts.podmanClient = podman.NewClient(log.IsDebugLevel(), log.Writer())
return opts.PreRunE(opts.InitOutput(cmd.OutOrStdout(), ""), opts.InitStore(opts.podmanClient))
opts.PodmanClient = podman.NewClient(log.IsDebugLevel(), log.Writer())
return opts.PreRunE(
opts.initAtlasStore(cmd.Context()),
opts.InitOutput(cmd.OutOrStdout(), ""),
opts.InitStore(opts.PodmanClient),
)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run(cmd.Context())
},
PostRunE: func(cmd *cobra.Command, args []string) error {
return opts.PostRun()
},
}

cmd.Flags().StringVar(&opts.DeploymentType, flag.TypeFlag, "", usage.DeploymentType)
cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)
cmd.Flags().BoolVarP(&opts.EnableWatch, flag.EnableWatch, flag.EnableWatchShort, false, usage.EnableWatch)
cmd.Flags().UintVar(&opts.Timeout, flag.WatchTimeout, 0, usage.WatchTimeout)

cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)

return cmd
}
Loading

0 comments on commit 8925e19

Please sign in to comment.