|
| 1 | +package reservediptypes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 8 | + "github.com/linode/linodego" |
| 9 | + "github.com/linode/terraform-provider-linode/v3/linode/helper" |
| 10 | + "github.com/linode/terraform-provider-linode/v3/linode/helper/frameworkfilter" |
| 11 | +) |
| 12 | + |
| 13 | +// priceModel is the tfsdk-tagged struct for a single price entry. |
| 14 | +type priceModel struct { |
| 15 | + Hourly types.Float64 `tfsdk:"hourly"` |
| 16 | + Monthly types.Float64 `tfsdk:"monthly"` |
| 17 | +} |
| 18 | + |
| 19 | +// regionPriceModel is the tfsdk-tagged struct for a region-specific price entry. |
| 20 | +type regionPriceModel struct { |
| 21 | + ID types.String `tfsdk:"id"` |
| 22 | + Hourly types.Float64 `tfsdk:"hourly"` |
| 23 | + Monthly types.Float64 `tfsdk:"monthly"` |
| 24 | +} |
| 25 | + |
| 26 | +type dataSourceModel struct { |
| 27 | + ID types.String `tfsdk:"id"` |
| 28 | + Label types.String `tfsdk:"label"` |
| 29 | + Price types.List `tfsdk:"price"` |
| 30 | + RegionPrices types.List `tfsdk:"region_prices"` |
| 31 | +} |
| 32 | + |
| 33 | +func (data *dataSourceModel) parseReservedIPType(ctx context.Context, ipType *linodego.ReservedIPType) diag.Diagnostics { |
| 34 | + data.ID = types.StringValue(ipType.ID) |
| 35 | + data.Label = types.StringValue(ipType.Label) |
| 36 | + |
| 37 | + price, diags := types.ListValueFrom(ctx, helper.PriceObjectType, []priceModel{ |
| 38 | + { |
| 39 | + Hourly: types.Float64Value(ipType.Price.Hourly), |
| 40 | + Monthly: types.Float64Value(ipType.Price.Monthly), |
| 41 | + }, |
| 42 | + }) |
| 43 | + if diags.HasError() { |
| 44 | + return diags |
| 45 | + } |
| 46 | + data.Price = price |
| 47 | + |
| 48 | + regionPrices := make([]regionPriceModel, len(ipType.RegionPrices)) |
| 49 | + for i, rp := range ipType.RegionPrices { |
| 50 | + regionPrices[i] = regionPriceModel{ |
| 51 | + ID: types.StringValue(rp.ID), |
| 52 | + Hourly: types.Float64Value(rp.Hourly), |
| 53 | + Monthly: types.Float64Value(rp.Monthly), |
| 54 | + } |
| 55 | + } |
| 56 | + rpList, diags := types.ListValueFrom(ctx, helper.RegionPriceObjectType, regionPrices) |
| 57 | + if diags.HasError() { |
| 58 | + return diags |
| 59 | + } |
| 60 | + data.RegionPrices = rpList |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +type reservedIPTypeFilterModel struct { |
| 66 | + ID types.String `tfsdk:"id"` |
| 67 | + Order types.String `tfsdk:"order"` |
| 68 | + OrderBy types.String `tfsdk:"order_by"` |
| 69 | + Filters frameworkfilter.FiltersModelType `tfsdk:"filter"` |
| 70 | + Types []dataSourceModel `tfsdk:"types"` |
| 71 | +} |
| 72 | + |
| 73 | +func (model *reservedIPTypeFilterModel) parseReservedIPTypes(ctx context.Context, ipTypes []linodego.ReservedIPType) diag.Diagnostics { |
| 74 | + result := make([]dataSourceModel, len(ipTypes)) |
| 75 | + |
| 76 | + for i := range ipTypes { |
| 77 | + var m dataSourceModel |
| 78 | + |
| 79 | + diags := m.parseReservedIPType(ctx, &ipTypes[i]) |
| 80 | + if diags.HasError() { |
| 81 | + return diags |
| 82 | + } |
| 83 | + |
| 84 | + result[i] = m |
| 85 | + } |
| 86 | + |
| 87 | + model.Types = result |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
0 commit comments