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:
156
internal/provider/data_source_server.go
Normal file
156
internal/provider/data_source_server.go
Normal file
@@ -0,0 +1,156 @@
|
||||
// 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 = &ServerDataSource{}
|
||||
var _ datasource.DataSourceWithConfigure = &ServerDataSource{}
|
||||
|
||||
// NewServerDataSource returns a new data source for reading a single server.
|
||||
func NewServerDataSource() datasource.DataSource {
|
||||
return &ServerDataSource{}
|
||||
}
|
||||
|
||||
// ServerDataSource defines the data source implementation.
|
||||
type ServerDataSource struct {
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
// ServerDataSourceModel describes the data source data model.
|
||||
type ServerDataSourceModel struct {
|
||||
ID types.Int64 `tfsdk:"id"`
|
||||
UUID types.String `tfsdk:"uuid"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Hostname types.String `tfsdk:"hostname"`
|
||||
OwnerID types.Int64 `tfsdk:"owner_id"`
|
||||
HypervisorID types.Int64 `tfsdk:"hypervisor_id"`
|
||||
Suspended types.Bool `tfsdk:"suspended"`
|
||||
CPUCores types.Int64 `tfsdk:"cpu_cores"`
|
||||
Memory types.Int64 `tfsdk:"memory"`
|
||||
Storage types.Int64 `tfsdk:"storage"`
|
||||
}
|
||||
|
||||
func (d *ServerDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_server"
|
||||
}
|
||||
|
||||
func (d *ServerDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Use this data source to read a single VirtFusion server by ID.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The server ID.",
|
||||
Required: true,
|
||||
},
|
||||
"uuid": schema.StringAttribute{
|
||||
MarkdownDescription: "The server UUID.",
|
||||
Computed: true,
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
MarkdownDescription: "The server display name.",
|
||||
Computed: true,
|
||||
},
|
||||
"hostname": schema.StringAttribute{
|
||||
MarkdownDescription: "The server hostname.",
|
||||
Computed: true,
|
||||
},
|
||||
"owner_id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The owner (user) ID who owns the server.",
|
||||
Computed: true,
|
||||
},
|
||||
"hypervisor_id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The hypervisor ID where the server is hosted.",
|
||||
Computed: true,
|
||||
},
|
||||
"suspended": schema.BoolAttribute{
|
||||
MarkdownDescription: "Whether the server is suspended.",
|
||||
Computed: true,
|
||||
},
|
||||
"cpu_cores": schema.Int64Attribute{
|
||||
MarkdownDescription: "The number of CPU cores.",
|
||||
Computed: true,
|
||||
},
|
||||
"memory": schema.Int64Attribute{
|
||||
MarkdownDescription: "The memory size in MB.",
|
||||
Computed: true,
|
||||
},
|
||||
"storage": schema.Int64Attribute{
|
||||
MarkdownDescription: "The storage size in GB.",
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *ServerDataSource) 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. Please report this issue to the provider developers.", req.ProviderData),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
d.client = c
|
||||
}
|
||||
|
||||
func (d *ServerDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data ServerDataSourceModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := d.client.Get(ctx, fmt.Sprintf("/servers/%d", data.ID.ValueInt64()))
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Error Reading Server", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var serverResp client.ServerResponse
|
||||
if err := json.Unmarshal(result, &serverResp); err != nil {
|
||||
resp.Diagnostics.AddError("Error Parsing Server Response", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
s := serverResp.Data
|
||||
data.ID = types.Int64Value(s.ID)
|
||||
data.UUID = types.StringValue(s.UUID)
|
||||
data.Name = types.StringValue(s.Name)
|
||||
data.Hostname = types.StringValue(s.Hostname)
|
||||
data.OwnerID = types.Int64Value(s.OwnerID)
|
||||
data.HypervisorID = types.Int64Value(s.HypervisorID)
|
||||
data.Suspended = types.BoolValue(s.Suspended)
|
||||
|
||||
// Extract nested resource values from the detailed server response.
|
||||
var cpuCores, memory, storage int64
|
||||
if s.CPU != nil {
|
||||
cpuCores = s.CPU.Cores
|
||||
}
|
||||
if s.Settings != nil && s.Settings.Resources != nil {
|
||||
memory = s.Settings.Resources.Memory
|
||||
storage = s.Settings.Resources.Storage
|
||||
}
|
||||
data.CPUCores = types.Int64Value(cpuCores)
|
||||
data.Memory = types.Int64Value(memory)
|
||||
data.Storage = types.Int64Value(storage)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
Reference in New Issue
Block a user