MCPcopy
hub / github.com/opentofu/opentofu / decodeProviderConfigRef

Function decodeProviderConfigRef

internal/configs/resource.go:903–1024  ·  view source on GitHub ↗
(expr hcl.Expression, argName string)

Source from the content-addressed store, hash-verified

901}
902
903func 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{

Callers 5

decodeImportBlockFunction · 0.85
decodeResourceBlockFunction · 0.85
decodeDataBlockFunction · 0.85
decodeEphemeralBlockFunction · 0.85

Calls 6

HasErrorsMethod · 0.95
shimTraversalInStringFunction · 0.85
exprIsNativeQuotedStringFunction · 0.85
RangeMethod · 0.65

Tested by

no test coverage detected