Skip to content

Commit

Permalink
Improve DX of managing actions runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jul 14, 2023
1 parent df44d0e commit cbef976
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 453 deletions.
4 changes: 2 additions & 2 deletions docs/resources/action.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Actions are secure, tenant-specific, versioned functions written in Node.js that
```terraform
resource "auth0_action" "my_action" {
name = format("Test Action %s", timestamp())
runtime = "node16"
runtime = "node18"
deploy = true
code = <<-EOT
/**
Expand Down Expand Up @@ -67,7 +67,7 @@ resource "auth0_action" "my_action" {

- `dependencies` (Block Set) List of third party npm modules, and their versions, that this action depends on. (see [below for nested schema](#nestedblock--dependencies))
- `deploy` (Boolean) Deploying an action will create a new immutable version of the action. If the action is currently bound to a trigger, then the system will begin executing the newly deployed version of the action immediately.
- `runtime` (String) The Node runtime. Defaults to `node12`. Possible values are: `node12`, `node16` or `node18-actions`.
- `runtime` (String) The Node runtime. Defaults to `node16`. Possible values are: `node12`, `node16`, `node18-beta` or `node18`.
- `secrets` (Block List) List of secrets that are included in an action or a version of an action. (see [below for nested schema](#nestedblock--secrets))

### Read-Only
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/auth0_action/resource.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "auth0_action" "my_action" {
name = format("Test Action %s", timestamp())
runtime = "node16"
runtime = "node18"
deploy = true
code = <<-EOT
/**
Expand Down
5 changes: 5 additions & 0 deletions internal/auth0/action/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/auth0/go-auth0"
"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -22,6 +23,10 @@ func expandAction(config cty.Value) *management.Action {
Secrets: expandActionSecrets(config.GetAttr("secrets")),
}

if action.GetRuntime() == "node18" {
action.Runtime = auth0.String("node18-actions")
}

return action
}

Expand Down
19 changes: 10 additions & 9 deletions internal/auth0/action/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ func NewResource() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{
"node12",
"node16",
"node18", // Node 18 beta.
"node18-actions", // Node 18 GA.
"node18",
}, false),
Description: "The Node runtime. Defaults to `node12`. Possible values are: " +
"`node12`, `node16` or `node18-actions`.",
Description: "The Node runtime. Defaults to `node16`. Possible values are: `node12` (deprecated), `node16` (not recommended), or `node18` (recommended).",
},
"secrets": {
Type: schema.TypeList,
Expand Down Expand Up @@ -165,13 +163,17 @@ func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag
}

result := multierror.Append(
d.Set("name", action.Name),
d.Set("name", action.GetName()),
d.Set("supported_triggers", flattenActionTriggers(action.SupportedTriggers)),
d.Set("code", action.Code),
d.Set("code", action.GetCode()),
d.Set("dependencies", flattenActionDependencies(action.GetDependencies())),
d.Set("runtime", action.Runtime),
d.Set("runtime", action.GetRuntime()),
)

if action.GetRuntime() == "node18-actions" {
result = multierror.Append(result, d.Set("runtime", "node18"))
}

if action.DeployedVersion != nil {
result = multierror.Append(result, d.Set("version_id", action.DeployedVersion.GetID()))
}
Expand Down Expand Up @@ -204,13 +206,12 @@ func deleteAction(ctx context.Context, d *schema.ResourceData, m interface{}) di

if err := api.Action.Delete(ctx, d.Id()); err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
d.SetId("")
return nil
}

return diag.FromErr(err)
}

d.SetId("")
return nil
}

Expand Down
103 changes: 48 additions & 55 deletions internal/auth0/action/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ import (
)

const testAccActionConfigCreateWithOnlyRequiredFields = `
resource auth0_action my_action {
resource "auth0_action" "my_action" {
name = "Test Action {{.testName}}"
code = "exports.onExecutePostLogin = async (event, api) => {};"
supported_triggers {
id = "post-login"
id = "post-login"
version = "v3"
}
}
`

const testAccActionConfigUpdateAllFields = `
resource auth0_action my_action {
name = "Test Action {{.testName}}"
code = "exports.onContinuePostLogin = async (event, api) => {};"
runtime = "node16"
deploy = true
resource "auth0_action" "my_action" {
name = "Test Action {{.testName}}"
code = "exports.onContinuePostLogin = async (event, api) => {};"
runtime = "node18"
deploy = true
supported_triggers {
id = "post-login"
id = "post-login"
version = "v3"
}
Expand All @@ -47,27 +47,27 @@ resource auth0_action my_action {
`

const testAccActionConfigUpdateAgain = `
resource auth0_action my_action {
name = "Test Action {{.testName}}"
resource "auth0_action" "my_action" {
name = "Test Action {{.testName}}"
deploy = true
code = <<-EOT
code = <<-EOT
exports.onContinuePostLogin = async (event, api) => {
console.log(event)
};"
EOT
supported_triggers {
id = "post-login"
id = "post-login"
version = "v3"
}
secrets {
name = "foo"
name = "foo"
value = "123456"
}
secrets {
name = "bar"
name = "bar"
value = "654321"
}
Expand All @@ -84,7 +84,7 @@ resource auth0_action my_action {
`

const testAccActionConfigResetToRequiredFields = `
resource auth0_action my_action {
resource "auth0_action" "my_action" {
name = "Test Action {{.testName}}"
code = <<-EOT
exports.onContinuePostLogin = async (event, api) => {
Expand All @@ -93,9 +93,38 @@ resource auth0_action my_action {
EOT
supported_triggers {
id = "post-login"
id = "post-login"
version = "v3"
}
}
`

// This config makes use of a crypto dependency definition that causes the
// action build to fail. This is because the crypto package has been
// deprecated https://www.npmjs.com/package/crypto.
//
// If this is ever fixed in the API, another means of failing the build will
// need to be used here.
const testAccActionConfigCreateWithFailedBuild = `
resource "auth0_action" "my_action" {
name = "Test Action {{.testName}}"
runtime = "node18-beta"
deploy = true
code = <<-EOT
exports.onContinuePostLogin = async (event, api) => {
console.log(event)
};"
EOT
supported_triggers {
id = "post-login"
version = "v3"
}
dependencies {
name = "crypto"
version = "17.7.1"
}
}
`

Expand All @@ -122,7 +151,7 @@ func TestAccAction(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_action.my_action", "name", fmt.Sprintf("Test Action %s", t.Name())),
resource.TestCheckResourceAttr("auth0_action.my_action", "code", "exports.onContinuePostLogin = async (event, api) => {};"),
resource.TestCheckResourceAttr("auth0_action.my_action", "runtime", "node16"),
resource.TestCheckResourceAttr("auth0_action.my_action", "runtime", "node18"),
resource.TestCheckResourceAttr("auth0_action.my_action", "deploy", "true"),
resource.TestCheckResourceAttrSet("auth0_action.my_action", "version_id"),
resource.TestCheckResourceAttr("auth0_action.my_action", "supported_triggers.#", "1"),
Expand All @@ -141,7 +170,7 @@ func TestAccAction(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_action.my_action", "name", fmt.Sprintf("Test Action %s", t.Name())),
resource.TestCheckResourceAttr("auth0_action.my_action", "code", "exports.onContinuePostLogin = async (event, api) => {\n\tconsole.log(event)\n};\"\n"),
resource.TestCheckResourceAttr("auth0_action.my_action", "runtime", "node16"),
resource.TestCheckResourceAttr("auth0_action.my_action", "runtime", "node18"),
resource.TestCheckResourceAttr("auth0_action.my_action", "deploy", "true"),
resource.TestCheckResourceAttrSet("auth0_action.my_action", "version_id"),
resource.TestCheckResourceAttr("auth0_action.my_action", "supported_triggers.#", "1"),
Expand All @@ -164,7 +193,7 @@ func TestAccAction(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_action.my_action", "name", fmt.Sprintf("Test Action %s", t.Name())),
resource.TestCheckResourceAttr("auth0_action.my_action", "code", "exports.onContinuePostLogin = async (event, api) => {\n\tconsole.log(event)\n};\"\n"),
resource.TestCheckResourceAttr("auth0_action.my_action", "runtime", "node16"),
resource.TestCheckResourceAttr("auth0_action.my_action", "runtime", "node18"),
resource.TestCheckResourceAttrSet("auth0_action.my_action", "version_id"),
resource.TestCheckResourceAttr("auth0_action.my_action", "supported_triggers.#", "1"),
resource.TestCheckResourceAttr("auth0_action.my_action", "supported_triggers.0.id", "post-login"),
Expand All @@ -173,42 +202,6 @@ func TestAccAction(t *testing.T) {
resource.TestCheckResourceAttr("auth0_action.my_action", "secrets.#", "0"),
),
},
},
})
}

// This config makes use of a crypto dependency definition that causes the
// action build to fail. This is because the crypto package has been
// deprecated https://www.npmjs.com/package/crypto.
//
// If this is ever fixed in the API, another means of failing the build will
// need to be used here.
const testAccActionConfigCreateWithFailedBuild = `
resource auth0_action my_action {
name = "Test Action {{.testName}}"
runtime = "node18-actions"
deploy = true
code = <<-EOT
exports.onContinuePostLogin = async (event, api) => {
console.log(event)
};"
EOT
supported_triggers {
id = "post-login"
version = "v3"
}
dependencies {
name = "crypto"
version = "17.7.1"
}
}
`

func TestAccAction_FailedBuild(t *testing.T) {
acctest.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
Config: acctest.ParseTestName(testAccActionConfigCreateWithFailedBuild, t.Name()),
Check: resource.ComposeTestCheckFunc(
Expand Down
Loading

0 comments on commit cbef976

Please sign in to comment.