mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add GET /api/v2/templatebuilder/modules endpoint (#26117)
Implement `GET /api/v2/templatebuilder/modules`, which returns the filtered list of modules available for a given base template. Reads from the bundled catalog via `LoadModules()` and applies OS-compatibility filtering based on the `base` query param. Computed variables (e.g. `agent_id`) are excluded from the API response at the `ToSDK()` conversion boundary since they are wired automatically by the builder. The `Computed` field is removed from the SDK type. Adds `CompatibleWithOS()` to `ModuleManifest` for OS filtering. Returns 400 for unknown base IDs and 404 when the template builder is disabled. Depends on #26116 > [!NOTE] > This PR was authored by Coder Agents on behalf of @jeremyruppel.
This commit is contained in:
@@ -137,11 +137,31 @@ func parseModulesFromFS(fsys fs.FS) ([]ModuleManifest, error) {
|
||||
return modules, nil
|
||||
}
|
||||
|
||||
// CompatibleWithOS reports whether the module is compatible with the given OS.
|
||||
// Modules with an empty CompatibleOS list are compatible with all platforms.
|
||||
func (m ModuleManifest) CompatibleWithOS(os string) bool {
|
||||
if len(m.CompatibleOS) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, supported := range m.CompatibleOS {
|
||||
if supported == os {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ToSDK converts a ModuleManifest to the API response type.
|
||||
// PinnedVersion is mapped to Version; tags are not part of the API surface.
|
||||
// Computed variables are excluded from the output.
|
||||
func (m ModuleManifest) ToSDK() codersdk.TemplateBuilderModule {
|
||||
variables := make([]codersdk.TemplateBuilderModuleVariable, 0, len(m.Variables))
|
||||
for _, v := range m.Variables {
|
||||
// Computed variables (e.g. agent_id) are wired by the builder
|
||||
// automatically and must not be surfaced to the user.
|
||||
if v.Computed {
|
||||
continue
|
||||
}
|
||||
variables = append(variables, codersdk.TemplateBuilderModuleVariable{
|
||||
Name: v.Name,
|
||||
Type: validVariableTypes[v.Type],
|
||||
@@ -149,7 +169,6 @@ func (m ModuleManifest) ToSDK() codersdk.TemplateBuilderModule {
|
||||
Default: v.Default,
|
||||
Required: v.Required,
|
||||
Sensitive: v.Sensitive,
|
||||
Computed: v.Computed,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -93,18 +93,10 @@ func TestToSDK(t *testing.T) {
|
||||
t.Run("AllVariableFields", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.Len(t, sdk.Variables, 3)
|
||||
// Computed variables (agent_id) are excluded from SDK output.
|
||||
require.Len(t, sdk.Variables, 2)
|
||||
|
||||
agent := sdk.Variables[0]
|
||||
require.Equal(t, "agent_id", agent.Name)
|
||||
require.Equal(t, codersdk.TemplateBuilderVariableTypeString, agent.Type)
|
||||
require.Equal(t, "The Coder agent ID.", agent.Description)
|
||||
require.Nil(t, agent.Default)
|
||||
require.True(t, agent.Required)
|
||||
require.False(t, agent.Sensitive)
|
||||
require.True(t, agent.Computed)
|
||||
|
||||
port := sdk.Variables[1]
|
||||
port := sdk.Variables[0]
|
||||
require.Equal(t, "port", port.Name)
|
||||
require.Equal(t, codersdk.TemplateBuilderVariableTypeNumber, port.Type)
|
||||
require.Equal(t, "Port to listen on.", port.Description)
|
||||
@@ -112,16 +104,14 @@ func TestToSDK(t *testing.T) {
|
||||
require.JSONEq(t, "8080", string(port.Default))
|
||||
require.False(t, port.Required)
|
||||
require.False(t, port.Sensitive)
|
||||
require.False(t, port.Computed)
|
||||
|
||||
secret := sdk.Variables[2]
|
||||
secret := sdk.Variables[1]
|
||||
require.Equal(t, "secret_key", secret.Name)
|
||||
require.Equal(t, codersdk.TemplateBuilderVariableTypeString, secret.Type)
|
||||
require.Equal(t, "A sensitive value.", secret.Description)
|
||||
require.Nil(t, secret.Default)
|
||||
require.True(t, secret.Required)
|
||||
require.True(t, secret.Sensitive)
|
||||
require.False(t, secret.Computed)
|
||||
})
|
||||
|
||||
t.Run("NilSlicesNormalizedToEmpty", func(t *testing.T) {
|
||||
@@ -141,3 +131,32 @@ func TestToSDK(t *testing.T) {
|
||||
require.Empty(t, s.Variables)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCompatibleWithOS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
compatibleOS []string
|
||||
os string
|
||||
want bool
|
||||
}{
|
||||
{"EmptyListMatchesAll", nil, "linux", true},
|
||||
{"ExactMatch", []string{"linux"}, "linux", true},
|
||||
{"MultipleMatch", []string{"linux", "windows"}, "windows", true},
|
||||
{"NoMatch", []string{"linux"}, "windows", false},
|
||||
{"CaseSensitive", []string{"linux"}, "Linux", false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
m := templatebuilder.ModuleManifest{
|
||||
ID: "test",
|
||||
PinnedVersion: "1.0.0",
|
||||
CompatibleOS: tc.compatibleOS,
|
||||
}
|
||||
require.Equal(t, tc.want, m.CompatibleWithOS(tc.os))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user