diff --git a/go.sum b/go.sum index a9270b6e5..7767488c2 100644 --- a/go.sum +++ b/go.sum @@ -128,7 +128,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -165,7 +164,6 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -500,7 +498,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= diff --git a/internal/auth0/actions.go b/internal/auth0/actions.go index 7b8ae74a4..a9af35768 100644 --- a/internal/auth0/actions.go +++ b/internal/auth0/actions.go @@ -18,3 +18,8 @@ type ActionVersionAPI interface { Delete(actionID string, id string, opts ...management.RequestOption) error Test(actionID string, id string, payload management.Object) (management.Object, error) } + +type ActionBindingAPI interface { + List(triggerID management.TriggerID, opts ...management.RequestOption) (c *management.ActionBindingList, err error) + Update(triggerID management.TriggerID, v *management.ActionBindingList) error +} diff --git a/internal/auth0/auth0.go b/internal/auth0/auth0.go index 9052c1e38..6d18af900 100644 --- a/internal/auth0/auth0.go +++ b/internal/auth0/auth0.go @@ -10,6 +10,7 @@ import ( type API struct { Action ActionAPI ActionVersion ActionVersionAPI + ActionBinding ActionBindingAPI Client ClientAPI Connection ConnectionAPI Log LogAPI @@ -21,6 +22,7 @@ func NewAPI(m *management.Management) *API { return &API{ Action: m.Action, ActionVersion: m.ActionVersion, + ActionBinding: m.ActionBinding, Client: m.Client, Connection: m.Connection, Log: m.Log, diff --git a/internal/cli/actions.go b/internal/cli/actions.go index 67620b035..270fc55f9 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -23,6 +23,20 @@ func actionsCmd(cli *cli) *cobra.Command { cmd.AddCommand(listActionsCmd(cli)) cmd.AddCommand(testActionCmd(cli)) cmd.AddCommand(createActionCmd(cli)) + cmd.AddCommand(triggersCmd(cli)) + + return cmd +} + +func triggersCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "triggers", + Short: "manage resources for action triggers.", + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.AddCommand(showTriggerCmd(cli)) + cmd.AddCommand(reorderTriggerCmd(cli)) return cmd } @@ -50,6 +64,28 @@ Lists your existing actions. To create one try: return cmd } +func readJsonFile(filePath string, out interface{}) error { + // Open our jsonFile + jsonFile, err := os.Open(filePath) + // if we os.Open returns an error then handle it + if err != nil { + return err + } + // defer the closing of our jsonFile so that we can parse it later on + defer jsonFile.Close() + + byteValue, err := ioutil.ReadAll(jsonFile) + if err != nil { + return err + } + + if err := json.Unmarshal(byteValue, out); err != nil { + return err + } + + return nil +} + func testActionCmd(cli *cli) *cobra.Command { var actionId string var versionId string @@ -61,27 +97,13 @@ func testActionCmd(cli *cli) *cobra.Command { Short: "Test an action draft against a payload", Long: `$ auth0 actions test --name --file `, RunE: func(cmd *cobra.Command, args []string) error { - // Open our jsonFile - jsonFile, err := os.Open(payloadFile) - // if we os.Open returns an error then handle it + err := readJsonFile(payloadFile, &payload) if err != nil { return err } - // defer the closing of our jsonFile so that we can parse it later on - defer jsonFile.Close() - - byteValue, err := ioutil.ReadAll(jsonFile) - if err != nil { - return err - } - - if err := json.Unmarshal([]byte(byteValue), &payload); err != nil { - return err - } var result management.Object err = ansi.Spinner(fmt.Sprintf("Testing action: %s, version: %s", actionId, versionId), func() error { - fmt.Println(payload) result, err = cli.api.ActionVersion.Test(actionId, versionId, payload) return err }) @@ -159,3 +181,78 @@ Creates a new action: return cmd } + +func showTriggerCmd(cli *cli) *cobra.Command { + var trigger string + + cmd := &cobra.Command{ + Use: "show", + Short: "Show actions by trigger", + Long: `$ auth0 actions triggers show --trigger post-login`, + RunE: func(cmd *cobra.Command, args []string) error { + if err := validators.TriggerID(trigger); err != nil { + return err + } + + triggerID := management.TriggerID(trigger) + + var list *management.ActionBindingList + err := ansi.Spinner("Loading actions", func() error { + var err error + list, err = cli.api.ActionBinding.List(triggerID) + return err + }) + + if err != nil { + return err + } + + cli.renderer.ActionTriggersList(list.Bindings) + return nil + }, + } + + cmd.Flags().StringVarP(&trigger, "trigger", "t", string(management.PostLogin), "Trigger type for action.") + + return cmd +} + +func reorderTriggerCmd(cli *cli) *cobra.Command { + var trigger string + var bindingsFile string + + cmd := &cobra.Command{ + Use: "reorder", + Short: "Reorders actions by trigger", + Long: `$ auth0 actions triggers reorder --trigger --file `, + RunE: func(cmd *cobra.Command, args []string) error { + if err := validators.TriggerID(trigger); err != nil { + return err + } + + triggerID := management.TriggerID(trigger) + + var list *management.ActionBindingList + err := readJsonFile(bindingsFile, &list) + if err != nil { + return err + } + + err = ansi.Spinner("Loading actions", func() error { + return cli.api.ActionBinding.Update(triggerID, list) + }) + + if err != nil { + return err + } + + cli.renderer.ActionTriggersList(list.Bindings) + return nil + }, + } + + cmd.Flags().StringVarP(&trigger, "trigger", "t", string(management.PostLogin), "Trigger type for action.") + cmd.Flags().StringVarP(&bindingsFile, "file", "f", "", "File containing the bindings") + + return cmd +} diff --git a/internal/display/actions.go b/internal/display/actions.go index c94fc3e0e..95dab9588 100644 --- a/internal/display/actions.go +++ b/internal/display/actions.go @@ -23,6 +23,20 @@ func (v *actionView) AsTableRow() []string { return []string{v.ID, v.Name, v.Type, v.CreatedAt} } +type triggerView struct { + ID string + ActionID string + DisplayName string +} + +func (v *triggerView) AsTableHeader() []string { + return []string{"ID", "Action ID", "Action Name"} +} + +func (v *triggerView) AsTableRow() []string { + return []string{v.ID, v.ActionID, v.DisplayName} +} + func (r *Renderer) ActionList(actions []*management.Action) { r.Heading(ansi.Bold(r.Tenant), "actions\n") @@ -68,3 +82,19 @@ func (r *Renderer) ActionCreate(action *management.Action) { r.Results([]View{v}) } + +func (r *Renderer) ActionTriggersList(bindings []*management.ActionBinding) { + r.Heading(ansi.Bold(r.Tenant), "triggers\n") + + var res []View + for _, b := range bindings { + res = append(res, &triggerView{ + ID: auth0.StringValue(b.ID), + ActionID: auth0.StringValue(b.Action.ID), + DisplayName: auth0.StringValue(b.DisplayName), + }) + + } + + r.Results(res) +}