Skip to content

Commit

Permalink
Add support for replicas in shell and connect commands (#846)
Browse files Browse the repository at this point in the history
* Add replica flag to shell and connect commands

* Fix up args to only add primary if flag is false

* Dynamically set database name

* remove unnecessary space

* Only call @primary for primary cluster, load with no defaults for replicas
  • Loading branch information
iheanyi authored Apr 3, 2024
1 parent 646f4d6 commit 59126ad
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
10 changes: 9 additions & 1 deletion internal/cmd/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func ConnectCmd(ch *cmdutil.Helper) *cobra.Command {
execCommandEnvURL string
role string
noRandom bool
replica bool
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -82,12 +83,16 @@ argument:
}
}

replica := flags.replica

role := cmdutil.AdministratorRole
if flags.role != "" {
role, err = cmdutil.RoleFromString(flags.role)
if err != nil {
return err
}
} else if replica {
role = cmdutil.ReaderRole
}

// check whether database and branch exist
Expand Down Expand Up @@ -116,6 +121,7 @@ argument:
Role: role,
Name: passwordutil.GenerateName("pscale-cli-connect"),
TTL: 5 * time.Minute,
Replica: replica,
})
if err != nil {
return cmdutil.HandleError(err)
Expand Down Expand Up @@ -207,7 +213,9 @@ argument:
cmd.PersistentFlags().StringVar(&flags.execCommandEnvURL, "execute-env-url", "DATABASE_URL",
"Environment variable name that contains the exposed Database URL.")
cmd.PersistentFlags().StringVar(&flags.role, "role",
"admin", "Role defines the access level, allowed values are : reader, writer, readwriter, admin. By default it is admin.")
"", "Role defines the access level, allowed values are: reader, writer, readwriter, admin. Defaults to 'reader' for replica passwords, otherwise defaults to 'admin'.")
cmd.Flags().BoolVar(&flags.replica, "replica", false, "When enabled, the password will route all reads to the branch's primary replicas and all read-only regions.")
cmd.Flags().MarkHidden("replica")

return cmd
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/password/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
},
}
cmd.PersistentFlags().StringVar(&flags.role, "role",
"", "Role defines the access level, allowed values are : reader, writer, readwriter, admin. Defaults to 'reader' for replica passwords, otherwise defaults to 'admin'.")
"", "Role defines the access level, allowed values are: reader, writer, readwriter, admin. Defaults to 'reader' for replica passwords, otherwise defaults to 'admin'.")
cmd.PersistentFlags().Var(&flags.ttl, "ttl", `TTL defines the time to live for the password. Durations such as "30m", "24h", or bare integers such as "3600" (seconds) are accepted. The default TTL is 0s, which means the password will never expire.`)
cmd.Flags().BoolVar(&flags.replica, "replica", false, "When enabled, the password will route all reads to the branch's primary replicas and all read-only regions.")
cmd.Flags().MarkHidden("replica")
Expand Down
17 changes: 15 additions & 2 deletions internal/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func ShellCmd(ch *cmdutil.Helper) *cobra.Command {
localAddr string
remoteAddr string
role string
replica bool
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -82,12 +83,16 @@ second argument:
}
}

replica := flags.replica

role := cmdutil.AdministratorRole
if flags.role != "" {
role, err = cmdutil.RoleFromString(flags.role)
if err != nil {
return err
}
} else if replica {
role = cmdutil.ReaderRole
}

// check whether database and branch exist
Expand Down Expand Up @@ -131,6 +136,7 @@ second argument:
Role: role,
Name: passwordutil.GenerateName("pscale-cli-shell"),
TTL: 5 * time.Minute,
Replica: replica,
})
if err != nil {
return cmdutil.HandleError(err)
Expand Down Expand Up @@ -182,7 +188,11 @@ second argument:
"-t", // the -s (silent) flag disables tabular output, re-enable it.
"-h", host,
"-P", port,
"-D", "@primary",
}
if replica {
mysqlArgs = append([]string{"--no-defaults"}, mysqlArgs...)
} else {
mysqlArgs = append(mysqlArgs, "-D", "@primary")
}

historyFile := historyFilePath(ch.Config.Organization, database, branch)
Expand Down Expand Up @@ -227,7 +237,10 @@ second argument:
cmd.PersistentFlags().StringVar(&flags.remoteAddr, "remote-addr", "",
"PlanetScale Database remote network address. By default the remote address is populated automatically from the PlanetScale API. (format: `hostname:port`)")
cmd.PersistentFlags().StringVar(&flags.role, "role",
"admin", "Role defines the access level, allowed values are : reader, writer, readwriter, admin. By default it is admin.")
"", "Role defines the access level, allowed values are: reader, writer, readwriter, admin. Defaults to 'reader' for replica passwords, otherwise defaults to 'admin'.")
cmd.Flags().BoolVar(&flags.replica, "replica", false, "When enabled, the password will route all reads to the branch's primary replicas and all read-only regions.")
cmd.Flags().MarkHidden("replica")

cmd.MarkPersistentFlagRequired("org") // nolint:errcheck

return cmd
Expand Down
2 changes: 2 additions & 0 deletions internal/passwordutil/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Options struct {
Role cmdutil.PasswordRole
Name string
TTL time.Duration
Replica bool
}

type Password struct {
Expand Down Expand Up @@ -78,6 +79,7 @@ func New(ctx context.Context, client *ps.Client, opt Options) (*Password, error)
Role: opt.Role.ToString(),
Name: opt.Name,
TTL: int(opt.TTL.Seconds()),
Replica: opt.Replica,
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 59126ad

Please sign in to comment.