diff --git a/internal/services/list_item/data_source.go b/internal/services/list_item/data_source.go index ae30b5e460..04827c5c48 100644 --- a/internal/services/list_item/data_source.go +++ b/internal/services/list_item/data_source.go @@ -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 { @@ -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)...) } diff --git a/internal/services/list_item/data_source_model.go b/internal/services/list_item/data_source_model.go index 90f2aee370..5a9f11d799 100644 --- a/internal/services/list_item/data_source_model.go +++ b/internal/services/list_item/data_source_model.go @@ -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"` diff --git a/internal/services/list_item/data_source_schema.go b/internal/services/list_item/data_source_schema.go index 65fc910681..f0dbce3b7d 100644 --- a/internal/services/list_item/data_source_schema.go +++ b/internal/services/list_item/data_source_schema.go @@ -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, @@ -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, + }, }, } } diff --git a/internal/services/list_item/list_data_source_schema.go b/internal/services/list_item/list_data_source_schema.go index c761ac5e3d..c03d5eefdd 100644 --- a/internal/services/list_item/list_data_source_schema.go +++ b/internal/services/list_item/list_data_source_schema.go @@ -47,7 +47,7 @@ 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{ @@ -55,11 +55,11 @@ func ListDataSourceSchema(ctx context.Context) schema.Schema { 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{