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:
226
internal/provider/resource_ssh_key.go
Normal file
226
internal/provider/resource_ssh_key.go
Normal file
@@ -0,0 +1,226 @@
|
||||
// Copyright (c) EZSCALE.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"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/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 = &SSHKeyResource{}
|
||||
_ resource.ResourceWithConfigure = &SSHKeyResource{}
|
||||
_ resource.ResourceWithImportState = &SSHKeyResource{}
|
||||
)
|
||||
|
||||
// NewSSHKeyResource creates a new SSH key resource.
|
||||
func NewSSHKeyResource() resource.Resource {
|
||||
return &SSHKeyResource{}
|
||||
}
|
||||
|
||||
// SSHKeyResource defines the resource implementation.
|
||||
type SSHKeyResource struct {
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
// SSHKeyResourceModel describes the resource data model.
|
||||
type SSHKeyResourceModel struct {
|
||||
ID types.Int64 `tfsdk:"id"`
|
||||
UserID types.Int64 `tfsdk:"user_id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
PublicKey types.String `tfsdk:"public_key"`
|
||||
}
|
||||
|
||||
func (r *SSHKeyResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_ssh_key"
|
||||
}
|
||||
|
||||
func (r *SSHKeyResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Manages a VirtFusion SSH key.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The ID of the SSH key.",
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.Int64{
|
||||
int64planmodifier.UseStateForUnknown(),
|
||||
},
|
||||
},
|
||||
"user_id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The ID of the user who owns this SSH key.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.Int64{
|
||||
int64planmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
MarkdownDescription: "The name of the SSH key.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"public_key": schema.StringAttribute{
|
||||
MarkdownDescription: "The public key content.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SSHKeyResource) 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 *SSHKeyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data SSHKeyResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
createReq := client.SSHKeyCreateRequest{
|
||||
UserID: data.UserID.ValueInt64(),
|
||||
Name: data.Name.ValueString(),
|
||||
PublicKey: data.PublicKey.ValueString(),
|
||||
}
|
||||
|
||||
respBody, err := r.client.Post(ctx, "/ssh_keys", createReq)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Creating SSH Key",
|
||||
fmt.Sprintf("Could not create SSH key: %s", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var sshKeyResp client.SSHKeyResponse
|
||||
if err := json.Unmarshal(respBody, &sshKeyResp); err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Parsing Response",
|
||||
fmt.Sprintf("Could not parse SSH key response: %s", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = types.Int64Value(sshKeyResp.Data.ID)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *SSHKeyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data SSHKeyResourceModel
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
apiPath := fmt.Sprintf("/ssh_keys/%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() {
|
||||
// SSH key no longer exists, remove from state.
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Reading SSH Key",
|
||||
fmt.Sprintf("Could not read SSH key %d: %s", data.ID.ValueInt64(), err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var sshKeyResp client.SSHKeyResponse
|
||||
if err := json.Unmarshal(respBody, &sshKeyResp); err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Parsing Response",
|
||||
fmt.Sprintf("Could not parse SSH key response: %s", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Update state from API response.
|
||||
data.Name = types.StringValue(sshKeyResp.Data.Name)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *SSHKeyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data SSHKeyResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *SSHKeyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data SSHKeyResourceModel
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
apiPath := fmt.Sprintf("/ssh_keys/%d", data.ID.ValueInt64())
|
||||
_, err := r.client.Delete(ctx, apiPath)
|
||||
if err != nil {
|
||||
var apiErr *client.APIError
|
||||
if errors.As(err, &apiErr) && apiErr.IsNotFound() {
|
||||
// Already deleted, nothing to do.
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.AddError(
|
||||
"Error Deleting SSH Key",
|
||||
fmt.Sprintf("Could not delete SSH key %d: %s", data.ID.ValueInt64(), err),
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SSHKeyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
id, err := strconv.ParseInt(req.ID, 10, 64)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Invalid Import ID",
|
||||
fmt.Sprintf("Could not parse SSH key ID %q as integer: %s", req.ID, err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), types.Int64Value(id))...)
|
||||
}
|
||||
Reference in New Issue
Block a user