|
| 1 | +// Copyright 2024 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 snapshots |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "net/http" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli" |
| 26 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require" |
| 27 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config" |
| 28 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" |
| 29 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" |
| 30 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" |
| 31 | + "github.com/spf13/afero" |
| 32 | + "github.com/spf13/cobra" |
| 33 | + atlasv2 "go.mongodb.org/atlas-sdk/v20241113004/admin" |
| 34 | +) |
| 35 | + |
| 36 | +type DownloadOpts struct { |
| 37 | + cli.ProjectOpts |
| 38 | + cli.DownloaderOpts |
| 39 | + cli.OutputOpts |
| 40 | + store store.SnapshotsDownloader |
| 41 | + clusterName string |
| 42 | + id string |
| 43 | +} |
| 44 | + |
| 45 | +func (opts *DownloadOpts) initStore(ctx context.Context) func() error { |
| 46 | + return func() error { |
| 47 | + var err error |
| 48 | + opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx)) |
| 49 | + return err |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +var downloadTemplate = "Snapshot '%s' downloaded.\n" |
| 54 | +var errEmptyURL = errors.New("'snapshotUrl' is empty") |
| 55 | +var errExtNotSupported = errors.New("only the '.tgz' extension is supported") |
| 56 | + |
| 57 | +func (opts *DownloadOpts) Run() error { |
| 58 | + r, err := opts.store.DownloadFlexClusterSnapshot(opts.ConfigProjectID(), opts.clusterName, opts.newFlexBackupSnapshotDownloadCreate()) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + return opts.Download(r.SnapshotUrl) |
| 64 | +} |
| 65 | + |
| 66 | +func (opts *DownloadOpts) newFlexBackupSnapshotDownloadCreate() *atlasv2.FlexBackupSnapshotDownloadCreate20241113 { |
| 67 | + return &atlasv2.FlexBackupSnapshotDownloadCreate20241113{ |
| 68 | + SnapshotId: opts.id, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func (opts *DownloadOpts) Download(url *string) error { |
| 73 | + if url == nil { |
| 74 | + return errEmptyURL |
| 75 | + } |
| 76 | + |
| 77 | + w, err := opts.NewWriteCloser() |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + defer w.Close() |
| 82 | + |
| 83 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, *url, nil) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + resp, err := http.DefaultClient.Do(req) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + defer resp.Body.Close() |
| 94 | + if err != nil { |
| 95 | + _ = opts.OnError(w) |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + if _, err := io.Copy(w, resp.Body); err != nil { |
| 100 | + _ = opts.OnError(w) |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Printf(downloadTemplate, opts.Out) |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (opts *DownloadOpts) initDefaultOut() error { |
| 109 | + if opts.Out == "" { |
| 110 | + opts.Out = opts.id + ".tgz" |
| 111 | + } else if !strings.Contains(opts.Out, ".tgz") { |
| 112 | + return errExtNotSupported |
| 113 | + } |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +// DownloadBuilder builds a cobra.Command that can run as: |
| 119 | +// atlas backup snapshots download snapshotId --clusterName string [--projectId string] [--out string]. |
| 120 | +func DownloadBuilder() *cobra.Command { |
| 121 | + opts := &DownloadOpts{} |
| 122 | + opts.Fs = afero.NewOsFs() |
| 123 | + cmd := &cobra.Command{ |
| 124 | + Use: "download <snapshotId>", |
| 125 | + Short: "Download one snapshot for the specified flex cluster.", |
| 126 | + Long: `You can download a snapshot for an Atlas Flex cluster. |
| 127 | +` + fmt.Sprintf("%s\n%s", fmt.Sprintf(usage.RequiredRole, "Project Owner"), "Atlas supports this command only for Flex clusters."), |
| 128 | + Args: require.ExactArgs(1), |
| 129 | + Annotations: map[string]string{ |
| 130 | + "snapshotIdDesc": "Unique 24-hexadecimal digit string that identifies the snapshot to download.", |
| 131 | + "output": downloadTemplate, |
| 132 | + }, |
| 133 | + PreRunE: func(cmd *cobra.Command, _ []string) error { |
| 134 | + return opts.PreRunE( |
| 135 | + opts.ValidateProjectID, |
| 136 | + opts.initStore(cmd.Context()), |
| 137 | + opts.InitOutput(cmd.OutOrStdout(), createTemplate), |
| 138 | + ) |
| 139 | + }, |
| 140 | + RunE: func(_ *cobra.Command, args []string) error { |
| 141 | + opts.id = args[0] |
| 142 | + if err := opts.initDefaultOut(); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + return opts.Run() |
| 146 | + }, |
| 147 | + } |
| 148 | + cmd.Flags().StringVar(&opts.clusterName, flag.ClusterName, "", usage.ClusterName) |
| 149 | + cmd.Flags().StringVar(&opts.Out, flag.Out, "", usage.SnapshotOut) |
| 150 | + |
| 151 | + opts.AddProjectOptsFlags(cmd) |
| 152 | + |
| 153 | + _ = cmd.MarkFlagRequired(flag.ClusterName) |
| 154 | + |
| 155 | + return cmd |
| 156 | +} |
0 commit comments