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>
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
// Copyright (c) EZSCALE.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package provider
|
|
|
|
import (
|
|
"context"
|
|
"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 = &SelfServiceHourlyStatsDataSource{}
|
|
_ datasource.DataSourceWithConfigure = &SelfServiceHourlyStatsDataSource{}
|
|
)
|
|
|
|
// NewSelfServiceHourlyStatsDataSource returns a new self-service hourly stats data source.
|
|
func NewSelfServiceHourlyStatsDataSource() datasource.DataSource {
|
|
return &SelfServiceHourlyStatsDataSource{}
|
|
}
|
|
|
|
// SelfServiceHourlyStatsDataSource defines the data source implementation.
|
|
type SelfServiceHourlyStatsDataSource struct {
|
|
client *client.Client
|
|
}
|
|
|
|
// SelfServiceHourlyStatsDataSourceModel describes the data source data model.
|
|
type SelfServiceHourlyStatsDataSourceModel struct {
|
|
UserID types.Int64 `tfsdk:"user_id"`
|
|
GroupID types.Int64 `tfsdk:"group_id"`
|
|
StatsJSON types.String `tfsdk:"stats_json"`
|
|
}
|
|
|
|
func (d *SelfServiceHourlyStatsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_self_service_hourly_stats"
|
|
}
|
|
|
|
func (d *SelfServiceHourlyStatsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
MarkdownDescription: "Fetches VirtFusion self-service hourly stats for a user and group.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"user_id": schema.Int64Attribute{
|
|
MarkdownDescription: "The user ID to fetch hourly stats for.",
|
|
Required: true,
|
|
},
|
|
"group_id": schema.Int64Attribute{
|
|
MarkdownDescription: "The group ID to fetch hourly stats for.",
|
|
Required: true,
|
|
},
|
|
"stats_json": schema.StringAttribute{
|
|
MarkdownDescription: "The raw JSON response containing hourly stats.",
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (d *SelfServiceHourlyStatsDataSource) 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 *SelfServiceHourlyStatsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
var data SelfServiceHourlyStatsDataSourceModel
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
rawResp, err := d.client.Get(ctx, fmt.Sprintf("/selfService/hourlyStats/byUser/%d/group/%d", data.UserID.ValueInt64(), data.GroupID.ValueInt64()))
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Error Reading Self-Service Hourly Stats", err.Error())
|
|
return
|
|
}
|
|
|
|
data.StatsJSON = types.StringValue(string(rawResp))
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|