Skip to content

fix(list_item): Data source #5874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions internal/services/list_item/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ package list_item
import (
"context"
"fmt"
"io"
"net/http"

"github.com/cloudflare/cloudflare-go/v5"
"github.com/cloudflare/cloudflare-go/v5/option"
"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/cloudflare/terraform-provider-cloudflare/internal/apijson"
"github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
)

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

// resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

// if resp.Diagnostics.HasError() {
// return
// }

// params, diags := data.toReadParams(ctx)
// resp.Diagnostics.Append(diags...)
// if resp.Diagnostics.HasError() {
// return
// }

// res := new(http.Response)
// env := ListItemResultDataSourceEnvelope{*data}
// _, err := d.client.Rules.Lists.Items.Get(
// ctx,
// data.ListID.ValueString(),
// data.ItemID.ValueString(),
// params,
// option.WithResponseBodyInto(&res),
// option.WithMiddleware(logging.Middleware(ctx)),
// )
// if err != nil {
// resp.Diagnostics.AddError("failed to make http request", err.Error())
// return
// }
// bytes, _ := io.ReadAll(res.Body)
// err = apijson.UnmarshalComputed(bytes, &env)
// if err != nil {
// resp.Diagnostics.AddError("failed to deserialize http request", err.Error())
// return
// }
// data = &env.Result
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

params, diags := data.toReadParams(ctx)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

res := new(http.Response)
env := ListItemResultDataSourceEnvelope{*data}
_, err := d.client.Rules.Lists.Items.Get(
ctx,
data.ListID.ValueString(),
data.ItemID.ValueString(),
params,
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
)
if err != nil {
resp.Diagnostics.AddError("failed to make http request", err.Error())
return
}
bytes, _ := io.ReadAll(res.Body)
err = apijson.UnmarshalComputed(bytes, &env)
if err != nil {
resp.Diagnostics.AddError("failed to deserialize http request", err.Error())
return
}
data = &env.Result

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
4 changes: 2 additions & 2 deletions internal/services/list_item/data_source_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type ListItemResultDataSourceEnvelope struct {

type ListItemDataSourceModel struct {
AccountID types.String `tfsdk:"account_id" path:"account_id,required"`
ItemID types.String `tfsdk:"item_id" path:"item_id,required"`
ListID types.String `tfsdk:"list_id" path:"list_id,required"`
ItemID types.String `tfsdk:"item_id" path:"item_id,required"`
ID types.String `tfsdk:"id" json:"id,computed" path:"item_id,computed"`
ASN types.Int64 `tfsdk:"asn" json:"asn,computed"`
Comment types.String `tfsdk:"comment" json:"comment,computed"`
CreatedOn types.String `tfsdk:"created_on" json:"created_on,computed"`
ID types.String `tfsdk:"id" json:"id,computed"`
IP types.String `tfsdk:"ip" json:"ip,computed"`
ModifiedOn types.String `tfsdk:"modified_on" json:"modified_on,computed"`
Hostname customfield.NestedObject[ListItemHostnameDataSourceModel] `tfsdk:"hostname" json:"hostname,computed"`
Expand Down
35 changes: 20 additions & 15 deletions internal/services/list_item/data_source_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,15 @@ func DataSourceSchema(ctx context.Context) schema.Schema {
Description: "The Account ID for this resource.",
Required: true,
},
"item_id": schema.StringAttribute{
Description: "Defines the unique ID of the item in the List.",
Required: true,
},
"list_id": schema.StringAttribute{
Description: "The unique ID of the list.",
Required: true,
},
"asn": schema.Int64Attribute{
Description: "Defines a non-negative 32 bit integer.",
Computed: true,
},
"comment": schema.StringAttribute{
Description: "Defines an informative summary of the list item.",
Computed: true,
},
"created_on": schema.StringAttribute{
Description: "The RFC 3339 timestamp of when the list was created.",
Computed: true,
"item_id": schema.StringAttribute{
Description: "Defines the unique ID of the item in the List.",
Required: true,
},

"id": schema.StringAttribute{
Description: "Defines the unique ID of the item in the List.",
Computed: true,
Expand Down Expand Up @@ -104,6 +93,22 @@ func DataSourceSchema(ctx context.Context) schema.Schema {
},
},
},
"asn": schema.Int64Attribute{
Description: "Defines a non-negative 32 bit integer.",
Computed: true,
},
"comment": schema.StringAttribute{
Description: "Defines an informative summary of the list item.",
Computed: true,
},
"created_on": schema.StringAttribute{
Description: "The RFC 3339 timestamp of when the list was created.",
Computed: true,
},
"modified_on": schema.StringAttribute{
Description: "The RFC 3339 timestamp of when the list was last modified.",
Computed: true,
},
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/services/list_item/list_data_source_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func ListDataSourceSchema(ctx context.Context) schema.Schema {
Computed: true,
},
"created_on": schema.StringAttribute{
Description: "The RFC 3339 timestamp of when the list was created.",
Description: "The RFC 3339 timestamp of when the item was created.",
Computed: true,
},
"ip": schema.StringAttribute{
Description: "An IPv4 address, an IPv4 CIDR, an IPv6 address, or an IPv6 CIDR.",
Computed: true,
},
"modified_on": schema.StringAttribute{
Description: "The RFC 3339 timestamp of when the list was last modified.",
Description: "The RFC 3339 timestamp of when the item was last modified.",
Computed: true,
},
"comment": schema.StringAttribute{
Description: "Defines an informative summary of the list item.",
Description: "Defines an informative summary of the list item.",
Computed: true,
},
"hostname": schema.SingleNestedAttribute{
Expand Down