// 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 = &HypervisorsDataSource{} _ datasource.DataSourceWithConfigure = &HypervisorsDataSource{} ) // NewHypervisorsDataSource returns a new hypervisors data source. func NewHypervisorsDataSource() datasource.DataSource { return &HypervisorsDataSource{} } // HypervisorsDataSource defines the data source implementation. type HypervisorsDataSource struct { client *client.Client } // HypervisorsDataSourceModel describes the data source data model. type HypervisorsDataSourceModel struct { Results types.Int64 `tfsdk:"results"` Hypervisors []HypervisorItemModel `tfsdk:"hypervisors"` } // HypervisorItemModel describes a single hypervisor in the list. type HypervisorItemModel struct { ID types.Int64 `tfsdk:"id"` Name types.String `tfsdk:"name"` Type types.String `tfsdk:"type"` Hostname types.String `tfsdk:"hostname"` Enabled types.Bool `tfsdk:"enabled"` } func (d *HypervisorsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_hypervisors" } func (d *HypervisorsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches all VirtFusion hypervisors.", Attributes: map[string]schema.Attribute{ "results": resultsSchemaAttribute(), "hypervisors": schema.ListNestedAttribute{ MarkdownDescription: "List of hypervisors.", Computed: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The hypervisor ID.", Computed: true, }, "name": schema.StringAttribute{ MarkdownDescription: "The hypervisor name.", Computed: true, }, "type": schema.StringAttribute{ MarkdownDescription: "The hypervisor type.", Computed: true, }, "hostname": schema.StringAttribute{ MarkdownDescription: "The hypervisor hostname.", Computed: true, }, "enabled": schema.BoolAttribute{ MarkdownDescription: "Whether the hypervisor is enabled.", Computed: true, }, }, }, }, }, } } func (d *HypervisorsDataSource) 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 *HypervisorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data HypervisorsDataSourceModel rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/compute/hypervisors?%s", resultsQueryParam(data.Results))) if err != nil { resp.Diagnostics.AddError("Error Reading Hypervisors", err.Error()) return } var listResp client.HypervisorListResponse if err := json.Unmarshal(rawResp, &listResp); err != nil { resp.Diagnostics.AddError("Error Parsing Hypervisors Response", err.Error()) return } data.Hypervisors = make([]HypervisorItemModel, len(listResp.Data)) for i, h := range listResp.Data { data.Hypervisors[i] = HypervisorItemModel{ ID: types.Int64Value(h.ID), Name: types.StringValue(h.Name), Type: types.StringValue(h.Type), Hostname: types.StringValue(h.Hostname), Enabled: types.BoolValue(h.Enabled), } } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }