Skip to content

feat: add default_apps field to coder_agent resource #147

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 10 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 8 additions & 2 deletions docs/resources/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
dir = "/workspace"
display_apps {
vscode = true
vscode_insiders = false
web_terminal = true
ssh_helper = false
}
}

resource "kubernetes_pod" "dev" {
Expand Down Expand Up @@ -72,8 +78,8 @@ resource "kubernetes_pod" "dev" {

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.
- `port_forwarding_helper` (Boolean) Display the port-forwarding helper button in the agent bar.
- `ssh_helper` (Boolean) Display the SSH 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.
Expand Down
6 changes: 6 additions & 0 deletions examples/resources/coder_agent/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
dir = "/workspace"
display_apps {
vscode = true
vscode_insiders = false
web_terminal = true
ssh_helper = false
}
}

resource "kubernetes_pod" "dev" {
Expand Down
5 changes: 2 additions & 3 deletions provider/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func agentResource() *schema.Resource {
return diag.FromErr(err)
}
}

return updateInitScript(resourceData, i)
},
ReadWithoutTimeout: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -261,14 +260,14 @@ func agentResource() *schema.Resource {
},
"port_forwarding_helper": {
Type: schema.TypeBool,
Description: "Display port-forwarding helper button in the agent bar.",
Description: "Display the 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.",
Description: "Display the SSH helper button in the agent bar.",
ForceNew: true,
Optional: true,
Default: true,
Expand Down
80 changes: 78 additions & 2 deletions provider/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"regexp"
"testing"

"github.com/coder/terraform-provider-coder/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/require"

"github.com/coder/terraform-provider-coder/provider"
)

func TestAgent(t *testing.T) {
Expand Down Expand Up @@ -249,7 +250,7 @@ func TestAgent_Metadata(t *testing.T) {
})
}

func TestAgent_DefaultApps(t *testing.T) {
func TestAgent_DisplayApps(t *testing.T) {
t.Parallel()
t.Run("OK", func(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -302,7 +303,52 @@ func TestAgent_DefaultApps(t *testing.T) {
},
}},
})
})

t.Run("Subset", 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: `
provider "coder" {
url = "https://example.com"
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
display_apps {
vscode_insiders = true
web_terminal = true
}
}
`,
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)
require.Equal(t, "true", resource.Primary.Attributes[key])
}
return nil
},
}},
})
})

// Assert all the defaults are set correctly.
Expand Down Expand Up @@ -350,4 +396,34 @@ func TestAgent_DefaultApps(t *testing.T) {
}},
})
})

t.Run("InvalidApp", 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: `
provider "coder" {
url = "https://example.com"
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
display_apps {
fake_app = false
vscode_insiders = true
web_terminal = false
port_forwarding_helper = false
ssh_helper = false
}
}
`,
ExpectError: regexp.MustCompile(`An argument named "fake_app" is not expected here.`),
}},
})
})

}
15 changes: 14 additions & 1 deletion provider/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@ import (
"os"
"testing"

"github.com/coder/terraform-provider-coder/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/require"

"github.com/coder/terraform-provider-coder/provider"
)

func TestExamples(t *testing.T) {
t.Parallel()

t.Run("coder_agent", func(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: mustReadFile(t, "../examples/resources/coder_parameter/resource.tf"),
}},
})
})

t.Run("coder_parameter", func(t *testing.T) {
t.Parallel()

Expand Down