From 81cc2c87b22fbead893847027982c65e627ff7ac Mon Sep 17 00:00:00 2001 From: Levon Tarver Date: Tue, 9 Apr 2024 13:13:50 -0500 Subject: [PATCH 1/5] Add address lot data source --- internal/provider/data_source_address_lot.go | 143 ++++++++++++++++++ .../provider/data_source_address_lot_test.go | 61 ++++++++ internal/provider/provider.go | 1 + 3 files changed, 205 insertions(+) create mode 100644 internal/provider/data_source_address_lot.go create mode 100644 internal/provider/data_source_address_lot_test.go diff --git a/internal/provider/data_source_address_lot.go b/internal/provider/data_source_address_lot.go new file mode 100644 index 0000000..2ecdaab --- /dev/null +++ b/internal/provider/data_source_address_lot.go @@ -0,0 +1,143 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +package provider + +import ( + "context" + "fmt" + + "github.com/google/uuid" + "github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/oxidecomputer/oxide.go/oxide" +) + +var ( + _ datasource.DataSource = (*networkingAddressLotsDataSource)(nil) + _ datasource.DataSourceWithConfigure = (*networkingAddressLotsDataSource)(nil) +) + +// NewNetworkingAddressLotsDataSource initialises an images datasource +func NewNetworkingAddressLotsDataSource() datasource.DataSource { + return &networkingAddressLotsDataSource{} +} + +type networkingAddressLotsDataSource struct { + client *oxide.Client +} + +type networkingAddressLotsDatasourceModel struct { + ID types.String `tfsdk:"id"` + Timeouts timeouts.Value `tfsdk:"timeouts"` + AddressLots []addressLotDatasourceModel `tfsdk:"address_lots"` +} + +type addressLotDatasourceModel struct { + ID types.String `tfsdk:"id"` + Description types.String `tfsdk:"description"` + Kind types.String `tfsdk:"kind"` + Name types.String `tfsdk:"name"` +} + +func (d *networkingAddressLotsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = "oxide_networking_address_lots" +} + +// Configure adds the provider configured client to the data source. +func (d *networkingAddressLotsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.client = req.ProviderData.(*oxide.Client) +} + +func (d *networkingAddressLotsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + }, + "timeouts": timeouts.Attributes(ctx), + "address_lots": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + Computed: true, + Description: "Address Lot ID", + }, + "description": schema.StringAttribute{ + Computed: true, + Description: "human-readable free-form text about an Address Lot", + }, + "kind": schema.StringAttribute{ + Computed: true, + Description: "Desired use of Address Lot", + }, + "name": schema.StringAttribute{ + Computed: true, + Description: "unique, mutable, user-controlled identifier for each resource", + }, + }, + }, + }, + }, + } +} + +func (d *networkingAddressLotsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var state networkingAddressLotsDatasourceModel + + // Read Terraform configuration data into the model + resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) + if resp.Diagnostics.HasError() { + return + } + + readTimeout, diags := state.Timeouts.Read(ctx, defaultTimeout()) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + ctx, cancel := context.WithTimeout(ctx, readTimeout) + defer cancel() + + params := oxide.NetworkingAddressLotListParams{} + + addressLots, err := d.client.NetworkingAddressLotListAllPages(ctx, params) + if err != nil { + resp.Diagnostics.AddError( + "Unable to read address lots:", + "API error: "+err.Error(), + ) + return + } + + tflog.Trace(ctx, fmt.Sprintf("read all address lots"), map[string]any{"success": true}) + + // Set a unique ID for the datasource payload + state.ID = types.StringValue(uuid.New().String()) + + // Map response body to model + for _, lot := range addressLots { + addressLotState := addressLotDatasourceModel{ + ID: types.StringValue(lot.Id), + Description: types.StringValue(lot.Description), + Kind: types.StringValue(string(lot.Kind)), + Name: types.StringValue(string(lot.Name)), + } + state.AddressLots = append(state.AddressLots, addressLotState) + } + + // Save state into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) + if resp.Diagnostics.HasError() { + return + } +} diff --git a/internal/provider/data_source_address_lot_test.go b/internal/provider/data_source_address_lot_test.go new file mode 100644 index 0000000..3e5acba --- /dev/null +++ b/internal/provider/data_source_address_lot_test.go @@ -0,0 +1,61 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +package provider + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +type dataSourceNetworkingAddressLotConfig struct { + BlockName string +} + +var datasourceNetworkingAddressLotsConfigTpl = ` +data "oxide_networking_address_lots" "{{.BlockName}}" { + timeouts = { + read = "1m" + } +} +` + +func TestAccCloudDataSourceNetworkingAddressLots_full(t *testing.T) { + blockName := newBlockName("datasource-networking-address-lots") + config, err := parsedAccConfig( + dataSourceNetworkingAddressLotConfig{ + BlockName: blockName, + }, + datasourceNetworkingAddressLotsConfigTpl, + ) + if err != nil { + t.Errorf("error parsing config template data: %e", err) + } + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(), + Steps: []resource.TestStep{ + { + Config: config, + Check: checkDataSourceNetworkingAddressLots( + fmt.Sprintf("data.oxide_networking_address_lots.%s", blockName), + ), + }, + }, + }) +} + +func checkDataSourceNetworkingAddressLots(dataName string) resource.TestCheckFunc { + return resource.ComposeAggregateTestCheckFunc([]resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(dataName, "id"), + resource.TestCheckResourceAttr(dataName, "timeouts.read", "1m"), + resource.TestCheckResourceAttrSet(dataName, "address_lots.0.id"), + resource.TestCheckResourceAttrSet(dataName, "address_lots.0.description"), + resource.TestCheckResourceAttrSet(dataName, "address_lots.0.kind"), + resource.TestCheckResourceAttrSet(dataName, "address_lots.0.name"), + }...) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 0f8012e..d516a84 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -140,6 +140,7 @@ func (p *oxideProvider) DataSources(_ context.Context) []func() datasource.DataS NewSSHKeyDataSource, NewVPCDataSource, NewVPCSubnetDataSource, + NewNetworkingAddressLotsDataSource, } } From 0b7b5d850cfe1306aea5d9febbfb1299c4b2433b Mon Sep 17 00:00:00 2001 From: Levon Tarver Date: Tue, 9 Apr 2024 14:07:08 -0500 Subject: [PATCH 2/5] golangci lint fix --- internal/provider/data_source_address_lot.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/provider/data_source_address_lot.go b/internal/provider/data_source_address_lot.go index 2ecdaab..67bf525 100644 --- a/internal/provider/data_source_address_lot.go +++ b/internal/provider/data_source_address_lot.go @@ -6,7 +6,6 @@ package provider import ( "context" - "fmt" "github.com/google/uuid" "github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts" @@ -119,7 +118,7 @@ func (d *networkingAddressLotsDataSource) Read(ctx context.Context, req datasour return } - tflog.Trace(ctx, fmt.Sprintf("read all address lots"), map[string]any{"success": true}) + tflog.Trace(ctx, "read all address lots", map[string]any{"success": true}) // Set a unique ID for the datasource payload state.ID = types.StringValue(uuid.New().String()) From d8875f8bdabdb118dd0c6dfec83150b59319668c Mon Sep 17 00:00:00 2001 From: Levon Tarver Date: Tue, 9 Apr 2024 14:19:53 -0500 Subject: [PATCH 3/5] rename files for consistency --- .../{data_source_address_lot.go => data_source_address_lots.go} | 0 ...ource_address_lot_test.go => data_source_address_lots_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename internal/provider/{data_source_address_lot.go => data_source_address_lots.go} (100%) rename internal/provider/{data_source_address_lot_test.go => data_source_address_lots_test.go} (100%) diff --git a/internal/provider/data_source_address_lot.go b/internal/provider/data_source_address_lots.go similarity index 100% rename from internal/provider/data_source_address_lot.go rename to internal/provider/data_source_address_lots.go diff --git a/internal/provider/data_source_address_lot_test.go b/internal/provider/data_source_address_lots_test.go similarity index 100% rename from internal/provider/data_source_address_lot_test.go rename to internal/provider/data_source_address_lots_test.go From bc95c058a7ae986253f6143b30ab51b4774a7b3c Mon Sep 17 00:00:00 2001 From: Levon Tarver Date: Tue, 9 Apr 2024 14:21:14 -0500 Subject: [PATCH 4/5] make capitalization consistent --- internal/provider/data_source_address_lots.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/provider/data_source_address_lots.go b/internal/provider/data_source_address_lots.go index 67bf525..263e35c 100644 --- a/internal/provider/data_source_address_lots.go +++ b/internal/provider/data_source_address_lots.go @@ -73,7 +73,7 @@ func (d *networkingAddressLotsDataSource) Schema(ctx context.Context, req dataso }, "description": schema.StringAttribute{ Computed: true, - Description: "human-readable free-form text about an Address Lot", + Description: "Human-readable free-form text about an Address Lot", }, "kind": schema.StringAttribute{ Computed: true, @@ -81,7 +81,7 @@ func (d *networkingAddressLotsDataSource) Schema(ctx context.Context, req dataso }, "name": schema.StringAttribute{ Computed: true, - Description: "unique, mutable, user-controlled identifier for each resource", + Description: "Unique, mutable, user-controlled identifier for each resource", }, }, }, From 44752d5613261f694690fdeb3dfd5c503230c76e Mon Sep 17 00:00:00 2001 From: Levon Tarver Date: Tue, 9 Apr 2024 14:22:35 -0500 Subject: [PATCH 5/5] rename test function --- internal/provider/data_source_address_lots_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/provider/data_source_address_lots_test.go b/internal/provider/data_source_address_lots_test.go index 3e5acba..83363ff 100644 --- a/internal/provider/data_source_address_lots_test.go +++ b/internal/provider/data_source_address_lots_test.go @@ -23,7 +23,7 @@ data "oxide_networking_address_lots" "{{.BlockName}}" { } ` -func TestAccCloudDataSourceNetworkingAddressLots_full(t *testing.T) { +func TestAccSiloDataSourceNetworkingAddressLots_full(t *testing.T) { blockName := newBlockName("datasource-networking-address-lots") config, err := parsedAccConfig( dataSourceNetworkingAddressLotConfig{