Skip to content

Commit 327d396

Browse files
committed
feat: add app sharing_level property
1 parent dfd2dd0 commit 327d396

File tree

5 files changed

+221
-71
lines changed

5 files changed

+221
-71
lines changed

docs/resources/app.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ EOF
2626
}
2727
2828
resource "coder_app" "code-server" {
29-
agent_id = coder_agent.dev.id
30-
name = "VS Code"
31-
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
32-
url = "http://localhost:13337"
33-
subdomain = false
29+
agent_id = coder_agent.dev.id
30+
name = "VS Code"
31+
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
32+
url = "http://localhost:13337"
33+
sharing_level = "owner"
34+
subdomain = false
3435
healthcheck {
3536
url = "http://localhost:13337/healthz"
3637
interval = 5
@@ -67,6 +68,7 @@ resource "coder_app" "intellij" {
6768
- `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icons. Use a built-in icon with `data.coder_workspace.me.access_url + "/icons/<path>"`.
6869
- `name` (String) A display name to identify the app.
6970
- `relative_path` (Boolean, Deprecated) Specifies whether the URL will be accessed via a relative path or wildcard. Use if wildcard routing is unavailable. Defaults to true.
71+
- `sharing_level` (String) Determines the sharing level of the app. Application sharing is an enterprise feature and any values will be ignored (and sharing disabled) if your deployment is not entitled to use application sharing. Valid values are "owner", "template", "authenticated" and "public". Level "owner" disables sharing on the app, so only the workspace owner can access it. Level "template" shares the app with all users that can read the workspace's template. Level "authenticated" shares the app with all authenticated users. Level "public" shares it with any user, including unauthenticated users. Permitted application sharing levels can be controlled via a flag on "coder server". Defaults to "owner" (sharing disabled).
7072
- `subdomain` (Boolean) Determines whether the app will be accessed via it's own subdomain or whether it will be accessed via a path on Coder. If wildcards have not been setup by the administrator then apps with "subdomain" set to true will not be accessible. Defaults to false.
7173
- `url` (String) A URL to be proxied to from inside the workspace. Either "command" or "url" may be specified, but not both.
7274

examples/resources/coder_app/resource.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ EOF
1111
}
1212

1313
resource "coder_app" "code-server" {
14-
agent_id = coder_agent.dev.id
15-
name = "VS Code"
16-
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
17-
url = "http://localhost:13337"
18-
subdomain = false
14+
agent_id = coder_agent.dev.id
15+
name = "VS Code"
16+
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
17+
url = "http://localhost:13337"
18+
sharing_level = "owner"
19+
subdomain = false
1920
healthcheck {
2021
url = "http://localhost:13337/healthz"
2122
interval = 5
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
data "coder_parameter" "example" {
2-
display_name = "Region"
3-
description = "Specify a region to place your workspace."
4-
immutable = true
5-
type = "string"
6-
option {
7-
value = "us-central1-a"
8-
label = "US Central"
9-
icon = "/icon/usa.svg"
10-
}
11-
option {
12-
value = "asia-central1-a"
13-
label = "Asia"
14-
icon = "/icon/asia.svg"
15-
}
2+
display_name = "Region"
3+
description = "Specify a region to place your workspace."
4+
immutable = true
5+
type = "string"
6+
option {
7+
value = "us-central1-a"
8+
label = "US Central"
9+
icon = "/icon/usa.svg"
10+
}
11+
option {
12+
value = "asia-central1-a"
13+
label = "Asia"
14+
icon = "/icon/asia.svg"
15+
}
1616
}
1717

1818
data "coder_parameter" "ami" {
19-
display_name = "Machine Image"
20-
option {
21-
value = "ami-xxxxxxxx"
22-
label = "Ubuntu"
23-
icon = "/icon/ubuntu.svg"
24-
}
19+
display_name = "Machine Image"
20+
option {
21+
value = "ami-xxxxxxxx"
22+
label = "Ubuntu"
23+
icon = "/icon/ubuntu.svg"
24+
}
2525
}
2626

2727
data "coder_parameter" "image" {
28-
display_name = "Docker Image"
29-
icon = "/icon/docker.svg"
30-
type = "bool"
28+
display_name = "Docker Image"
29+
icon = "/icon/docker.svg"
30+
type = "bool"
3131
}
3232

3333
data "coder_parameter" "cores" {
34-
display_name = "CPU Cores"
35-
icon = "/icon/"
34+
display_name = "CPU Cores"
35+
icon = "/icon/"
3636
}
3737

3838
data "coder_parameter" "disk_size" {
39-
display_name = "Disk Size"
40-
type = "number"
41-
validation {
42-
# This can apply to number and string types.
43-
min = 0
44-
max = 10
45-
}
39+
display_name = "Disk Size"
40+
type = "number"
41+
validation {
42+
# This can apply to number and string types.
43+
min = 0
44+
max = 10
45+
}
4646
}

provider/app.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66

77
"github.com/google/uuid"
8+
"github.com/hashicorp/go-cty/cty"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
)
@@ -76,6 +77,39 @@ func appResource() *schema.Resource {
7677
ForceNew: true,
7778
Optional: true,
7879
},
80+
"sharing_level": {
81+
Type: schema.TypeString,
82+
Description: "Determines the sharing level of the app. " +
83+
"Application sharing is an enterprise feature and any " +
84+
"values will be ignored (and sharing disabled) if your " +
85+
"deployment is not entitled to use application sharing. " +
86+
`Valid values are "owner", "template", "authenticated" ` +
87+
`and "public". Level "owner" disables sharing on the ` +
88+
"app, so only the workspace owner can access it. Level " +
89+
`"template" shares the app with all users that can read ` +
90+
`the workspace's template. Level "authenticated" shares ` +
91+
`the app with all authenticated users. Level "public" ` +
92+
"shares it with any user, including unauthenticated " +
93+
"users. Permitted application sharing levels can be " +
94+
`controlled via a flag on "coder server". Defaults to ` +
95+
`"owner" (sharing disabled).`,
96+
ForceNew: true,
97+
Optional: true,
98+
Default: "owner",
99+
ValidateDiagFunc: func(val interface{}, c cty.Path) diag.Diagnostics {
100+
valStr, ok := val.(string)
101+
if !ok {
102+
return diag.Errorf("expected string, got %T", val)
103+
}
104+
105+
switch valStr {
106+
case "owner", "template", "authenticated", "public":
107+
return nil
108+
}
109+
110+
return diag.Errorf(`invalid app sharing_level %q, must be one of "owner", "template", "authenticated", "public"`, valStr)
111+
},
112+
},
79113
"url": {
80114
Type: schema.TypeString,
81115
Description: "A URL to be proxied to from inside the workspace. " +

provider/app_test.go

Lines changed: 142 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package provider_test
22

33
import (
4+
"fmt"
5+
"regexp"
46
"testing"
57

68
"github.com/coder/terraform-provider-coder/provider"
@@ -12,13 +14,17 @@ import (
1214

1315
func TestApp(t *testing.T) {
1416
t.Parallel()
15-
resource.Test(t, resource.TestCase{
16-
Providers: map[string]*schema.Provider{
17-
"coder": provider.New(),
18-
},
19-
IsUnitTest: true,
20-
Steps: []resource.TestStep{{
21-
Config: `
17+
18+
t.Run("OK", func(t *testing.T) {
19+
t.Parallel()
20+
21+
resource.Test(t, resource.TestCase{
22+
Providers: map[string]*schema.Provider{
23+
"coder": provider.New(),
24+
},
25+
IsUnitTest: true,
26+
Steps: []resource.TestStep{{
27+
Config: `
2228
provider "coder" {
2329
}
2430
resource "coder_agent" "dev" {
@@ -38,28 +44,135 @@ func TestApp(t *testing.T) {
3844
}
3945
}
4046
`,
41-
Check: func(state *terraform.State) error {
42-
require.Len(t, state.Modules, 1)
43-
require.Len(t, state.Modules[0].Resources, 2)
44-
resource := state.Modules[0].Resources["coder_app.code-server"]
45-
require.NotNil(t, resource)
46-
for _, key := range []string{
47-
"agent_id",
48-
"name",
49-
"icon",
50-
"subdomain",
51-
"url",
52-
"healthcheck.0.url",
53-
"healthcheck.0.interval",
54-
"healthcheck.0.threshold",
55-
} {
56-
value := resource.Primary.Attributes[key]
57-
t.Logf("%q = %q", key, value)
58-
require.NotNil(t, value)
59-
require.Greater(t, len(value), 0)
60-
}
61-
return nil
47+
Check: func(state *terraform.State) error {
48+
require.Len(t, state.Modules, 1)
49+
require.Len(t, state.Modules[0].Resources, 2)
50+
resource := state.Modules[0].Resources["coder_app.code-server"]
51+
require.NotNil(t, resource)
52+
for _, key := range []string{
53+
"agent_id",
54+
"name",
55+
"icon",
56+
"subdomain",
57+
// Should be set by default even though it isn't
58+
// specified.
59+
"sharing_level",
60+
"url",
61+
"healthcheck.0.url",
62+
"healthcheck.0.interval",
63+
"healthcheck.0.threshold",
64+
} {
65+
value := resource.Primary.Attributes[key]
66+
t.Logf("%q = %q", key, value)
67+
require.NotNil(t, value)
68+
require.Greater(t, len(value), 0)
69+
}
70+
return nil
71+
},
72+
}},
73+
})
74+
})
75+
76+
t.Run("SharingLevel", func(t *testing.T) {
77+
t.Parallel()
78+
79+
cases := []struct {
80+
name string
81+
value string
82+
expectValue string
83+
expectError *regexp.Regexp
84+
}{
85+
{
86+
name: "Default",
87+
value: "", // default
88+
expectValue: "owner",
89+
},
90+
{
91+
name: "InvalidValue",
92+
value: "blah",
93+
expectError: regexp.MustCompile(`invalid app sharing_level "blah"`),
94+
},
95+
{
96+
name: "ExplicitOwner",
97+
value: "owner",
98+
expectValue: "owner",
6299
},
63-
}},
100+
{
101+
name: "ExplicitTemplate",
102+
value: "template",
103+
expectValue: "template",
104+
},
105+
{
106+
name: "ExplicitAuthenticated",
107+
value: "authenticated",
108+
expectValue: "authenticated",
109+
},
110+
{
111+
name: "ExplicitPublic",
112+
value: "public",
113+
expectValue: "public",
114+
},
115+
}
116+
117+
for _, c := range cases {
118+
c := c
119+
120+
t.Run(c.name, func(t *testing.T) {
121+
t.Parallel()
122+
123+
sharingLine := ""
124+
if c.value != "" {
125+
sharingLine = fmt.Sprintf("sharing_level = %q", c.value)
126+
}
127+
config := fmt.Sprintf(`
128+
provider "coder" {
129+
}
130+
resource "coder_agent" "dev" {
131+
os = "linux"
132+
arch = "amd64"
133+
}
134+
resource "coder_app" "code-server" {
135+
agent_id = coder_agent.dev.id
136+
name = "code-server"
137+
icon = "builtin:vim"
138+
url = "http://localhost:13337"
139+
%s
140+
healthcheck {
141+
url = "http://localhost:13337/healthz"
142+
interval = 5
143+
threshold = 6
144+
}
145+
}
146+
`, sharingLine)
147+
148+
checkFn := func(state *terraform.State) error {
149+
require.Len(t, state.Modules, 1)
150+
require.Len(t, state.Modules[0].Resources, 2)
151+
resource := state.Modules[0].Resources["coder_app.code-server"]
152+
require.NotNil(t, resource)
153+
154+
// Read sharing_level and ensure it matches the expected
155+
// value.
156+
value := resource.Primary.Attributes["sharing_level"]
157+
require.Equal(t, c.expectValue, value)
158+
return nil
159+
}
160+
if c.expectError != nil {
161+
checkFn = nil
162+
}
163+
164+
resource.Test(t, resource.TestCase{
165+
Providers: map[string]*schema.Provider{
166+
"coder": provider.New(),
167+
},
168+
IsUnitTest: true,
169+
Steps: []resource.TestStep{{
170+
Config: config,
171+
Check: checkFn,
172+
ExpectError: c.expectError,
173+
}},
174+
})
175+
})
176+
}
64177
})
65178
}

0 commit comments

Comments
 (0)