Skip to content

Commit

Permalink
feat(cmd/influx): add profile management
Browse files Browse the repository at this point in the history
  • Loading branch information
kelwang committed Mar 10, 2020
1 parent 9e8da7c commit 1337bd9
Show file tree
Hide file tree
Showing 21 changed files with 885 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
1. [17114](https://github.com/influxdata/influxdb/pull/17114): Allow for retention to be provided to influx setup command as a duration
1. [17138](https://github.com/influxdata/influxdb/pull/17138): Extend pkger export all capabilities to support filtering by lable name and resource type
1. [17049](https://github.com/influxdata/influxdb/pull/17049): Added new login and sign-up screen that for cloud users that allows direct login from their region
1. [17170](https://github.com/influxdata/influxdb/pull/17170): Added new cli multiple profiles management tool

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func authCreateCmd() *cobra.Command {
}

func authorizationCreateF(cmd *cobra.Command, args []string) error {
if err := authCreateFlags.org.validOrgFlags(); err != nil {
if err := authCreateFlags.org.validOrgFlags(&flags); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/influx/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func init() {

func newBackupService() (influxdb.BackupService, error) {
return &http.BackupService{
Addr: flags.host,
Token: flags.token,
Addr: flags.Host,
Token: flags.Token,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (b *cmdBucketBuilder) cmdCreate() *cobra.Command {
}

func (b *cmdBucketBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
if err := b.org.validOrgFlags(); err != nil {
if err := b.org.validOrgFlags(b.globalFlags); err != nil {
return err
}

Expand Down Expand Up @@ -183,7 +183,7 @@ func (b *cmdBucketBuilder) cmdFind() *cobra.Command {
}

func (b *cmdBucketBuilder) cmdFindRunEFn(cmd *cobra.Command, args []string) error {
if err := b.org.validOrgFlags(); err != nil {
if err := b.org.validOrgFlags(b.globalFlags); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ in the following ways:

// inspectReportTSMF runs the report-tsm tool.
func inspectReportTSMF(cmd *cobra.Command, args []string) error {
if err := inspectReportTSMFlags.organization.validOrgFlags(); err != nil {
if err := inspectReportTSMFlags.organization.validOrgFlags(&flags); err != nil {
return err
}
report := &tsm1.Report{
Expand Down
4 changes: 2 additions & 2 deletions cmd/influx/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func fluxDeleteF(cmd *cobra.Command, args []string) error {
}

s := &http.DeleteService{
Addr: flags.host,
Token: flags.token,
Addr: flags.Host,
Token: flags.Token,
InsecureSkipVerify: flags.skipVerify,
}

Expand Down
76 changes: 54 additions & 22 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newHTTPClient() (*httpc.Client, error) {
return httpClient, nil
}

c, err := http.NewHTTPClient(flags.host, flags.token, flags.skipVerify)
c, err := http.NewHTTPClient(flags.Host, flags.Token, flags.skipVerify)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,8 +95,7 @@ func out(w io.Writer) genericCLIOptFn {
}

type globalFlags struct {
token string
host string
influxdb.Profile
local bool
skipVerify bool
}
Expand Down Expand Up @@ -141,14 +140,14 @@ func (b *cmdInfluxBuilder) cmd(childCmdFns ...func(f *globalFlags, opt genericCL

fOpts := flagOpts{
{
DestP: &flags.token,
DestP: &flags.Token,
Flag: "token",
Short: 't',
Desc: "API token to be used throughout client calls",
Persistent: true,
},
{
DestP: &flags.host,
DestP: &flags.Host,
Flag: "host",
Default: "http://localhost:9999",
Desc: "HTTP address of Influx",
Expand All @@ -157,11 +156,14 @@ func (b *cmdInfluxBuilder) cmd(childCmdFns ...func(f *globalFlags, opt genericCL
}
fOpts.mustRegister(cmd)

if flags.token == "" {
if flags.Token == "" {
// migration credential token
migrateOldCredential()

// this is after the flagOpts register b/c we don't want to show the default value
// in the usage display. This will add it as the token value, then if a token flag
// in the usage display. This will add it as the profile, then if a token flag
// is provided too, the flag will take precedence.
flags.token = getTokenFromDefaultPath()
flags.Profile = getProfileFromDefaultPath()
}

cmd.PersistentFlags().BoolVar(&flags.local, "local", false, "Run commands locally against the filesystem")
Expand All @@ -185,6 +187,7 @@ func influxCmd(opts ...genericCLIOptFn) *cobra.Command {
cmdOrganization,
cmdPing,
cmdPkg,
cmdProfile,
cmdQuery,
cmdTranspile,
cmdREPL,
Expand Down Expand Up @@ -224,31 +227,56 @@ func seeHelp(c *cobra.Command, args []string) {
c.Printf("See '%s -h' for help\n", c.CommandPath())
}

func defaultTokenPath() (string, string, error) {
func defaultProfilePath() (string, string, error) {
dir, err := fs.InfluxDir()
if err != nil {
return "", "", err
}
return filepath.Join(dir, http.DefaultTokenFile), dir, nil
return filepath.Join(dir, http.DefaultProfilesFile), dir, nil
}

func getTokenFromDefaultPath() string {
path, _, err := defaultTokenPath()
func getProfileFromDefaultPath() influxdb.Profile {
path, _, err := defaultProfilePath()
if err != nil {
return ""
return influxdb.DefaultProfile
}
b, err := ioutil.ReadFile(path)
r, err := os.Open(path)
if err != nil {
return ""
return influxdb.DefaultProfile
}
return strings.TrimSpace(string(b))
activated, _ := influxdb.ParseActiveProfile(r)
return activated
}

func writeTokenToPath(tok, path, dir string) error {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
func migrateOldCredential() {
dir, err := fs.InfluxDir()
if err != nil {
return // no need for migration
}
tokB, err := ioutil.ReadFile(filepath.Join(dir, http.DefaultTokenFile))
if err != nil {
return // no need for migration
}
err = writeProfileToPath(strings.TrimSpace(string(tokB)), "", filepath.Join(dir, http.DefaultProfilesFile), dir)
if err != nil {
return
}
return ioutil.WriteFile(path, []byte(tok), 0600)
// ignore the remove err
_ = os.Remove(filepath.Join(dir, http.DefaultTokenFile))
}

func writeProfileToPath(tok, org, path, dir string) error {
p := &influxdb.DefaultProfile
p.Token = tok
p.Org = org
pp := map[string]influxdb.Profile{
"default": *p,
}

return influxdb.LocalProfilesSVC{
Path: path,
Dir: dir,
}.WriteProfiles(pp)
}

func checkSetup(host string, skipVerify bool) error {
Expand Down Expand Up @@ -277,7 +305,7 @@ func checkSetupRunEMiddleware(f *globalFlags) cobraRuneEMiddleware {
return nil
}

if setupErr := checkSetup(f.host, f.skipVerify); setupErr != nil && influxdb.EUnauthorized != influxdb.ErrorCode(setupErr) {
if setupErr := checkSetup(f.Host, f.skipVerify); setupErr != nil && influxdb.EUnauthorized != influxdb.ErrorCode(setupErr) {
return internal.ErrorFmt(setupErr)
}

Expand Down Expand Up @@ -350,7 +378,11 @@ func (o *organization) getID(orgSVC influxdb.OrganizationService) (influxdb.ID,
return 0, fmt.Errorf("failed to locate an organization id")
}

func (o *organization) validOrgFlags() error {
func (o *organization) validOrgFlags(f *globalFlags) error {
if o.id == "" && o.name == "" && f != nil {
o.name = f.Org
}

if o.id == "" && o.name == "" {
return fmt.Errorf("must specify org-id, or org name")
} else if o.id != "" && o.name != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/influx/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func cmdPing(f *globalFlags, opts genericCLIOpts) *cobra.Command {
c := http.Client{
Timeout: 5 * time.Second,
}
url := flags.host + "/health"
url := flags.Host + "/health"
resp, err := c.Get(url)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {
}

func (b *cmdPkgBuilder) pkgApplyRunEFn(cmd *cobra.Command, args []string) error {
if err := b.org.validOrgFlags(); err != nil {
if err := b.org.validOrgFlags(&flags); err != nil {
return err
}
color.NoColor = b.disableColor
Expand All @@ -115,7 +115,7 @@ func (b *cmdPkgBuilder) pkgApplyRunEFn(cmd *cobra.Command, args []string) error
return err
}

if err := b.org.validOrgFlags(); err != nil {
if err := b.org.validOrgFlags(&flags); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 1337bd9

Please sign in to comment.