10000 feat(provisioner/terraform/tfparse): implement WorkspaceTagDefaultsFromFile by johnstcn · Pull Request #15236 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(provisioner/terraform/tfparse): implement WorkspaceTagDefaultsFromFile #15236

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 14 commits into from
Nov 14, 2024
Merged
Prev Previous commit
Next Next commit
bring back logging
  • Loading branch information
johnstcn committed Nov 11, 2024
commit 3db3e6bfcf4b8b4b44273e8420a090ce5733c4e3
2 changes: 1 addition & 1 deletion provisioner/terraform/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *server) Parse(sess *provisionersdk.Session, _ *proto.ParseRequest, _ <-
return pr 10000 ovisionersdk.ParseErrorf("load module: %s", formatDiagnostics(sess.WorkDirectory, diags))
}

workspaceTags, err := tfparse.WorkspaceTags(nil, module)
workspaceTags, err := tfparse.WorkspaceTags(ctx, s.logger, nil, module)
if err != nil {
return provisionersdk.ParseErrorf("can't load workspace tags: %v", err)
}
Expand Down
22 changes: 13 additions & 9 deletions provisioner/terraform/tfparse/tfextract.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ type Parser interface {
// Note that this only returns the lexical values of the data source, and does not
// evaluate variables and such. To do this, see evalProvisionerTags below.
// If the provided Parser is nil, a new instance of hclparse.Parser will be used instead.
func WorkspaceTags(parser Parser, module *tfconfig.Module) (map[string]string, error) {
func WorkspaceTags(ctx context.Context, logger slog.Logger, parser Parser, module *tfconfig.Module) (map[string]string, error) {
if parser == nil {
parser = hclparse.NewParser()
}
tags := map[string]string{}

var skipped []string
for _, dataResource := range module.DataResources {
if dataResource.Type != "coder_workspace_tags" {
skipped = append(skipped, strings.Join([]string{"data", dataResource.Type, dataResource.Name}, "."))
continue
}

Expand Down Expand Up @@ -118,6 +119,7 @@ func WorkspaceTags(parser Parser, module *tfconfig.Module) (map[string]string, e
}
}
}
logger.Debug(ctx, "found workspace tags", slog.F("tags", maps.Keys(tags)), slog.F("skipped", skipped))
return tags, nil
}

Expand Down Expand Up @@ -145,18 +147,18 @@ func WorkspaceTagDefaultsFromFile(ctx context.Context, logger slog.Logger, file

// This only gets us the expressions. We need to evaluate them.
// Example: var.region -> "us"
tags, err = WorkspaceTags(parser, module)
tags, err = WorkspaceTags(ctx, logger, parser, module)
if err != nil {
return nil, xerrors.Errorf("extract workspace tags: %w", err)
}

// To evaluate the expressions, we need to load the default values for
// variables and parameters.
varsDefaults, err := loadVarsDefaults(maps.Values(module.Variables))
varsDefaults, err := loadVarsDefaults(ctx, logger, maps.Values(module.Variables))
if err != nil {
return nil, xerrors.Errorf("load variable defaults: %w", err)
}
paramsDefaults, err := loadParamsDefaults(parser, maps.Values(module.DataResources))
paramsDefaults, err := loadParamsDefaults(ctx, logger, parser, maps.Values(module.DataResources))
if err != nil {
return nil, xerrors.Errorf("load parameter defaults: %w", err)
}
Expand All @@ -176,8 +178,6 @@ func WorkspaceTagDefaultsFromFile(ctx context.Context, logger slog.Logger, file
}
return nil, xerrors.Errorf("provisioner tag %q evaluated to an empty value, please set a default value", k)
}
logger.Info(ctx, "found workspace tags", slog.F("tags", evalTags))

return evalTags, nil
}

Expand Down Expand Up @@ -224,7 +224,7 @@ func loadModuleFromFile(file []byte, mimetype string) (module *tfconfig.Module,
}

// loadVarsDefaults returns the default values for all variables passed to it.
func loadVarsDefaults(variables []*tfconfig.Variable) (map[string]string, error) {
func loadVarsDefaults(ctx context.Context, logger slog.Logger, variables []*tfconfig.Variable) (map[string]string, error) {
// iterate through vars to get the default values for all
// variables.
m := make(map[string]string)
Expand All @@ -238,18 +238,21 @@ func loadVarsDefaults(variables []*tfconfig.Variable) (map[string]string, error)
}
m[v.Name] = strings.Trim(sv, `"`)
}
logger.Debug(ctx, "found default values for variables", slog.F("defaults", m))
return m, nil
}

// loadParamsDefaults returns the default values of all coder_parameter data sources data sources provided.
func loadParamsDefaults(parser Parser, dataSources []*tfconfig.Resource) (map[string]string, error) {
func loadParamsDefaults(ctx context.Context, logger slog.Logger, parser Parser, dataSources []*tfconfig.Resource) (map[string]string, error) {
defaultsM := make(map[string]string)
var skipped []string
for _, dataResource := range dataSources {
if dataResource == nil {
continue
}

if dataResource.Type != "coder_parameter" {
skipped = append(skipped, strings.Join([]string{"data", dataResource.Type, dataResource.Name}, "."))
continue
}

Expand Down Expand Up @@ -296,6 +299,7 @@ func loadParamsDefaults(parser Parser, dataSources []*tfconfig.Resource) (map[st
}
}
}
logger.Debug(ctx, "found default values for parameters", slog.F("defaults", defaultsM), slog.F("skipped", skipped))
return defaultsM, nil
}

Expand Down
38 changes: 34 additions & 4 deletions provisioner/terraform/tfparse/tfparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand All @@ -71,6 +74,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand All @@ -91,6 +97,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand Down Expand Up @@ -122,6 +131,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "eu"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand Down Expand Up @@ -159,6 +171,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand All @@ -184,6 +199,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand Down Expand Up @@ -218,6 +236,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
variable "notregion" {
type = string
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand Down Expand Up @@ -247,6 +268,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand Down Expand Up @@ -280,6 +304,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand Down Expand Up @@ -311,6 +338,9 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
type = string
default = "region.us"
}
data "base" "ours" {
all = true
}
data "coder_parameter" "az" {
name = "az"
type = "string"
Expand All @@ -334,7 +364,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
tar := createTar(t, tc.files)
logger := slogtest.Make(t, nil)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
tags, err := tfparse.WorkspaceTagDefaultsFromFile(ctx, logger, tar, "application/x-tar")
if tc.expectError != "" {
require.NotNil(t, err)
Expand All @@ -348,7 +378,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
zip := createZip(t, tc.files)
logger := slogtest.Make(t, nil)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
tags, err := tfparse.WorkspaceTagDefaultsFromFile(ctx, logger, zip, "application/zip")
if tc.expectError != "" {
require.Contains(t, err.Error(), tc.expectError)
Expand Down Expand Up @@ -395,8 +425,8 @@ func createZip(t testing.TB, files map[string]string) []byte {
// goarch: amd64
// pkg: github.com/coder/coder/v2/provisioner/terraform/tfparse
// cpu: AMD EPYC 7502P 32-Core Processor
// BenchmarkWorkspaceTagDefaultsFromFile/Tar-16 1077 1081951 ns/op 200754 B/op 1312 allocs/op
// BenchmarkWorkspaceTagDefaultsFromFile/Zip-16 984 1187299 ns/op 249574 B/op 1369 allocs/op
// BenchmarkWorkspaceTagDefaultsFromFile/Tar-16 1770 1064920 ns/op 197638 B/op 1312 allocs/op
// BenchmarkWorkspaceTagDefaultsFromFile/Zip-16 954 1197070 ns/op 246819 B/op 1369 allocs/op
// PASS
func BenchmarkWorkspaceTagDefaultsFromFile(b *testing.B) {
files := map[string]string{
Expand Down
Loading
0