/
serve_import.go
160 lines (132 loc) · 5.32 KB
/
serve_import.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package proto6server
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/internal/toproto6"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// importedResource represents a resource that was imported.
//
// This type is not exported as the framework import implementation is
// currently designed for the most common use case of single resource import.
type importedResource struct {
Private []byte
State tfsdk.State
TypeName string
}
func (r importedResource) toTfprotov6(ctx context.Context) (*tfprotov6.ImportedResource, diag.Diagnostics) {
var diags diag.Diagnostics
irProto6 := &tfprotov6.ImportedResource{
Private: r.Private,
TypeName: r.TypeName,
}
stateProto6, err := tfprotov6.NewDynamicValue(r.State.Schema.TerraformType(ctx), r.State.Raw)
if err != nil {
diags.AddError(
"Error converting imported resource response",
"An unexpected error was encountered when converting the imported resource response to a usable type. This is always a problem with the provider. Please give the following information to the provider developer:\n\n"+err.Error(),
)
return nil, diags
}
irProto6.State = &stateProto6
return irProto6, diags
}
// importResourceStateResponse is a thin abstraction to allow native Diagnostics usage
type importResourceStateResponse struct {
Diagnostics diag.Diagnostics
ImportedResources []importedResource
}
func (r importResourceStateResponse) toTfprotov6(ctx context.Context) *tfprotov6.ImportResourceStateResponse {
resp := &tfprotov6.ImportResourceStateResponse{
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
for _, ir := range r.ImportedResources {
irProto6, diags := ir.toTfprotov6(ctx)
resp.Diagnostics = append(resp.Diagnostics, toproto6.Diagnostics(diags)...)
if diags.HasError() {
continue
}
resp.ImportedResources = append(resp.ImportedResources, irProto6)
}
return resp
}
func (s *Server) importResourceState(ctx context.Context, req *tfprotov6.ImportResourceStateRequest, resp *importResourceStateResponse) {
resourceType, diags := s.getResourceType(ctx, req.TypeName)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
logging.FrameworkDebug(ctx, "Calling provider defined ResourceType GetSchema")
resourceSchema, diags := resourceType.GetSchema(ctx)
logging.FrameworkDebug(ctx, "Called provider defined ResourceType GetSchema")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
logging.FrameworkDebug(ctx, "Calling provider defined ResourceType NewResource")
resource, diags := resourceType.NewResource(ctx, s.Provider)
logging.FrameworkDebug(ctx, "Called provider defined ResourceType NewResource")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resourceWithImportState, ok := resource.(tfsdk.ResourceWithImportState)
if !ok {
// If there is a feature request for customizing this messaging,
// provider developers can implement a ImportState method that
// immediately returns a custom error diagnostic.
//
// However, implementing the ImportState method could cause issues
// with automated documentation generation, which likely would check
// if the resource implements the ResourceWithImportState interface.
// Instead, a separate "ResourceWithoutImportState" interface could be
// created with a method such as:
// ImportNotImplementedMessage(context.Context) string.
resp.Diagnostics.AddError(
"Resource Import Not Implemented",
"This resource does not support import. Please contact the provider developer for additional information.",
)
return
}
emptyState := tftypes.NewValue(resourceSchema.TerraformType(ctx), nil)
importReq := tfsdk.ImportResourceStateRequest{
ID: req.ID,
}
importResp := tfsdk.ImportResourceStateResponse{
State: tfsdk.State{
Raw: emptyState,
Schema: resourceSchema,
},
}
logging.FrameworkDebug(ctx, "Calling provider defined Resource ImportState")
resourceWithImportState.ImportState(ctx, importReq, &importResp)
logging.FrameworkDebug(ctx, "Called provider defined Resource ImportState")
resp.Diagnostics.Append(importResp.Diagnostics...)
if resp.Diagnostics.HasError() {
return
}
if importResp.State.Raw.Equal(emptyState) {
resp.Diagnostics.AddError(
"Missing Resource Import State",
"An unexpected error was encountered when importing the resource. This is always a problem with the provider. Please give the following information to the provider developer:\n\n"+
"Resource ImportState method returned no State in response. If import is intentionally not supported, remove the Resource type ImportState method or return an error.",
)
return
}
resp.ImportedResources = []importedResource{
{
State: importResp.State,
TypeName: req.TypeName,
},
}
}
// ImportResourceState satisfies the tfprotov6.ProviderServer interface.
func (s *Server) ImportResourceState(ctx context.Context, req *tfprotov6.ImportResourceStateRequest) (*tfprotov6.ImportResourceStateResponse, error) {
ctx = s.registerContext(ctx)
resp := &importResourceStateResponse{}
s.importResourceState(ctx, req, resp)
return resp.toTfprotov6(ctx), nil
}