Skip to content

Commit 51263cf

Browse files
Added support for the new identity attribute in import blocks (Terraform 1.12.0+) (#472)
* Added support for the new `identity` attribute in import blocks, introduced in Terraform 1.12.0. * Ran go fmt * Ran 'go generate ./schema
1 parent b2df465 commit 51263cf

File tree

6 files changed

+192
-1
lines changed

6 files changed

+192
-1
lines changed

internal/schema/1.12/import.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"github.com/hashicorp/hcl-lang/lang"
8+
"github.com/hashicorp/hcl-lang/schema"
9+
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
10+
"github.com/zclconf/go-cty/cty"
11+
)
12+
13+
func importBlock() *schema.BlockSchema {
14+
return &schema.BlockSchema{
15+
Description: lang.Markdown("Import resources into Terraform to bring them under Terraform's management"),
16+
Body: &schema.BodySchema{
17+
HoverURL: "https://developer.hashicorp.com/terraform/language/import",
18+
Attributes: map[string]*schema.AttributeSchema{
19+
"provider": {
20+
Constraint: schema.Reference{OfScopeId: refscope.ProviderScope},
21+
IsOptional: true,
22+
Description: lang.Markdown("Reference to a `provider` configuration block, e.g. `mycloud.west` or `mycloud`"),
23+
},
24+
"id": {
25+
Constraint: schema.OneOf{
26+
schema.AnyExpression{OfType: cty.String},
27+
},
28+
IsOptional: true,
29+
IsDeprecated: false,
30+
Description: lang.Markdown("ID of the resource to be imported. e.g. `i-abcd1234`. Either `id` or `identity` must be specified, but not both."),
31+
},
32+
"identity": {
33+
Constraint: schema.OneOf{
34+
schema.AnyExpression{OfType: cty.Map(cty.String)},
35+
schema.AnyExpression{OfType: cty.Object(map[string]cty.Type{})},
36+
},
37+
IsOptional: true,
38+
Description: lang.Markdown("Key-value pairs to identify the resource to be imported. Either `id` or `identity` must be specified, but not both."),
39+
},
40+
"to": {
41+
Constraint: schema.Reference{OfScopeId: refscope.ResourceScope},
42+
IsRequired: true,
43+
Description: lang.Markdown("An address of the resource instance to import to. e.g. `aws_instance.example` or `module.foo.aws_instance.bar`"),
44+
},
45+
},
46+
},
47+
}
48+
}

internal/schema/1.12/import_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/hcl-lang/lang"
10+
)
11+
12+
func TestImportBlock(t *testing.T) {
13+
schema := importBlock()
14+
15+
// Test basic schema properties
16+
if schema.Description != lang.Markdown("Import resources into Terraform to bring them under Terraform's management") {
17+
t.Errorf("unexpected description: %v", schema.Description)
18+
}
19+
20+
if schema.Body.HoverURL != "https://developer.hashicorp.com/terraform/language/import" {
21+
t.Errorf("unexpected hover URL: %v", schema.Body.HoverURL)
22+
}
23+
24+
// Test that all expected attributes are present
25+
expectedAttrs := []string{"provider", "id", "identity", "to"}
26+
for _, attr := range expectedAttrs {
27+
if _, ok := schema.Body.Attributes[attr]; !ok {
28+
t.Errorf("missing expected attribute: %s", attr)
29+
}
30+
}
31+
32+
// Test id attribute
33+
idAttr := schema.Body.Attributes["id"]
34+
if !idAttr.IsOptional {
35+
t.Error("id attribute should be optional")
36+
}
37+
if idAttr.Description != lang.Markdown("ID of the resource to be imported. e.g. `i-abcd1234`. Either `id` or `identity` must be specified, but not both.") {
38+
t.Errorf("unexpected id description: %v", idAttr.Description)
39+
}
40+
41+
// Test identity attribute
42+
identityAttr := schema.Body.Attributes["identity"]
43+
if !identityAttr.IsOptional {
44+
t.Error("identity attribute should be optional")
45+
}
46+
if identityAttr.Description != lang.Markdown("Key-value pairs to identify the resource to be imported. Either `id` or `identity` must be specified, but not both.") {
47+
t.Errorf("unexpected identity description: %v", identityAttr.Description)
48+
}
49+
}
50+
51+
func TestImportBlock_Attributes(t *testing.T) {
52+
schema := importBlock()
53+
54+
// Test that all expected attributes are present
55+
expectedAttrs := []string{"provider", "id", "identity", "to"}
56+
for _, attr := range expectedAttrs {
57+
if _, ok := schema.Body.Attributes[attr]; !ok {
58+
t.Errorf("missing expected attribute: %s", attr)
59+
}
60+
}
61+
62+
// Test that id and identity are optional
63+
if !schema.Body.Attributes["id"].IsOptional {
64+
t.Error("id attribute should be optional")
65+
}
66+
if !schema.Body.Attributes["identity"].IsOptional {
67+
t.Error("identity attribute should be optional")
68+
}
69+
70+
// Test that to is required
71+
if !schema.Body.Attributes["to"].IsRequired {
72+
t.Error("to attribute should be required")
73+
}
74+
75+
// Test that provider is optional
76+
if !schema.Body.Attributes["provider"].IsOptional {
77+
t.Error("provider attribute should be optional")
78+
}
79+
}

internal/schema/1.12/root.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"github.com/hashicorp/go-version"
8+
"github.com/hashicorp/hcl-lang/schema"
9+
10+
v1_10_mod "github.com/hashicorp/terraform-schema/internal/schema/1.10"
11+
)
12+
13+
func ModuleSchema(v *version.Version) *schema.BodySchema {
14+
bs := v1_10_mod.ModuleSchema(v)
15+
16+
// Override the import block with the new schema that supports identity attribute
17+
bs.Blocks["import"] = importBlock()
18+
19+
return bs
20+
}

schema/core_schema.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
mod_v0_15 "github.com/hashicorp/terraform-schema/internal/schema/0.15"
1313
mod_v1_1 "github.com/hashicorp/terraform-schema/internal/schema/1.1"
1414
mod_v1_10 "github.com/hashicorp/terraform-schema/internal/schema/1.10"
15+
mod_v1_12 "github.com/hashicorp/terraform-schema/internal/schema/1.12"
1516
mod_v1_2 "github.com/hashicorp/terraform-schema/internal/schema/1.2"
1617
mod_v1_4 "github.com/hashicorp/terraform-schema/internal/schema/1.4"
1718
mod_v1_5 "github.com/hashicorp/terraform-schema/internal/schema/1.5"
@@ -36,13 +37,17 @@ var (
3637
v1_8 = version.Must(version.NewVersion("1.8"))
3738
v1_9 = version.Must(version.NewVersion("1.9"))
3839
v1_10 = version.Must(version.NewVersion("1.10"))
40+
v1_12 = version.Must(version.NewVersion("1.12"))
3941
)
4042

4143
// CoreModuleSchemaForVersion finds a module schema which is relevant
4244
// for the given Terraform version.
4345
// It will return error if such schema cannot be found.
4446
func CoreModuleSchemaForVersion(v *version.Version) (*schema.BodySchema, error) {
4547
ver := v.Core()
48+
if ver.GreaterThanOrEqual(v1_12) {
49+
return mod_v1_12.ModuleSchema(ver), nil
50+
}
4651
if ver.GreaterThanOrEqual(v1_10) {
4752
return mod_v1_10.ModuleSchema(ver), nil
4853
}

schema/core_schema_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
mod_v0_13 "github.com/hashicorp/terraform-schema/internal/schema/0.13"
1616
mod_v0_14 "github.com/hashicorp/terraform-schema/internal/schema/0.14"
1717
mod_v1_1 "github.com/hashicorp/terraform-schema/internal/schema/1.1"
18+
mod_v1_12 "github.com/hashicorp/terraform-schema/internal/schema/1.12"
1819
mod_v1_2 "github.com/hashicorp/terraform-schema/internal/schema/1.2"
1920
"github.com/zclconf/go-cty-debug/ctydebug"
2021
)
@@ -44,6 +45,7 @@ func TestCoreModuleSchemaForVersion_validate(t *testing.T) {
4445
"1.0.0",
4546
"1.1.0",
4647
"1.2.0",
48+
"1.12.0",
4749
}
4850

4951
for _, v := range versions {
@@ -92,6 +94,10 @@ func TestCoreModuleSchemaForVersion_matching(t *testing.T) {
9294
version.Must(version.NewVersion("1.2.0")),
9395
mod_v1_2.ModuleSchema,
9496
},
97+
{
98+
version.Must(version.NewVersion("1.12.0")),
99+
mod_v1_12.ModuleSchema,
100+
},
95101
}
96102

97103
for i, tc := range testCases {

schema/versions_gen.go

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)