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>
205 lines
7.0 KiB
Go
205 lines
7.0 KiB
Go
// Copyright (c) EZSCALE.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"terraform-provider-virtfusion/internal/client"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
// Ensure provider-defined types fully satisfy framework interfaces.
|
|
var (
|
|
_ resource.Resource = &SelfServicePackServersActionResource{}
|
|
_ resource.ResourceWithConfigure = &SelfServicePackServersActionResource{}
|
|
)
|
|
|
|
// NewSelfServicePackServersActionResource creates a new self-service pack servers action resource.
|
|
func NewSelfServicePackServersActionResource() resource.Resource {
|
|
return &SelfServicePackServersActionResource{}
|
|
}
|
|
|
|
// SelfServicePackServersActionResource defines the resource implementation.
|
|
type SelfServicePackServersActionResource struct {
|
|
client *client.Client
|
|
}
|
|
|
|
// SelfServicePackServersActionResourceModel describes the resource data model.
|
|
type SelfServicePackServersActionResourceModel struct {
|
|
ID types.String `tfsdk:"id"`
|
|
PackID types.Int64 `tfsdk:"pack_id"`
|
|
Action types.String `tfsdk:"action"`
|
|
Triggers types.Map `tfsdk:"triggers"`
|
|
}
|
|
|
|
func (r *SelfServicePackServersActionResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_self_service_pack_servers_action"
|
|
}
|
|
|
|
func (r *SelfServicePackServersActionResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
MarkdownDescription: "Performs an action on all servers in a self-service resource pack. This is a trigger-style resource — the action is executed on create and can be re-triggered by changing the `triggers` attribute.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.StringAttribute{
|
|
MarkdownDescription: "The identifier for this pack servers action.",
|
|
Computed: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.UseStateForUnknown(),
|
|
},
|
|
},
|
|
"pack_id": schema.Int64Attribute{
|
|
MarkdownDescription: "The ID of the resource pack.",
|
|
Required: true,
|
|
},
|
|
"action": schema.StringAttribute{
|
|
MarkdownDescription: "The action to perform on the pack servers. Must be one of: `suspend`, `unsuspend`, `delete`.",
|
|
Required: true,
|
|
},
|
|
"triggers": schema.MapAttribute{
|
|
MarkdownDescription: "A map of arbitrary strings that, when changed, will cause the action to be re-executed. Works like `triggers` in `terraform_data`.",
|
|
ElementType: types.StringType,
|
|
Optional: true,
|
|
PlanModifiers: []planmodifier.Map{
|
|
mapplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *SelfServicePackServersActionResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
|
if req.ProviderData == nil {
|
|
return
|
|
}
|
|
|
|
c, ok := req.ProviderData.(*client.Client)
|
|
if !ok {
|
|
resp.Diagnostics.AddError(
|
|
"Unexpected Resource Configure Type",
|
|
fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
|
|
)
|
|
return
|
|
}
|
|
|
|
r.client = c
|
|
}
|
|
|
|
func (r *SelfServicePackServersActionResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var data SelfServicePackServersActionResourceModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
action := data.Action.ValueString()
|
|
packID := data.PackID.ValueInt64()
|
|
|
|
switch action {
|
|
case "suspend":
|
|
apiPath := fmt.Sprintf("/selfService/resourcePack/%d/servers/suspend", packID)
|
|
_, err := r.client.Post(ctx, apiPath, nil)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error Suspending Pack Servers",
|
|
fmt.Sprintf("Could not suspend servers for resource pack %d: %s", packID, err),
|
|
)
|
|
return
|
|
}
|
|
case "unsuspend":
|
|
apiPath := fmt.Sprintf("/selfService/resourcePack/%d/servers/unsuspend", packID)
|
|
_, err := r.client.Post(ctx, apiPath, nil)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error Unsuspending Pack Servers",
|
|
fmt.Sprintf("Could not unsuspend servers for resource pack %d: %s", packID, err),
|
|
)
|
|
return
|
|
}
|
|
case "delete":
|
|
apiPath := fmt.Sprintf("/selfService/resourcePack/%d/servers", packID)
|
|
_, err := r.client.Delete(ctx, apiPath)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error Deleting Pack Servers",
|
|
fmt.Sprintf("Could not delete servers for resource pack %d: %s", packID, err),
|
|
)
|
|
return
|
|
}
|
|
}
|
|
|
|
data.ID = types.StringValue(fmt.Sprintf("%d-%s-%d", packID, action, time.Now().UnixNano()))
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *SelfServicePackServersActionResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var data SelfServicePackServersActionResourceModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// Return stored state as-is for trigger-style resources.
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *SelfServicePackServersActionResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
var data SelfServicePackServersActionResourceModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *SelfServicePackServersActionResource) Delete(_ context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) {
|
|
// No-op: pack server actions are not reversible. Removing from state only.
|
|
}
|
|
|
|
// ValidateConfig validates the resource configuration.
|
|
func (r *SelfServicePackServersActionResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
|
|
var data SelfServicePackServersActionResourceModel
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// Validate pack_id is positive.
|
|
if !data.PackID.IsNull() && !data.PackID.IsUnknown() && data.PackID.ValueInt64() <= 0 {
|
|
resp.Diagnostics.AddAttributeError(
|
|
path.Root("pack_id"),
|
|
"Invalid Pack ID",
|
|
"pack_id must be a positive integer.",
|
|
)
|
|
}
|
|
|
|
// Validate action is one of the allowed values.
|
|
if !data.Action.IsNull() && !data.Action.IsUnknown() {
|
|
action := data.Action.ValueString()
|
|
validActions := map[string]bool{
|
|
"suspend": true,
|
|
"unsuspend": true,
|
|
"delete": true,
|
|
}
|
|
if !validActions[action] {
|
|
resp.Diagnostics.AddAttributeError(
|
|
path.Root("action"),
|
|
"Invalid Pack Servers Action",
|
|
fmt.Sprintf("action must be one of: suspend, unsuspend, delete. Got: %q", action),
|
|
)
|
|
}
|
|
}
|
|
}
|