CoderParameterDefaults returns the default values of all coder_parameter data sources in the parsed module.
(ctx context.Context, varsDefaults map[string]string, names map[string]struct{})
| 315 | // CoderParameterDefaults returns the default values of all coder_parameter data sources |
| 316 | // in the parsed module. |
| 317 | func (p *Parser) CoderParameterDefaults(ctx context.Context, varsDefaults map[string]string, names map[string]struct{}) (map[string]string, error) { |
| 318 | defaultsM := make(map[string]string) |
| 319 | var ( |
| 320 | skipped []string |
| 321 | file *hcl.File |
| 322 | diags hcl.Diagnostics |
| 323 | ) |
| 324 | |
| 325 | for _, dataResource := range p.module.DataResources { |
| 326 | if dataResource == nil { |
| 327 | continue |
| 328 | } |
| 329 | |
| 330 | if !strings.HasSuffix(dataResource.Pos.Filename, ".tf") { |
| 331 | continue |
| 332 | } |
| 333 | |
| 334 | needle := strings.Join([]string{"data", dataResource.Type, dataResource.Name}, ".") |
| 335 | if dataResource.Type != "coder_parameter" { |
| 336 | skipped = append(skipped, needle) |
| 337 | continue |
| 338 | } |
| 339 | |
| 340 | if _, found := names[needle]; !found { |
| 341 | skipped = append(skipped, needle) |
| 342 | continue |
| 343 | } |
| 344 | |
| 345 | // We know in which HCL file is the data resource defined. |
| 346 | // NOTE: hclparse.Parser will cache multiple successive calls to parse the same file. |
| 347 | file, diags = p.underlying.ParseHCLFile(dataResource.Pos.Filename) |
| 348 | if diags.HasErrors() { |
| 349 | return nil, xerrors.Errorf("can't parse the resource file %q: %s", dataResource.Pos.Filename, diags.Error()) |
| 350 | } |
| 351 | |
| 352 | // Parse root to find "coder_parameter". |
| 353 | content, _, diags := file.Body.PartialContent(rootTemplateSchema) |
| 354 | if diags.HasErrors() { |
| 355 | return nil, xerrors.Errorf("can't parse the resource file: %s", diags.Error()) |
| 356 | } |
| 357 | |
| 358 | // Iterate over blocks to locate the exact "coder_parameter" data resource. |
| 359 | for _, block := range content.Blocks { |
| 360 | if !slices.Equal(block.Labels, []string{"coder_parameter", dataResource.Name}) { |
| 361 | continue |
| 362 | } |
| 363 | |
| 364 | // Parse "coder_parameter" to find the default value. |
| 365 | resContent, _, diags := block.Body.PartialContent(coderParameterSchema) |
| 366 | if diags.HasErrors() { |
| 367 | return nil, xerrors.Errorf(`can't parse the coder_parameter: %s`, diags.Error()) |
| 368 | } |
| 369 | |
| 370 | if _, ok := resContent.Attributes["default"]; !ok { |
| 371 | p.logger.Warn(ctx, "coder_parameter data source does not have a default value", slog.F("name", dataResource.Name)) |
| 372 | defaultsM[dataResource.Name] = "" |
| 373 | } else { |
| 374 | expr := resContent.Attributes["default"].Expr |
no test coverage detected