2021-08-23 19:35:54 +02:00
|
|
|
package cloud
|
|
|
|
|
|
|
|
import (
|
2021-11-10 01:02:59 +01:00
|
|
|
"github.com/hashicorp/hcl/v2/hclwrite"
|
2021-08-23 19:35:54 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
|
|
|
)
|
|
|
|
|
|
|
|
func allowedSourceType(source terraform.ValueSourceType) bool {
|
|
|
|
return source == terraform.ValueFromNamedFile || source == terraform.ValueFromCLIArg || source == terraform.ValueFromEnvVar
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseCloudRunVariables accepts a mapping of unparsed values and a mapping of variable
|
|
|
|
// declarations and returns a name/value variable map appropriate for an API run context,
|
2021-11-10 01:02:59 +01:00
|
|
|
// that is, containing variables only sourced from non-file inputs like CLI args
|
2021-08-23 19:35:54 +02:00
|
|
|
// and environment variables. However, all variable parsing diagnostics are returned
|
|
|
|
// in order to allow callers to short circuit cloud runs that contain variable
|
|
|
|
// declaration or parsing errors. The only exception is that missing required values are not
|
|
|
|
// considered errors because they may be defined within the cloud workspace.
|
|
|
|
func ParseCloudRunVariables(vv map[string]backend.UnparsedVariableValue, decls map[string]*configs.Variable) (map[string]string, tfdiags.Diagnostics) {
|
|
|
|
declared, diags := backend.ParseDeclaredVariableValues(vv, decls)
|
|
|
|
_, undedeclaredDiags := backend.ParseUndeclaredVariableValues(vv, decls)
|
|
|
|
diags = diags.Append(undedeclaredDiags)
|
|
|
|
|
|
|
|
ret := make(map[string]string, len(declared))
|
|
|
|
|
|
|
|
// Even if there are parsing or declaration errors, populate the return map with the
|
|
|
|
// variables that could be used for cloud runs
|
|
|
|
for name, v := range declared {
|
|
|
|
if !allowedSourceType(v.SourceType) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-10 01:02:59 +01:00
|
|
|
// RunVariables are always expressed as HCL strings
|
|
|
|
tokens := hclwrite.TokensForValue(v.Value)
|
|
|
|
ret[name] = string(tokens.Bytes())
|
2021-08-23 19:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, diags
|
|
|
|
}
|