Skip to content
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

feat: add default_apps field to coder_agent resource #147

Merged
merged 10 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ resource "kubernetes_pod" "dev" {
- `owner_email` (String) Email address of the workspace owner.
- `owner_id` (String) UUID of the workspace owner.
- `owner_oidc_access_token` (String) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string.
- `owner_session_token` (String) Session token for interfacing with a Coder deployment. It is regenerated everytime a workspace is started.
- `owner_session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.
- `start_count` (Number) A computed count based on "transition" state. If "start", count will equal 1.
- `transition` (String) Either "start" or "stop". Use this to start/stop resources with "count".
13 changes: 13 additions & 0 deletions docs/resources/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ resource "kubernetes_pod" "dev" {
- `auth` (String) The authentication type the agent will use. Must be one of: "token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity".
- `connection_timeout` (Number) Time in seconds until the agent is marked as timed out when a connection with the server cannot be established. A value of zero never marks the agent as timed out.
- `dir` (String) The starting directory when a user creates a shell session. Defaults to $HOME.
- `display_apps` (Block Set, Max: 1) The list of built-in apps to display in the agent bar. (see [below for nested schema](#nestedblock--display_apps))
- `env` (Map of String) A mapping of environment variables to set inside the workspace.
- `login_before_ready` (Boolean, Deprecated) This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.
- `metadata` (Block List) Each "metadata" block defines a single item consisting of a key/value pair. This feature is in alpha and may break in future releases. (see [below for nested schema](#nestedblock--metadata))
Expand All @@ -66,6 +67,18 @@ resource "kubernetes_pod" "dev" {
- `init_script` (String) Run this script on startup of an instance to initialize the agent.
- `token` (String, Sensitive) Set the environment variable "CODER_AGENT_TOKEN" with this token to authenticate an agent.

<a id="nestedblock--display_apps"></a>
### Nested Schema for `display_apps`

Optional:

- `port_forwarding_helper` (Boolean) Display port-forwarding helper button in the agent bar.
- `ssh_helper` (Boolean) Display port-forwarding helper button in the agent bar.
- `vscode` (Boolean) Display the VSCode Desktop app in the agent bar.
- `vscode_insiders` (Boolean) Display the VSCode Insiders app in the agent bar.
- `web_terminal` (Boolean) Display the web terminal app in the agent bar.


<a id="nestedblock--metadata"></a>
### Nested Schema for `metadata`

Expand Down
78 changes: 78 additions & 0 deletions provider/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,44 @@ func agentResource() *schema.Resource {
if err != nil {
return diag.FromErr(err)
}

if _, ok := resourceData.GetOk("display_apps"); !ok {
mtojek marked this conversation as resolved.
Show resolved Hide resolved
err = resourceData.Set("display_apps", []interface{}{
map[string]bool{
"vscode": true,
"vscode_insiders": false,
"web_terminal": true,
"ssh_helper": true,
"port_forwarding_helper": true,
},
})
if err != nil {
return diag.FromErr(err)
}
}

return updateInitScript(resourceData, i)
},
ReadWithoutTimeout: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
err := resourceData.Set("token", uuid.NewString())
if err != nil {
return diag.FromErr(err)
}
if _, ok := resourceData.GetOk("display_apps"); !ok {
err = resourceData.Set("display_apps", []interface{}{
map[string]bool{
"vscode": true,
"vscode_insiders": false,
"web_terminal": true,
"ssh_helper": true,
"port_forwarding_helper": true,
},
})
if err != nil {
return diag.FromErr(err)
}
}

return updateInitScript(resourceData, i)
},
DeleteContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -198,6 +229,53 @@ func agentResource() *schema.Resource {
},
},
},
"display_apps": {
Type: schema.TypeSet,
Description: "The list of built-in apps to display in the agent bar.",
ForceNew: true,
Optional: true,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"vscode": {
Type: schema.TypeBool,
Description: "Display the VSCode Desktop app in the agent bar.",
ForceNew: true,
Optional: true,
Default: true,
},
"vscode_insiders": {
Type: schema.TypeBool,
Description: "Display the VSCode Insiders app in the agent bar.",
ForceNew: true,
Optional: true,
Default: false,
},
"web_terminal": {
Type: schema.TypeBool,
Description: "Display the web terminal app in the agent bar.",
ForceNew: true,
Optional: true,
Default: true,
},
"port_forwarding_helper": {
Type: schema.TypeBool,
Description: "Display port-forwarding helper button in the agent bar.",
ForceNew: true,
Optional: true,
Default: true,
},
"ssh_helper": {
Type: schema.TypeBool,
Description: "Display port-forwarding helper button in the agent bar.",
ForceNew: true,
Optional: true,
Default: true,
},
},
},
},
},
}
}
Expand Down
104 changes: 104 additions & 0 deletions provider/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider_test

import (
"fmt"
"regexp"
"testing"

Expand Down Expand Up @@ -247,3 +248,106 @@ func TestAgent_Metadata(t *testing.T) {
}},
})
}

func TestAgent_DefaultApps(t *testing.T) {
t.Parallel()
t.Run("OK", func(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
// Test the fields with non-default values.
Config: `
sreya marked this conversation as resolved.
Show resolved Hide resolved
provider "coder" {
url = "https://example.com"
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
display_apps {
vscode = false
Copy link
Member

Choose a reason for hiding this comment

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

Do we need an extra test case to check .tf with only a few apps selected:

display_apps {
   vscode = true
   web_terminal = true

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since they all have default values I tested that specifying their non-defaults (in OK) and then omitting them to produce the defaults (in Omitted) produces the desired result. I added another one anyway but I don't think it adds much value.

vscode_insiders = true
web_terminal = false
port_forwarding_helper = false
ssh_helper = false
}
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)

resource := state.Modules[0].Resources["coder_agent.dev"]
require.NotNil(t, resource)

t.Logf("resource: %v", resource.Primary.Attributes)

for _, app := range []string{
"web_terminal",
"vscode_insiders",
"vscode",
"port_forwarding_helper",
"ssh_helper",
} {
key := fmt.Sprintf("display_apps.0.%s", app)
if app == "vscode_insiders" {
require.Equal(t, "true", resource.Primary.Attributes[key])
} else {
require.Equal(t, "false", resource.Primary.Attributes[key])
}
}
return nil
},
}},
})

})

// Assert all the defaults are set correctly.
t.Run("Omitted", func(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
url = "https://example.com"
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)

resource := state.Modules[0].Resources["coder_agent.dev"]
require.NotNil(t, resource)

t.Logf("resource: %v", resource.Primary.Attributes)

for _, app := range []string{
"web_terminal",
"vscode_insiders",
"vscode",
"port_forwarding_helper",
"ssh_helper",
} {
key := fmt.Sprintf("display_apps.0.%s", app)
if app == "vscode_insiders" {
require.Equal(t, "false", resource.Primary.Attributes[key])
} else {
require.Equal(t, "true", resource.Primary.Attributes[key])
}
}
return nil
},
}},
})
})
}
2 changes: 1 addition & 1 deletion provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func workspaceDataSource() *schema.Resource {
"owner_session_token": {
Type: schema.TypeString,
Computed: true,
Description: "Session token for interfacing with a Coder deployment. It is regenerated everytime a workspace is started.",
Description: "Session token for authenticating with a Coder deployment. It is regenerated everytime a workspace is started.",
},
},
}
Expand Down
Loading