Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Add app support #17

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

Merged
merged 1 commit into from
May 6, 2022
Merged
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
65 changes: 65 additions & 0 deletions docs/resources/app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "coder_app Resource - terraform-provider-coder"
subcategory: ""
description: |-
Use this resource to define shortcuts to access applications in a workspace.
---

# coder_app (Resource)

Use this resource to define shortcuts to access applications in a workspace.

## Example Usage

```terraform
data "coder_workspace" "me" {}

resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
dir = "/workspace"
startup_script = <<EOF
curl -fsSL https://code-server.dev/install.sh | sh
code-server --auth none --port 13337
EOF
}

resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
name = "VS Code"
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
target = "http://localhost:13337"
}

resource "coder_app" "vim" {
agent_id = coder_agent.dev.id
name = "Vim"
icon = data.coder_workspace.me.access_url + "/icons/vim.svg"
command = "vim"
}

resource "coder_app" "intellij" {
agent_id = coder_agent.dev.id
icon = data.coder_workspace.me.access_url + "/icons/intellij.svg"
name = "JetBrains IntelliJ"
command = "projector run"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `agent_id` (String) The "id" property of a "coder_agent" resource to associate with.

### 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.
- `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>"`.
- `id` (String) The ID of this resource.
- `name` (String) A display name to identify the app.
- `target` (String) A URL to be proxied to from inside the workspace.


32 changes: 32 additions & 0 deletions examples/resources/coder_app/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
data "coder_workspace" "me" {}

resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
dir = "/workspace"
startup_script = <<EOF
curl -fsSL https://code-server.dev/install.sh | sh
code-server --auth none --port 13337
EOF
}

resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
name = "VS Code"
icon = data.coder_workspace.me.access_url + "/icons/vscode.svg"
target = "http://localhost:13337"
}

resource "coder_app" "vim" {
agent_id = coder_agent.dev.id
name = "Vim"
icon = data.coder_workspace.me.access_url + "/icons/vim.svg"
command = "vim"
}

resource "coder_app" "intellij" {
agent_id = coder_agent.dev.id
icon = data.coder_workspace.me.access_url + "/icons/intellij.svg"
name = "JetBrains IntelliJ"
command = "projector run"
}
69 changes: 69 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,19 @@ func New() *schema.Provider {
id = uuid.NewString()
}
rd.SetId(id)
config, valid := i.(config)
if !valid {
return diag.Errorf("config was unexpected type %q", reflect.TypeOf(i).String())
}
rd.Set("access_url", config.URL.String())
return nil
},
Schema: map[string]*schema.Schema{
"access_url": {
Type: schema.TypeString,
Computed: true,
Description: "The access URL of the Coder deployment provisioning this workspace.",
},
"start_count": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -227,6 +237,65 @@ func New() *schema.Provider {
},
},
},
"coder_app": {
Description: "Use this resource to define shortcuts to access applications in a workspace.",
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
resourceData.SetId(uuid.NewString())
return nil
},
ReadContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
return nil
},
DeleteContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
return nil
},
Schema: map[string]*schema.Schema{
"agent_id": {
Type: schema.TypeString,
Description: `The "id" property of a "coder_agent" resource to associate with.`,
ForceNew: true,
Required: true,
},
"command": {
Type: schema.TypeString,
Description: "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 \"target\" may be specified, but not both.",
ConflictsWith: []string{"target"},
Optional: true,
ForceNew: true,
},
"icon": {
Type: schema.TypeString,
Description: "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>\"`.",
ForceNew: true,
Optional: true,
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
_, err := url.Parse(s)
if err != nil {
return nil, []error{err}
}
return nil, nil
},
},
"name": {
Type: schema.TypeString,
Description: "A display name to identify the app.",
ForceNew: true,
Optional: true,
},
"target": {
Type: schema.TypeString,
Description: "A URL to be proxied to from inside the workspace. " +
"Either \"command\" or \"target\" may be specified, but not both.",
ForceNew: true,
Optional: true,
ConflictsWith: []string{"command"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't seem to document the ConflictsWith requirements at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I shall fix!

},
},
},
},
}
}
Expand Down
44 changes: 44 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,47 @@ func TestAgentInstance(t *testing.T) {
}},
})
}

func TestApp(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
}
resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
name = "code-server"
icon = "builtin:vim"
target = "http://localhost:13337"
}
`,
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.code-server"]
require.NotNil(t, resource)
for _, key := range []string{
"agent_id",
"name",
"icon",
"target",
} {
value := resource.Primary.Attributes[key]
t.Logf("%q = %q", key, value)
require.NotNil(t, value)
require.Greater(t, len(value), 0)
}
return nil
},
}},
})
}