-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathimportresourcestate.go
61 lines (48 loc) · 2.04 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
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package fromproto6
import (
"context"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"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"
)
// ImportResourceStateRequest returns the *fwserver.ImportResourceStateRequest
// equivalent of a *tfprotov6.ImportResourceStateRequest.
func ImportResourceStateRequest(ctx context.Context, proto6 *tfprotov6.ImportResourceStateRequest, reqResource resource.Resource, resourceSchema fwschema.Schema, identitySchema 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,
IdentitySchema: identitySchema,
Resource: reqResource,
TypeName: proto6.TypeName,
ClientCapabilities: ImportStateClientCapabilities(proto6.ClientCapabilities),
}
identity, identityDiags := ResourceIdentity(ctx, proto6.Identity, identitySchema)
diags.Append(identityDiags...)
fw.Identity = identity
return fw, diags
}