Skip to content

Commit

Permalink
CLOUDP-186602: add --tag flag to quickstart and setup commands (#2050)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibulca authored Jun 28, 2023
1 parent ca01579 commit 736315c
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/atlascli/command/atlas-quickstart.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Options
-
- false
- Indicates whether to skip loading sample data into your Atlas cluster.
* - --tag
- stringToString
- false
- List that contains key-value pairs between 1 to 255 characters in length for tagging and categorizing the cluster. This value defaults to [].
* - --tier
- string
- false
Expand Down
4 changes: 4 additions & 0 deletions docs/atlascli/command/atlas-setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Options
-
- false
- Indicates whether to skip loading sample data into your Atlas cluster.
* - --tag
- stringToString
- false
- List that contains key-value pairs between 1 to 255 characters in length for tagging and categorizing the cluster. This value defaults to [].
* - --tier
- string
- false
Expand Down
4 changes: 4 additions & 0 deletions docs/mongocli/command/mongocli-atlas-quickstart.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Options
-
- false
- Indicates whether to skip loading sample data into your Atlas cluster.
* - --tag
- stringToString
- false
- List that contains key-value pairs between 1 to 255 characters in length for tagging and categorizing the cluster. This value defaults to [].
* - --tier
- string
- false
Expand Down
4 changes: 4 additions & 0 deletions internal/cli/atlas/quickstart/cluster_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (opts *Opts) newCluster() *atlasv2.AdvancedClusterDescription {
TerminationProtectionEnabled: &opts.EnableTerminationProtection,
}

for k, v := range opts.Tag {
cluster.Tags = append(cluster.Tags, atlasv2.ResourceTag{Key: pointer.Get(k), Value: pointer.Get(v)})
}

if opts.providerName() != tenant {
diskSizeGB := defaultDiskSizeGB(opts.providerName(), opts.Tier)
mdbVersion, _ := cli.DefaultMongoDBMajorVersion()
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/atlas/quickstart/quick_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type Opts struct {
Confirm bool
CurrentIP bool
EnableTerminationProtection bool
Tag map[string]string
store store.AtlasClusterQuickStarter
shouldRunLogin bool
flags *pflag.FlagSet
Expand All @@ -135,6 +136,7 @@ type quickstart struct {
EnableTerminationProtection bool
SkipSampleData bool
SkipMongosh bool
Tag map[string]string
}

type Flow interface {
Expand Down Expand Up @@ -456,6 +458,7 @@ func (opts *Opts) newDefaultValues() (*quickstart, error) {

values.Tier = opts.Tier
values.EnableTerminationProtection = opts.EnableTerminationProtection
values.Tag = opts.Tag

return values, nil
}
Expand Down Expand Up @@ -488,6 +491,7 @@ func (opts *Opts) replaceWithDefaultSettings(values *quickstart) {
opts.EnableTerminationProtection = values.EnableTerminationProtection
opts.SkipSampleData = values.SkipSampleData
opts.SkipMongosh = values.SkipMongosh
opts.Tag = values.Tag
}

func (opts *Opts) interactiveSetup() error {
Expand Down Expand Up @@ -565,6 +569,7 @@ func Builder() *cobra.Command {
cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.ForceQuickstart)
cmd.Flags().BoolVar(&opts.CurrentIP, flag.CurrentIP, false, usage.CurrentIPSimplified)
cmd.Flags().BoolVar(&opts.EnableTerminationProtection, flag.EnableTerminationProtection, false, usage.EnableTerminationProtection)
cmd.Flags().StringToStringVar(&opts.Tag, flag.Tag, nil, usage.Tag)

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

Expand Down
2 changes: 2 additions & 0 deletions internal/cli/atlas/quickstart/quick_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestBuilder(t *testing.T) {
flag.EnableTerminationProtection,
flag.SkipMongosh,
flag.SkipSampleData,
flag.Tag,
},
)
}
Expand Down Expand Up @@ -81,6 +82,7 @@ func TestQuickstartOpts_Run(t *testing.T) {
SkipMongosh: true,
SkipSampleData: true,
Confirm: true,
Tag: map[string]string{"env": "test"},
}
opts.WithFlow(mockFlow)

Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas/setup/setup_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func Builder() *cobra.Command {
cmd.Flags().BoolVar(&qsOpts.SkipMongosh, flag.SkipMongosh, false, usage.SkipMongosh)
cmd.Flags().BoolVar(&qsOpts.Confirm, flag.Force, false, usage.Force)
cmd.Flags().BoolVar(&qsOpts.CurrentIP, flag.CurrentIP, false, usage.CurrentIPSimplified)
cmd.Flags().StringToStringVar(&qsOpts.Tag, flag.Tag, nil, usage.Tag)

cmd.Flags().StringVar(&qsOpts.ProjectID, flag.ProjectID, "", usage.ProjectID)
_ = cmd.Flags().MarkHidden(flag.ProjectID)
Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas/setup/setup_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestBuilder(t *testing.T) {
flag.Password,
flag.SkipMongosh,
flag.SkipSampleData,
flag.Tag,
},
)
}
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/atlas/setup_force_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/test/e2e"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/admin"
"go.mongodb.org/atlas/mongodbatlas"
)

Expand All @@ -43,6 +44,9 @@ func TestSetup(t *testing.T) {
dbUserUsername, err := RandUsername()
req.NoError(err)

tagKey := "env"
tagValue := "e2etest"

t.Run("Run", func(t *testing.T) {
cmd := exec.Command(cliPath,
"setup",
Expand All @@ -51,6 +55,7 @@ func TestSetup(t *testing.T) {
"--skipMongosh",
"--skipSampleData",
"--projectId", g.projectID,
"--tag", tagKey+"="+tagValue,
"--force")
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
Expand Down Expand Up @@ -88,6 +93,27 @@ func TestSetup(t *testing.T) {
assert.Equal(t, dbUserUsername, user.Username)
})

t.Run("Describe Cluster", func(t *testing.T) {
cmd := exec.Command(cliPath,
clustersEntity,
"describe",
clusterName,
"-o=json",
"--projectId", g.projectID,
)
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
require.NoError(t, err, string(resp))

var cluster atlasv2.AdvancedClusterDescription
require.NoError(t, json.Unmarshal(resp, &cluster), string(resp))
assert.Equal(t, clusterName, *cluster.Name)

assert.Len(t, cluster.Tags, 1)
assert.Equal(t, tagKey, *cluster.Tags[0].Key)
assert.Equal(t, tagValue, *cluster.Tags[0].Value)
})

t.Run("Delete Cluster", func(t *testing.T) {
cmd := exec.Command(cliPath,
clustersEntity,
Expand Down

0 comments on commit 736315c

Please sign in to comment.