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:
128
internal/provider/data_source_user.go
Normal file
128
internal/provider/data_source_user.go
Normal file
@@ -0,0 +1,128 @@
|
||||
// Copyright (c) EZSCALE.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"terraform-provider-virtfusion/internal/client"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
var (
|
||||
_ datasource.DataSource = &UserDataSource{}
|
||||
_ datasource.DataSourceWithConfigure = &UserDataSource{}
|
||||
)
|
||||
|
||||
// NewUserDataSource returns a new user data source.
|
||||
func NewUserDataSource() datasource.DataSource {
|
||||
return &UserDataSource{}
|
||||
}
|
||||
|
||||
// UserDataSource defines the data source implementation.
|
||||
type UserDataSource struct {
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
// UserDataSourceModel describes the data source data model.
|
||||
type UserDataSourceModel struct {
|
||||
ExtRelationID types.String `tfsdk:"ext_relation_id"`
|
||||
ID types.Int64 `tfsdk:"id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Email types.String `tfsdk:"email"`
|
||||
Enabled types.Bool `tfsdk:"enabled"`
|
||||
CreatedAt types.String `tfsdk:"created_at"`
|
||||
UpdatedAt types.String `tfsdk:"updated_at"`
|
||||
}
|
||||
|
||||
func (d *UserDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_user"
|
||||
}
|
||||
|
||||
func (d *UserDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Fetches a VirtFusion user by external relation ID.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"ext_relation_id": schema.StringAttribute{
|
||||
MarkdownDescription: "The external relation ID of the user.",
|
||||
Required: true,
|
||||
},
|
||||
"id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The numeric ID of the user.",
|
||||
Computed: true,
|
||||
},
|
||||
"name": schema.StringAttribute{
|
||||
MarkdownDescription: "The user name.",
|
||||
Computed: true,
|
||||
},
|
||||
"email": schema.StringAttribute{
|
||||
MarkdownDescription: "The user email address.",
|
||||
Computed: true,
|
||||
},
|
||||
"enabled": schema.BoolAttribute{
|
||||
MarkdownDescription: "Whether the user is enabled.",
|
||||
Computed: true,
|
||||
},
|
||||
"created_at": schema.StringAttribute{
|
||||
MarkdownDescription: "The creation timestamp.",
|
||||
Computed: true,
|
||||
},
|
||||
"updated_at": schema.StringAttribute{
|
||||
MarkdownDescription: "The last update timestamp.",
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *UserDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
c, ok := req.ProviderData.(*client.Client)
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError(
|
||||
"Unexpected Data Source Configure Type",
|
||||
fmt.Sprintf("Expected *client.Client, got: %T.", req.ProviderData),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
d.client = c
|
||||
}
|
||||
|
||||
func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data UserDataSourceModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
rawResp, err := d.client.Get(ctx, fmt.Sprintf("/users/%s/byExtRelation", data.ExtRelationID.ValueString()))
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Error Reading User", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var userResp client.UserResponse
|
||||
if err := json.Unmarshal(rawResp, &userResp); err != nil {
|
||||
resp.Diagnostics.AddError("Error Parsing User Response", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = types.Int64Value(userResp.Data.ID)
|
||||
data.Name = types.StringValue(userResp.Data.Name)
|
||||
data.Email = types.StringValue(userResp.Data.Email)
|
||||
data.Enabled = types.BoolValue(userResp.Data.Enabled)
|
||||
data.CreatedAt = types.StringValue(userResp.Data.CreatedAt)
|
||||
data.UpdatedAt = types.StringValue(userResp.Data.UpdatedAt)
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
Reference in New Issue
Block a user