diff --git a/coderd/templatebuilder/bases/gcp-linux/base.json b/coderd/templatebuilder/bases/gcp-linux/base.json index d03489c572dcf..02ad23ce0bc84 100644 --- a/coderd/templatebuilder/bases/gcp-linux/base.json +++ b/coderd/templatebuilder/bases/gcp-linux/base.json @@ -2,5 +2,15 @@ "id": "gcp-linux", "display_name": "Google Compute Engine (Linux)", "os": "linux", - "default_context": {} + "default_context": {}, + "variables": [ + { + "name": "project_id", + "type": "string", + "description": "Which Google Compute Project should your workspace live in?", + "required": true, + "sensitive": false, + "computed": false + } + ] } diff --git a/coderd/templatebuilder/bases/gcp-linux/main.tf.tmpl b/coderd/templatebuilder/bases/gcp-linux/main.tf.tmpl index acb8e8fa48e37..bb6e3211c8c50 100644 --- a/coderd/templatebuilder/bases/gcp-linux/main.tf.tmpl +++ b/coderd/templatebuilder/bases/gcp-linux/main.tf.tmpl @@ -13,6 +13,7 @@ provider "coder" {} variable "project_id" { description = "Which Google Compute Project should your workspace live in?" + default = {{ .Variables.project_id }} } # See https://registry.coder.com/modules/coder/gcp-region diff --git a/coderd/templatebuilder/bases/gcp-windows/base.json b/coderd/templatebuilder/bases/gcp-windows/base.json index 98f8106f0ceda..c9bbb73fba44c 100644 --- a/coderd/templatebuilder/bases/gcp-windows/base.json +++ b/coderd/templatebuilder/bases/gcp-windows/base.json @@ -2,5 +2,15 @@ "id": "gcp-windows", "display_name": "Google Compute Engine (Windows)", "os": "windows", - "default_context": {} + "default_context": {}, + "variables": [ + { + "name": "project_id", + "type": "string", + "description": "Which Google Compute Project should your workspace live in?", + "required": true, + "sensitive": false, + "computed": false + } + ] } diff --git a/coderd/templatebuilder/bases/gcp-windows/main.tf.tmpl b/coderd/templatebuilder/bases/gcp-windows/main.tf.tmpl index aea409eee7ac8..45cc09f24093b 100644 --- a/coderd/templatebuilder/bases/gcp-windows/main.tf.tmpl +++ b/coderd/templatebuilder/bases/gcp-windows/main.tf.tmpl @@ -13,6 +13,7 @@ provider "coder" {} variable "project_id" { description = "Which Google Compute Project should your workspace live in?" + default = {{ .Variables.project_id }} } # See https://registry.coder.com/modules/coder/gcp-region diff --git a/coderd/templatebuilder/compose_test.go b/coderd/templatebuilder/compose_test.go index b15692036fd2e..276741c81689b 100644 --- a/coderd/templatebuilder/compose_test.go +++ b/coderd/templatebuilder/compose_test.go @@ -78,36 +78,44 @@ func TestCompose(t *testing.T) { require.Contains(t, result.ExtraFiles, "cloud-init/userdata.sh.tftpl") }) - t.Run("GCPWindowsBase", func(t *testing.T) { + t.Run("GCPLinuxBaseWithProjectID", func(t *testing.T) { t.Parallel() result, err := templatebuilder.Compose(templatebuilder.ComposeRequest{ - BaseTemplateID: "gcp-windows", - RegistryURL: "https://registry.coder.com", + BaseTemplateID: "gcp-linux", + BaseVariableValues: map[string]string{"project_id": "my-gcp-project"}, + RegistryURL: "https://registry.coder.com", }) require.NoError(t, err) require.NotEmpty(t, result.MainTF) - require.Contains(t, string(result.MainTF), `resource "coder_agent" "main"`) + mainTF := string(result.MainTF) + require.Contains(t, mainTF, `resource "coder_agent" "main"`) + require.Contains(t, mainTF, `default = "my-gcp-project"`) + require.Contains(t, mainTF, `project = var.project_id`) }) - t.Run("AzureLinuxExtraFiles", func(t *testing.T) { + t.Run("GCPWindowsBase", func(t *testing.T) { t.Parallel() result, err := templatebuilder.Compose(templatebuilder.ComposeRequest{ - BaseTemplateID: "azure-linux", - RegistryURL: "https://registry.coder.com", + BaseTemplateID: "gcp-windows", + BaseVariableValues: map[string]string{"project_id": "my-gcp-project"}, + RegistryURL: "https://registry.coder.com", }) require.NoError(t, err) - require.Contains(t, result.ExtraFiles, "cloud-init/cloud-config.yaml.tftpl") + require.NotEmpty(t, result.MainTF) + mainTF := string(result.MainTF) + require.Contains(t, mainTF, `resource "coder_agent" "main"`) + require.Contains(t, mainTF, `default = "my-gcp-project"`) + require.Contains(t, mainTF, `project = var.project_id`) }) - t.Run("GCPWindowsBase", func(t *testing.T) { + t.Run("GCPMissingRequiredBaseVariable", func(t *testing.T) { t.Parallel() - result, err := templatebuilder.Compose(templatebuilder.ComposeRequest{ - BaseTemplateID: "gcp-windows", + _, err := templatebuilder.Compose(templatebuilder.ComposeRequest{ + BaseTemplateID: "gcp-linux", RegistryURL: "https://registry.coder.com", }) - require.NoError(t, err) - require.NotEmpty(t, result.MainTF) - require.Contains(t, string(result.MainTF), `resource "coder_agent" "main"`) + require.Error(t, err) + require.Contains(t, err.Error(), `variable "project_id" is required`) }) t.Run("AzureLinuxExtraFiles", func(t *testing.T) { diff --git a/coderd/templatebuilder/render_test.go b/coderd/templatebuilder/render_test.go index fd75eb1b1d689..12927737e2d6f 100644 --- a/coderd/templatebuilder/render_test.go +++ b/coderd/templatebuilder/render_test.go @@ -2,6 +2,7 @@ package templatebuilder_test import ( "flag" + "fmt" "os" "path/filepath" "testing" @@ -14,6 +15,26 @@ import ( var updateGolden = flag.Bool("update", false, "update golden files") +// testRenderContext builds a BaseRenderContext from defaults and fills +// in deterministic test values for any required variables that have no +// default. This keeps the all-bases test loops working without silently +// producing broken HCL. +func testRenderContext(exampleID string) templatebuilder.BaseRenderContext { + rc := templatebuilder.DefaultBaseRenderContext(exampleID) + if rc.Variables == nil { + rc.Variables = make(map[string]string) + } + for _, v := range templatebuilder.BaseVariables(exampleID) { + if v.Computed || v.Sensitive { + continue + } + if _, ok := rc.Variables[v.Name]; !ok { + rc.Variables[v.Name] = fmt.Sprintf("%q", "test-"+v.Name) + } + } + return rc +} + func TestRenderBaseTemplate(t *testing.T) { t.Parallel() @@ -297,7 +318,7 @@ func TestAllBasesRenderAndExtractAgent(t *testing.T) { for _, id := range templatebuilder.BaseTemplateIDs() { t.Run(id, func(t *testing.T) { t.Parallel() - renderCtx := templatebuilder.DefaultBaseRenderContext(id) + renderCtx := testRenderContext(id) rendered, err := templatebuilder.RenderBaseTemplate(id, "main.tf.tmpl", renderCtx) require.NoError(t, err, "base %q should render without error", id) require.NotEmpty(t, rendered) @@ -330,7 +351,7 @@ func TestBaseTemplateSnapshot(t *testing.T) { t.Run(tc.exampleID, func(t *testing.T) { t.Parallel() - renderCtx := templatebuilder.DefaultBaseRenderContext(tc.exampleID) + renderCtx := testRenderContext(tc.exampleID) rendered, err := templatebuilder.RenderBaseTemplate(tc.exampleID, "main.tf.tmpl", renderCtx) require.NoError(t, err) require.NotEmpty(t, rendered) diff --git a/coderd/templatebuilder/testdata/gcp-linux.tf.golden b/coderd/templatebuilder/testdata/gcp-linux.tf.golden index acb8e8fa48e37..4d81ed0759d6e 100644 --- a/coderd/templatebuilder/testdata/gcp-linux.tf.golden +++ b/coderd/templatebuilder/testdata/gcp-linux.tf.golden @@ -13,6 +13,7 @@ provider "coder" {} variable "project_id" { description = "Which Google Compute Project should your workspace live in?" + default = "test-project_id" } # See https://registry.coder.com/modules/coder/gcp-region diff --git a/coderd/templatebuilder/testdata/gcp-windows.tf.golden b/coderd/templatebuilder/testdata/gcp-windows.tf.golden index aea409eee7ac8..1cc6d2b76ad25 100644 --- a/coderd/templatebuilder/testdata/gcp-windows.tf.golden +++ b/coderd/templatebuilder/testdata/gcp-windows.tf.golden @@ -13,6 +13,7 @@ provider "coder" {} variable "project_id" { description = "Which Google Compute Project should your workspace live in?" + default = "test-project_id" } # See https://registry.coder.com/modules/coder/gcp-region diff --git a/coderd/templatebuilder/testdata/kubernetes.tf.golden b/coderd/templatebuilder/testdata/kubernetes.tf.golden index 876f3c1dfc2d1..6345f4d893dba 100644 --- a/coderd/templatebuilder/testdata/kubernetes.tf.golden +++ b/coderd/templatebuilder/testdata/kubernetes.tf.golden @@ -153,7 +153,7 @@ resource "coder_agent" "main" { resource "kubernetes_persistent_volume_claim_v1" "home" { metadata { name = "coder-${data.coder_workspace.me.id}-home" - namespace = + namespace = "test-namespace" labels = { "app.kubernetes.io/name" = "coder-pvc" "app.kubernetes.io/instance" = "coder-pvc-${data.coder_workspace.me.id}" @@ -188,7 +188,7 @@ resource "kubernetes_deployment_v1" "main" { wait_for_rollout = false metadata { name = "coder-${data.coder_workspace.me.id}" - namespace = + namespace = "test-namespace" labels = { "app.kubernetes.io/name" = "coder-workspace" "app.kubernetes.io/instance" = "coder-workspace-${data.coder_workspace.me.id}" diff --git a/coderd/templatebuilder_handler_test.go b/coderd/templatebuilder_handler_test.go index baa89e80aeb5a..453555acd6401 100644 --- a/coderd/templatebuilder_handler_test.go +++ b/coderd/templatebuilder_handler_test.go @@ -66,7 +66,8 @@ func TestTemplateBuilderBases(t *testing.T) { { id: "gcp-windows", expectedOS: "windows", - hasVariables: false, + hasVariables: true, + expectedVars: []string{"project_id"}, }, }