(expr hcl.Expression, argName string)
| 901 | } |
| 902 | |
| 903 | func decodeProviderConfigRef(expr hcl.Expression, argName string) (*ProviderConfigRef, hcl.Diagnostics) { |
| 904 | var diags hcl.Diagnostics |
| 905 | |
| 906 | var keyExpr hcl.Expression |
| 907 | |
| 908 | const ( |
| 909 | // name.alias[const_key] |
| 910 | nameIndex = 0 |
| 911 | aliasIndex = 1 |
| 912 | keyIndex = 2 |
| 913 | ) |
| 914 | var maxTraversalLength = keyIndex + 1 |
| 915 | |
| 916 | if ok := hcljson.IsJSONExpression(expr); ok { |
| 917 | expr, diags = hcl2shim.ConvertJSONExpressionToHCL(expr) |
| 918 | if diags.HasErrors() { |
| 919 | return nil, diags |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | // name.alias[expr_key] |
| 924 | if iex, ok := expr.(*hclsyntax.IndexExpr); ok { |
| 925 | maxTraversalLength = aliasIndex + 1 // expr key found, no const key allowed |
| 926 | |
| 927 | keyExpr = iex.Key |
| 928 | expr = iex.Collection |
| 929 | } |
| 930 | |
| 931 | var shimDiags hcl.Diagnostics |
| 932 | expr, shimDiags = shimTraversalInString(expr, false) |
| 933 | diags = append(diags, shimDiags...) |
| 934 | |
| 935 | traversal, travDiags := hcl.AbsTraversalForExpr(expr) |
| 936 | |
| 937 | // AbsTraversalForExpr produces only generic errors, so we'll discard |
| 938 | // the errors given and produce our own with extra context. If we didn't |
| 939 | // get any errors then we might still have warnings, though. |
| 940 | if !travDiags.HasErrors() { |
| 941 | diags = append(diags, travDiags...) |
| 942 | } |
| 943 | |
| 944 | if len(traversal) == 0 || len(traversal) > maxTraversalLength { |
| 945 | // A provider reference was given as a string literal in the legacy |
| 946 | // configuration language and there are lots of examples out there |
| 947 | // showing that usage, so we'll sniff for that situation here and |
| 948 | // produce a specialized error message for it to help users find |
| 949 | // the new correct form. |
| 950 | if exprIsNativeQuotedString(expr) { |
| 951 | diags = append(diags, &hcl.Diagnostic{ |
| 952 | Severity: hcl.DiagError, |
| 953 | Summary: "Invalid provider configuration reference", |
| 954 | Detail: "A provider configuration reference must not be given in quotes.", |
| 955 | Subject: expr.Range().Ptr(), |
| 956 | }) |
| 957 | return nil, diags |
| 958 | } |
| 959 | |
| 960 | diags = append(diags, &hcl.Diagnostic{ |
no test coverage detected