Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add enable/disable rules support to cli #16

Merged
merged 15 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
deviceCodeEndpoint = "https://auth0.auth0.com/oauth/device/code"
oauthTokenEndpoint = "https://auth0.auth0.com/oauth/token"
// TODO(jfatta) extend the scope as we extend the CLI:
scope = "openid read:roles read:clients read:logs read:rules"
scope = "openid read:roles read:clients read:logs read:rules update:rules delete:rules create:rules"
audiencePath = "/api/v2/"
)

Expand Down
129 changes: 129 additions & 0 deletions internal/cli/rules.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package cli

import (
"errors"
"fmt"

"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
)

var rules []string

func rulesCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "rules",
Expand All @@ -12,6 +18,8 @@ func rulesCmd(cli *cli) *cobra.Command {

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(listRulesCmd(cli))
cmd.AddCommand(enableRuleCmd(cli))
cmd.AddCommand(disableRuleCmd(cli))
return cmd
}

Expand All @@ -34,3 +42,124 @@ func listRulesCmd(cli *cli) *cobra.Command {

return cmd
}

func enableRuleCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "enable",
Short: "enable rule(s)",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(rules) == 0 {
return errors.New("No rules to process")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// fmt.Printf("Got following rules (%d):\n%s\n", len(rules), rules)

// @TODO Cleanup error handling, some can pass some can fail
updateErrors := enableRules(rules, cli.api.Client.Rule)
if updateErrors != nil {
for _, err := range updateErrors {
fmt.Println(err)
}
return errors.New("Some rule updates failed")
}

// @TODO Only display modified rules
rules, err := cli.api.Client.Rule.List()

if err != nil {
return err
}

cli.renderer.RulesList(rules)

return nil
},
}

cmd.Flags().StringSliceVarP(&rules, "rules", "r", nil, "rule ids")
// @TODO Take a look at this later
// err := cmd.MarkFlagRequired("rules")

return cmd
}

func disableRuleCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "disable",
Short: "disable rule(s)",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(rules) == 0 {
return errors.New("No rules to process")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// fmt.Printf("Got following rules (%d):\n%s\n", len(rules), rules)

// @TODO Cleanup error handling, some can pass some can fail
updateErrors := disableRules(rules, cli.api.Client.Rule)
if updateErrors != nil {
for _, err := range updateErrors {
fmt.Println(err)
}
return errors.New("Some rule updates failed")
}

// @TODO Only display modified rules
rules, err := cli.api.Client.Rule.List()

if err != nil {
return err
}

cli.renderer.RulesList(rules)

return nil
},
}

cmd.Flags().StringSliceVarP(&rules, "rules", "r", nil, "rule ids")
// @TODO Take a look at this later
// err := cmd.MarkFlagRequired("rules")

return cmd
}

// @TODO refactor to rules package
// @TODO can probably run these concurrently

func enableRules(ruleIds []string, ruleManager *management.RuleManager) []error {
var updateErrors []error
enable := true
for _, ruleID := range ruleIds {
err := ruleManager.Update(ruleID, &management.Rule{Enabled: &enable})
vprasanth marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
updateErrors = append(updateErrors, err)
}
}

if len(updateErrors) != 0 {
return updateErrors
}

return nil
}

func disableRules(ruleIds []string, ruleManager *management.RuleManager) []error {
var updateErrors []error
enable := false
for _, ruleID := range ruleIds {
err := ruleManager.Update(ruleID, &management.Rule{Enabled: &enable})
vprasanth marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
updateErrors = append(updateErrors, err)
}
}

if len(updateErrors) != 0 {
return updateErrors
}

return nil
}
1 change: 1 addition & 0 deletions internal/rules/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rules