Skip to content

Commit c25f19a

Browse files
authored
feat(sdn): add support for VNets datasource (#2299)
Signed-off-by: Maciej Lech <maciej.lech@mlit.pro>
1 parent d8b0f5c commit c25f19a

File tree

10 files changed

+517
-2
lines changed

10 files changed

+517
-2
lines changed

docs/data-sources/virtual_environment_sdn_vnet.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@ description: |-
1111

1212
Retrieves information about an existing SDN VNet.
1313

14-
14+
## Example Usage
15+
16+
```terraform
17+
data "proxmox_virtual_environment_sdn_vnet" "example" {
18+
id = "vnet1"
19+
}
20+
21+
output "data_proxmox_virtual_environment_sdn_vnet" {
22+
value = {
23+
id = data.proxmox_virtual_environment_sdn_vnet.example.id
24+
zone = data.proxmox_virtual_environment_sdn_vnet.example.zone
25+
alias = data.proxmox_virtual_environment_sdn_vnet.example.alias
26+
isolate_ports = data.proxmox_virtual_environment_sdn_vnet.example.isolate_ports
27+
tag = data.proxmox_virtual_environment_sdn_vnet.example.tag
28+
vlan_aware = data.proxmox_virtual_environment_sdn_vnet.example.vlan_aware
29+
}
30+
}
31+
```
1532

1633
<!-- schema generated by tfplugindocs -->
1734
## Schema
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
layout: page
3+
title: proxmox_virtual_environment_sdn_vnets
4+
parent: Data Sources
5+
subcategory: Virtual Environment
6+
description: |-
7+
Retrieves information about all SDN VNets in Proxmox. This data source lists all virtual networks configured in the Software-Defined Networking setup.
8+
---
9+
10+
# Data Source: proxmox_virtual_environment_sdn_vnets
11+
12+
Retrieves information about all SDN VNets in Proxmox. This data source lists all virtual networks configured in the Software-Defined Networking setup.
13+
14+
## Example Usage
15+
16+
```terraform
17+
# List all SDN VNets
18+
data "proxmox_virtual_environment_sdn_vnets" "all" {}
19+
20+
output "data_proxmox_virtual_environment_sdn_vnets_all" {
21+
value = {
22+
vnets = data.proxmox_virtual_environment_sdn_vnets.all.vnets
23+
}
24+
}
25+
```
26+
27+
<!-- schema generated by tfplugindocs -->
28+
## Schema
29+
30+
### Read-Only
31+
32+
- `vnets` (List of Object) List of SDN VNets. (see [below for nested schema](#nestedatt--vnets))
33+
34+
<a id="nestedatt--vnets"></a>
35+
### Nested Schema for `vnets`
36+
37+
Read-Only:
38+
39+
- `alias` (String)
40+
- `id` (String)
41+
- `isolate_ports` (Boolean)
42+
- `tag` (Number)
43+
- `vlan_aware` (Boolean)
44+
- `zone` (String)

example/data_source_virtual_environment_sdn.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ data "proxmox_virtual_environment_sdn_vnet" "test_vnet_1" {
2323
id = proxmox_virtual_environment_sdn_vnet.test_vnet_1.id
2424
}
2525

26+
data "proxmox_virtual_environment_sdn_vnets" "all" {
27+
depends_on = [
28+
proxmox_virtual_environment_sdn_vnet.test_vnet_1,
29+
proxmox_virtual_environment_sdn_vnet.test_vnet_2,
30+
]
31+
}
32+
2633
data "proxmox_virtual_environment_sdn_subnet" "test_subnet_dhcp" {
2734
depends_on = [proxmox_virtual_environment_sdn_subnet.test_subnet_dhcp]
2835
cidr = proxmox_virtual_environment_sdn_subnet.test_subnet_dhcp.cidr
@@ -45,6 +52,10 @@ output "proxmox_virtual_environment_sdn_vnet" {
4552
value = data.proxmox_virtual_environment_sdn_vnet.test_vnet_1
4653
}
4754

55+
output "proxmox_virtual_environment_sdn_vnets" {
56+
value = data.proxmox_virtual_environment_sdn_vnets.all
57+
}
58+
4859
output "proxmox_virtual_environment_sdn_subnet" {
4960
value = data.proxmox_virtual_environment_sdn_subnet.test_subnet_dhcp
5061
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
data "proxmox_virtual_environment_sdn_vnet" "example" {
2+
id = "vnet1"
3+
}
4+
5+
output "data_proxmox_virtual_environment_sdn_vnet" {
6+
value = {
7+
id = data.proxmox_virtual_environment_sdn_vnet.example.id
8+
zone = data.proxmox_virtual_environment_sdn_vnet.example.zone
9+
alias = data.proxmox_virtual_environment_sdn_vnet.example.alias
10+
isolate_ports = data.proxmox_virtual_environment_sdn_vnet.example.isolate_ports
11+
tag = data.proxmox_virtual_environment_sdn_vnet.example.tag
12+
vlan_aware = data.proxmox_virtual_environment_sdn_vnet.example.vlan_aware
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# List all SDN VNets
2+
data "proxmox_virtual_environment_sdn_vnets" "all" {}
3+
4+
output "data_proxmox_virtual_environment_sdn_vnets_all" {
5+
value = {
6+
vnets = data.proxmox_virtual_environment_sdn_vnets.all.vnets
7+
}
8+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
package vnet
8+
9+
import (
10+
"context"
11+
"fmt"
12+
13+
"github.com/hashicorp/terraform-plugin-framework/attr"
14+
"github.com/hashicorp/terraform-plugin-framework/datasource"
15+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
16+
"github.com/hashicorp/terraform-plugin-framework/types"
17+
18+
"github.com/bpg/terraform-provider-proxmox/fwprovider/config"
19+
"github.com/bpg/terraform-provider-proxmox/proxmox/cluster/sdn"
20+
"github.com/bpg/terraform-provider-proxmox/proxmox/cluster/sdn/vnets"
21+
)
22+
23+
// Ensure the implementation satisfies the required interfaces.
24+
var (
25+
_ datasource.DataSource = &vnetsDataSource{}
26+
_ datasource.DataSourceWithConfigure = &vnetsDataSource{}
27+
)
28+
29+
// vnetsDataSource is the data source implementation for SDN VNets.
30+
type vnetsDataSource struct {
31+
client *vnets.Client
32+
}
33+
34+
// vnetsDataSourceModel represents the data source model for listing VNets.
35+
type vnetsDataSourceModel struct {
36+
VNets types.List `tfsdk:"vnets"`
37+
}
38+
39+
// vnetDataModel represents individual VNet data in the list.
40+
type vnetDataModel struct {
41+
ID types.String `tfsdk:"id"`
42+
Zone types.String `tfsdk:"zone"`
43+
Alias types.String `tfsdk:"alias"`
44+
IsolatePorts types.Bool `tfsdk:"isolate_ports"`
45+
Tag types.Int64 `tfsdk:"tag"`
46+
VlanAware types.Bool `tfsdk:"vlan_aware"`
47+
}
48+
49+
// Configure adds the provider-configured client to the data source.
50+
func (d *vnetsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
51+
if req.ProviderData == nil {
52+
return
53+
}
54+
55+
cfg, ok := req.ProviderData.(config.DataSource)
56+
if !ok {
57+
resp.Diagnostics.AddError(
58+
"Unexpected DataSource Configure Type",
59+
fmt.Sprintf("Expected config.DataSource, got: %T", req.ProviderData),
60+
)
61+
62+
return
63+
}
64+
65+
d.client = &vnets.Client{Client: cfg.Client.Cluster()}
66+
}
67+
68+
// Metadata returns the data source type name.
69+
func (d *vnetsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
70+
resp.TypeName = req.ProviderTypeName + "_sdn_vnets"
71+
}
72+
73+
// Schema defines the schema for the data source.
74+
func (d *vnetsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
75+
resp.Schema = schema.Schema{
76+
Description: "Retrieves information about all SDN VNets in Proxmox.",
77+
MarkdownDescription: "Retrieves information about all SDN VNets in Proxmox. " +
78+
"This data source lists all virtual networks configured in the Software-Defined Networking setup.",
79+
Attributes: map[string]schema.Attribute{
80+
"vnets": schema.ListAttribute{
81+
Description: "List of SDN VNets.",
82+
Computed: true,
83+
ElementType: types.ObjectType{
84+
AttrTypes: map[string]attr.Type{
85+
"id": types.StringType,
86+
"zone": types.StringType,
87+
"alias": types.StringType,
88+
"isolate_ports": types.BoolType,
89+
"tag": types.Int64Type,
90+
"vlan_aware": types.BoolType,
91+
},
92+
},
93+
},
94+
},
95+
}
96+
}
97+
98+
// Read fetches all SDN VNets from the Proxmox VE API.
99+
func (d *vnetsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
100+
var data vnetsDataSourceModel
101+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
102+
103+
if resp.Diagnostics.HasError() {
104+
return
105+
}
106+
107+
vnetsList, err := d.client.GetVnetsWithParams(ctx, &sdn.QueryParams{})
108+
if err != nil {
109+
resp.Diagnostics.AddError(
110+
"Unable to Read SDN VNets",
111+
err.Error(),
112+
)
113+
114+
return
115+
}
116+
117+
// Convert VNets to list elements
118+
vnetElements := make([]attr.Value, len(vnetsList))
119+
for i, vnet := range vnetsList {
120+
vnetData := vnetDataModel{
121+
ID: types.StringValue(vnet.ID),
122+
Zone: types.StringPointerValue(vnet.Zone),
123+
Alias: types.StringPointerValue(vnet.Alias),
124+
IsolatePorts: types.BoolPointerValue(vnet.IsolatePorts.PointerBool()),
125+
Tag: types.Int64PointerValue(vnet.Tag),
126+
VlanAware: types.BoolPointerValue(vnet.VlanAware.PointerBool()),
127+
}
128+
129+
objValue, objDiag := types.ObjectValueFrom(ctx, map[string]attr.Type{
130+
"id": types.StringType,
131+
"zone": types.StringType,
132+
"alias": types.StringType,
133+
"isolate_ports": types.BoolType,
134+
"tag": types.Int64Type,
135+
"vlan_aware": types.BoolType,
136+
}, vnetData)
137+
resp.Diagnostics.Append(objDiag...)
138+
139+
if resp.Diagnostics.HasError() {
140+
return
141+
}
142+
143+
vnetElements[i] = objValue
144+
}
145+
146+
listValue, listDiag := types.ListValue(types.ObjectType{
147+
AttrTypes: map[string]attr.Type{
148+
"id": types.StringType,
149+
"zone": types.StringType,
150+
"alias": types.StringType,
151+
"isolate_ports": types.BoolType,
152+
"tag": types.Int64Type,
153+
"vlan_aware": types.BoolType,
154+
},
155+
}, vnetElements)
156+
resp.Diagnostics.Append(listDiag...)
157+
158+
if resp.Diagnostics.HasError() {
159+
return
160+
}
161+
162+
data.VNets = listValue
163+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
164+
}
165+
166+
// NewVNetsDataSource returns a new data source for SDN VNets.
167+
func NewVNetsDataSource() datasource.DataSource {
168+
return &vnetsDataSource{}
169+
}

0 commit comments

Comments
 (0)