From 2e2020e359a935aa2c87f3077535dd423fe7cd4a Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 26 Jan 2021 12:19:33 -0500 Subject: [PATCH] feat: actions triggers show (#36) --- go.sum | 4 --- internal/auth0/actions.go | 4 +++ internal/auth0/auth0.go | 2 ++ internal/cli/actions.go | 50 ++++++++++++++++++++++++++++++++++++- internal/display/actions.go | 28 +++++++++++++++++++++ 5 files changed, 83 insertions(+), 5 deletions(-) diff --git a/go.sum b/go.sum index ad707a2fd..796e34f68 100644 --- a/go.sum +++ b/go.sum @@ -124,7 +124,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= @@ -158,10 +157,8 @@ 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/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= @@ -486,7 +483,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..4ac3628cd 100644 --- a/internal/auth0/actions.go +++ b/internal/auth0/actions.go @@ -18,3 +18,7 @@ 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) +} 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..df8e2e658 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -23,6 +23,19 @@ 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)) return cmd } @@ -75,7 +88,7 @@ func testActionCmd(cli *cli) *cobra.Command { return err } - if err := json.Unmarshal([]byte(byteValue), &payload); err != nil { + if err := json.Unmarshal(byteValue, &payload); err != nil { return err } @@ -159,3 +172,38 @@ 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 +} diff --git a/internal/display/actions.go b/internal/display/actions.go index c94fc3e0e..ac22b3a65 100644 --- a/internal/display/actions.go +++ b/internal/display/actions.go @@ -23,6 +23,19 @@ func (v *actionView) AsTableRow() []string { return []string{v.ID, v.Name, v.Type, v.CreatedAt} } +type triggerView struct { + ActionID string + DisplayName string +} + +func (v *triggerView) AsTableHeader() []string { + return []string{"Action ID", "Action Name"} +} + +func (v *triggerView) AsTableRow() []string { + return []string{v.ActionID, v.DisplayName} +} + func (r *Renderer) ActionList(actions []*management.Action) { r.Heading(ansi.Bold(r.Tenant), "actions\n") @@ -68,3 +81,18 @@ 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{ + ActionID: auth0.StringValue(b.Action.ID), + DisplayName: auth0.StringValue(b.DisplayName), + }) + + } + + r.Results(res) +}