Skip to content

Commit 56f177f

Browse files
jmcarpsudomateo
authored andcommitted
Add address lot resource and data source.
Resolves SSE-67, SSE-68.
1 parent 29c78bb commit 56f177f

10 files changed

Lines changed: 792 additions & 22 deletions

File tree

.changelog/0.18.0.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ title = ""
33
description = ""
44

55
[[features]]
6-
title = ""
7-
description = ""
6+
title = "New resource"
7+
description = "`oxide_address_lot` [#489](https://github.com/oxidecomputer/terraform-provider-oxide/pull/489)."
8+
9+
[[features]]
10+
title = "New data source"
11+
description = "`oxide_address_lot` [#489](https://github.com/oxidecomputer/terraform-provider-oxide/pull/489)."
12+
813

914
[[enhancements]]
1015
title = ""

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ terrafmt: tools
8181
@ echo "-> Running terraform docs codeblocks linter"
8282
@ find ./docs -type f -name "*.md" -exec $(GOBIN)/terrafmt diff -f {} \;
8383

84+
.PHONY: terrafmt-fmt
85+
terrafmt-fmt: tools
86+
@ echo "-> Running terraform docs codeblocks linter"
87+
@ find ./docs -type f -name "*.md" -exec $(GOBIN)/terrafmt fmt -f {} \;
88+
8489
configfmt:
8590
@ echo "-> Running terraform linters on .tf files"
8691
@ terraform fmt -write=false -recursive -check

docs/data-sources/address_lot.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "oxide_address_lot Data Source - terraform-provider-oxide"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# oxide_address_lot (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) Name of the address lot.
21+
22+
### Optional
23+
24+
- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))
25+
26+
### Read-Only
27+
28+
- `blocks` (Attributes Set) (see [below for nested schema](#nestedatt--blocks))
29+
- `description` (String) Description for the address lot.
30+
- `id` (String) Unique, immutable, system-controlled identifier of the address lot.
31+
- `kind` (String) Kind for the address lot.
32+
- `time_created` (String) Timestamp of when this address lot was created.
33+
- `time_modified` (String) Timestamp of when this address lot was last modified.
34+
35+
<a id="nestedatt--timeouts"></a>
36+
### Nested Schema for `timeouts`
37+
38+
Optional:
39+
40+
- `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).
41+
42+
43+
<a id="nestedatt--blocks"></a>
44+
### Nested Schema for `blocks`
45+
46+
Read-Only:
47+
48+
- `first_address` (String) First address in the lot.
49+
- `id` (String) ID of the address lot block.
50+
- `last_address` (String) Last address in the lot.

docs/resources/address_lot.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "oxide_address_lot Resource - terraform-provider-oxide"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# oxide_address_lot (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `blocks` (Attributes Set) (see [below for nested schema](#nestedatt--blocks))
21+
- `description` (String) Description for the address lot.
22+
- `kind` (String) Kind for the address lot. Must be one of "infra" or "pool".
23+
- `name` (String) Name of the address lot.
24+
25+
### Optional
26+
27+
- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))
28+
29+
### Read-Only
30+
31+
- `id` (String) Unique, immutable, system-controlled identifier of the address lot.
32+
- `time_created` (String) Timestamp of when this address lot was created.
33+
- `time_modified` (String) Timestamp of when this address lot was last modified.
34+
35+
<a id="nestedatt--blocks"></a>
36+
### Nested Schema for `blocks`
37+
38+
Required:
39+
40+
- `first_address` (String) First address in the lot.
41+
- `last_address` (String) Last address in the lot.
42+
43+
Read-Only:
44+
45+
- `id` (String) ID of the address lot block.
46+
47+
48+
<a id="nestedatt--timeouts"></a>
49+
### Nested Schema for `timeouts`
50+
51+
Optional:
52+
53+
- `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).
54+
- `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.
55+
- `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.
56+
- `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).
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
)
12+
13+
var testDataSourceAddressLotConfig = `
14+
resource "oxide_address_lot" "test" {
15+
description = "a test address lot"
16+
name = "terraform-acc-my-address-lot"
17+
kind = "infra"
18+
blocks = [
19+
{
20+
first_address = "172.0.1.1"
21+
last_address = "172.0.1.10"
22+
},
23+
]
24+
}
25+
26+
data "oxide_address_lot" "test" {
27+
name = oxide_address_lot.test.name
28+
}
29+
`
30+
31+
func TestAccDataSourceAddressLot_full(t *testing.T) {
32+
resourceName := "oxide_address_lot.test"
33+
resource.ParallelTest(t, resource.TestCase{
34+
PreCheck: func() { testAccPreCheck(t) },
35+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
36+
Steps: []resource.TestStep{
37+
{
38+
Config: testDataSourceAddressLotConfig,
39+
Check: checkDataSourceAddressLot(resourceName),
40+
},
41+
},
42+
})
43+
}
44+
45+
func checkDataSourceAddressLot(dataName string) resource.TestCheckFunc {
46+
return resource.ComposeAggregateTestCheckFunc([]resource.TestCheckFunc{
47+
resource.TestCheckResourceAttrSet(dataName, "id"),
48+
resource.TestCheckResourceAttr(dataName, "description", "a test address lot"),
49+
resource.TestCheckResourceAttr(dataName, "name", "terraform-acc-my-address-lot"),
50+
resource.TestCheckResourceAttrSet(dataName, "blocks.0.first_address"),
51+
resource.TestCheckResourceAttrSet(dataName, "blocks.0.last_address"),
52+
resource.TestCheckResourceAttr(dataName, "blocks.0.first_address", "172.0.1.1"),
53+
resource.TestCheckResourceAttr(dataName, "blocks.0.last_address", "172.0.1.10"),
54+
resource.TestCheckResourceAttrSet(dataName, "time_created"),
55+
resource.TestCheckResourceAttrSet(dataName, "time_modified"),
56+
}...)
57+
}

0 commit comments

Comments
 (0)