Skip to content

app: add cors_behavior attribute #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ resource "coder_app" "vim" {
### Optional

- `command` (String) A command to run in a terminal opening this app. In the web, this will open in a new tab. In the CLI, this will SSH and execute the command. Either `command` or `url` may be specified, but not both.
- `cors_behavior` (String)
- `display_name` (String) A display name to identify the app. Defaults to the slug.
- `external` (Boolean) Specifies whether `url` is opened on the client machine instead of proxied through the workspace.
- `healthcheck` (Block Set, Max: 1) HTTP health checking to determine the application readiness. (see [below for nested schema](#nestedblock--healthcheck))
Expand Down
19 changes: 19 additions & 0 deletions provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ func appResource() *schema.Resource {
return diag.Errorf("invalid app share %q, must be one of \"owner\", \"authenticated\", \"public\"", valStr)
},
},
"cors_behavior": {
Type: schema.TypeString,
Default: "simple",
ForceNew: true,
Optional: true,
ValidateDiagFunc: func(val interface{}, c cty.Path) diag.Diagnostics {
valStr, ok := val.(string)
if !ok {
return diag.Errorf("expected string, got %T", val)
}

switch valStr {
case "simple", "passthru":
return nil
}

return diag.Errorf("invalid app CORS behavior %q, must be one of \"simple\", \"passthru\"", valStr)
},
},
"url": {
Type: schema.TypeString,
Description: "An external url if `external=true` or a URL to be proxied to from inside the workspace. " +
Expand Down
59 changes: 59 additions & 0 deletions provider/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,63 @@ func TestApp(t *testing.T) {
}
})

t.Run("CORS Behavior", func(t *testing.T) {
t.Parallel()

baseConfig := `
provider "coder" {}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
}
resource "coder_app" "test" {
agent_id = coder_agent.dev.id
slug = "test"
display_name = "Testing"
url = "https://google.com"
external = true
%s
}`

cases := []struct {
name string
config string
behavior string
}{{
name: "Default CORS",
config: fmt.Sprintf(baseConfig, ""),
behavior: "simple",
}, {
name: "Simple CORS",
config: fmt.Sprintf(baseConfig, "cors_behavior = \"simple\""),
behavior: "simple",
}, {
name: "Passthru CORS",
config: fmt.Sprintf(baseConfig, "cors_behavior = \"passthru\""),
behavior: "passthru",
}}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
ProviderFactories: coderFactory(),
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: tc.config,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 2)
resource := state.Modules[0].Resources["coder_app.test"]
require.NotNil(t, resource)
require.Equal(t, tc.behavior, resource.Primary.Attributes["cors_behavior"])
return nil
},
ExpectError: nil,
}},
})
})
}
})

}
Loading