Files
terraform-provider-virtfusion/internal/provider/resource_ip_block_range.go
Andrew 6b7430b67b 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>
2026-03-16 02:01:16 -04:00

163 lines
5.4 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/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 = &IPBlockRangeResource{}
_ resource.ResourceWithConfigure = &IPBlockRangeResource{}
)
// NewIPBlockRangeResource creates a new IP block range resource.
func NewIPBlockRangeResource() resource.Resource {
return &IPBlockRangeResource{}
}
// IPBlockRangeResource defines the resource implementation.
type IPBlockRangeResource struct {
client *client.Client
}
// IPBlockRangeResourceModel describes the resource data model.
type IPBlockRangeResourceModel struct {
ID types.String `tfsdk:"id"`
IPBlockID types.Int64 `tfsdk:"ip_block_id"`
StartIP types.String `tfsdk:"start_ip"`
EndIP types.String `tfsdk:"end_ip"`
Gateway types.String `tfsdk:"gateway"`
Netmask types.String `tfsdk:"netmask"`
}
func (r *IPBlockRangeResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ip_block_range"
}
func (r *IPBlockRangeResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Adds an IPv4 address range to a VirtFusion IP block. This is a create-only resource — ranges cannot be deleted via the API.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The identifier of the IP block range.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"ip_block_id": schema.Int64Attribute{
MarkdownDescription: "The ID of the IP block to add the range to.",
Required: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
"start_ip": schema.StringAttribute{
MarkdownDescription: "The starting IP address of the range.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"end_ip": schema.StringAttribute{
MarkdownDescription: "The ending IP address of the range.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"gateway": schema.StringAttribute{
MarkdownDescription: "The gateway address for the range.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"netmask": schema.StringAttribute{
MarkdownDescription: "The netmask for the range.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
func (r *IPBlockRangeResource) 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 *IPBlockRangeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data IPBlockRangeResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
rangeReq := client.IPBlockRangeRequest{
StartIP: data.StartIP.ValueString(),
EndIP: data.EndIP.ValueString(),
Gateway: data.Gateway.ValueString(),
Netmask: data.Netmask.ValueString(),
}
apiPath := fmt.Sprintf("/connectivity/ipblocks/%d/ipv4", data.IPBlockID.ValueInt64())
_, err := r.client.Post(ctx, apiPath, rangeReq)
if err != nil {
resp.Diagnostics.AddError(
"Error Creating IP Block Range",
fmt.Sprintf("Could not create IP block range on block %d: %s", data.IPBlockID.ValueInt64(), err),
)
return
}
// Generate a composite ID since the API does not return one.
data.ID = types.StringValue(fmt.Sprintf("%d/%s-%s", data.IPBlockID.ValueInt64(), data.StartIP.ValueString(), data.EndIP.ValueString()))
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *IPBlockRangeResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data IPBlockRangeResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *IPBlockRangeResource) Update(_ context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) {
// All attributes require replacement — updates are never called.
}
func (r *IPBlockRangeResource) Delete(_ context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) {
// No delete API — removing from state only.
}