Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/schema: Remove timeouts during ImportResourceState #1146

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230213-150146.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'helper/schema: Prevented unexpected difference for timeouts on first plan after
import'
time: 2023-02-13T15:01:46.441029-05:00
custom:
Issue: "1146"
16 changes: 16 additions & 0 deletions helper/schema/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,22 @@ func (s *GRPCProviderServer) ImportResourceState(ctx context.Context, req *tfpro
// Normalize the value and fill in any missing blocks.
newStateVal = objchange.NormalizeObjectFromLegacySDK(newStateVal, schemaBlock)

// Ensure any timeouts block is null in the imported state. There is no
// configuration to read from during import, so it is never valid to
// return a known value for the block.
//
// This is done without modifying HCL2ValueFromFlatmap or
// NormalizeObjectFromLegacySDK to prevent other unexpected changes.
//
// Reference: https://github.com/hashicorp/terraform-plugin-sdk/issues/1145
newStateType := newStateVal.Type()

if newStateVal != cty.NilVal && !newStateVal.IsNull() && newStateType.IsObjectType() && newStateType.HasAttribute(TimeoutsConfigKey) {
newStateValueMap := newStateVal.AsValueMap()
newStateValueMap[TimeoutsConfigKey] = cty.NullVal(newStateType.AttributeType(TimeoutsConfigKey))
newStateVal = cty.ObjectVal(newStateValueMap)
}

newStateMP, err := msgpack.Marshal(newStateVal, schemaBlock.ImpliedType())
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
Expand Down
154 changes: 154 additions & 0 deletions helper/schema/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,160 @@ func TestApplyResourceChange_bigint(t *testing.T) {
}
}

// Timeouts should never be present in imported resources.
// Reference: https://github.com/hashicorp/terraform-plugin-sdk/issues/1145
func TestImportResourceState_Timeouts_None(t *testing.T) {
t.Parallel()

resourceDefinition := &Resource{
Importer: &ResourceImporter{
StateContext: ImportStatePassthroughContext,
},
Schema: map[string]*Schema{
"string_attribute": {
Type: TypeString,
Optional: true,
},
},
}
resourceTypeName := "test"

server := NewGRPCProviderServer(&Provider{
ResourcesMap: map[string]*Resource{
resourceTypeName: resourceDefinition,
},
})

schema := resourceDefinition.CoreConfigSchema()

// Import shim state should not require all attributes.
stateVal, err := schema.CoerceValue(cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("test"),
}))

if err != nil {
t.Fatalf("unable to coerce state value: %s", err)
}

testReq := &tfprotov5.ImportResourceStateRequest{
ID: "test",
TypeName: resourceTypeName,
}

resp, err := server.ImportResourceState(context.Background(), testReq)

if err != nil {
t.Fatalf("unexpected error during ImportResourceState: %s", err)
}

if resp == nil {
t.Fatal("expected ImportResourceState response")
}

if len(resp.Diagnostics) > 0 {
var diagnostics []string

for _, diagnostic := range resp.Diagnostics {
diagnostics = append(diagnostics, fmt.Sprintf("%s: %s: %s", diagnostic.Severity, diagnostic.Summary, diagnostic.Detail))
}

t.Fatalf("unexpected ImportResourceState diagnostics: %s", strings.Join(diagnostics, " | "))
}

if len(resp.ImportedResources) != 1 {
t.Fatalf("expected 1 ImportedResource, got: %#v", resp.ImportedResources)
}

gotStateVal, err := msgpack.Unmarshal(resp.ImportedResources[0].State.MsgPack, schema.ImpliedType())

if err != nil {
t.Fatalf("unexpected error during MessagePack unmarshal: %s", err)
}

if diff := cmp.Diff(stateVal, gotStateVal, valueComparer); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
}

// Timeouts should never be present in imported resources.
// Reference: https://github.com/hashicorp/terraform-plugin-sdk/issues/1145
func TestImportResourceState_Timeouts_Removed(t *testing.T) {
t.Parallel()

resourceDefinition := &Resource{
Importer: &ResourceImporter{
StateContext: ImportStatePassthroughContext,
},
Schema: map[string]*Schema{
"string_attribute": {
Type: TypeString,
Optional: true,
},
},
Timeouts: &ResourceTimeout{
Create: DefaultTimeout(10 * time.Minute),
Read: DefaultTimeout(10 * time.Minute),
},
}
resourceTypeName := "test"

server := NewGRPCProviderServer(&Provider{
ResourcesMap: map[string]*Resource{
resourceTypeName: resourceDefinition,
},
})

schema := resourceDefinition.CoreConfigSchema()

// Import shim state should not require all attributes.
stateVal, err := schema.CoerceValue(cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("test"),
}))

if err != nil {
t.Fatalf("unable to coerce state value: %s", err)
}

testReq := &tfprotov5.ImportResourceStateRequest{
ID: "test",
TypeName: resourceTypeName,
}

resp, err := server.ImportResourceState(context.Background(), testReq)

if err != nil {
t.Fatalf("unexpected error during ImportResourceState: %s", err)
}

if resp == nil {
t.Fatal("expected ImportResourceState response")
}

if len(resp.Diagnostics) > 0 {
var diagnostics []string

for _, diagnostic := range resp.Diagnostics {
diagnostics = append(diagnostics, fmt.Sprintf("%s: %s: %s", diagnostic.Severity, diagnostic.Summary, diagnostic.Detail))
}

t.Fatalf("unexpected ImportResourceState diagnostics: %s", strings.Join(diagnostics, " | "))
}

if len(resp.ImportedResources) != 1 {
t.Fatalf("expected 1 ImportedResource, got: %#v", resp.ImportedResources)
}

gotStateVal, err := msgpack.Unmarshal(resp.ImportedResources[0].State.MsgPack, schema.ImpliedType())

if err != nil {
t.Fatalf("unexpected error during MessagePack unmarshal: %s", err)
}

if diff := cmp.Diff(stateVal, gotStateVal, valueComparer); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
}

func TestReadDataSource(t *testing.T) {
t.Parallel()

Expand Down