// 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 = &HypervisorGroupResourcesDataSource{} _ datasource.DataSourceWithConfigure = &HypervisorGroupResourcesDataSource{} ) // NewHypervisorGroupResourcesDataSource returns a new hypervisor group resources data source. func NewHypervisorGroupResourcesDataSource() datasource.DataSource { return &HypervisorGroupResourcesDataSource{} } // HypervisorGroupResourcesDataSource defines the data source implementation. type HypervisorGroupResourcesDataSource struct { client *client.Client } // HypervisorGroupResourcesDataSourceModel describes the data source data model. type HypervisorGroupResourcesDataSourceModel struct { ID types.Int64 `tfsdk:"id"` Results types.Int64 `tfsdk:"results"` CPUCores types.Int64 `tfsdk:"cpu_cores"` Memory types.Int64 `tfsdk:"memory"` Storage types.Int64 `tfsdk:"storage"` } func (d *HypervisorGroupResourcesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_hypervisor_group_resources" } func (d *HypervisorGroupResourcesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches resource information for a VirtFusion hypervisor group.", Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The hypervisor group ID.", Required: true, }, "results": resultsSchemaAttribute(), "cpu_cores": schema.Int64Attribute{ MarkdownDescription: "The number of CPU cores available in the group.", Computed: true, }, "memory": schema.Int64Attribute{ MarkdownDescription: "The amount of memory available in the group.", Computed: true, }, "storage": schema.Int64Attribute{ MarkdownDescription: "The amount of storage available in the group.", Computed: true, }, }, } } func (d *HypervisorGroupResourcesDataSource) 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 *HypervisorGroupResourcesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data HypervisorGroupResourcesDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/compute/hypervisors/groups/%d/resources?%s", data.ID.ValueInt64(), resultsQueryParam(data.Results))) if err != nil { resp.Diagnostics.AddError("Error Reading Hypervisor Group Resources", err.Error()) return } var resourcesResp client.HypervisorGroupResourcesResponse if err := json.Unmarshal(rawResp, &resourcesResp); err != nil { resp.Diagnostics.AddError("Error Parsing Hypervisor Group Resources Response", err.Error()) return } // Aggregate resource totals across all hypervisors in the group. var totalCPU, totalMemory, totalStorage int64 for _, entry := range resourcesResp.Data { totalCPU += entry.Resources.CPUCores.Max totalMemory += entry.Resources.Memory.Max totalStorage += entry.Resources.LocalStorage.Max } data.CPUCores = types.Int64Value(totalCPU) data.Memory = types.Int64Value(totalMemory) data.Storage = types.Int64Value(totalStorage) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }