Skip to content

Commit

Permalink
fix(influx): add env vars to cli usage and normalize usage and flag/e…
Browse files Browse the repository at this point in the history
…nv var priority

also rids us of the cobra tutorial code and encapsulates things into funcs
  • Loading branch information
jsteenb2 committed Jan 10, 2020
1 parent ba7502a commit 7a98a48
Show file tree
Hide file tree
Showing 18 changed files with 771 additions and 756 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
1. [16439](https://github.com/influxdata/influxdb/pull/16439): Prevent negative zero and allow zero to have decimal places
1. [16376](https://github.com/influxdata/influxdb/pull/16413): Limit data loader bucket selection to non system buckets
1. [16458](https://github.com/influxdata/influxdb/pull/16458): Fix EOF error when manually running tasks from the Task Page.
1. [16491](https://github.com/influxdata/influxdb/pull/16491): Add missing env vals to influx cli usage and fixes precedence of flag/env var priority

### UI Improvements
1. [16444](https://github.com/influxdata/influxdb/pull/16444): Add honeybadger reporting to create checks
Expand Down
123 changes: 46 additions & 77 deletions cmd/influx/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@ import (
"github.com/influxdata/influxdb/cmd/influx/internal"
"github.com/influxdata/influxdb/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// AuthorizationCreateFlags are command line args used when creating a authorization
type AuthorizationCreateFlags struct {
func cmdAuth() *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Aliases: []string{"authorization"},
Short: "Authorization management commands",
Run: seeHelp,
}
cmd.AddCommand(
authActiveCmd(),
authCreateCmd(),
authDeleteCmd(),
authFindCmd(),
authInactiveCmd(),
)

return cmd
}

var authCreateFlags struct {
user string
org string
organization

writeUserPermission bool
readUserPermission bool
Expand Down Expand Up @@ -47,39 +63,13 @@ type AuthorizationCreateFlags struct {
readNotificationEndpointPermission bool
}

var authCreateFlags AuthorizationCreateFlags

func authCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Aliases: []string{"authorization"},
Short: "Authorization management commands",
Run: seeHelp,
}
cmd.AddCommand(
authActiveCmd(),
authCreateCmd(),
authDeleteCmd(),
authFindCmd(),
authInactiveCmd(),
)

return cmd
}

func authCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create authorization",
RunE: wrapCheckSetup(authorizationCreateF),
}

cmd.Flags().StringVarP(&authCreateFlags.org, "org", "o", "", "The organization name (required)")
cmd.MarkFlagRequired("org")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
authCreateFlags.org = h
}
authCreateFlags.organization.register(cmd, false)

cmd.Flags().StringVarP(&authCreateFlags.user, "user", "u", "", "The user name")

Expand Down Expand Up @@ -117,15 +107,16 @@ func authCreateCmd() *cobra.Command {
}

func authorizationCreateF(cmd *cobra.Command, args []string) error {
var permissions []platform.Permission
if err := authCreateFlags.organization.validOrgFlags(); err != nil {
return err
}

orgSvc, err := newOrganizationService()
if err != nil {
return err
}

ctx := context.Background()
orgFilter := platform.OrganizationFilter{Name: &authCreateFlags.org}
o, err := orgSvc.FindOrganization(ctx, orgFilter)
orgID, err := authCreateFlags.organization.getID(orgSvc)
if err != nil {
return err
}
Expand All @@ -138,14 +129,15 @@ func authorizationCreateF(cmd *cobra.Command, args []string) error {
{action: platform.WriteAction, perms: authCreateFlags.writeBucketPermissions},
}

var permissions []platform.Permission
for _, bp := range bucketPerms {
for _, p := range bp.perms {
var id platform.ID
if err := id.DecodeFromString(p); err != nil {
return err
}

p, err := platform.NewPermissionAtID(id, bp.action, platform.BucketsResourceType, o.ID)
p, err := platform.NewPermissionAtID(id, bp.action, platform.BucketsResourceType, orgID)
if err != nil {
return err
}
Expand Down Expand Up @@ -216,7 +208,7 @@ func authorizationCreateF(cmd *cobra.Command, args []string) error {
}

for _, action := range actions {
p, err := platform.NewPermission(action, provided.ResourceType, o.ID)
p, err := platform.NewPermission(action, provided.ResourceType, orgID)
if err != nil {
return err
}
Expand All @@ -226,15 +218,15 @@ func authorizationCreateF(cmd *cobra.Command, args []string) error {

authorization := &platform.Authorization{
Permissions: permissions,
OrgID: o.ID,
OrgID: orgID,
}

if userName := authCreateFlags.user; userName != "" {
userSvc, err := newUserService()
if err != nil {
return err
}
user, err := userSvc.FindUser(ctx, platform.UserFilter{
user, err := userSvc.FindUser(context.Background(), platform.UserFilter{
Name: &userName,
})
if err != nil {
Expand All @@ -243,7 +235,7 @@ func authorizationCreateF(cmd *cobra.Command, args []string) error {
authorization.UserID = user.ID
}

s, err := newAuthorizationService(flags)
s, err := newAuthorizationService()
if err != nil {
return err
}
Expand Down Expand Up @@ -279,17 +271,13 @@ func authorizationCreateF(cmd *cobra.Command, args []string) error {
return nil
}

// AuthorizationFindFlags are command line args used when finding a authorization
type AuthorizationFindFlags struct {
var authorizationFindFlags struct {
organization
user string
userID string
org string
orgID string
id string
}

var authorizationFindFlags AuthorizationFindFlags

func authFindCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "find",
Expand All @@ -299,23 +287,13 @@ func authFindCmd() *cobra.Command {

cmd.Flags().StringVarP(&authorizationFindFlags.user, "user", "u", "", "The user")
cmd.Flags().StringVarP(&authorizationFindFlags.userID, "user-id", "", "", "The user ID")
cmd.Flags().StringVarP(&authorizationFindFlags.org, "org", "o", "", "The org")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
authorizationFindFlags.org = h
}

cmd.Flags().StringVarP(&authorizationFindFlags.orgID, "org-id", "", "", "The org ID")
viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" {
authorizationFindFlags.orgID = h
}
cmd.Flags().StringVarP(&authorizationFindFlags.id, "id", "i", "", "The authorization ID")

return cmd
}

func newAuthorizationService(f Flags) (platform.AuthorizationService, error) {
func newAuthorizationService() (platform.AuthorizationService, error) {
if flags.local {
return newLocalKVService()
}
Expand All @@ -331,7 +309,7 @@ func newAuthorizationService(f Flags) (platform.AuthorizationService, error) {
}

func authorizationFindF(cmd *cobra.Command, args []string) error {
s, err := newAuthorizationService(flags)
s, err := newAuthorizationService()
if err != nil {
return err
}
Expand All @@ -354,11 +332,11 @@ func authorizationFindF(cmd *cobra.Command, args []string) error {
}
filter.UserID = uID
}
if authorizationFindFlags.org != "" {
filter.Org = &authorizationFindFlags.org
if authorizationFindFlags.organization.name != "" {
filter.Org = &authorizationFindFlags.organization.name
}
if authorizationFindFlags.orgID != "" {
oID, err := platform.IDFromString(authorizationFindFlags.orgID)
if authorizationFindFlags.organization.id != "" {
oID, err := platform.IDFromString(authorizationFindFlags.organization.id)
if err != nil {
return err
}
Expand Down Expand Up @@ -400,13 +378,10 @@ func authorizationFindF(cmd *cobra.Command, args []string) error {
return nil
}

// AuthorizationDeleteFlags are command line args used when deleting a authorization
type AuthorizationDeleteFlags struct {
var authorizationDeleteFlags struct {
id string
}

var authorizationDeleteFlags AuthorizationDeleteFlags

func authDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Expand All @@ -421,7 +396,7 @@ func authDeleteCmd() *cobra.Command {
}

func authorizationDeleteF(cmd *cobra.Command, args []string) error {
s, err := newAuthorizationService(flags)
s, err := newAuthorizationService()
if err != nil {
return err
}
Expand Down Expand Up @@ -469,13 +444,10 @@ func authorizationDeleteF(cmd *cobra.Command, args []string) error {
return nil
}

// AuthorizationActiveFlags are command line args used when enabling an authorization
type AuthorizationActiveFlags struct {
var authorizationActiveFlags struct {
id string
}

var authorizationActiveFlags AuthorizationActiveFlags

func authActiveCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "active",
Expand All @@ -490,7 +462,7 @@ func authActiveCmd() *cobra.Command {
}

func authorizationActiveF(cmd *cobra.Command, args []string) error {
s, err := newAuthorizationService(flags)
s, err := newAuthorizationService()
if err != nil {
return err
}
Expand Down Expand Up @@ -540,13 +512,10 @@ func authorizationActiveF(cmd *cobra.Command, args []string) error {
return nil
}

// AuthorizationInactiveFlags are command line args used when disabling an authorization
type AuthorizationInactiveFlags struct {
var authorizationInactiveFlags struct {
id string
}

var authorizationInactiveFlags AuthorizationInactiveFlags

func authInactiveCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "inactive",
Expand All @@ -561,7 +530,7 @@ func authInactiveCmd() *cobra.Command {
}

func authorizationInactiveF(cmd *cobra.Command, args []string) error {
s, err := newAuthorizationService(flags)
s, err := newAuthorizationService()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 7a98a48

Please sign in to comment.