Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
12 changes: 11 additions & 1 deletion coderd/templatebuilder/bases/gcp-linux/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
1 change: 1 addition & 0 deletions coderd/templatebuilder/bases/gcp-linux/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ provider "coder" {}

variable "project_id" {
description = "Which Google Compute Project should your workspace live in?"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still learning and trying to form and idea so please indulge me.

base.json is for the template builder UI, right? It declares the UI fields. The template builder compose takes the user input + main.tf.tmpl and I suppose generates a .tf file that is then used by the provisioner to instantiate a "template version". I can't figure out though how do you link the project_id if you removed it in this file.

default = {{ .Variables.project_id }}
}

# See https://registry.coder.com/modules/coder/gcp-region
Expand Down
12 changes: 11 additions & 1 deletion coderd/templatebuilder/bases/gcp-windows/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
1 change: 1 addition & 0 deletions coderd/templatebuilder/bases/gcp-windows/main.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 22 additions & 14 deletions coderd/templatebuilder/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 23 additions & 2 deletions coderd/templatebuilder/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package templatebuilder_test

import (
"flag"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions coderd/templatebuilder/testdata/gcp-linux.tf.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions coderd/templatebuilder/testdata/gcp-windows.tf.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions coderd/templatebuilder/testdata/kubernetes.tf.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <no value>
namespace = "test-namespace"
labels = {
"app.kubernetes.io/name" = "coder-pvc"
"app.kubernetes.io/instance" = "coder-pvc-${data.coder_workspace.me.id}"
Expand Down Expand Up @@ -188,7 +188,7 @@ resource "kubernetes_deployment_v1" "main" {
wait_for_rollout = false
metadata {
name = "coder-${data.coder_workspace.me.id}"
namespace = <no value>
namespace = "test-namespace"
labels = {
"app.kubernetes.io/name" = "coder-workspace"
"app.kubernetes.io/instance" = "coder-workspace-${data.coder_workspace.me.id}"
Expand Down
3 changes: 2 additions & 1 deletion coderd/templatebuilder_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func TestTemplateBuilderBases(t *testing.T) {
{
id: "gcp-windows",
expectedOS: "windows",
hasVariables: false,
hasVariables: true,
expectedVars: []string{"project_id"},
},
}

Expand Down
Loading