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
reduce logging
  • Loading branch information
johnstcn committed Nov 11, 2024
commit 1d77ed56202cc9da538ee86d0cb0a87478dc9b79
19 changes: 8 additions & 11 deletions provisioner/terraform/tfparse/tfextract.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ const maxFileSizeBytes = 10 * (10 << 20) // 10 MB
// WorkspaceTags extracts tags from coder_workspace_tags data sources defined in module.
// 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.
func WorkspaceTags(ctx context.Context, logger slog.Logger, module *tfconfig.Module) (map[string]string, error) {
func WorkspaceTags(ctx context.Context, module *tfconfig.Module) (map[string]string, error) {
workspaceTags := map[string]string{}

for _, dataResource := range module.DataResources {
if dataResource.Type != "coder_workspace_tags" {
logger.Debug(ctx, "skip resource as it is not a coder_workspace_tags", "resource_name", dataResource.Name, "resource_type", dataResource.Type)
continue
}

Expand All @@ -47,7 +46,6 @@ func WorkspaceTags(ctx context.Context, logger slog.Logger, module *tfconfig.Mod
parser := hclparse.NewParser()

if !strings.HasSuffix(dataResource.Pos.Filename, ".tf") {
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.
Expand Down Expand Up @@ -101,8 +99,6 @@ func WorkspaceTags(ctx context.Context, logger slog.Logger, module *tfconfig.Mod
return nil, xerrors.Errorf("can't preview the resource file: %v", err)
}

logger.Info(ctx, "workspace tag found", "key", key, "value", value)

if _, ok := workspaceTags[key]; ok {
return nil, xerrors.Errorf(`workspace tag %q is defined multiple times`, key)
}
Expand Down Expand Up @@ -135,14 +131,14 @@ 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(ctx, logger, module)
tags, err = WorkspaceTags(ctx, module)
if err != nil {
return nil, xerrors.Errorf("extract workspace tags: %w", err)
}

// To evalute the expressions, we need to load the default values for
// To evaluate the expressions, we need to load the default values for
// variables and parameters.
varsDefaults, paramsDefaults, err := loadDefaults(ctx, logger, module)
varsDefaults, paramsDefaults, err := loadDefaults(module)
if err != nil {
return nil, xerrors.Errorf("load defaults: %w", err)
}
Expand All @@ -162,6 +158,8 @@ 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 @@ -209,7 +207,7 @@ func loadModuleFromFile(file []byte, mimetype string) (module *tfconfig.Module,

// loadDefaults inspects the given module and returns the default values for
// all variables and coder_parameter data sources referenced there.
func loadDefaults(ctx context.Context, logger slog.Logger, module *tfconfig.Module) (varsDefaults map[string]string, paramsDefaults map[string]string, err error) {
func loadDefaults(module *tfconfig.Module) (varsDefaults map[string]string, paramsDefaults map[string]string, err error) {
// iterate through module.Variables to get the default values for all
// variables.
varsDefaults = make(map[string]string)
Expand All @@ -226,15 +224,14 @@ func loadDefaults(ctx context.Context, logger slog.Logger, module *tfconfig.Modu
paramsDefaults = make(map[string]string)
for _, dataResource := range module.DataResources {
if dataResource.Type != "coder_parameter" {
logger.Debug(ctx, "skip resource as it is not a coder_parameter", "resource_name", dataResource.Name, "resource_type", dataResource.Type)
continue
}

var file *hcl.File
var diags hcl.Diagnostics
parser := hclparse.NewParser()

if !strings.HasSuffix(dataResource.Pos.Filename, ".tf") {
logger.Debug(ctx, "only .tf files can be parsed", "filename", dataResource.Pos.Filename)
continue
}

Expand Down