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:
215
internal/provider/resource_self_service_resource_pack.go
Normal file
215
internal/provider/resource_self_service_resource_pack.go
Normal file
@@ -0,0 +1,215 @@
|
||||
// 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/types"
|
||||
)
|
||||
|
||||
// Ensure provider-defined types fully satisfy framework interfaces.
|
||||
var (
|
||||
_ resource.Resource = &SelfServiceResourcePackResource{}
|
||||
_ resource.ResourceWithConfigure = &SelfServiceResourcePackResource{}
|
||||
)
|
||||
|
||||
// NewSelfServiceResourcePackResource creates a new self-service resource pack resource.
|
||||
func NewSelfServiceResourcePackResource() resource.Resource {
|
||||
return &SelfServiceResourcePackResource{}
|
||||
}
|
||||
|
||||
// SelfServiceResourcePackResource defines the resource implementation.
|
||||
type SelfServiceResourcePackResource struct {
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
// SelfServiceResourcePackResourceModel describes the resource data model.
|
||||
type SelfServiceResourcePackResourceModel struct {
|
||||
ID types.Int64 `tfsdk:"id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
UserID types.Int64 `tfsdk:"user_id"`
|
||||
PackID types.Int64 `tfsdk:"pack_id"`
|
||||
}
|
||||
|
||||
func (r *SelfServiceResourcePackResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_self_service_resource_pack"
|
||||
}
|
||||
|
||||
func (r *SelfServiceResourcePackResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Manages a self-service resource pack in VirtFusion.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The identifier of the resource pack.",
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.Int64{
|
||||
int64planmodifier.UseStateForUnknown(),
|
||||
},
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
MarkdownDescription: "The name of the resource pack.",
|
||||
Required: true,
|
||||
},
|
||||
"user_id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The ID of the user who owns the resource pack.",
|
||||
Required: true,
|
||||
},
|
||||
"pack_id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The ID of the pack.",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SelfServiceResourcePackResource) 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 *SelfServiceResourcePackResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data SelfServiceResourcePackResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
packReq := client.SelfServiceResourcePackRequest{
|
||||
Name: data.Name.ValueString(),
|
||||
UserID: data.UserID.ValueInt64(),
|
||||
PackID: data.PackID.ValueInt64(),
|
||||
}
|
||||
|
||||
respBody, err := r.client.Post(ctx, "/selfService/resourcePack", packReq)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Creating Resource Pack",
|
||||
fmt.Sprintf("Could not create resource pack: %s", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var packResp client.SelfServiceResourcePackResponse
|
||||
if err := json.Unmarshal(respBody, &packResp); err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Parsing Response",
|
||||
fmt.Sprintf("Could not parse resource pack response: %s", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = types.Int64Value(packResp.Data.ID)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *SelfServiceResourcePackResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data SelfServiceResourcePackResourceModel
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
apiPath := fmt.Sprintf("/selfService/resourcePack/%d", data.ID.ValueInt64())
|
||||
respBody, err := r.client.Get(ctx, apiPath)
|
||||
if err != nil {
|
||||
var apiErr *client.APIError
|
||||
if errors.As(err, &apiErr) && apiErr.IsNotFound() {
|
||||
// Resource no longer exists, remove from state.
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Reading Resource Pack",
|
||||
fmt.Sprintf("Could not read resource pack %d: %s", data.ID.ValueInt64(), err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var packResp client.SelfServiceResourcePackResponse
|
||||
if err := json.Unmarshal(respBody, &packResp); err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Parsing Response",
|
||||
fmt.Sprintf("Could not parse resource pack response: %s", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
data.Name = types.StringValue(packResp.Data.Name)
|
||||
data.UserID = types.Int64Value(packResp.Data.UserID)
|
||||
data.PackID = types.Int64Value(packResp.Data.PackID)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *SelfServiceResourcePackResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data SelfServiceResourcePackResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
packReq := client.SelfServiceResourcePackRequest{
|
||||
Name: data.Name.ValueString(),
|
||||
UserID: data.UserID.ValueInt64(),
|
||||
PackID: data.PackID.ValueInt64(),
|
||||
}
|
||||
|
||||
apiPath := fmt.Sprintf("/selfService/resourcePack/%d", data.ID.ValueInt64())
|
||||
_, err := r.client.Put(ctx, apiPath, packReq)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Updating Resource Pack",
|
||||
fmt.Sprintf("Could not update resource pack %d: %s", data.ID.ValueInt64(), err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *SelfServiceResourcePackResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data SelfServiceResourcePackResourceModel
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
apiPath := fmt.Sprintf("/selfService/resourcePack/%d", 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 Resource Pack",
|
||||
fmt.Sprintf("Could not delete resource pack %d: %s", data.ID.ValueInt64(), err),
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user