Skip to content

Commit

Permalink
CLOUDP-64975: Add a message with link to get keys at the top of the c…
Browse files Browse the repository at this point in the history
…onfig command (#221)

Co-authored-by: Melissa Mahoney <melissa.mahoney@mongodb.com>
  • Loading branch information
gssbzn and melissamahoney-mongodb authored Jun 26, 2020
1 parent 8aed559 commit 1be9187
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 34 deletions.
104 changes: 74 additions & 30 deletions internal/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/description"
"github.com/mongodb/mongocli/internal/flag"
Expand All @@ -27,13 +26,21 @@ import (
"github.com/spf13/cobra"
)

const (
atlasAPIHelp = "Please provide your API keys. To create new keys, see the documentation: https://docs.atlas.mongodb.com/configure-api-access/"
omAPIHelp = "Please provide your API keys. To create new keys, see the documentation: https://docs.opsmanager.mongodb.com/current/tutorial/configure-public-api-access/"
omBaseURLHelp = "FQDN and port number of the Ops Manager Application."
projectHelp = "ID of an existing project that your API keys have access to. If you don't enter an ID, you must use --projectId for every command that requires it."
orgHelp = "ID of an existing organization that your API keys have access to. If you don't enter an ID, you must use --orgId for every command that requires it."
)

type configOpts struct {
cli.GlobalOpts
Service string
PublicAPIKey string
PrivateAPIKey string
OpsManagerURL string
ProjectID string
OrgID string
}

func (opts *configOpts) IsCloud() bool {
Expand Down Expand Up @@ -62,18 +69,55 @@ func (opts *configOpts) Save() error {
if opts.ProjectID != "" {
config.SetProjectID(opts.ProjectID)
}
if opts.OrgID != "" {
config.SetOrgID(opts.OrgID)
}

return config.Save()
}

func (opts *configOpts) Run() error {
helpLink := "https://docs.atlas.mongodb.com/configure-api-access/"
fmt.Printf(`You are configuring a profile for %s.
All values are optional and you can use environment variables (MCLI_*) instead.
Enter [?] on any option to get help.
`, config.ToolName)

q := opts.accessQuestions()
if err := survey.Ask(q, opts); err != nil {
return err
}

q = opts.defaultQuestions()
if err := survey.Ask(q, opts); err != nil {
return err
}

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

fmt.Printf("\nYour profile is now configured.\n")
if config.Name() != config.DefaultProfile {
fmt.Printf("To use this profile, you must set the flag [-%s %s] for every command.\n", flag.ProfileShort, config.Name())
}
fmt.Printf("You can use [%s config set] to change these settings at a later time.\n", config.ToolName)
return nil
}

func (opts *configOpts) apiKeyHelp() string {
if opts.IsOpsManager() {
helpLink = "https://docs.opsmanager.mongodb.com/current/tutorial/configure-public-api-access/"
return omAPIHelp
}
return atlasAPIHelp
}

func (opts *configOpts) accessQuestions() []*survey.Question {
helpLink := opts.apiKeyHelp()

defaultQuestions := []*survey.Question{
q := []*survey.Question{
{
Name: "publicAPIKey",
Prompt: &survey.Input{
Expand All @@ -89,46 +133,46 @@ func (opts *configOpts) Run() error {
Help: helpLink,
},
},
{
Name: "projectId",
Prompt: &survey.Input{
Message: "Default Project ID [optional]:",
Help: "This is the ID of an existing project your API keys have access to, you can leave this blank and specify it on every command with --projectId",
Default: config.ProjectID(),
},
Validate: validate.OptionalObjectID,
},
}

if opts.IsOpsManager() {
opsManagerQuestions := []*survey.Question{
{
Name: "opsManagerURL",
Prompt: &survey.Input{
Message: "URL to Access Ops Manager:",
Default: config.OpsManagerURL(),
Help: "https://docs.opsmanager.mongodb.com/current/reference/config/ui-settings/#URL-to-Access-Ops-Manager",
Help: omBaseURLHelp,
},
Validate: validate.URL,
},
}
defaultQuestions = append(opsManagerQuestions, defaultQuestions...)
}

if err := survey.Ask(defaultQuestions, opts); err != nil {
return err
}

if err := opts.Save(); err != nil {
return err
q = append(opsManagerQuestions, q...)
}
return q
}

fmt.Printf("%s is all set now\n", config.ToolName)
fmt.Printf("You can use [%s config set] to change more settings. \n", config.ToolName)
if config.Name() != config.DefaultProfile {
fmt.Printf("To use this profile remember to set the flag [-%s %s]\n", flag.ProfileShort, config.Name())
func (opts *configOpts) defaultQuestions() []*survey.Question {
q := []*survey.Question{
{
Name: "projectId",
Prompt: &survey.Input{
Message: "Default Project ID:",
Help: projectHelp,
Default: config.ProjectID(),
},
Validate: validate.OptionalObjectID,
},
{
Name: "orgId",
Prompt: &survey.Input{
Message: "Default Org ID:",
Help: orgHelp,
Default: config.OrgID(),
},
Validate: validate.OptionalObjectID,
},
}
return nil
return q
}

func Builder() *cobra.Command {
Expand Down
12 changes: 8 additions & 4 deletions internal/description/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const (
CloudManager = "Cloud Manager operations."
ShutdownCluster = "Shutdown a cluster."
StartUpCluster = "Start up a cluster."
ConfigDescription = "Configure a profile. This let you store access settings to your cloud."
ConfigSetDescription = "Configure specific properties of the profile."
ConfigDescription = "Configure a profile to store access settings for your MongoDB deployment."
ConfigSetDescription = "Configure specific properties of a profile."
ConfigList = "List available profiles."
IAM = "Organization and projects operations."
Organization = "Organization operations."
Expand Down Expand Up @@ -118,8 +118,12 @@ The name of the log file must be one of: mongodb.gz, mongos.gz, mongodb-audit-lo
DBUsersLong = `The dbusers command retrieves, creates and modifies the MongoDB database users in your cluster.
Each user has a set of roles that provide access to the project’s databases.
A user’s roles apply to all the clusters in the project.`
ConfigLongDescription = `Setting API keys is optional and env variables can be used instead.
Leaving any option empty won't override existing stored values.`
ConfigLongDescription = `Configure settings in a user profile.
All settings are optional. You can specify settings individually by running:
$ mongocli config set --help
You can also use environment variables (MCLI_*) when running the tool.
To find out more, see the documentation: https://docs.mongodb.com/mongocli/stable/configure/environment-variables/.`
ConfigSetLong = `Configure specific properties of the profile.
Available properties include: %v.`
CreateClusterLong = `You can create MongoDB clusters using this command.
Expand Down

0 comments on commit 1be9187

Please sign in to comment.