Skip to content

Commit 410ceab

Browse files
authored
CLOUDP-331543: Replace register flow with login flow (#4051)
1 parent f73c8a8 commit 410ceab

File tree

5 files changed

+43
-61
lines changed

5 files changed

+43
-61
lines changed

docs/command/atlas-setup.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ atlas setup
1212
:depth: 1
1313
:class: singlecol
1414

15-
Register, authenticate, create, and access an Atlas cluster.
15+
Login, authenticate, create, and access an Atlas cluster.
1616

1717
Public Preview: The atlas api sub-command, automatically generated from the MongoDB Atlas Admin API, offers full coverage of the Admin API and is currently in Public Preview (please provide feedback at https://feedback.mongodb.com/forums/930808-atlas-cli).
1818
Admin API capabilities have their own release lifecycle, which you can check via the provided API endpoint documentation link.
1919

2020

2121

22-
This command takes you through registration, login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell.
22+
This command takes you through login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell.
2323

2424
Syntax
2525
------
@@ -79,7 +79,7 @@ Options
7979
* - --gov
8080
-
8181
- false
82-
- Register with Atlas for Government.
82+
- Login with Atlas for Government.
8383
* - -h, --help
8484
-
8585
- false

docs/command/atlas.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Related Commands
8787
* :ref:`atlas-processes` - Manage MongoDB processes for your project.
8888
* :ref:`atlas-projects` - Manage your Atlas projects.
8989
* :ref:`atlas-security` - Manage security configuration for your project.
90-
* :ref:`atlas-setup` - Register, authenticate, create, and access an Atlas cluster.
90+
* :ref:`atlas-setup` - Login, authenticate, create, and access an Atlas cluster.
9191
* :ref:`atlas-streams` - Manage your Atlas Stream Processing deployments.
9292
* :ref:`atlas-teams` - Manage your Atlas teams.
9393
* :ref:`atlas-users` - Manage your Atlas users.

internal/cli/auth/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func (opts *LoginOpts) handleBrowser(uri string) {
338338
}
339339

340340
if !opts.force {
341-
_, _ = fmt.Fprintf(opts.OutWriter, "\nPress Enter to open the browser to complete authentication...")
341+
_, _ = fmt.Fprintf(opts.OutWriter, "\nPress Enter to open the browser and complete authentication...")
342342
_, _ = fmt.Scanln()
343343
}
344344
if errBrowser := browser.OpenURL(uri); errBrowser != nil {

internal/cli/setup/setup_cmd.go

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type AtlasClusterQuickStarter interface {
135135
type Opts struct {
136136
cli.ProjectOpts
137137
cli.WatchOpts
138-
register auth.RegisterOpts
138+
login auth.LoginOpts
139139
config profileReader
140140
store AtlasClusterQuickStarter
141141
defaultName string
@@ -164,8 +164,7 @@ type Opts struct {
164164
connectionString string
165165

166166
// control
167-
skipRegister bool
168-
skipLogin bool
167+
skipLogin bool
169168
}
170169

171170
type clusterSettings struct {
@@ -408,26 +407,18 @@ func (opts *Opts) setupCloseHandler() {
408407
}
409408

410409
func (opts *Opts) Run(ctx context.Context) error {
411-
if !opts.skipRegister {
412-
_, _ = fmt.Fprintf(opts.OutWriter, `
413-
This command will help you:
414-
1. Create and verify your MongoDB Atlas account in your browser.
415-
2. Return to the terminal to create your first free MongoDB database in Atlas.
416-
`)
417-
if err := opts.register.RegisterRun(ctx); err != nil {
418-
return err
419-
}
420-
} else if !opts.skipLogin {
410+
if !opts.skipLogin {
421411
_, _ = fmt.Fprintf(opts.OutWriter, `Next steps:
422412
1. Log in and verify your MongoDB Atlas account in your browser.
423413
2. Return to the terminal to create your first free MongoDB database in Atlas.
414+
415+
If you wish to register a new account, run: 'atlas auth register'.
424416
`)
425417

426-
if err := opts.register.LoginRun(ctx); err != nil {
418+
if err := opts.login.LoginRun(ctx); err != nil {
427419
return err
428420
}
429421
}
430-
431422
if err := opts.clusterPreRun(ctx, opts.OutWriter); err != nil {
432423
return err
433424
}
@@ -445,8 +436,8 @@ func (opts *Opts) clusterPreRun(ctx context.Context, outWriter io.Writer) error
445436

446437
return opts.PreRunE(
447438
opts.initStore(ctx),
448-
opts.register.SyncWithOAuthAccessProfile(defaultProfile),
449-
opts.register.InitFlow(defaultProfile),
439+
opts.login.SyncWithOAuthAccessProfile(defaultProfile),
440+
opts.login.InitFlow(defaultProfile),
450441
opts.InitOutput(outWriter, ""),
451442
)
452443
}
@@ -586,7 +577,6 @@ func (opts *Opts) promptConnect() error {
586577
}
587578

588579
func (opts *Opts) PreRun(ctx context.Context) error {
589-
opts.skipRegister = true
590580
opts.skipLogin = true
591581

592582
if err := validate.NoAPIKeys(); err != nil {
@@ -596,14 +586,14 @@ func (opts *Opts) PreRun(ctx context.Context) error {
596586
// The error is useful in other components that call `validate.NoAPIKeys()`
597587
return nil
598588
}
599-
if err := opts.register.RefreshAccessToken(ctx); err != nil && commonerrors.IsInvalidRefreshToken(err) {
600-
opts.skipLogin = false
601-
return nil
602-
}
589+
590+
// if profile has access token and refresh token is valid, we can skip login
603591
if _, err := auth.AccountWithAccessToken(); err == nil {
604-
return nil
592+
if err := opts.login.RefreshAccessToken(ctx); err != nil && !commonerrors.IsInvalidRefreshToken(err) {
593+
return nil
594+
}
605595
}
606-
opts.skipRegister = false
596+
opts.skipLogin = false
607597
return nil
608598
}
609599

@@ -669,8 +659,8 @@ func Builder() *cobra.Command {
669659
cmd := &cobra.Command{
670660
Use: "setup",
671661
Aliases: []string{"quickstart"},
672-
Short: "Register, authenticate, create, and access an Atlas cluster.",
673-
Long: `This command takes you through registration, login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell.`,
662+
Short: "Login, authenticate, create, and access an Atlas cluster.",
663+
Long: `This command takes you through login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell.`,
674664
Example: ` # Override default cluster settings like name, provider, or database username by using the command options
675665
atlas setup --clusterName Test --provider GCP --username dbuserTest`,
676666
Hidden: false,
@@ -679,29 +669,22 @@ func Builder() *cobra.Command {
679669
defaultProfile := config.Default()
680670
opts.config = defaultProfile
681671
opts.OutWriter = cmd.OutOrStdout()
682-
opts.register.OutWriter = opts.OutWriter
672+
opts.login.OutWriter = opts.OutWriter
683673

684-
if err := opts.register.SyncWithOAuthAccessProfile(defaultProfile)(); err != nil {
674+
if err := opts.login.SyncWithOAuthAccessProfile(defaultProfile)(); err != nil {
685675
return err
686676
}
687-
if err := opts.register.InitFlow(defaultProfile)(); err != nil {
677+
if err := opts.login.InitFlow(defaultProfile)(); err != nil {
688678
return err
689679
}
690680
if err := opts.PreRun(cmd.Context()); err != nil {
691681
return nil
692682
}
693683
var preRun []prerun.CmdOpt
694-
// registration pre run if applicable
695-
if !opts.skipRegister {
696-
preRun = append(preRun,
697-
opts.register.LoginPreRun(cmd.Context()),
698-
validate.NoAPIKeys,
699-
validate.NoAccessToken,
700-
)
701-
}
702-
703-
if !opts.skipLogin && opts.skipRegister {
704-
preRun = append(preRun, opts.register.LoginPreRun(cmd.Context()))
684+
// login pre run if applicable
685+
if !opts.skipLogin {
686+
opts.login.Asker = &telemetry.Ask{}
687+
preRun = append(preRun, opts.login.LoginPreRun(cmd.Context()))
705688
}
706689
preRun = append(preRun, opts.validateTier)
707690
preRun = append(preRun, validate.AutoScalingMode(opts.AutoScalingMode))
@@ -714,9 +697,9 @@ func Builder() *cobra.Command {
714697
},
715698
}
716699

717-
// Register and login related
718-
cmd.Flags().BoolVar(&opts.register.IsGov, "gov", false, "Register with Atlas for Government.")
719-
cmd.Flags().BoolVar(&opts.register.NoBrowser, "noBrowser", false, "Don't try to open a browser session.")
700+
// Login related
701+
cmd.Flags().BoolVar(&opts.login.IsGov, "gov", false, "Login with Atlas for Government.")
702+
cmd.Flags().BoolVar(&opts.login.NoBrowser, "noBrowser", false, "Don't try to open a browser session.")
720703
// Setup related
721704
cmd.Flags().StringVar(&opts.MDBVersion, flag.MDBVersion, "", usage.DeploymentMDBVersion)
722705
cmd.Flags().StringVar(&opts.connectWith, flag.ConnectWith, "", usage.ConnectWithAtlasSetup)

internal/cli/setup/setup_cmd_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,33 @@ func Test_setupOpts_PreRunWithAPIKeys(t *testing.T) {
4141
opts := &Opts{}
4242

4343
opts.OutWriter = buf
44-
opts.register.WithFlow(mockFlow)
44+
opts.login.WithFlow(mockFlow)
4545

4646
config.SetPublicAPIKey("publicKey")
4747
config.SetPrivateAPIKey("privateKey")
4848

4949
require.NoError(t, opts.PreRun(ctx))
5050

51-
assert.True(t, opts.skipRegister)
5251
assert.Equal(t, 0, buf.Len())
5352
assert.True(t, opts.skipLogin)
53+
t.Cleanup(func() {
54+
config.SetPublicAPIKey("")
55+
config.SetPrivateAPIKey("")
56+
})
5457
}
5558

56-
func Test_setupOpts_RunSkipRegister(t *testing.T) {
59+
func Test_setupOpts_RunSkipLogin(t *testing.T) {
5760
ctrl := gomock.NewController(t)
5861
mockFlow := mocks.NewMockRefresher(ctrl)
5962
ctx := t.Context()
6063
buf := new(bytes.Buffer)
6164

62-
opts := &Opts{
63-
skipLogin: true,
64-
}
65-
opts.register.WithFlow(mockFlow)
66-
67-
config.SetAccessToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ")
65+
opts := &Opts{}
66+
opts.login.WithFlow(mockFlow)
6867

6968
opts.OutWriter = buf
7069
require.NoError(t, opts.PreRun(ctx))
71-
assert.True(t, opts.skipRegister)
70+
assert.False(t, opts.skipLogin)
7271
}
7372

7473
func TestCluster_Run(t *testing.T) {
@@ -109,7 +108,7 @@ func TestCluster_Run(t *testing.T) {
109108
Tag: map[string]string{"env": "test"},
110109
AutoScalingMode: clusterWideScaling,
111110
}
112-
opts.register.WithFlow(mockFlow)
111+
opts.login.WithFlow(mockFlow)
113112

114113
projectIPAccessList := opts.newProjectIPAccessList()
115114

@@ -169,7 +168,7 @@ func TestCluster_Run_LatestAPI(t *testing.T) {
169168
Tag: map[string]string{"env": "test"},
170169
AutoScalingMode: "independentShardingScaling",
171170
}
172-
opts.register.WithFlow(mockFlow)
171+
opts.login.WithFlow(mockFlow)
173172

174173
projectIPAccessList := opts.newProjectIPAccessList()
175174

@@ -237,7 +236,7 @@ func TestCluster_Run_CheckFlagsSet(t *testing.T) {
237236
MDBVersion: "7.0",
238237
AutoScalingMode: clusterWideScaling,
239238
}
240-
opts.register.WithFlow(mockFlow)
239+
opts.login.WithFlow(mockFlow)
241240

242241
projectIPAccessList := opts.newProjectIPAccessList()
243242

0 commit comments

Comments
 (0)