github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/configs/compat_shim.go (about)

     1  package configs
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  	"github.com/hashicorp/hcl/v2/hclsyntax"
     6  	"github.com/zclconf/go-cty/cty"
     7  )
     8  
     9  // -------------------------------------------------------------------------
    10  // Functions in this file are compatibility shims intended to ease conversion
    11  // from the old configuration loader. Any use of these functions that makes
    12  // a change should generate a deprecation warning explaining to the user how
    13  // to update their code for new patterns.
    14  //
    15  // Shims are particularly important for any patterns that have been widely
    16  // documented in books, tutorials, etc. Users will still be starting from
    17  // these examples and we want to help them adopt the latest patterns rather
    18  // than leave them stranded.
    19  // -------------------------------------------------------------------------
    20  
    21  // shimTraversalInString takes any arbitrary expression and checks if it is
    22  // a quoted string in the native syntax. If it _is_, then it is parsed as a
    23  // traversal and re-wrapped into a synthetic traversal expression and a
    24  // warning is generated. Otherwise, the given expression is just returned
    25  // verbatim.
    26  //
    27  // This function has no effect on expressions from the JSON syntax, since
    28  // traversals in strings are the required pattern in that syntax.
    29  //
    30  // If wantKeyword is set, the generated warning diagnostic will talk about
    31  // keywords rather than references. The behavior is otherwise unchanged, and
    32  // the caller remains responsible for checking that the result is indeed
    33  // a keyword, e.g. using hcl.ExprAsKeyword.
    34  func shimTraversalInString(expr hcl.Expression, wantKeyword bool) (hcl.Expression, hcl.Diagnostics) {
    35  	// ObjectConsKeyExpr is a special wrapper type used for keys on object
    36  	// constructors to deal with the fact that naked identifiers are normally
    37  	// handled as "bareword" strings rather than as variable references. Since
    38  	// we know we're interpreting as a traversal anyway (and thus it won't
    39  	// matter whether it's a string or an identifier) we can safely just unwrap
    40  	// here and then process whatever we find inside as normal.
    41  	if ocke, ok := expr.(*hclsyntax.ObjectConsKeyExpr); ok {
    42  		expr = ocke.Wrapped
    43  	}
    44  
    45  	if !exprIsNativeQuotedString(expr) {
    46  		return expr, nil
    47  	}
    48  
    49  	strVal, diags := expr.Value(nil)
    50  	if diags.HasErrors() || strVal.IsNull() || !strVal.IsKnown() {
    51  		// Since we're not even able to attempt a shim here, we'll discard
    52  		// the diagnostics we saw so far and let the caller's own error
    53  		// handling take care of reporting the invalid expression.
    54  		return expr, nil
    55  	}
    56  
    57  	// The position handling here isn't _quite_ right because it won't
    58  	// take into account any escape sequences in the literal string, but
    59  	// it should be close enough for any error reporting to make sense.
    60  	srcRange := expr.Range()
    61  	startPos := srcRange.Start // copy
    62  	startPos.Column++          // skip initial quote
    63  	startPos.Byte++            // skip initial quote
    64  
    65  	traversal, tDiags := hclsyntax.ParseTraversalAbs(
    66  		[]byte(strVal.AsString()),
    67  		srcRange.Filename,
    68  		startPos,
    69  	)
    70  	diags = append(diags, tDiags...)
    71  
    72  	// For initial release our deprecation warnings are disabled to allow
    73  	// a period where modules can be compatible with both old and new
    74  	// conventions.
    75  	// FIXME: Re-enable these deprecation warnings in a release prior to
    76  	// Terraform 0.13 and then remove the shims altogether for 0.13.
    77  	/*
    78  		if wantKeyword {
    79  			diags = append(diags, &hcl.Diagnostic{
    80  				Severity: hcl.DiagWarning,
    81  				Summary:  "Quoted keywords are deprecated",
    82  				Detail:   "In this context, keywords are expected literally rather than in quotes. Previous versions of Terraform required quotes, but that usage is now deprecated. Remove the quotes surrounding this keyword to silence this warning.",
    83  				Subject:  &srcRange,
    84  			})
    85  		} else {
    86  			diags = append(diags, &hcl.Diagnostic{
    87  				Severity: hcl.DiagWarning,
    88  				Summary:  "Quoted references are deprecated",
    89  				Detail:   "In this context, references are expected literally rather than in quotes. Previous versions of Terraform required quotes, but that usage is now deprecated. Remove the quotes surrounding this reference to silence this warning.",
    90  				Subject:  &srcRange,
    91  			})
    92  		}
    93  	*/
    94  
    95  	return &hclsyntax.ScopeTraversalExpr{
    96  		Traversal: traversal,
    97  		SrcRange:  srcRange,
    98  	}, diags
    99  }
   100  
   101  // shimIsIgnoreChangesStar returns true if the given expression seems to be
   102  // a string literal whose value is "*". This is used to support a legacy
   103  // form of ignore_changes = all .
   104  //
   105  // This function does not itself emit any diagnostics, so it's the caller's
   106  // responsibility to emit a warning diagnostic when this function returns true.
   107  func shimIsIgnoreChangesStar(expr hcl.Expression) bool {
   108  	val, valDiags := expr.Value(nil)
   109  	if valDiags.HasErrors() {
   110  		return false
   111  	}
   112  	if val.Type() != cty.String || val.IsNull() || !val.IsKnown() {
   113  		return false
   114  	}
   115  	return val.AsString() == "*"
   116  }