-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}, | ||||||
"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 | ||||||
} | ||||||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
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"), | ||||||
}...) | ||||||
} |
There was a problem hiding this comment.
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 :)