|
| 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