Skip to content

Commit e0bcc6a

Browse files
committed
feat: Add app support
This adds what are presently named "devurls" in v1. It seems this is a dated term, since this allows much more than accessing applications via URL. "coder open <name>" will launch any apps defined. If in the web, it'll open either a web terminal or port forward to the desired application. If in the terminal, it'll open the browser, or launch the command over SSH.
1 parent a336507 commit e0bcc6a

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed

docs/resources/app.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coder_app Resource - terraform-provider-coder"
4+
subcategory: ""
5+
description: |-
6+
Use this resource to define shortcuts to access applications in a workspace.
7+
---
8+
9+
# coder_app (Resource)
10+
11+
Use this resource to define shortcuts to access applications in a workspace.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "coder_workspace" "me" {}
17+
18+
resource "coder_agent" "dev" {
19+
os = "linux"
20+
arch = "amd64"
21+
dir = "/workspace"
22+
startup_script = <<EOF
23+
curl -fsSL https://code-server.dev/install.sh | sh
24+
code-server --auth none --port 13337
25+
EOF
26+
}
27+
28+
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+
target = "http://localhost:13337"
33+
}
34+
35+
resource "coder_app" "vim" {
36+
agent_id = coder_agent.dev.id
37+
name = "Vim"
38+
icon = data.coder_workspace.me.access_url + "/icons/vim.svg"
39+
command = "vim"
40+
}
41+
42+
resource "coder_app" "intellij" {
43+
agent_id = coder_agent.dev.id
44+
icon = data.coder_workspace.me.access_url + "/icons/intellij.svg"
45+
name = "JetBrains IntelliJ"
46+
command = "projector run"
47+
}
48+
```
49+
50+
<!-- schema generated by tfplugindocs -->
51+
## Schema
52+
53+
### Required
54+
55+
- `agent_id` (String) The "id" property of a "coder_agent" resource to associate with.
56+
57+
### Optional
58+
59+
- `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.
60+
- `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>"`.
61+
- `id` (String) The ID of this resource.
62+
- `name` (String) A display name to identify the app.
63+
- `target` (String) A URL to be proxied to from inside the workspace.
64+
65+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
data "coder_workspace" "me" {}
2+
3+
resource "coder_agent" "dev" {
4+
os = "linux"
5+
arch = "amd64"
6+
dir = "/workspace"
7+
startup_script = <<EOF
8+
curl -fsSL https://code-server.dev/install.sh | sh
9+
code-server --auth none --port 13337
10+
EOF
11+
}
12+
13+
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+
target = "http://localhost:13337"
18+
}
19+
20+
resource "coder_app" "vim" {
21+
agent_id = coder_agent.dev.id
22+
name = "Vim"
23+
icon = data.coder_workspace.me.access_url + "/icons/vim.svg"
24+
command = "vim"
25+
}
26+
27+
resource "coder_app" "intellij" {
28+
agent_id = coder_agent.dev.id
29+
icon = data.coder_workspace.me.access_url + "/icons/intellij.svg"
30+
name = "JetBrains IntelliJ"
31+
command = "projector run"
32+
}

internal/provider/provider.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ func New() *schema.Provider {
8989
id = uuid.NewString()
9090
}
9191
rd.SetId(id)
92+
config, valid := i.(config)
93+
if !valid {
94+
return diag.Errorf("config was unexpected type %q", reflect.TypeOf(i).String())
95+
}
96+
rd.Set("access_url", config.URL.String())
9297
return nil
9398
},
9499
Schema: map[string]*schema.Schema{
@@ -227,6 +232,63 @@ func New() *schema.Provider {
227232
},
228233
},
229234
},
235+
"coder_app": {
236+
Description: "Use this resource to define shortcuts to access applications in a workspace.",
237+
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
238+
resourceData.SetId(uuid.NewString())
239+
return nil
240+
},
241+
ReadContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
242+
return nil
243+
},
244+
DeleteContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
245+
return nil
246+
},
247+
Schema: map[string]*schema.Schema{
248+
"agent_id": {
249+
Type: schema.TypeString,
250+
Description: `The "id" property of a "coder_agent" resource to associate with.`,
251+
ForceNew: true,
252+
Required: true,
253+
},
254+
"name": {
255+
Type: schema.TypeString,
256+
Description: "A display name to identify the app.",
257+
ForceNew: true,
258+
Optional: true,
259+
},
260+
"icon": {
261+
Type: schema.TypeString,
262+
Description: "A URL to an icon that will display in the dashboard. View built-in " +
263+
"icons here: https://github.com/coder/coder/tree/main/site/static/icons. Use a " +
264+
"built-in icon with `data.coder_workspace.me.access_url + \"/icons/<path>\"`.",
265+
ForceNew: true,
266+
Optional: true,
267+
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
268+
_, err := url.Parse(s)
269+
if err != nil {
270+
return nil, []error{err}
271+
}
272+
return nil, nil
273+
},
274+
},
275+
"command": {
276+
Type: schema.TypeString,
277+
Description: "A command to run in a terminal opening this app. In the web, " +
278+
"this will open in a new tab. In the CLI, this will SSH and execute the command.",
279+
ConflictsWith: []string{"target"},
280+
Optional: true,
281+
ForceNew: true,
282+
},
283+
"target": {
284+
Type: schema.TypeString,
285+
Description: "A URL to be proxied to from inside the workspace.",
286+
ForceNew: true,
287+
Optional: true,
288+
ConflictsWith: []string{"command"},
289+
},
290+
},
291+
},
230292
},
231293
}
232294
}

internal/provider/provider_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,47 @@ func TestAgentInstance(t *testing.T) {
134134
}},
135135
})
136136
}
137+
138+
func TestApp(t *testing.T) {
139+
t.Parallel()
140+
resource.Test(t, resource.TestCase{
141+
Providers: map[string]*schema.Provider{
142+
"coder": provider.New(),
143+
},
144+
IsUnitTest: true,
145+
Steps: []resource.TestStep{{
146+
Config: `
147+
provider "coder" {
148+
}
149+
resource "coder_agent" "dev" {
150+
os = "linux"
151+
arch = "amd64"
152+
}
153+
resource "coder_app" "code-server" {
154+
agent_id = coder_agent.dev.id
155+
name = "code-server"
156+
icon = "builtin:vim"
157+
target = "http://localhost:13337"
158+
}
159+
`,
160+
Check: func(state *terraform.State) error {
161+
require.Len(t, state.Modules, 1)
162+
require.Len(t, state.Modules[0].Resources, 2)
163+
resource := state.Modules[0].Resources["coder_app.code-server"]
164+
require.NotNil(t, resource)
165+
for _, key := range []string{
166+
"agent_id",
167+
"name",
168+
"icon",
169+
"target",
170+
} {
171+
value := resource.Primary.Attributes[key]
172+
t.Logf("%q = %q", key, value)
173+
require.NotNil(t, value)
174+
require.Greater(t, len(value), 0)
175+
}
176+
return nil
177+
},
178+
}},
179+
})
180+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy