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>
174 lines
5.5 KiB
Go
174 lines
5.5 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/float64planmodifier"
|
|
"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 = &SelfServiceCreditResource{}
|
|
_ resource.ResourceWithConfigure = &SelfServiceCreditResource{}
|
|
)
|
|
|
|
// NewSelfServiceCreditResource creates a new self-service credit resource.
|
|
func NewSelfServiceCreditResource() resource.Resource {
|
|
return &SelfServiceCreditResource{}
|
|
}
|
|
|
|
// SelfServiceCreditResource defines the resource implementation.
|
|
type SelfServiceCreditResource struct {
|
|
client *client.Client
|
|
}
|
|
|
|
// SelfServiceCreditResourceModel describes the resource data model.
|
|
type SelfServiceCreditResourceModel struct {
|
|
ID types.Int64 `tfsdk:"id"`
|
|
Amount types.Float64 `tfsdk:"amount"`
|
|
CurrencyCode types.String `tfsdk:"currency_code"`
|
|
UserID types.Int64 `tfsdk:"user_id"`
|
|
}
|
|
|
|
func (r *SelfServiceCreditResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_self_service_credit"
|
|
}
|
|
|
|
func (r *SelfServiceCreditResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
MarkdownDescription: "Manages self-service credit in VirtFusion. Deleting this resource cancels the credit.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.Int64Attribute{
|
|
MarkdownDescription: "The identifier of the credit entry.",
|
|
Computed: true,
|
|
PlanModifiers: []planmodifier.Int64{
|
|
int64planmodifier.UseStateForUnknown(),
|
|
},
|
|
},
|
|
"amount": schema.Float64Attribute{
|
|
MarkdownDescription: "The credit amount.",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.Float64{
|
|
float64planmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"currency_code": schema.StringAttribute{
|
|
MarkdownDescription: "The currency code (e.g. `USD`, `EUR`).",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{
|
|
stringplanmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
"user_id": schema.Int64Attribute{
|
|
MarkdownDescription: "The ID of the user to add credit to.",
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.Int64{
|
|
int64planmodifier.RequiresReplace(),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *SelfServiceCreditResource) 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 *SelfServiceCreditResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var data SelfServiceCreditResourceModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
creditReq := client.SelfServiceCreditRequest{
|
|
Amount: data.Amount.ValueFloat64(),
|
|
CurrencyCode: data.CurrencyCode.ValueString(),
|
|
UserID: data.UserID.ValueInt64(),
|
|
}
|
|
|
|
respBody, err := r.client.Post(ctx, "/selfService/credit", creditReq)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error Creating Credit",
|
|
fmt.Sprintf("Could not create credit for user %d: %s", data.UserID.ValueInt64(), err),
|
|
)
|
|
return
|
|
}
|
|
|
|
var creditResp client.SelfServiceCreditResponse
|
|
if err := json.Unmarshal(respBody, &creditResp); err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error Parsing Response",
|
|
fmt.Sprintf("Could not parse credit response: %s", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
data.ID = types.Int64Value(creditResp.Data.ID)
|
|
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *SelfServiceCreditResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var data SelfServiceCreditResourceModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
|
}
|
|
|
|
func (r *SelfServiceCreditResource) Update(_ context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) {
|
|
// All attributes require replacement — updates are never called.
|
|
}
|
|
|
|
func (r *SelfServiceCreditResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var data SelfServiceCreditResourceModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
apiPath := fmt.Sprintf("/selfService/credit/%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 Cancelling Credit",
|
|
fmt.Sprintf("Could not cancel credit %d: %s", data.ID.ValueInt64(), err),
|
|
)
|
|
return
|
|
}
|
|
}
|