@@ -21,7 +21,10 @@ import (
21
21
mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map"
22
22
)
23
23
24
- var _ resource.Resource = (* petResource )(nil )
24
+ var (
25
+ _ resource.Resource = (* petResource )(nil )
26
+ _ resource.ResourceWithImportState = (* petResource )(nil )
27
+ )
25
28
26
29
func NewPetResource () resource.Resource {
27
30
return & petResource {}
@@ -151,6 +154,43 @@ func (r *petResource) Update(ctx context.Context, req resource.UpdateRequest, re
151
154
func (r * petResource ) Delete (ctx context.Context , req resource.DeleteRequest , resp * resource.DeleteResponse ) {
152
155
}
153
156
157
+ func (r * petResource ) ImportState (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
158
+ parts := strings .Split (req .ID , "," )
159
+ if len (parts ) != 3 {
160
+ resp .Diagnostics .AddError (
161
+ "Unexpected Import Identifier" ,
162
+ fmt .Sprintf ("Expected import identifier with format: pet_name,separator,prefix. Got: %q" , req .ID ),
163
+ )
164
+ return
165
+ }
166
+
167
+ id , separator , prefix := parts [0 ], parts [1 ], parts [2 ]
168
+ if separator == "" {
169
+ separator = "-"
170
+ }
171
+
172
+ nameLength := len (strings .Split (id , separator ))
173
+
174
+ prefixVal := types .StringNull ()
175
+ if prefix != "" {
176
+ prefixVal = types .StringValue (prefix )
177
+ }
178
+
179
+ state := petModelV0 {
180
+ ID : types .StringValue (id ),
181
+ Length : types .Int64Value (int64 (nameLength )),
182
+ Prefix : prefixVal ,
183
+ Separator : types .StringValue (separator ),
184
+ Keepers : types .MapNull (types .StringType ),
185
+ }
186
+
187
+ diags := resp .State .Set (ctx , & state )
188
+ resp .Diagnostics .Append (diags ... )
189
+ if resp .Diagnostics .HasError () {
190
+ return
191
+ }
192
+ }
193
+
154
194
type petModelV0 struct {
155
195
ID types.String `tfsdk:"id"`
156
196
Keepers types.Map `tfsdk:"keepers"`
0 commit comments