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