GenerateCoreBuildProvisionerFromHCLBody converts a parsed enforced provisioner body into a legacy JSON core build provisioner, using the same runtime behavior as normal JSON templates.
(
provisionerType string,
configBody hcl.Body,
override map[string]interface{},
pauseBefore time.Duration,
maxRetries int,
timeout time.Duration,
rawName string,
)
| 325 | // GenerateCoreBuildProvisionerFromHCLBody converts a parsed enforced provisioner body into |
| 326 | // a legacy JSON core build provisioner, using the same runtime behavior as normal JSON templates. |
| 327 | func (c *Core) GenerateCoreBuildProvisionerFromHCLBody( |
| 328 | provisionerType string, |
| 329 | configBody hcl.Body, |
| 330 | override map[string]interface{}, |
| 331 | pauseBefore time.Duration, |
| 332 | maxRetries int, |
| 333 | timeout time.Duration, |
| 334 | rawName string, |
| 335 | ) (CoreBuildProvisioner, hcl.Diagnostics) { |
| 336 | var diags hcl.Diagnostics |
| 337 | |
| 338 | if !c.components.PluginConfig.Provisioners.Has(provisionerType) { |
| 339 | return CoreBuildProvisioner{}, hcl.Diagnostics{&hcl.Diagnostic{ |
| 340 | Severity: hcl.DiagError, |
| 341 | Summary: fmt.Sprintf("Failed to start enforced provisioner %q", provisionerType), |
| 342 | Detail: fmt.Sprintf("The provisioner plugin %q could not be loaded.", provisionerType), |
| 343 | }} |
| 344 | } |
| 345 | |
| 346 | provisioner, err := c.components.PluginConfig.Provisioners.Start(provisionerType) |
| 347 | if err != nil { |
| 348 | return CoreBuildProvisioner{}, hcl.Diagnostics{&hcl.Diagnostic{ |
| 349 | Severity: hcl.DiagError, |
| 350 | Summary: fmt.Sprintf("Failed to start enforced provisioner %q", provisionerType), |
| 351 | Detail: fmt.Sprintf("The provisioner plugin could not be loaded: %s", err.Error()), |
| 352 | }} |
| 353 | } |
| 354 | if provisioner == nil { |
| 355 | return CoreBuildProvisioner{}, hcl.Diagnostics{&hcl.Diagnostic{ |
| 356 | Severity: hcl.DiagError, |
| 357 | Summary: fmt.Sprintf("Failed to start enforced provisioner %q", provisionerType), |
| 358 | Detail: "The provisioner failed to start and returned no instance.", |
| 359 | }} |
| 360 | } |
| 361 | |
| 362 | flatProvisionerCfg, moreDiags := hcldec.Decode(configBody, provisioner.ConfigSpec(), &hcl.EvalContext{Variables: map[string]cty.Value{}}) |
| 363 | diags = append(diags, moreDiags...) |
| 364 | if diags.HasErrors() { |
| 365 | return CoreBuildProvisioner{}, diags |
| 366 | } |
| 367 | |
| 368 | flatProvisionerCfg = hcl2shim.WriteUnknownPlaceholderValues(flatProvisionerCfg) |
| 369 | decodedConfig := hcl2shim.ConfigValueFromHCL2(flatProvisionerCfg) |
| 370 | configMap, _ := decodedConfig.(map[string]interface{}) |
| 371 | if configMap == nil { |
| 372 | configMap = make(map[string]interface{}) |
| 373 | } |
| 374 | |
| 375 | rawProvisioner := &template.Provisioner{ |
| 376 | Type: provisionerType, |
| 377 | Config: configMap, |
| 378 | Override: override, |
| 379 | PauseBefore: pauseBefore, |
| 380 | Timeout: timeout, |
| 381 | } |
| 382 | if maxRetries > 0 { |
| 383 | rawProvisioner.MaxRetries = strconv.Itoa(maxRetries) |
| 384 | } |
no test coverage detected