Skip to content

Commit 7141d6e

Browse files
authored
added data_source_system_ip_pools (#505)
Added a new data source to lookup ip pools that at the System level. This is different from the current ip pools data source which looks up the pools within a silo.
1 parent 536e0dd commit 7141d6e

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

.changelog/0.14.0.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ description = ""
66
title = "New resource"
77
description = "`oxide_silo_saml_identity_provider` [#442](https://github.com/oxidecomputer/terraform-provider-oxide/pull/442)."
88

9+
[[features]]
10+
title = "New data source"
11+
description = "`oxide_system_ip_pools` [#505](https://github.com/oxidecomputer/terraform-provider-oxide/pull/505)."
12+
913
[[enhancements]]
1014
title = ""
1115
description = ""
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
page_title: "oxide_system_ip_pools Data Source - terraform-provider-oxide"
3+
---
4+
5+
# oxide_system_ip_pools (Data Source)
6+
7+
Retrieve all configured IP pools for the Oxide system.
8+
9+
## Example Usage
10+
11+
```hcl
12+
data "oxide_system_ip_pools" "example" {
13+
timeouts = {
14+
read = "1m"
15+
}
16+
}
17+
```
18+
19+
## Schema
20+
21+
### Optional
22+
23+
- `timeouts` (Attribute, Optional) (see [below for nested schema](#nestedatt--timeouts)).
24+
25+
### Read-Only
26+
27+
- `id` (String) The ID of this data source.
28+
- `ip_pools` (List of Objects) A list of IP pool objects configured for the system (see [below for nested schema](#nestedatt--ip_pools)).
29+
30+
<a id="nestedatt--timeouts"></a>
31+
32+
### Nested Schema for `timeouts`
33+
34+
Optional:
35+
36+
- `read` (String, Default `10m`)
37+
38+
<a id="nestedatt--ip_pools"></a>
39+
40+
### Nested Schema for `ip_pools`
41+
42+
Read-Only:
43+
44+
- `description` (String) Description for the IP pool.
45+
- `id` (String) Unique, immutable, system-controlled identifier of the IP pool.
46+
- `name` (String) Name of the IP pool.
47+
- `time_created` (String) Timestamp of when this IP pool was created.
48+
- `time_modified` (String) Timestamp of when this IP pool was last modified.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
10+
"github.com/google/uuid"
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 = (*systemIpPoolsDataSource)(nil)
20+
var _ datasource.DataSourceWithConfigure = (*systemIpPoolsDataSource)(nil)
21+
22+
type systemIpPoolsDataSource struct {
23+
client *oxide.Client
24+
}
25+
26+
type systemIpPoolsDataSourceModel struct {
27+
ID types.String `tfsdk:"id"`
28+
Timeouts timeouts.Value `tfsdk:"timeouts"`
29+
IpPools []systemIpPoolModel `tfsdk:"ip_pools"`
30+
}
31+
32+
type systemIpPoolModel struct {
33+
Description types.String `tfsdk:"description"`
34+
ID types.String `tfsdk:"id"`
35+
Name types.String `tfsdk:"name"`
36+
TimeCreated types.String `tfsdk:"time_created"`
37+
TimeModified types.String `tfsdk:"time_modified"`
38+
}
39+
40+
// NewSystemIpPoolsDataSource initialises a system_ip_pools data source.
41+
func NewSystemIpPoolsDataSource() datasource.DataSource {
42+
return &systemIpPoolsDataSource{}
43+
}
44+
45+
func (d *systemIpPoolsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
46+
resp.TypeName = "oxide_system_ip_pools"
47+
}
48+
49+
// Configure adds the provider configured client to the data source.
50+
func (d *systemIpPoolsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
51+
if req.ProviderData == nil {
52+
return
53+
}
54+
55+
d.client = req.ProviderData.(*oxide.Client)
56+
}
57+
58+
func (d *systemIpPoolsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
59+
resp.Schema = schema.Schema{
60+
Attributes: map[string]schema.Attribute{
61+
"id": schema.StringAttribute{
62+
Computed: true,
63+
},
64+
"timeouts": timeouts.Attributes(ctx),
65+
"ip_pools": schema.ListNestedAttribute{
66+
Computed: true,
67+
NestedObject: schema.NestedAttributeObject{
68+
Attributes: map[string]schema.Attribute{
69+
"name": schema.StringAttribute{
70+
Computed: true,
71+
Description: "Name of the IP pool.",
72+
},
73+
"description": schema.StringAttribute{
74+
Computed: true,
75+
Description: "Description for the IP pool.",
76+
},
77+
"id": schema.StringAttribute{
78+
Computed: true,
79+
Description: "Unique, immutable, system-controlled identifier of the IP pool.",
80+
},
81+
"time_created": schema.StringAttribute{
82+
Computed: true,
83+
Description: "Timestamp of when this IP pool was created.",
84+
},
85+
"time_modified": schema.StringAttribute{
86+
Computed: true,
87+
Description: "Timestamp of when this IP pool was last modified.",
88+
},
89+
},
90+
},
91+
},
92+
},
93+
}
94+
}
95+
96+
func (d *systemIpPoolsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
97+
var state systemIpPoolsDataSourceModel
98+
99+
// Read Terraform configuration data into the model
100+
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
101+
if resp.Diagnostics.HasError() {
102+
return
103+
}
104+
105+
readTimeout, diags := state.Timeouts.Read(ctx, defaultTimeout())
106+
resp.Diagnostics.Append(diags...)
107+
if resp.Diagnostics.HasError() {
108+
return
109+
}
110+
ctx, cancel := context.WithTimeout(ctx, readTimeout)
111+
defer cancel()
112+
113+
params := oxide.IpPoolListParams{
114+
SortBy: oxide.NameOrIdSortModeIdAscending,
115+
}
116+
ipPools, err := d.client.IpPoolListAllPages(ctx, params)
117+
if err != nil {
118+
resp.Diagnostics.AddError(
119+
"Unable to read system IP pools list:",
120+
err.Error(),
121+
)
122+
return
123+
}
124+
125+
tflog.Trace(ctx, "read all pools from system")
126+
127+
// Set a unique ID for the data source payload
128+
state.ID = types.StringValue(uuid.New().String())
129+
130+
for _, ipPool := range ipPools {
131+
poolState := systemIpPoolModel{
132+
Description: types.StringValue(ipPool.Description),
133+
ID: types.StringValue(ipPool.Id),
134+
Name: types.StringValue(string(ipPool.Name)),
135+
TimeCreated: types.StringValue(ipPool.TimeCreated.String()),
136+
TimeModified: types.StringValue(ipPool.TimeCreated.String()),
137+
}
138+
139+
state.IpPools = append(state.IpPools, poolState)
140+
}
141+
142+
// Save state into Terraform state
143+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
144+
if resp.Diagnostics.HasError() {
145+
return
146+
}
147+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
"fmt"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
)
13+
14+
type dataSourceSystemIPPoolsConfig struct {
15+
BlockName string
16+
SupportBlockName string
17+
}
18+
19+
var dataSourceSystemIPPoolsConfigTpl = `
20+
data "oxide_system_ip_pools" "{{.BlockName}}" {
21+
timeouts = {
22+
read = "1m"
23+
}
24+
}
25+
`
26+
27+
func TestAccSiloDataSourceSystemIPPools_full(t *testing.T) {
28+
blockName := newBlockName("datasource-ip-pool")
29+
config, err := parsedAccConfig(
30+
dataSourceSystemIPPoolsConfig{
31+
BlockName: blockName,
32+
SupportBlockName: newBlockName("support"),
33+
},
34+
dataSourceSystemIPPoolsConfigTpl,
35+
)
36+
if err != nil {
37+
t.Errorf("error parsing config template data: %e", err)
38+
}
39+
40+
resource.ParallelTest(t, resource.TestCase{
41+
PreCheck: func() { testAccPreCheck(t) },
42+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(),
43+
Steps: []resource.TestStep{
44+
{
45+
Config: config,
46+
Check: checkDataSourceSystemIPPools(
47+
fmt.Sprintf("data.oxide_system_ip_pools.%s", blockName),
48+
),
49+
},
50+
},
51+
})
52+
}
53+
54+
func checkDataSourceSystemIPPools(dataName string) resource.TestCheckFunc {
55+
return resource.ComposeAggregateTestCheckFunc([]resource.TestCheckFunc{
56+
resource.TestCheckResourceAttr(dataName, "timeouts.read", "1m"),
57+
resource.TestCheckResourceAttrSet(dataName, "id"),
58+
resource.TestCheckResourceAttrSet(dataName, "ip_pools.0.id"),
59+
resource.TestCheckResourceAttrSet(dataName, "ip_pools.0.name"),
60+
resource.TestCheckResourceAttrSet(dataName, "ip_pools.0.description"),
61+
resource.TestCheckResourceAttrSet(dataName, "ip_pools.0.time_created"),
62+
resource.TestCheckResourceAttrSet(dataName, "ip_pools.0.time_modified"),
63+
}...)
64+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func (p *oxideProvider) DataSources(_ context.Context) []func() datasource.DataS
176176
NewVPCRouterRouteDataSource,
177177
NewVPCSubnetDataSource,
178178
NewFloatingIPDataSource,
179+
NewSystemIpPoolsDataSource,
179180
}
180181
}
181182

0 commit comments

Comments
 (0)