/
importresourcestate.go
49 lines (41 loc) · 1.65 KB
/
importresourcestate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package fromproto6
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// ImportResourceStateRequest returns the *fwserver.ImportResourceStateRequest
// equivalent of a *tfprotov6.ImportResourceStateRequest.
func ImportResourceStateRequest(ctx context.Context, proto6 *tfprotov6.ImportResourceStateRequest, resource resource.Resource, resourceSchema fwschema.Schema) (*fwserver.ImportResourceStateRequest, diag.Diagnostics) {
if proto6 == nil {
return nil, nil
}
var diags diag.Diagnostics
// Panic prevention here to simplify the calling implementations.
// This should not happen, but just in case.
if resourceSchema == nil {
diags.AddError(
"Unable to Create Empty State",
"An unexpected error was encountered when creating the empty state. "+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
"Please report this to the provider developer:\n\n"+
"Missing schema.",
)
return nil, diags
}
fw := &fwserver.ImportResourceStateRequest{
EmptyState: tfsdk.State{
Raw: tftypes.NewValue(resourceSchema.Type().TerraformType(ctx), nil),
Schema: resourceSchema,
},
ID: proto6.ID,
Resource: resource,
TypeName: proto6.TypeName,
}
return fw, diags
}