Skip to content
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

Address Lot Datasource and Resource #304

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
143 changes: 143 additions & 0 deletions internal/provider/data_source_address_lot.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the name of the file to data_source_address_lots.go so it's consistent with the data source name :)

Original file line number Diff line number Diff line change
@@ -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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
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})

Check failure on line 122 in internal/provider/data_source_address_lot.go

View workflow job for this annotation

GitHub Actions / build-test

S1039: unnecessary use of fmt.Sprintf (gosimple)

// 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
}
}
61 changes: 61 additions & 0 deletions internal/provider/data_source_address_lot_test.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about the name change on this file

Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prefix is to differentiate between "cloud" and "silo" endpoints. I use this manly for testing on the colo rack, where the user on the GH action doesn't have admin privileges

Suggested change
func TestAccCloudDataSourceNetworkingAddressLots_full(t *testing.T) {
func TestAccSiloDataSourceNetworkingAddressLots_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"),
}...)
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (p *oxideProvider) DataSources(_ context.Context) []func() datasource.DataS
NewSSHKeyDataSource,
NewVPCDataSource,
NewVPCSubnetDataSource,
NewNetworkingAddressLotsDataSource,
}
}

Expand Down
Loading