Files
terraform-provider-virtfusion/internal/provider/data_source_packages.go
Andrew 6b7430b67b Overhaul VirtFusion provider: 20 resources, 30 data sources, multipage pagination
Complete rewrite of the VirtFusion Terraform provider with full API coverage:

- 20 managed resources (server, build, SSH key, user, firewall, IP blocks, etc.)
- 30 data sources (hypervisors, packages, servers, IP blocks, self-service, etc.)
- New HTTP client with proper error handling, query parameter support, and
  automatic multipage pagination via GetAllPages (fetches all pages from
  Laravel-style paginated endpoints and merges into a single response)
- Fixed type mismatches against live API: ServerData.Suspended (int→bool),
  IPBlockData.Type (string→int), PackageData json tags (primaryStorage, etc.),
  ServerData nested CPU/Settings/Resources structure, HypervisorGroupResources
  array response
- Configurable results-per-page (default 300) on all list data sources
- Migrated CI from GitHub Actions to Gitea Actions
- Updated goreleaser config, go.mod dependencies, and examples

Verified against live VirtFusion instance at cp.vps.ezscale.tech:
all data sources return correct data with full pagination support.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 02:01:16 -04:00

164 lines
5.4 KiB
Go

// Copyright (c) EZSCALE.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"encoding/json"
"fmt"
"terraform-provider-virtfusion/internal/client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = &PackagesDataSource{}
_ datasource.DataSourceWithConfigure = &PackagesDataSource{}
)
// NewPackagesDataSource returns a new packages data source.
func NewPackagesDataSource() datasource.DataSource {
return &PackagesDataSource{}
}
// PackagesDataSource defines the data source implementation.
type PackagesDataSource struct {
client *client.Client
}
// PackagesDataSourceModel describes the data source data model.
type PackagesDataSourceModel struct {
Results types.Int64 `tfsdk:"results"`
Packages []PackageItemModel `tfsdk:"packages"`
}
// PackageItemModel describes a single package in the list.
type PackageItemModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Enabled types.Bool `tfsdk:"enabled"`
CPUCores types.Int64 `tfsdk:"cpu_cores"`
Memory types.Int64 `tfsdk:"memory"`
Storage types.Int64 `tfsdk:"storage"`
Traffic types.Int64 `tfsdk:"traffic"`
NetworkSpeedInbound types.Int64 `tfsdk:"network_speed_inbound"`
NetworkSpeedOutbound types.Int64 `tfsdk:"network_speed_outbound"`
Ipv4 types.Int64 `tfsdk:"ipv4"`
}
func (d *PackagesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_packages"
}
func (d *PackagesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Fetches all VirtFusion packages.",
Attributes: map[string]schema.Attribute{
"results": resultsSchemaAttribute(),
"packages": schema.ListNestedAttribute{
MarkdownDescription: "List of packages.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "The package ID.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The package name.",
Computed: true,
},
"enabled": schema.BoolAttribute{
MarkdownDescription: "Whether the package is enabled.",
Computed: true,
},
"cpu_cores": schema.Int64Attribute{
MarkdownDescription: "The number of CPU cores in the package.",
Computed: true,
},
"memory": schema.Int64Attribute{
MarkdownDescription: "The amount of memory in the package.",
Computed: true,
},
"storage": schema.Int64Attribute{
MarkdownDescription: "The amount of storage in the package.",
Computed: true,
},
"traffic": schema.Int64Attribute{
MarkdownDescription: "The traffic limit in the package.",
Computed: true,
},
"network_speed_inbound": schema.Int64Attribute{
MarkdownDescription: "The inbound network speed in the package.",
Computed: true,
},
"network_speed_outbound": schema.Int64Attribute{
MarkdownDescription: "The outbound network speed in the package.",
Computed: true,
},
"ipv4": schema.Int64Attribute{
MarkdownDescription: "The number of IPv4 addresses in the package.",
Computed: true,
},
},
},
},
},
}
}
func (d *PackagesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
c, ok := req.ProviderData.(*client.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.Client, got: %T.", req.ProviderData),
)
return
}
d.client = c
}
func (d *PackagesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data PackagesDataSourceModel
rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/packages?%s", resultsQueryParam(data.Results)))
if err != nil {
resp.Diagnostics.AddError("Error Reading Packages", err.Error())
return
}
var listResp client.PackageListResponse
if err := json.Unmarshal(rawResp, &listResp); err != nil {
resp.Diagnostics.AddError("Error Parsing Packages Response", err.Error())
return
}
data.Packages = make([]PackageItemModel, len(listResp.Data))
for i, p := range listResp.Data {
data.Packages[i] = PackageItemModel{
ID: types.Int64Value(p.ID),
Name: types.StringValue(p.Name),
Enabled: types.BoolValue(p.Enabled),
CPUCores: types.Int64Value(p.CPUCores),
Memory: types.Int64Value(p.Memory),
Storage: types.Int64Value(p.Storage),
Traffic: types.Int64Value(p.Traffic),
NetworkSpeedInbound: types.Int64Value(p.NetworkSpeedInbound),
NetworkSpeedOutbound: types.Int64Value(p.NetworkSpeedOutbound),
Ipv4: types.Int64Value(p.Ipv4),
}
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}