- Notifications
You must be signed in to change notification settings - Fork22
feat: adddefault_apps
field tocoder_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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
f14f186
6354863
c526179
41f4ebd
ce6fd70
018a016
2486658
3d29895
6daeca7
aafdee4
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -23,13 +23,43 @@ 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 resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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 { | ||
@@ -198,6 +228,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 the port-forwarding helper button in the agent bar.", | ||
ForceNew: true, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
"ssh_helper": { | ||
Type: schema.TypeBool, | ||
Description: "Display the SSH helper button in the agent bar.", | ||
ForceNew: true, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package provider_test | ||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
"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) { | ||
@@ -247,3 +249,181 @@ func TestAgent_Metadata(t *testing.T) { | ||
}}, | ||
}) | ||
} | ||
func TestAgent_DisplayApps(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 resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
provider "coder" { | ||
url = "https://example.com" | ||
} | ||
resource "coder_agent" "dev" { | ||
os = "linux" | ||
arch = "amd64" | ||
display_apps { | ||
vscode = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
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 | ||
}, | ||
}}, | ||
}) | ||
}) | ||
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. | ||
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 | ||
}, | ||
}}, | ||
}) | ||
}) | ||
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.`), | ||
}}, | ||
}) | ||
}) | ||
} |