@@ -14,6 +14,7 @@ import (
14
14
"github.com/cloudflare/cloudflare-go/v5/option"
15
15
"github.com/cloudflare/cloudflare-go/v5/rules"
16
16
"github.com/cloudflare/terraform-provider-cloudflare/internal/apijson"
17
+ "github.com/cloudflare/terraform-provider-cloudflare/internal/importpath"
17
18
"github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
18
19
"github.com/hashicorp/terraform-plugin-framework/resource"
19
20
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -23,6 +24,7 @@ import (
23
24
// Ensure provider defined types fully satisfy framework interfaces.
24
25
var _ resource.ResourceWithConfigure = (* ListItemResource )(nil )
25
26
var _ resource.ResourceWithModifyPlan = (* ListItemResource )(nil )
27
+ var _ resource.ResourceWithImportState = (* ListItemResource )(nil )
26
28
27
29
func NewResource () resource.Resource {
28
30
return & ListItemResource {}
@@ -265,6 +267,55 @@ func (r *ListItemResource) Delete(ctx context.Context, req resource.DeleteReques
265
267
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
266
268
}
267
269
270
+ func (r * ListItemResource ) ImportState (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
271
+ var data * ListItemModel = new (ListItemModel )
272
+
273
+ path_account_id := ""
274
+ path_list_id := ""
275
+ path_item_id := ""
276
+ diags := importpath .ParseImportID (
277
+ req .ID ,
278
+ "<account_id>/<list_id>/<item_id>" ,
279
+ & path_account_id ,
280
+ & path_list_id ,
281
+ & path_item_id ,
282
+ )
283
+ resp .Diagnostics .Append (diags ... )
284
+ if resp .Diagnostics .HasError () {
285
+ return
286
+ }
287
+
288
+ data .AccountID = types .StringValue (path_account_id )
289
+ data .ListID = types .StringValue (path_list_id )
290
+ data .ID = types .StringValue (path_item_id )
291
+
292
+ res := new (http.Response )
293
+ env := ListItemResultEnvelope {* data }
294
+ _ , err := r .client .Rules .Lists .Items .Get (
295
+ ctx ,
296
+ path_list_id ,
297
+ path_item_id ,
298
+ rules.ListItemGetParams {
299
+ AccountID : cloudflare .F (path_account_id ),
300
+ },
301
+ option .WithResponseBodyInto (& res ),
302
+ option .WithMiddleware (logging .Middleware (ctx )),
303
+ )
304
+ if err != nil {
305
+ resp .Diagnostics .AddError ("failed to make http request" , err .Error ())
306
+ return
307
+ }
308
+ bytes , _ := io .ReadAll (res .Body )
309
+ err = apijson .Unmarshal (bytes , & env )
310
+ if err != nil {
311
+ resp .Diagnostics .AddError ("failed to deserialize http request" , err .Error ())
312
+ return
313
+ }
314
+ data = & env .Result
315
+
316
+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
317
+ }
318
+
268
319
func (r * ListItemResource ) ModifyPlan (_ context.Context , _ resource.ModifyPlanRequest , _ * resource.ModifyPlanResponse ) {
269
320
270
321
}
0 commit comments