mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
f9a6adc704
Closes https://github.com/coder/coder/issues/18356. This change finds and selects a matching preset if one was not chosen during workspace creation. This solidifies the relationship between presets and parameters. When a workspace is created without in explicitly chosen preset, it will now still be eligible to claim a prebuilt workspace if one is available.
43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package prebuilds
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
)
|
|
|
|
// FindMatchingPresetID finds a preset ID that matches the provided parameters.
|
|
// It returns the preset ID if a match is found, or uuid.Nil if no match is found.
|
|
// The function performs a bidirectional comparison to ensure all parameters match exactly.
|
|
func FindMatchingPresetID(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
templateVersionID uuid.UUID,
|
|
parameterNames []string,
|
|
parameterValues []string,
|
|
) (uuid.UUID, error) {
|
|
if len(parameterNames) != len(parameterValues) {
|
|
return uuid.Nil, xerrors.New("parameter names and values must have the same length")
|
|
}
|
|
|
|
result, err := store.FindMatchingPresetID(ctx, database.FindMatchingPresetIDParams{
|
|
TemplateVersionID: templateVersionID,
|
|
ParameterNames: parameterNames,
|
|
ParameterValues: parameterValues,
|
|
})
|
|
if err != nil {
|
|
// Handle the case where no matching preset is found (no rows returned)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return uuid.Nil, nil
|
|
}
|
|
return uuid.Nil, xerrors.Errorf("find matching preset ID: %w", err)
|
|
}
|
|
|
|
return result, nil
|
|
}
|