Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions .changelog/0.18.0.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ title = ""
description = ""

[[features]]
title = ""
description = ""
title = "New resource"
description = "`oxide_address_lot` [#489](https://github.com/oxidecomputer/terraform-provider-oxide/pull/489)."

[[features]]
title = "New data source"
description = "`oxide_address_lot` [#489](https://github.com/oxidecomputer/terraform-provider-oxide/pull/489)."


[[enhancements]]
title = ""
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ terrafmt: tools
@ echo "-> Running terraform docs codeblocks linter"
@ find ./docs -type f -name "*.md" -exec $(GOBIN)/terrafmt diff -f {} \;

.PHONY: terrafmt-fmt
terrafmt-fmt: tools
@ echo "-> Running terraform docs codeblocks linter"
@ find ./docs -type f -name "*.md" -exec $(GOBIN)/terrafmt fmt -f {} \;

configfmt:
@ echo "-> Running terraform linters on .tf files"
@ terraform fmt -write=false -recursive -check
Expand Down
50 changes: 50 additions & 0 deletions docs/data-sources/address_lot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "oxide_address_lot Data Source - terraform-provider-oxide"
subcategory: ""
description: |-

---

# oxide_address_lot (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the address lot.

### Optional

- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))

### Read-Only

- `blocks` (Attributes Set) (see [below for nested schema](#nestedatt--blocks))
- `description` (String) Description for the address lot.
- `id` (String) Unique, immutable, system-controlled identifier of the address lot.
- `kind` (String) Kind for the address lot.
- `time_created` (String) Timestamp of when this address lot was created.
- `time_modified` (String) Timestamp of when this address lot was last modified.

<a id="nestedatt--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).


<a id="nestedatt--blocks"></a>
### Nested Schema for `blocks`

Read-Only:

- `first_address` (String) First address in the lot.
- `id` (String) ID of the address lot block.
- `last_address` (String) Last address in the lot.
56 changes: 56 additions & 0 deletions docs/resources/address_lot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "oxide_address_lot Resource - terraform-provider-oxide"
subcategory: ""
description: |-

---

# oxide_address_lot (Resource)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `blocks` (Attributes Set) (see [below for nested schema](#nestedatt--blocks))
- `description` (String) Description for the address lot.
- `kind` (String) Kind for the address lot. Must be one of "infra" or "pool".
- `name` (String) Name of the address lot.

### Optional

- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))

### Read-Only

- `id` (String) Unique, immutable, system-controlled identifier of the address lot.
- `time_created` (String) Timestamp of when this address lot was created.
- `time_modified` (String) Timestamp of when this address lot was last modified.

<a id="nestedatt--blocks"></a>
### Nested Schema for `blocks`

Required:

- `first_address` (String) First address in the lot.
- `last_address` (String) Last address in the lot.

Read-Only:

- `id` (String) ID of the address lot block.


<a id="nestedatt--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs.
- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Read operations occur during any refresh or planning operation when refresh is enabled.
- `update` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
164 changes: 164 additions & 0 deletions internal/provider/data_source_address_lot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// 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/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 = (*addressLotDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*addressLotDataSource)(nil)

type addressLotDataSource struct {
client *oxide.Client
}

type addressLotDataSourceModel struct {
Blocks []addressLotDataSourceBlockModel `tfsdk:"blocks"`
Description types.String `tfsdk:"description"`
Kind types.String `tfsdk:"kind"`
Name types.String `tfsdk:"name"`
ID types.String `tfsdk:"id"`
TimeCreated types.String `tfsdk:"time_created"`
TimeModified types.String `tfsdk:"time_modified"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

type addressLotDataSourceBlockModel struct {
ID types.String `tfsdk:"id"`
FirstAddress types.String `tfsdk:"first_address"`
LastAddress types.String `tfsdk:"last_address"`
}

// NewAddressLotDataSource initialises an address_lot datasource.
func NewAddressLotDataSource() datasource.DataSource {
return &addressLotDataSource{}
}

func (d *addressLotDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "oxide_address_lot"
}

// Configure adds the provider configured client to the data source.
func (d *addressLotDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

d.client = req.ProviderData.(*oxide.Client)
}

func (d *addressLotDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Description: "Name of the address lot.",
},
"description": schema.StringAttribute{
Computed: true,
Description: "Description for the address lot.",
},
"kind": schema.StringAttribute{
Computed: true,
Description: "Kind for the address lot.",
},
"blocks": schema.SetNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "ID of the address lot block.",
Computed: true,
},
"first_address": schema.StringAttribute{
Description: "First address in the lot.",
Computed: true,
},
"last_address": schema.StringAttribute{
Description: "Last address in the lot.",
Computed: true,
},
},
},
},
"id": schema.StringAttribute{
Computed: true,
Description: "Unique, immutable, system-controlled identifier of the address lot.",
},
"time_created": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this address lot was created.",
},
"time_modified": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this address lot was last modified.",
},
"timeouts": timeouts.Attributes(ctx),
},
}
}

func (d *addressLotDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state addressLotDataSourceModel

// 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()

addressLot, err := d.client.NetworkingAddressLotView(ctx, oxide.NetworkingAddressLotViewParams{
AddressLot: oxide.NameOrId(state.Name.ValueString()),
})
if err != nil {
resp.Diagnostics.AddError(
"Unable to read address lot:",
"API error: "+err.Error(),
)
return
}
lot := addressLot.Lot
tflog.Trace(ctx, fmt.Sprintf("read address lot with ID: %v", lot.Id), map[string]any{"success": true})

state.ID = types.StringValue(lot.Id)
state.Name = types.StringValue(string(lot.Name))
state.Kind = types.StringValue(string(lot.Kind))
state.Description = types.StringValue(lot.Description)
state.TimeCreated = types.StringValue(lot.TimeCreated.String())
state.TimeModified = types.StringValue(lot.TimeCreated.String())

blockModels := make([]addressLotDataSourceBlockModel, len(addressLot.Blocks))
for index, item := range addressLot.Blocks {
blockModels[index] = addressLotDataSourceBlockModel{
ID: types.StringValue(item.Id),
FirstAddress: types.StringValue(item.FirstAddress),
LastAddress: types.StringValue(item.LastAddress),
}
}
state.Blocks = blockModels

// Save state into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}
57 changes: 57 additions & 0 deletions internal/provider/data_source_address_lot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

var testDataSourceAddressLotConfig = `
resource "oxide_address_lot" "test" {
description = "a test address lot"
name = "terraform-acc-my-address-lot"
kind = "infra"
blocks = [
{
first_address = "172.0.1.1"
last_address = "172.0.1.10"
},
]
}

data "oxide_address_lot" "test" {
name = oxide_address_lot.test.name
}
`

func TestAccDataSourceAddressLot_full(t *testing.T) {
resourceName := "oxide_address_lot.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testDataSourceAddressLotConfig,
Check: checkDataSourceAddressLot(resourceName),
},
},
})
}

func checkDataSourceAddressLot(dataName string) resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc([]resource.TestCheckFunc{
resource.TestCheckResourceAttrSet(dataName, "id"),
resource.TestCheckResourceAttr(dataName, "description", "a test address lot"),
resource.TestCheckResourceAttr(dataName, "name", "terraform-acc-my-address-lot"),
resource.TestCheckResourceAttrSet(dataName, "blocks.0.first_address"),
resource.TestCheckResourceAttrSet(dataName, "blocks.0.last_address"),
resource.TestCheckResourceAttr(dataName, "blocks.0.first_address", "172.0.1.1"),
resource.TestCheckResourceAttr(dataName, "blocks.0.last_address", "172.0.1.10"),
resource.TestCheckResourceAttrSet(dataName, "time_created"),
resource.TestCheckResourceAttrSet(dataName, "time_modified"),
}...)
}
Loading
Loading