Skip to content

fix!: add extra validation for start_blocks_login #192

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
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
4 changes: 4 additions & 0 deletions provider/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ func scriptResource() *schema.Resource {
CreateContext: func(_ context.Context, rd *schema.ResourceData, _ interface{}) diag.Diagnostics {
rd.SetId(uuid.NewString())
runOnStart, _ := rd.Get("run_on_start").(bool)
startBlocksLogin, _ := rd.Get("start_blocks_login").(bool)
runOnStop, _ := rd.Get("run_on_stop").(bool)
cron, _ := rd.Get("cron").(string)

if !runOnStart && !runOnStop && cron == "" {
return diag.Errorf("at least one of run_on_start, run_on_stop, or cron must be set")
}
if !runOnStart && startBlocksLogin {
return diag.Errorf("start_blocks_login can only be set if run_on_start is true")
}
return nil
},
ReadContext: schema.NoopContext,
Expand Down
61 changes: 61 additions & 0 deletions provider/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,64 @@ func TestScriptNeverRuns(t *testing.T) {
}},
})
}

func TestScriptStartBlocksLoginRequiresRunOnStart(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_script" "example" {
agent_id = ""
display_name = "Hey"
script = "Wow"
run_on_stop = true
start_blocks_login = true
}
`,
ExpectError: regexp.MustCompile(`start_blocks_login can only be set if run_on_start is true`),
}},
})
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
}
resource "coder_script" "example" {
agent_id = ""
display_name = "Hey"
script = "Wow"
start_blocks_login = true
run_on_start = true
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)
script := state.Modules[0].Resources["coder_script.example"]
require.NotNil(t, script)
t.Logf("script attributes: %#v", script.Primary.Attributes)
for key, expected := range map[string]string{
"agent_id": "",
"display_name": "Hey",
"script": "Wow",
"start_blocks_login": "true",
"run_on_start": "true",
} {
require.Equal(t, expected, script.Primary.Attributes[key])
}
return nil
},
}},
})
}