|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package provider |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 16 | + "github.com/oxidecomputer/oxide.go/oxide" |
| 17 | +) |
| 18 | + |
| 19 | +var _ datasource.DataSource = (*addressLotDataSource)(nil) |
| 20 | +var _ datasource.DataSourceWithConfigure = (*addressLotDataSource)(nil) |
| 21 | + |
| 22 | +type addressLotDataSource struct { |
| 23 | + client *oxide.Client |
| 24 | +} |
| 25 | + |
| 26 | +type addressLotDataSourceModel struct { |
| 27 | + Blocks []addressLotDataSourceBlockModel `tfsdk:"blocks"` |
| 28 | + Description types.String `tfsdk:"description"` |
| 29 | + Kind types.String `tfsdk:"kind"` |
| 30 | + Name types.String `tfsdk:"name"` |
| 31 | + ID types.String `tfsdk:"id"` |
| 32 | + TimeCreated types.String `tfsdk:"time_created"` |
| 33 | + TimeModified types.String `tfsdk:"time_modified"` |
| 34 | + Timeouts timeouts.Value `tfsdk:"timeouts"` |
| 35 | +} |
| 36 | + |
| 37 | +type addressLotDataSourceBlockModel struct { |
| 38 | + ID types.String `tfsdk:"id"` |
| 39 | + FirstAddress types.String `tfsdk:"first_address"` |
| 40 | + LastAddress types.String `tfsdk:"last_address"` |
| 41 | +} |
| 42 | + |
| 43 | +// NewAddressLotDataSource initialises an address_lot datasource. |
| 44 | +func NewAddressLotDataSource() datasource.DataSource { |
| 45 | + return &addressLotDataSource{} |
| 46 | +} |
| 47 | + |
| 48 | +func (d *addressLotDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 49 | + resp.TypeName = "oxide_address_lot" |
| 50 | +} |
| 51 | + |
| 52 | +// Configure adds the provider configured client to the data source. |
| 53 | +func (d *addressLotDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { |
| 54 | + if req.ProviderData == nil { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + d.client = req.ProviderData.(*oxide.Client) |
| 59 | +} |
| 60 | + |
| 61 | +func (d *addressLotDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 62 | + resp.Schema = schema.Schema{ |
| 63 | + Attributes: map[string]schema.Attribute{ |
| 64 | + "name": schema.StringAttribute{ |
| 65 | + Required: true, |
| 66 | + Description: "Name of the address lot.", |
| 67 | + }, |
| 68 | + "description": schema.StringAttribute{ |
| 69 | + Computed: true, |
| 70 | + Description: "Description for the address lot.", |
| 71 | + }, |
| 72 | + "kind": schema.StringAttribute{ |
| 73 | + Computed: true, |
| 74 | + Description: "Kind for the address lot.", |
| 75 | + }, |
| 76 | + "blocks": schema.SetNestedAttribute{ |
| 77 | + Computed: true, |
| 78 | + NestedObject: schema.NestedAttributeObject{ |
| 79 | + Attributes: map[string]schema.Attribute{ |
| 80 | + "id": schema.StringAttribute{ |
| 81 | + Description: "ID of the address lot block.", |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "first_address": schema.StringAttribute{ |
| 85 | + Description: "First address in the lot.", |
| 86 | + Computed: true, |
| 87 | + }, |
| 88 | + "last_address": schema.StringAttribute{ |
| 89 | + Description: "Last address in the lot.", |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + "id": schema.StringAttribute{ |
| 96 | + Computed: true, |
| 97 | + Description: "Unique, immutable, system-controlled identifier of the address lot.", |
| 98 | + }, |
| 99 | + "time_created": schema.StringAttribute{ |
| 100 | + Computed: true, |
| 101 | + Description: "Timestamp of when this address lot was created.", |
| 102 | + }, |
| 103 | + "time_modified": schema.StringAttribute{ |
| 104 | + Computed: true, |
| 105 | + Description: "Timestamp of when this address lot was last modified.", |
| 106 | + }, |
| 107 | + "timeouts": timeouts.Attributes(ctx), |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (d *addressLotDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 113 | + var state addressLotDataSourceModel |
| 114 | + |
| 115 | + // Read Terraform configuration data into the model |
| 116 | + resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) |
| 117 | + if resp.Diagnostics.HasError() { |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + readTimeout, diags := state.Timeouts.Read(ctx, defaultTimeout()) |
| 122 | + resp.Diagnostics.Append(diags...) |
| 123 | + if resp.Diagnostics.HasError() { |
| 124 | + return |
| 125 | + } |
| 126 | + ctx, cancel := context.WithTimeout(ctx, readTimeout) |
| 127 | + defer cancel() |
| 128 | + |
| 129 | + addressLot, err := d.client.NetworkingAddressLotView(ctx, oxide.NetworkingAddressLotViewParams{ |
| 130 | + AddressLot: oxide.NameOrId(state.Name.ValueString()), |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + resp.Diagnostics.AddError( |
| 134 | + "Unable to read address lot:", |
| 135 | + "API error: "+err.Error(), |
| 136 | + ) |
| 137 | + return |
| 138 | + } |
| 139 | + lot := addressLot.Lot |
| 140 | + tflog.Trace(ctx, fmt.Sprintf("read address lot with ID: %v", lot.Id), map[string]any{"success": true}) |
| 141 | + |
| 142 | + state.ID = types.StringValue(lot.Id) |
| 143 | + state.Name = types.StringValue(string(lot.Name)) |
| 144 | + state.Kind = types.StringValue(string(lot.Kind)) |
| 145 | + state.Description = types.StringValue(lot.Description) |
| 146 | + state.TimeCreated = types.StringValue(lot.TimeCreated.String()) |
| 147 | + state.TimeModified = types.StringValue(lot.TimeCreated.String()) |
| 148 | + |
| 149 | + blockModels := make([]addressLotDataSourceBlockModel, len(addressLot.Blocks)) |
| 150 | + for index, item := range addressLot.Blocks { |
| 151 | + blockModels[index] = addressLotDataSourceBlockModel{ |
| 152 | + ID: types.StringValue(item.Id), |
| 153 | + FirstAddress: types.StringValue(item.FirstAddress), |
| 154 | + LastAddress: types.StringValue(item.LastAddress), |
| 155 | + } |
| 156 | + } |
| 157 | + state.Blocks = blockModels |
| 158 | + |
| 159 | + // Save state into Terraform state |
| 160 | + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) |
| 161 | + if resp.Diagnostics.HasError() { |
| 162 | + return |
| 163 | + } |
| 164 | +} |
0 commit comments