Skip to content

Commit 6adf9b4

Browse files
authored
feat: add app share property (#62)
1 parent dfd2dd0 commit 6adf9b4

File tree

5 files changed

+210
-61
lines changed

5 files changed

+210
-61
lines changed

Diff for: docs/resources/app.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ resource "coder_app" "code-server" {
3030
name = "VS Code"
3131
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
3232
url = "http://localhost:13337"
33+
share = "owner"
3334
subdomain = false
3435
healthcheck {
3536
url = "http://localhost:13337/healthz"
@@ -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+
- `share` (String) 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 configured 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

Diff for: examples/resources/coder_app/resource.tf

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ resource "coder_app" "code-server" {
1515
name = "VS Code"
1616
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
1717
url = "http://localhost:13337"
18+
share = "owner"
1819
subdomain = false
1920
healthcheck {
2021
url = "http://localhost:13337/healthz"

Diff for: examples/resources/coder_parameter/resource.tf

+32-32
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
}

Diff for: provider/app.go

+33
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,38 @@ func appResource() *schema.Resource {
7677
ForceNew: true,
7778
Optional: true,
7879
},
80+
"share": {
81+
Type: schema.TypeString,
82+
Description: "Application sharing is an enterprise feature " +
83+
"and any values will be ignored (and sharing disabled) " +
84+
"if your deployment is not entitled to use application " +
85+
`sharing. Valid values are "owner", "template", ` +
86+
`"authenticated" and "public". Level "owner" disables ` +
87+
"sharing on the app, so only the workspace owner can " +
88+
`access it. Level "template" shares the app with all users ` +
89+
`that can read the workspace's template. Level ` +
90+
`"authenticated" shares the app with all authenticated ` +
91+
`users. Level "public" shares it with any user, ` +
92+
"including unauthenticated users. Permitted application " +
93+
`sharing levels can be configured via a flag on "coder ` +
94+
`server". Defaults to "owner" (sharing disabled).`,
95+
ForceNew: true,
96+
Optional: true,
97+
Default: "owner",
98+
ValidateDiagFunc: func(val interface{}, c cty.Path) diag.Diagnostics {
99+
valStr, ok := val.(string)
100+
if !ok {
101+
return diag.Errorf("expected string, got %T", val)
102+
}
103+
104+
switch valStr {
105+
case "owner", "template", "authenticated", "public":
106+
return nil
107+
}
108+
109+
return diag.Errorf(`invalid app share %q, must be one of "owner", "template", "authenticated", "public"`, valStr)
110+
},
111+
},
79112
"url": {
80113
Type: schema.TypeString,
81114
Description: "A URL to be proxied to from inside the workspace. " +

Diff for: provider/app_test.go

+142-29
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+
"share",
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 share "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("share = %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 share and ensure it matches the expected
155+
// value.
156+
value := resource.Primary.Attributes["share"]
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)