From 3bf0f9cebebce5f61e1dedef9e4c1818b27e5d3e Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 11 Jan 2023 13:37:23 -0500 Subject: [PATCH] DXCDT-320: Fix management of action triggers (#597) Co-authored-by: Will Vedder Co-authored-by: Sergiu Ghitea Co-authored-by: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> --- docs/auth0_actions_update.md | 12 ++--- internal/cli/actions.go | 88 ++++++++++++------------------------ 2 files changed, 34 insertions(+), 66 deletions(-) diff --git a/docs/auth0_actions_update.md b/docs/auth0_actions_update.md index 08d92ea77..d1e2b63c1 100644 --- a/docs/auth0_actions_update.md +++ b/docs/auth0_actions_update.md @@ -7,7 +7,7 @@ Update an action. To update interactively, use `auth0 actions update` with no arguments. -To update non-interactively, supply the action id, name, trigger, secrets and dependencies through the flags. +To update non-interactively, supply the action id, name, code, secrets and dependencies through the flags. ## Usage ``` @@ -19,11 +19,10 @@ auth0 actions update [flags] ``` auth0 actions update auth0 actions update --name myaction - auth0 actions update --name myaction --trigger post-login - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value" - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value" auth0 actions update -n myaction -t post-login -c "$(cat path/to/code.js)" -d "lodash=4.0.0" -d "uuid=9.0.0" -s "API_KEY=value" -s "SECRET=value" --json ``` @@ -36,7 +35,6 @@ auth0 actions update [flags] --json Output in json format. -n, --name string Name of the action. -s, --secret stringToString Secrets to be used in the action. (default []) - -t, --trigger string Trigger of the action. At this time, an action can only target a single trigger at a time. ``` diff --git a/internal/cli/actions.go b/internal/cli/actions.go index 685875bc2..47ca7e6cb 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -148,7 +148,7 @@ func showActionCmd(cli *cli) *cobra.Command { action, err = cli.api.Action.Read(inputs.ID) return err }); err != nil { - return fmt.Errorf("Unable to get an action with Id '%s': %w", inputs.ID, err) + return fmt.Errorf("Unable to get an action with ID '%s': %w", inputs.ID, err) } cli.renderer.ActionShow(action) @@ -190,12 +190,17 @@ func createActionCmd(cli *cli) *cobra.Command { return err } - triggers, version, err := latestActionTriggers(cli) + triggers, err := getCurrentTriggers(cli) if err != nil { return err } - if err := actionTrigger.Select(cmd, &inputs.Trigger, triggers, nil); err != nil { + triggerIds := make([]string, 0) + for _, t := range triggers { + triggerIds = append(triggerIds, t.GetID()) + } + + if err := actionTrigger.Select(cmd, &inputs.Trigger, triggerIds, nil); err != nil { return err } @@ -212,6 +217,14 @@ func createActionCmd(cli *cli) *cobra.Command { return err } + var version string + for _, t := range triggers { + if t.GetID() == inputs.Trigger { + version = t.GetVersion() + break + } + } + action := &management.Action{ Name: &inputs.Name, SupportedTriggers: []management.ActionTrigger{ @@ -250,7 +263,6 @@ func updateActionCmd(cli *cli) *cobra.Command { var inputs struct { ID string Name string - Trigger string Code string Dependencies map[string]string Secrets map[string]string @@ -262,15 +274,14 @@ func updateActionCmd(cli *cli) *cobra.Command { Short: "Update an action", Long: "Update an action.\n\n" + "To update interactively, use `auth0 actions update` with no arguments.\n\n" + - "To update non-interactively, supply the action id, name, trigger, secrets and " + + "To update non-interactively, supply the action id, name, code, secrets and " + "dependencies through the flags.", Example: ` auth0 actions update auth0 actions update --name myaction - auth0 actions update --name myaction --trigger post-login - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value" - auth0 actions update --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value" + auth0 actions update --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value" auth0 actions update -n myaction -t post-login -c "$(cat path/to/code.js)" -d "lodash=4.0.0" -d "uuid=9.0.0" -s "API_KEY=value" -s "SECRET=value" --json`, RunE: func(cmd *cobra.Command, args []string) error { if len(args) > 0 { @@ -296,20 +307,6 @@ func updateActionCmd(cli *cli) *cobra.Command { return err } - triggers, version, err := latestActionTriggers(cli) - if err != nil { - return err - } - - var currentTriggerId = "" - if len(current.SupportedTriggers) > 0 { - currentTriggerId = current.SupportedTriggers[0].GetID() - } - - if err := actionTrigger.SelectU(cmd, &inputs.Trigger, triggers, ¤tTriggerId); err != nil { - return err - } - // TODO(cyx): we can re-think this once we have // `--stdin` based commands. For now we don't have // those yet, so keeping this simple. @@ -330,10 +327,6 @@ func updateActionCmd(cli *cli) *cobra.Command { inputs.Name = current.GetName() } - if inputs.Trigger == "" && currentTriggerId != "" { - inputs.Trigger = currentTriggerId - } - if inputs.Code == "" { inputs.Code = current.GetCode() } @@ -342,14 +335,9 @@ func updateActionCmd(cli *cli) *cobra.Command { // re-hydrated by the SDK, which we'll use below during // display. action := &management.Action{ - Name: &inputs.Name, - SupportedTriggers: []management.ActionTrigger{ - { - ID: &inputs.Trigger, - Version: &version, - }, - }, - Code: &inputs.Code, + Name: &inputs.Name, + SupportedTriggers: current.SupportedTriggers, + Code: &inputs.Code, } if len(inputs.Dependencies) == 0 { @@ -377,7 +365,6 @@ func updateActionCmd(cli *cli) *cobra.Command { cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.") actionName.RegisterStringU(cmd, &inputs.Name, "") - actionTrigger.RegisterStringU(cmd, &inputs.Trigger, "") actionCode.RegisterStringU(cmd, &inputs.Code, "") actionDependency.RegisterStringMapU(cmd, &inputs.Dependencies, nil) actionSecret.RegisterStringMapU(cmd, &inputs.Secrets, nil) @@ -547,27 +534,17 @@ func formatActionDetailsPath(id string) string { return fmt.Sprintf("actions/library/details/%s", id) } -func latestActionTriggerVersion(list []*management.ActionTrigger) string { - latestVersion := "v1" - for _, t := range list { - if t.GetVersion() > latestVersion { - latestVersion = t.GetVersion() - } - } - return latestVersion -} - -func filterActionTriggersByVersion(list []*management.ActionTrigger, version string) []*management.ActionTrigger { +func filterDeprecatedActionTriggers(list []*management.ActionTrigger) []*management.ActionTrigger { res := []*management.ActionTrigger{} for _, t := range list { - if t.GetVersion() == version && t.GetStatus() == "CURRENT" { + if t.GetStatus() == "CURRENT" { res = append(res, t) } } return res } -func latestActionTriggers(cli *cli) ([]string, string, error) { +func getCurrentTriggers(cli *cli) ([]*management.ActionTrigger, error) { var triggers []*management.ActionTrigger if err := ansi.Waiting(func() error { list, err := cli.api.Action.Triggers() @@ -577,17 +554,10 @@ func latestActionTriggers(cli *cli) ([]string, string, error) { triggers = list.Triggers return nil }); err != nil { - return nil, "", err + return nil, err } - latestTriggerVersion := latestActionTriggerVersion(triggers) - triggers = filterActionTriggersByVersion(triggers, latestTriggerVersion) - var triggerIds []string - - for _, t := range triggers { - triggerIds = append(triggerIds, t.GetID()) - } - return triggerIds, latestTriggerVersion, nil + return filterDeprecatedActionTriggers(triggers), nil } func actionTemplate(key string) string {