-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #22219
- Loading branch information
Showing
7 changed files
with
356 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package configs | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/terraform/internal/addrs" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
type Import struct { | ||
ID string | ||
To addrs.AbsResourceInstance | ||
|
||
DeclRange hcl.Range | ||
} | ||
|
||
func decodeImportBlock(block *hcl.Block) (*Import, hcl.Diagnostics) { | ||
var diags hcl.Diagnostics | ||
imp := &Import{ | ||
DeclRange: block.DefRange, | ||
} | ||
|
||
content, moreDiags := block.Body.Content(importBlockSchema) | ||
diags = append(diags, moreDiags...) | ||
|
||
if attr, exists := content.Attributes["id"]; exists { | ||
id, idDiags := decodeId(attr.Expr) | ||
diags = append(diags, idDiags...) | ||
imp.ID = id | ||
} | ||
|
||
if attr, exists := content.Attributes["to"]; exists { | ||
to, traversalDiags := hcl.AbsTraversalForExpr(attr.Expr) | ||
diags = append(diags, traversalDiags...) | ||
if !traversalDiags.HasErrors() { | ||
to, toDiags := addrs.ParseAbsResourceInstance(to) | ||
diags = append(diags, toDiags.ToHCL()...) | ||
imp.To = to | ||
if !toDiags.HasErrors() && to.Resource.Resource.Mode != addrs.ManagedResourceMode { | ||
diags = append(diags, &hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: "Invalid Resource", | ||
Detail: fmt.Sprintf("%v is not a managed resource. Importing into a data source is not allowed.", to), | ||
Subject: attr.Expr.Range().Ptr(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return imp, diags | ||
|
||
} | ||
|
||
func decodeId(expr hcl.Expression) (string, hcl.Diagnostics) { | ||
id, diags := expr.Value(nil) | ||
if diags.HasErrors() { | ||
return "", diags | ||
} | ||
if id.Type() != cty.String { | ||
diags = append(diags, &hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: "Invalid Attribute", | ||
Detail: fmt.Sprintf("Invalid attribute value for import id: %#v", id), | ||
Subject: expr.Range().Ptr(), | ||
}) | ||
return "", diags | ||
} | ||
return id.AsString(), diags | ||
} | ||
|
||
var importBlockSchema = &hcl.BodySchema{ | ||
Attributes: []hcl.AttributeSchema{ | ||
{ | ||
Name: "id", | ||
Required: true, | ||
}, | ||
{ | ||
Name: "to", | ||
Required: true, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package configs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hcltest" | ||
"github.com/hashicorp/terraform/internal/addrs" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestImportBlock_decode(t *testing.T) { | ||
blockRange := hcl.Range{ | ||
Filename: "mock.tf", | ||
Start: hcl.Pos{Line: 3, Column: 12, Byte: 27}, | ||
End: hcl.Pos{Line: 3, Column: 19, Byte: 34}, | ||
} | ||
|
||
id := "test1" | ||
id_expr := hcltest.MockExprLiteral(cty.StringVal(id)) | ||
|
||
res_expr := hcltest.MockExprTraversalSrc("test_instance.foo") | ||
|
||
index_expr := hcltest.MockExprTraversalSrc("test_instance.foo[1]") | ||
|
||
mod_expr := hcltest.MockExprTraversalSrc("module.foo.test_instance.this") | ||
|
||
tests := map[string]struct { | ||
input *hcl.Block | ||
want *Import | ||
err string | ||
}{ | ||
"success": { | ||
&hcl.Block{ | ||
Type: "import", | ||
Body: hcltest.MockBody(&hcl.BodyContent{ | ||
Attributes: hcl.Attributes{ | ||
"id": { | ||
Name: "id", | ||
Expr: id_expr, | ||
}, | ||
"to": { | ||
Name: "to", | ||
Expr: res_expr, | ||
}, | ||
}, | ||
}), | ||
DefRange: blockRange, | ||
}, | ||
&Import{ | ||
Id: id, | ||
To: mustImportEndpointFromExpr(res_expr), | ||
DeclRange: blockRange, | ||
}, | ||
``, | ||
}, | ||
"indexed_resource": { | ||
&hcl.Block{ | ||
Type: "import", | ||
Body: hcltest.MockBody(&hcl.BodyContent{ | ||
Attributes: hcl.Attributes{ | ||
"id": { | ||
Name: "id", | ||
Expr: id_expr, | ||
}, | ||
"to": { | ||
Name: "to", | ||
Expr: index_expr, | ||
}, | ||
}, | ||
}), | ||
DefRange: blockRange, | ||
}, | ||
&Import{ | ||
Id: id, | ||
To: mustImportEndpointFromExpr(index_expr), | ||
DeclRange: blockRange, | ||
}, | ||
``, | ||
}, | ||
"module": { | ||
&hcl.Block{ | ||
Type: "import", | ||
Body: hcltest.MockBody(&hcl.BodyContent{ | ||
Attributes: hcl.Attributes{ | ||
"id": { | ||
Name: "id", | ||
Expr: id_expr, | ||
}, | ||
"to": { | ||
Name: "to", | ||
Expr: mod_expr, | ||
}, | ||
}, | ||
}), | ||
DefRange: blockRange, | ||
}, | ||
&Import{ | ||
Id: id, | ||
To: mustImportEndpointFromExpr(mod_expr), | ||
DeclRange: blockRange, | ||
}, | ||
``, | ||
}, | ||
"error: missing argument": { | ||
&hcl.Block{ | ||
Type: "import", | ||
Body: hcltest.MockBody(&hcl.BodyContent{ | ||
Attributes: hcl.Attributes{ | ||
"id": { | ||
Name: "id", | ||
Expr: id_expr, | ||
}, | ||
}, | ||
}), | ||
DefRange: blockRange, | ||
}, | ||
&Import{ | ||
Id: id, | ||
DeclRange: blockRange, | ||
}, | ||
"Missing required argument", | ||
}, | ||
"error: type mismatch": { | ||
&hcl.Block{ | ||
Type: "import", | ||
Body: hcltest.MockBody(&hcl.BodyContent{ | ||
Attributes: hcl.Attributes{ | ||
"id": { | ||
Name: "id", | ||
Expr: hcltest.MockExprLiteral(cty.NumberIntVal(0)), | ||
}, | ||
"to": { | ||
Name: "to", | ||
Expr: res_expr, | ||
}, | ||
}, | ||
}), | ||
DefRange: blockRange, | ||
}, | ||
&Import{ | ||
To: mustImportEndpointFromExpr(res_expr), | ||
DeclRange: blockRange, | ||
}, | ||
"Invalid Attribute", | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
got, diags := decodeImportBlock(test.input) | ||
|
||
if diags.HasErrors() { | ||
if test.err == "" { | ||
t.Fatalf("unexpected error: %s", diags.Errs()) | ||
} | ||
if gotErr := diags[0].Summary; gotErr != test.err { | ||
t.Errorf("wrong error, got %q, want %q", gotErr, test.err) | ||
} | ||
} else if test.err != "" { | ||
t.Fatalf("expected error") | ||
} | ||
if !cmp.Equal(got, test.want, cmp.AllowUnexported(addrs.AbsResourceInstance{})) { | ||
t.Fatalf("wrong result: %s", cmp.Diff(got, test.want)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func mustImportEndpointFromExpr(expr hcl.Expression) addrs.AbsResourceInstance { | ||
traversal, hcldiags := hcl.AbsTraversalForExpr(expr) | ||
if hcldiags.HasErrors() { | ||
panic(hcldiags.Errs()) | ||
} | ||
ep, diags := addrs.ParseAbsResourceInstance(traversal) | ||
if diags.HasErrors() { | ||
panic(diags.Err()) | ||
} | ||
return ep | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.