Closes CODAGT-292 Relates to CODAGT-1607 `coderd/export_test.go` re-exported `chatCreateWorkspace`, `chatStartWorkspace`, `chatStopWorkspace` and `chatRenderTemplateParameters`, just so four tests could call them directly. This PR removes those tests and their aliases, and the last alias in that file goes in the next PR in the stack. The start and stop tests only checked which template version the build ends up on. That decision lived in `coderd`, behind the function the tool calls, so the only way to test it was to call that function directly. Now `start_workspace` makes the decision itself, much like `coder start` does: it checks whether the template requires its active version, sends that version with the build request, and tells the model how to retry when the update needs parameters. All of that is covered by the tool's own tests. With the decision gone, the start and stop functions were identical, so both tools now share `chatBuildWorkspace`, which just creates a build as the workspace owner. Two small behaviour changes. `start_workspace` now returns `template_id` whenever it asks the model to retry with parameters. Previously it was missing for templates on the classic parameter flow, even though the message says to use it. The tool also reads the template's setting directly, like `coder start`, so chat updates on start too when the setting is still on after a licence lapses. `TestStartWorkspaceTool_EndToEnd` now starts a workspace whose active version needs a new parameter, so we still know the whole flow works against a real `coderd`. `chatRenderTemplateParameters` keeps a direct test, it's just an internal one now, against a database instead of a whole `coderd`. It no longer checks the owner default, as that's already tested in the renderer and in the tool. The bind failure test needs a real transaction, so it moves to `chatd` and runs through the `create_workspace` tool.
144 lines
3.8 KiB
Go
144 lines
3.8 KiB
Go
package dynamicparameters
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// ErrParameterValidation is a sentinel indicating that a workspace
|
|
// build failed because a template-version parameter could not be
|
|
// validated (missing required value, immutable change, etc.).
|
|
var ErrParameterValidation = xerrors.New("parameter validation failed")
|
|
|
|
func parameterValidationError(diags hcl.Diagnostics) *DiagnosticError {
|
|
return &DiagnosticError{
|
|
Message: "Unable to validate parameters",
|
|
Diagnostics: diags,
|
|
KeyedDiagnostics: make(map[string]hcl.Diagnostics),
|
|
}
|
|
}
|
|
|
|
func tagValidationError(diags hcl.Diagnostics) *DiagnosticError {
|
|
return &DiagnosticError{
|
|
Message: "Unable to parse workspace tags",
|
|
Diagnostics: diags,
|
|
KeyedDiagnostics: make(map[string]hcl.Diagnostics),
|
|
}
|
|
}
|
|
|
|
func presetValidationError(diags hcl.Diagnostics) *DiagnosticError {
|
|
return &DiagnosticError{
|
|
Message: "Unable to validate presets",
|
|
Diagnostics: diags,
|
|
KeyedDiagnostics: make(map[string]hcl.Diagnostics),
|
|
}
|
|
}
|
|
|
|
type DiagnosticError struct {
|
|
// Message is the human-readable message that will be returned to the user.
|
|
Message string
|
|
// Diagnostics are top level diagnostics that will be returned as "Detail" in the response.
|
|
Diagnostics hcl.Diagnostics
|
|
// KeyedDiagnostics translate to Validation errors in the response. A key could
|
|
// be a parameter name, or a tag name. This allows diagnostics to be more closely
|
|
// associated with a specific index/parameter/tag.
|
|
KeyedDiagnostics map[string]hcl.Diagnostics
|
|
}
|
|
|
|
// Error is a pretty bad format for these errors. Try to avoid using this.
|
|
func (e *DiagnosticError) Error() string {
|
|
var diags hcl.Diagnostics
|
|
diags = diags.Extend(e.Diagnostics)
|
|
for _, d := range e.KeyedDiagnostics {
|
|
diags = diags.Extend(d)
|
|
}
|
|
|
|
return diags.Error()
|
|
}
|
|
|
|
func (e *DiagnosticError) HasError() bool {
|
|
if e.Diagnostics.HasErrors() {
|
|
return true
|
|
}
|
|
|
|
for _, diags := range e.KeyedDiagnostics {
|
|
if diags.HasErrors() {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (e *DiagnosticError) Append(key string, diag *hcl.Diagnostic) {
|
|
e.Extend(key, hcl.Diagnostics{diag})
|
|
}
|
|
|
|
func (e *DiagnosticError) Extend(key string, diag hcl.Diagnostics) {
|
|
if e.KeyedDiagnostics == nil {
|
|
e.KeyedDiagnostics = make(map[string]hcl.Diagnostics)
|
|
}
|
|
if _, ok := e.KeyedDiagnostics[key]; !ok {
|
|
e.KeyedDiagnostics[key] = hcl.Diagnostics{}
|
|
}
|
|
e.KeyedDiagnostics[key] = e.KeyedDiagnostics[key].Extend(diag)
|
|
}
|
|
|
|
func (e *DiagnosticError) Response() (int, codersdk.Response) {
|
|
resp := codersdk.Response{
|
|
Message: e.Message,
|
|
Validations: nil,
|
|
}
|
|
|
|
// Sort the parameter names so that the order is consistent.
|
|
sortedNames := make([]string, 0, len(e.KeyedDiagnostics))
|
|
for name := range e.KeyedDiagnostics {
|
|
sortedNames = append(sortedNames, name)
|
|
}
|
|
slices.Sort(sortedNames)
|
|
|
|
for _, name := range sortedNames {
|
|
diag := e.KeyedDiagnostics[name]
|
|
resp.Validations = append(resp.Validations, codersdk.ValidationError{
|
|
Field: name,
|
|
Detail: DiagnosticsErrorString(diag),
|
|
})
|
|
}
|
|
|
|
if e.Diagnostics.HasErrors() {
|
|
resp.Detail = DiagnosticsErrorString(e.Diagnostics)
|
|
}
|
|
|
|
return http.StatusBadRequest, resp
|
|
}
|
|
|
|
func DiagnosticErrorString(d *hcl.Diagnostic) string {
|
|
return fmt.Sprintf("%s; %s", d.Summary, d.Detail)
|
|
}
|
|
|
|
func DiagnosticsErrorString(d hcl.Diagnostics) string {
|
|
count := len(d)
|
|
switch count {
|
|
case 0:
|
|
return "no diagnostics"
|
|
case 1:
|
|
return DiagnosticErrorString(d[0])
|
|
default:
|
|
for _, d := range d {
|
|
// Render the first error diag.
|
|
// If there are warnings, do not priority them over errors.
|
|
if d.Severity == hcl.DiagError {
|
|
return fmt.Sprintf("%s, and %d other diagnostic(s)", DiagnosticErrorString(d), count-1)
|
|
}
|
|
}
|
|
|
|
// All warnings? ok...
|
|
return fmt.Sprintf("%s, and %d other diagnostic(s)", DiagnosticErrorString(d[0]), count-1)
|
|
}
|
|
}
|