Skip to content

Commit e4604df

Browse files
broswenAndrew Jeffery
authored andcommitted
fix(list_item) data source
1 parent b12e7f2 commit e4604df

File tree

4 files changed

+79
-72
lines changed

4 files changed

+79
-72
lines changed

internal/services/list_item/data_source.go

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ package list_item
55
import (
66
"context"
77
"fmt"
8+
"io"
9+
"net/http"
810

911
"github.com/cloudflare/cloudflare-go/v5"
12+
"github.com/cloudflare/cloudflare-go/v5/option"
1013
"github.com/hashicorp/terraform-plugin-framework/datasource"
14+
15+
"github.com/cloudflare/terraform-provider-cloudflare/internal/apijson"
16+
"github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
1117
)
1218

1319
type ListItemDataSource struct {
@@ -46,39 +52,39 @@ func (d *ListItemDataSource) Configure(ctx context.Context, req datasource.Confi
4652
func (d *ListItemDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
4753
var data *ListItemDataSourceModel
4854

49-
// resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
50-
51-
// if resp.Diagnostics.HasError() {
52-
// return
53-
// }
54-
55-
// params, diags := data.toReadParams(ctx)
56-
// resp.Diagnostics.Append(diags...)
57-
// if resp.Diagnostics.HasError() {
58-
// return
59-
// }
60-
61-
// res := new(http.Response)
62-
// env := ListItemResultDataSourceEnvelope{*data}
63-
// _, err := d.client.Rules.Lists.Items.Get(
64-
// ctx,
65-
// data.ListID.ValueString(),
66-
// data.ItemID.ValueString(),
67-
// params,
68-
// option.WithResponseBodyInto(&res),
69-
// option.WithMiddleware(logging.Middleware(ctx)),
70-
// )
71-
// if err != nil {
72-
// resp.Diagnostics.AddError("failed to make http request", err.Error())
73-
// return
74-
// }
75-
// bytes, _ := io.ReadAll(res.Body)
76-
// err = apijson.UnmarshalComputed(bytes, &env)
77-
// if err != nil {
78-
// resp.Diagnostics.AddError("failed to deserialize http request", err.Error())
79-
// return
80-
// }
81-
// data = &env.Result
55+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
56+
57+
if resp.Diagnostics.HasError() {
58+
return
59+
}
60+
61+
params, diags := data.toReadParams(ctx)
62+
resp.Diagnostics.Append(diags...)
63+
if resp.Diagnostics.HasError() {
64+
return
65+
}
66+
67+
res := new(http.Response)
68+
env := ListItemResultDataSourceEnvelope{*data}
69+
_, err := d.client.Rules.Lists.Items.Get(
70+
ctx,
71+
data.ListID.ValueString(),
72+
data.ItemID.ValueString(),
73+
params,
74+
option.WithResponseBodyInto(&res),
75+
option.WithMiddleware(logging.Middleware(ctx)),
76+
)
77+
if err != nil {
78+
resp.Diagnostics.AddError("failed to make http request", err.Error())
79+
return
80+
}
81+
bytes, _ := io.ReadAll(res.Body)
82+
err = apijson.UnmarshalComputed(bytes, &env)
83+
if err != nil {
84+
resp.Diagnostics.AddError("failed to deserialize http request", err.Error())
85+
return
86+
}
87+
data = &env.Result
8288

8389
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
8490
}

internal/services/list_item/data_source_model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ type ListItemResultDataSourceEnvelope struct {
1818

1919
type ListItemDataSourceModel struct {
2020
AccountID types.String `tfsdk:"account_id" path:"account_id,required"`
21-
ItemID types.String `tfsdk:"item_id" path:"item_id,required"`
2221
ListID types.String `tfsdk:"list_id" path:"list_id,required"`
22+
ItemID types.String `tfsdk:"item_id" path:"item_id,required"`
23+
ID types.String `tfsdk:"id" json:"id,computed" path:"item_id,computed"`
2324
ASN types.Int64 `tfsdk:"asn" json:"asn,computed"`
2425
Comment types.String `tfsdk:"comment" json:"comment,computed"`
2526
CreatedOn types.String `tfsdk:"created_on" json:"created_on,computed"`
26-
ID types.String `tfsdk:"id" json:"id,computed"`
2727
IP types.String `tfsdk:"ip" json:"ip,computed"`
2828
ModifiedOn types.String `tfsdk:"modified_on" json:"modified_on,computed"`
2929
Hostname customfield.NestedObject[ListItemHostnameDataSourceModel] `tfsdk:"hostname" json:"hostname,computed"`

internal/services/list_item/data_source_schema.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,17 @@ func DataSourceSchema(ctx context.Context) schema.Schema {
2121
Description: "The Account ID for this resource.",
2222
Required: true,
2323
},
24-
"item_id": schema.StringAttribute{
25-
Description: "Defines the unique ID of the item in the List.",
26-
Required: true,
27-
},
2824
"list_id": schema.StringAttribute{
2925
Description: "The unique ID of the list.",
3026
Required: true,
3127
},
32-
"asn": schema.Int64Attribute{
33-
Description: "Defines a non-negative 32 bit integer.",
34-
Computed: true,
35-
},
36-
"comment": schema.StringAttribute{
37-
Description: "Defines an informative summary of the list item.",
38-
Computed: true,
39-
},
40-
"created_on": schema.StringAttribute{
41-
Description: "The RFC 3339 timestamp of when the list was created.",
42-
Computed: true,
28+
"item_id": schema.StringAttribute{
29+
Description: "Defines the unique ID of the item in the List.",
30+
Required: true,
4331
},
32+
4433
"id": schema.StringAttribute{
45-
Description: "The unique ID of the list.",
46-
Computed: true,
47-
},
48-
"ip": schema.StringAttribute{
49-
Description: "An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.",
50-
Computed: true,
51-
},
52-
"modified_on": schema.StringAttribute{
53-
Description: "The RFC 3339 timestamp of when the list was last modified.",
34+
Description: "Defines the unique ID of the item in the List.",
5435
Computed: true,
5536
},
5637
"hostname": schema.SingleNestedAttribute{
@@ -67,6 +48,10 @@ func DataSourceSchema(ctx context.Context) schema.Schema {
6748
},
6849
},
6950
},
51+
"ip": schema.StringAttribute{
52+
Description: "An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.",
53+
Computed: true,
54+
},
7055
"redirect": schema.SingleNestedAttribute{
7156
Description: "The definition of the redirect.",
7257
Computed: true,
@@ -104,6 +89,22 @@ func DataSourceSchema(ctx context.Context) schema.Schema {
10489
},
10590
},
10691
},
92+
"asn": schema.Int64Attribute{
93+
Description: "Defines a non-negative 32 bit integer.",
94+
Computed: true,
95+
},
96+
"comment": schema.StringAttribute{
97+
Description: "Defines an informative summary of the list item.",
98+
Computed: true,
99+
},
100+
"created_on": schema.StringAttribute{
101+
Description: "The RFC 3339 timestamp of when the list was created.",
102+
Computed: true,
103+
},
104+
"modified_on": schema.StringAttribute{
105+
Description: "The RFC 3339 timestamp of when the list was last modified.",
106+
Computed: true,
107+
},
107108
},
108109
}
109110
}

internal/services/list_item/list_data_source_schema.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ func ListDataSourceSchema(ctx context.Context) schema.Schema {
4343
NestedObject: schema.NestedAttributeObject{
4444
Attributes: map[string]schema.Attribute{
4545
"id": schema.StringAttribute{
46-
Description: "The unique ID of the list.",
46+
Description: "Defines the unique ID of the item in the List.",
4747
Computed: true,
4848
},
49-
"created_on": schema.StringAttribute{
50-
Description: "The RFC 3339 timestamp of when the list was created.",
49+
"asn": schema.Int64Attribute{
50+
Description: "A non-negative 32 bit integer",
5151
Computed: true,
5252
},
53-
"ip": schema.StringAttribute{
54-
Description: "An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.",
53+
"comment": schema.StringAttribute{
54+
Description: "An informative summary of the list item.",
5555
Computed: true,
5656
},
57-
"modified_on": schema.StringAttribute{
58-
Description: "The RFC 3339 timestamp of when the list was last modified.",
57+
"created_on": schema.StringAttribute{
58+
Description: "The RFC 3339 timestamp of when the item was created.",
5959
Computed: true,
6060
},
61-
"comment": schema.StringAttribute{
62-
Description: "Defines an informative summary of the list item.",
61+
"modified_on": schema.StringAttribute{
62+
Description: "The RFC 3339 timestamp of when the item was last modified.",
6363
Computed: true,
6464
},
6565
"hostname": schema.SingleNestedAttribute{
@@ -76,6 +76,10 @@ func ListDataSourceSchema(ctx context.Context) schema.Schema {
7676
},
7777
},
7878
},
79+
"ip": schema.StringAttribute{
80+
Description: "An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.",
81+
Computed: true,
82+
},
7983
"redirect": schema.SingleNestedAttribute{
8084
Description: "The definition of the redirect.",
8185
Computed: true,
@@ -113,10 +117,6 @@ func ListDataSourceSchema(ctx context.Context) schema.Schema {
113117
},
114118
},
115119
},
116-
"asn": schema.Int64Attribute{
117-
Description: "Defines a non-negative 32 bit integer.",
118-
Computed: true,
119-
},
120120
},
121121
},
122122
},

0 commit comments

Comments
 (0)