Skip to content

Commit

Permalink
DXCDT-320: Fix management of action triggers (#597)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Vedder <[email protected]>
Co-authored-by: Sergiu Ghitea <[email protected]>
Co-authored-by: Sergiu Ghitea <[email protected]>
  • Loading branch information
4 people authored Jan 11, 2023
1 parent d1ab6f1 commit 3bf0f9c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 66 deletions.
12 changes: 5 additions & 7 deletions docs/auth0_actions_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -19,11 +19,10 @@ auth0 actions update [flags]
```
auth0 actions update <action-id>
auth0 actions update <action-id> --name myaction
auth0 actions update <action-id> --name myaction --trigger post-login
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --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 <action-id> --name myaction --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --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 <action-id> -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
```

Expand All @@ -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.
```


Expand Down
88 changes: 29 additions & 59 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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 <action-id>
auth0 actions update <action-id> --name myaction
auth0 actions update <action-id> --name myaction --trigger post-login
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --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 <action-id> --name myaction --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --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 <action-id> -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 {
Expand All @@ -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, &currentTriggerId); 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.
Expand All @@ -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()
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down

0 comments on commit 3bf0f9c

Please sign in to comment.