github.com/hugorut/terraform@v1.1.3/src/cloud/cloud_variables.go (about)

     1  package cloud
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2/hclwrite"
     5  	"github.com/hugorut/terraform/src/backend"
     6  	"github.com/hugorut/terraform/src/configs"
     7  	"github.com/hugorut/terraform/src/terraform"
     8  	"github.com/hugorut/terraform/src/tfdiags"
     9  )
    10  
    11  func allowedSourceType(source terraform.ValueSourceType) bool {
    12  	return source == terraform.ValueFromNamedFile || source == terraform.ValueFromCLIArg || source == terraform.ValueFromEnvVar
    13  }
    14  
    15  // ParseCloudRunVariables accepts a mapping of unparsed values and a mapping of variable
    16  // declarations and returns a name/value variable map appropriate for an API run context,
    17  // that is, containing variables only sourced from non-file inputs like CLI args
    18  // and environment variables. However, all variable parsing diagnostics are returned
    19  // in order to allow callers to short circuit cloud runs that contain variable
    20  // declaration or parsing errors. The only exception is that missing required values are not
    21  // considered errors because they may be defined within the cloud workspace.
    22  func ParseCloudRunVariables(vv map[string]backend.UnparsedVariableValue, decls map[string]*configs.Variable) (map[string]string, tfdiags.Diagnostics) {
    23  	declared, diags := backend.ParseDeclaredVariableValues(vv, decls)
    24  	_, undedeclaredDiags := backend.ParseUndeclaredVariableValues(vv, decls)
    25  	diags = diags.Append(undedeclaredDiags)
    26  
    27  	ret := make(map[string]string, len(declared))
    28  
    29  	// Even if there are parsing or declaration errors, populate the return map with the
    30  	// variables that could be used for cloud runs
    31  	for name, v := range declared {
    32  		if !allowedSourceType(v.SourceType) {
    33  			continue
    34  		}
    35  
    36  		// RunVariables are always expressed as HCL strings
    37  		tokens := hclwrite.TokensForValue(v.Value)
    38  		ret[name] = string(tokens.Bytes())
    39  	}
    40  
    41  	return ret, diags
    42  }