Skip to content

feat: store coder_workspace_tags in the database #13294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
more unit test
  • Loading branch information
mtojek committed May 17, 2024
commit 1167fd4460d75b58b0cca20aa23fef4c4ac39f55
11 changes: 9 additions & 2 deletions provisioner/terraform/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -83,20 +84,26 @@ func (s *server) loadWorkspaceTags(ctx context.Context, module *tfconfig.Module)
s.logger.Debug(ctx, "only .tf files can be parsed", "filename", dataResource.Pos.Filename)
continue
}
// We know in which HCL file is the data resource defined.
file, diags = parser.ParseHCLFile(dataResource.Pos.Filename)

if diags.HasErrors() {
return nil, xerrors.Errorf("can't parse the resource file: %s", diags.Error())
}

// Parse root to find "coder_workspace_tags"
// Parse root to find "coder_workspace_tags".
content, _, diags := file.Body.PartialContent(rootTemplateSchema)
if diags.HasErrors() {
return nil, xerrors.Errorf("can't parse the resource file: %s", diags.Error())
}

// Iterate over blocks to locate the exact "coder_workspace_tags" data resource.
for _, block := range content.Blocks {
// Parse "coder_workspace_tags" to find all key-value tags
if !slices.Equal(block.Labels, []string{"coder_workspace_tags", dataResource.Name}) {
continue
}

// Parse "coder_workspace_tags" to find all key-value tags.
resContent, _, diags := block.Body.PartialContent(coderWorkspaceTagsSchema)
if diags.HasErrors() {
return nil, xerrors.Errorf(`can't parse the resource coder_workspace_tags: %s`, diags.Error())
Expand Down
64 changes: 64 additions & 0 deletions provisioner/terraform/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,70 @@ func TestParse(t *testing.T) {
},
},
},
{
Name: "workspace-tags-in-a-single-file",
Files: map[string]string{
"main.tf": `

data "coder_parameter" "os_selector" {
name = "os_selector"
display_name = "Operating System"
mutable = false

default = "osx"

option {
icon = "/icons/linux.png"
name = "Linux"
value = "linux"
}
option {
icon = "/icons/osx.png"
name = "OSX"
value = "osx"
}
option {
icon = "/icons/windows.png"
name = "Windows"
value = "windows"
}
}

data "coder_parameter" "feature_cache_enabled" {
name = "feature_cache_enabled"
display_name = "Enable cache?"
type = "bool"

default = false
}

data "coder_parameter" "feature_debug_enabled" {
name = "feature_debug_enabled"
display_name = "Enable debug?"
type = "bool"

default = true
}

data "coder_workspace_tags" "custom_workspace_tags" {
tags = {
"cluster" = "developers"
"os" = data.coder_parameter.os_selector.value
"debug" = "${data.coder_parameter.feature_debug_enabled.value}+12345"
"cache" = data.coder_parameter.feature_cache_enabled.value == "true" ? "nix-with-cache" : "no-cache"
}
}
`,
},
Response: &proto.ParseComplete{
WorkspaceTags: map[string]string{
"cluster": `"developers"`,
"os": `data.coder_parameter.os_selector.value`,
"debug": `"${data.coder_parameter.feature_debug_enabled.value}+12345"`,
"cache": `data.coder_parameter.feature_cache_enabled.value == "true" ? "nix-with-cache" : "no-cache"`,
},
},
},
}

for _, testCase := range testCases {
Expand Down
Loading