Skip to content

Commit cc36aa1

Browse files
Merge pull request #5852 from broswen/bswenson/list-item-import
implement import for list_item
2 parents a944ca1 + 2d1f2e1 commit cc36aa1

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

internal/services/list_item/resource.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/cloudflare/cloudflare-go/v5/option"
1515
"github.com/cloudflare/cloudflare-go/v5/rules"
1616
"github.com/cloudflare/terraform-provider-cloudflare/internal/apijson"
17+
"github.com/cloudflare/terraform-provider-cloudflare/internal/importpath"
1718
"github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
1819
"github.com/hashicorp/terraform-plugin-framework/resource"
1920
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -23,6 +24,7 @@ import (
2324
// Ensure provider defined types fully satisfy framework interfaces.
2425
var _ resource.ResourceWithConfigure = (*ListItemResource)(nil)
2526
var _ resource.ResourceWithModifyPlan = (*ListItemResource)(nil)
27+
var _ resource.ResourceWithImportState = (*ListItemResource)(nil)
2628

2729
func NewResource() resource.Resource {
2830
return &ListItemResource{}
@@ -265,6 +267,55 @@ func (r *ListItemResource) Delete(ctx context.Context, req resource.DeleteReques
265267
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
266268
}
267269

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+
268319
func (r *ListItemResource) ModifyPlan(_ context.Context, _ resource.ModifyPlanRequest, _ *resource.ModifyPlanResponse) {
269320

270321
}

internal/services/list_item/resource_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
99
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
10+
"github.com/hashicorp/terraform-plugin-testing/terraform"
1011

1112
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1213
)
@@ -33,6 +34,49 @@ func TestAccCloudflareListItem_Basic(t *testing.T) {
3334
})
3435
}
3536

37+
func TestAccCloudflareListItem_Import(t *testing.T) {
38+
rnd := utils.GenerateRandomResourceName()
39+
itemName := fmt.Sprintf("cloudflare_list_item.%s", rnd)
40+
listName := fmt.Sprintf("cloudflare_list.%s", rnd)
41+
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
42+
ip := "192.0.2.0"
43+
44+
resource.ParallelTest(t, resource.TestCase{
45+
PreCheck: func() {
46+
acctest.TestAccPreCheck_AccountID(t)
47+
acctest.TestAccPreCheck_Credentials(t)
48+
},
49+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
50+
Steps: []resource.TestStep{
51+
{
52+
Config: testAccCheckCloudflareIPListItem(rnd, rnd, rnd, accountID),
53+
Check: resource.ComposeTestCheckFunc(
54+
resource.TestCheckResourceAttr(itemName, "ip", ip),
55+
resource.TestCheckResourceAttr(itemName, "comment", rnd),
56+
),
57+
},
58+
{
59+
ResourceName: itemName,
60+
ImportState: true,
61+
ImportStateVerify: true,
62+
ImportStateIdFunc: func(s *terraform.State) (string, error) {
63+
rs, ok := s.RootModule().Resources[listName]
64+
if !ok {
65+
return "", fmt.Errorf("list resource not found: %s", listName)
66+
}
67+
listID := rs.Primary.ID
68+
rs, ok = s.RootModule().Resources[itemName]
69+
if !ok {
70+
return "", fmt.Errorf("list_item resource not found: %s", itemName)
71+
}
72+
itemID := rs.Primary.ID
73+
return fmt.Sprintf("%s/%s/%s", accountID, listID, itemID), nil
74+
},
75+
},
76+
},
77+
})
78+
}
79+
3680
func TestAccCloudflareListItem_MultipleItems(t *testing.T) {
3781
t.Skip("FIXME: Getting rate limited. Probably causing the cascading failures with the rest.")
3882
rnd := utils.GenerateRandomResourceName()

0 commit comments

Comments
 (0)