Skip to content

Fix ImportResourceState RPC response decoding for identity data #36806

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

Merged
merged 4 commits into from
Apr 1, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/plugin/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateReques
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type %q", imported.TypeName))
continue
}
importedIdentity, err := decodeDynamicValue(imported.Identity.IdentityData, importedIdentitySchema.Body.ImpliedType())
importedIdentity, err := decodeDynamicValue(imported.Identity.IdentityData, importedIdentitySchema.Identity.ImpliedType())
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
Expand Down
57 changes: 54 additions & 3 deletions internal/plugin/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func providerResourceIdentitySchemas() *proto.GetResourceIdentitySchemas_Respons
Version: 1,
IdentityAttributes: []*proto.ResourceIdentitySchema_IdentityAttribute{
{
Name: "attr",
Name: "id_attr",
Type: []byte(`"string"`),
RequiredForImport: true,
},
Expand Down Expand Up @@ -438,12 +438,12 @@ func TestGRPCProvider_UpgradeResourceIdentity(t *testing.T) {
&proto.UpgradeResourceIdentity_Response{
UpgradedIdentity: &proto.ResourceIdentityData{
IdentityData: &proto.DynamicValue{
Json: []byte(`{"attr":"bar"}`),
Json: []byte(`{"id_attr":"bar"}`),
},
},
},
false,
cty.ObjectVal(map[string]cty.Value{"attr": cty.StringVal("bar")}),
cty.ObjectVal(map[string]cty.Value{"id_attr": cty.StringVal("bar")}),
},
{
"response with error diagnostic",
Expand Down Expand Up @@ -936,6 +936,7 @@ func TestGRPCProvider_ImportResourceState(t *testing.T) {
t.Fatal(cmp.Diff(expectedResource, imported, typeComparer, valueComparer, equateEmpty))
}
}

func TestGRPCProvider_ImportResourceStateJSON(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
Expand Down Expand Up @@ -980,6 +981,56 @@ func TestGRPCProvider_ImportResourceStateJSON(t *testing.T) {
}
}

func TestGRPCProvider_ImportResourceState_Identity(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
client: client,
}

client.EXPECT().ImportResourceState(
gomock.Any(),
gomock.Any(),
).Return(&proto.ImportResourceState_Response{
ImportedResources: []*proto.ImportResourceState_ImportedResource{
{
TypeName: "resource",
State: &proto.DynamicValue{
Msgpack: []byte("\x81\xa4attr\xa3bar"),
},
Identity: &proto.ResourceIdentityData{
IdentityData: &proto.DynamicValue{
Msgpack: []byte("\x81\xa7id_attr\xa3foo"),
},
},
},
},
}, nil)

resp := p.ImportResourceState(providers.ImportResourceStateRequest{
TypeName: "resource",
Identity: cty.ObjectVal(map[string]cty.Value{
"id_attr": cty.StringVal("foo"),
}),
})

checkDiags(t, resp.Diagnostics)

expectedResource := providers.ImportedResource{
TypeName: "resource",
State: cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("bar"),
}),
Identity: cty.ObjectVal(map[string]cty.Value{
"id_attr": cty.StringVal("foo"),
}),
}

imported := resp.ImportedResources[0]
if !cmp.Equal(expectedResource, imported, typeComparer, valueComparer, equateEmpty) {
t.Fatal(cmp.Diff(expectedResource, imported, typeComparer, valueComparer, equateEmpty))
}
}

func TestGRPCProvider_MoveResourceState(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin6/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateReques
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type %q", imported.TypeName))
continue
}
importedIdentity, err := decodeDynamicValue(imported.Identity.IdentityData, importedIdentitySchema.Body.ImpliedType())
importedIdentity, err := decodeDynamicValue(imported.Identity.IdentityData, importedIdentitySchema.Identity.ImpliedType())
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
Expand Down
57 changes: 54 additions & 3 deletions internal/plugin6/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func providerResourceIdentitySchemas() *proto.GetResourceIdentitySchemas_Respons
Version: 1,
IdentityAttributes: []*proto.ResourceIdentitySchema_IdentityAttribute{
{
Name: "attr",
Name: "id_attr",
Type: []byte(`"string"`),
RequiredForImport: true,
},
Expand Down Expand Up @@ -445,12 +445,12 @@ func TestGRPCProvider_UpgradeResourceIdentity(t *testing.T) {
&proto.UpgradeResourceIdentity_Response{
UpgradedIdentity: &proto.ResourceIdentityData{
IdentityData: &proto.DynamicValue{
Json: []byte(`{"attr":"bar"}`),
Json: []byte(`{"id_attr":"bar"}`),
},
},
},
false,
cty.ObjectVal(map[string]cty.Value{"attr": cty.StringVal("bar")}),
cty.ObjectVal(map[string]cty.Value{"id_attr": cty.StringVal("bar")}),
},
{
"response with error diagnostic",
Expand Down Expand Up @@ -943,6 +943,7 @@ func TestGRPCProvider_ImportResourceState(t *testing.T) {
t.Fatal(cmp.Diff(expectedResource, imported, typeComparer, valueComparer, equateEmpty))
}
}

func TestGRPCProvider_ImportResourceStateJSON(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
Expand Down Expand Up @@ -987,6 +988,56 @@ func TestGRPCProvider_ImportResourceStateJSON(t *testing.T) {
}
}

func TestGRPCProvider_ImportResourceState_Identity(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
client: client,
}

client.EXPECT().ImportResourceState(
gomock.Any(),
gomock.Any(),
).Return(&proto.ImportResourceState_Response{
ImportedResources: []*proto.ImportResourceState_ImportedResource{
{
TypeName: "resource",
State: &proto.DynamicValue{
Msgpack: []byte("\x81\xa4attr\xa3bar"),
},
Identity: &proto.ResourceIdentityData{
IdentityData: &proto.DynamicValue{
Msgpack: []byte("\x81\xa7id_attr\xa3foo"),
},
},
},
},
}, nil)

resp := p.ImportResourceState(providers.ImportResourceStateRequest{
TypeName: "resource",
Identity: cty.ObjectVal(map[string]cty.Value{
"id_attr": cty.StringVal("foo"),
}),
})

checkDiags(t, resp.Diagnostics)

expectedResource := providers.ImportedResource{
TypeName: "resource",
State: cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("bar"),
}),
Identity: cty.ObjectVal(map[string]cty.Value{
"id_attr": cty.StringVal("foo"),
}),
}

imported := resp.ImportedResources[0]
if !cmp.Equal(expectedResource, imported, typeComparer, valueComparer, equateEmpty) {
t.Fatal(cmp.Diff(expectedResource, imported, typeComparer, valueComparer, equateEmpty))
}
}

func TestGRPCProvider_MoveResourceState(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
Expand Down