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:
138
internal/provider/data_source_server_traffic_blocks.go
Normal file
138
internal/provider/data_source_server_traffic_blocks.go
Normal file
@@ -0,0 +1,138 @@
|
||||
// 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/attr"
|
||||
"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 = &ServerTrafficBlocksDataSource{}
|
||||
var _ datasource.DataSourceWithConfigure = &ServerTrafficBlocksDataSource{}
|
||||
|
||||
// NewServerTrafficBlocksDataSource returns a new data source for listing server traffic blocks.
|
||||
func NewServerTrafficBlocksDataSource() datasource.DataSource {
|
||||
return &ServerTrafficBlocksDataSource{}
|
||||
}
|
||||
|
||||
// ServerTrafficBlocksDataSource defines the data source implementation.
|
||||
type ServerTrafficBlocksDataSource struct {
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
// ServerTrafficBlocksDataSourceModel describes the data source data model.
|
||||
type ServerTrafficBlocksDataSourceModel struct {
|
||||
ServerID types.Int64 `tfsdk:"server_id"`
|
||||
Results types.Int64 `tfsdk:"results"`
|
||||
Blocks types.List `tfsdk:"blocks"`
|
||||
}
|
||||
|
||||
func (d *ServerTrafficBlocksDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_server_traffic_blocks"
|
||||
}
|
||||
|
||||
func (d *ServerTrafficBlocksDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: "Use this data source to list traffic blocks for a VirtFusion server.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"server_id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The server ID to list traffic blocks for.",
|
||||
Required: true,
|
||||
},
|
||||
"results": resultsSchemaAttribute(),
|
||||
"blocks": schema.ListNestedAttribute{
|
||||
MarkdownDescription: "The list of traffic blocks.",
|
||||
Computed: true,
|
||||
NestedObject: schema.NestedAttributeObject{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.Int64Attribute{
|
||||
MarkdownDescription: "The traffic block ID.",
|
||||
Computed: true,
|
||||
},
|
||||
"type": schema.StringAttribute{
|
||||
MarkdownDescription: "The traffic block type.",
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *ServerTrafficBlocksDataSource) 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. Please report this issue to the provider developers.", req.ProviderData),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
d.client = c
|
||||
}
|
||||
|
||||
func (d *ServerTrafficBlocksDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data ServerTrafficBlocksDataSourceModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := d.client.GetAllPages(ctx, fmt.Sprintf("/servers/%d/traffic/blocks?%s", data.ServerID.ValueInt64(), resultsQueryParam(data.Results)))
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Error Reading Server Traffic Blocks", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var blocksResp client.TrafficBlockListResponse
|
||||
if err := json.Unmarshal(result, &blocksResp); err != nil {
|
||||
resp.Diagnostics.AddError("Error Parsing Server Traffic Blocks Response", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
blockAttrTypes := map[string]attr.Type{
|
||||
"id": types.Int64Type,
|
||||
"type": types.StringType,
|
||||
}
|
||||
|
||||
blockObjects := make([]attr.Value, len(blocksResp.Data))
|
||||
for i, b := range blocksResp.Data {
|
||||
obj, diags := types.ObjectValue(
|
||||
blockAttrTypes,
|
||||
map[string]attr.Value{
|
||||
"id": types.Int64Value(b.ID),
|
||||
"type": types.StringValue(b.Type),
|
||||
},
|
||||
)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
blockObjects[i] = obj
|
||||
}
|
||||
|
||||
blocksList, diags := types.ListValue(types.ObjectType{AttrTypes: blockAttrTypes}, blockObjects)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
data.Blocks = blocksList
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
Reference in New Issue
Block a user