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>
164 lines
5.2 KiB
Go
164 lines
5.2 KiB
Go
// Copyright (c) EZSCALE.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"terraform-provider-virtfusion/internal/client"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
|
|
"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 = &ServerTrafficBlockResource{}
|
|
_ resource.ResourceWithConfigure = &ServerTrafficBlockResource{}
|
|
)
|
|
|
|
// NewServerTrafficBlockResource creates a new server traffic block resource.
|
|
func NewServerTrafficBlockResource() resource.Resource {
|
|
return &ServerTrafficBlockResource{}
|
|
}
|
|
|
|
// ServerTrafficBlockResource defines the resource implementation.
|
|
type ServerTrafficBlockResource struct {
|
|
client *client.Client
|
|
}
|
|
|
|
// ServerTrafficBlockResourceModel describes the resource data model.
|
|
type ServerTrafficBlockResourceModel struct {
|
|
ID types.Int64 `tfsdk:"id"`
|
|
ServerID types.Int64 `tfsdk:"server_id"`
|
|
Type types.String `tfsdk:"type"`
|
|
}
|
|
|
|
func (r *ServerTrafficBlockResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_server_traffic_block"
|
|
}
|
|
|
|
func (r *ServerTrafficBlockResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
MarkdownDescription: "Manages a traffic block on a VirtFusion server.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.Int64Attribute{
|
|
MarkdownDescription: "The identifier of the traffic block.",
|
|
Computed: true,
|
|
PlanModifiers: []planmodifier.Int64{
|
|
int64planmodifier.UseStateForUnknown(),
|
|
},
|
|
},
|
|
"server_id": schema.Int64Attribute{
|
|
MarkdownDescription: "The ID of the server to add the traffic block to.",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.Int64{
|
|
int64planmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"type": schema.StringAttribute{
|
|
MarkdownDescription: "The type of traffic block (e.g. `inbound` or `outbound`).",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *ServerTrafficBlockResource) 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 *ServerTrafficBlockResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var data ServerTrafficBlockResourceModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
blockReq := client.TrafficBlockRequest{
|
|
Type: data.Type.ValueString(),
|
|
}
|
|
|
|
apiPath := fmt.Sprintf("/servers/%d/traffic/blocks", data.ServerID.ValueInt64())
|
|
respBody, err := r.client.Post(ctx, apiPath, blockReq)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error Creating Traffic Block",
|
|
fmt.Sprintf("Could not create traffic block on server %d: %s", data.ServerID.ValueInt64(), err),
|
|
)
|
|
return
|
|
}
|
|
|
|
var blockResp client.TrafficBlockResponse
|
|
if err := json.Unmarshal(respBody, &blockResp); err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error Parsing Response",
|
|
fmt.Sprintf("Could not parse traffic block response: %s", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
data.ID = types.Int64Value(blockResp.Data.ID)
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *ServerTrafficBlockResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var data ServerTrafficBlockResourceModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *ServerTrafficBlockResource) Update(_ context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) {
|
|
// All attributes require replacement — updates are never called.
|
|
}
|
|
|
|
func (r *ServerTrafficBlockResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var data ServerTrafficBlockResourceModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
apiPath := fmt.Sprintf("/servers/%d/traffic/blocks/%d", data.ServerID.ValueInt64(), data.ID.ValueInt64())
|
|
_, err := r.client.Delete(ctx, apiPath)
|
|
if err != nil {
|
|
var apiErr *client.APIError
|
|
if errors.As(err, &apiErr) && apiErr.IsNotFound() {
|
|
return
|
|
}
|
|
resp.Diagnostics.AddError(
|
|
"Error Deleting Traffic Block",
|
|
fmt.Sprintf("Could not delete traffic block %d on server %d: %s", data.ID.ValueInt64(), data.ServerID.ValueInt64(), err),
|
|
)
|
|
return
|
|
}
|
|
}
|