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>
This commit is contained in:
2026-03-16 02:01:16 -04:00
parent a3a16f46fa
commit 6b7430b67b
92 changed files with 18443 additions and 1488 deletions

View File

@@ -0,0 +1,124 @@
// 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 = &PackageTemplatesDataSource{}
_ datasource.DataSourceWithConfigure = &PackageTemplatesDataSource{}
)
// NewPackageTemplatesDataSource returns a new package templates data source.
func NewPackageTemplatesDataSource() datasource.DataSource {
return &PackageTemplatesDataSource{}
}
// PackageTemplatesDataSource defines the data source implementation.
type PackageTemplatesDataSource struct {
client *client.Client
}
// PackageTemplatesDataSourceModel describes the data source data model.
type PackageTemplatesDataSourceModel struct {
PackageID types.Int64 `tfsdk:"package_id"`
Results types.Int64 `tfsdk:"results"`
Templates []PackageTemplateItemModel `tfsdk:"templates"`
}
// PackageTemplateItemModel describes a single template in the list.
type PackageTemplateItemModel struct {
ID types.Int64 `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
func (d *PackageTemplatesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_package_templates"
}
func (d *PackageTemplatesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Fetches templates available for a VirtFusion server package.",
Attributes: map[string]schema.Attribute{
"package_id": schema.Int64Attribute{
MarkdownDescription: "The package ID to fetch templates for.",
Required: true,
},
"results": resultsSchemaAttribute(),
"templates": schema.ListNestedAttribute{
MarkdownDescription: "List of templates available for the package.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
MarkdownDescription: "The template ID.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The template name.",
Computed: true,
},
},
},
},
},
}
}
func (d *PackageTemplatesDataSource) 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 *PackageTemplatesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data PackageTemplatesDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/media/templates/fromServerPackageSpec/%d?%s", data.PackageID.ValueInt64(), resultsQueryParam(data.Results)))
if err != nil {
resp.Diagnostics.AddError("Error Reading Package Templates", err.Error())
return
}
var templateResp client.TemplateResponse
if err := json.Unmarshal(rawResp, &templateResp); err != nil {
resp.Diagnostics.AddError("Error Parsing Package Templates Response", err.Error())
return
}
data.Templates = make([]PackageTemplateItemModel, len(templateResp.Data))
for i, t := range templateResp.Data {
data.Templates[i] = PackageTemplateItemModel{
ID: types.Int64Value(t.ID),
Name: types.StringValue(t.Name),
}
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}