// 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 = &HypervisorGroupDataSource{} _ datasource.DataSourceWithConfigure = &HypervisorGroupDataSource{} ) // NewHypervisorGroupDataSource returns a new hypervisor group data source. func NewHypervisorGroupDataSource() datasource.DataSource { return &HypervisorGroupDataSource{} } // HypervisorGroupDataSource defines the data source implementation. type HypervisorGroupDataSource struct { client *client.Client } // HypervisorGroupDataSourceModel describes the data source data model. type HypervisorGroupDataSourceModel struct { ID types.Int64 `tfsdk:"id"` Name types.String `tfsdk:"name"` Enabled types.Bool `tfsdk:"enabled"` } func (d *HypervisorGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_hypervisor_group" } func (d *HypervisorGroupDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches a single VirtFusion hypervisor group by ID.", Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The hypervisor group ID.", Required: 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 *HypervisorGroupDataSource) 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 *HypervisorGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data HypervisorGroupDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } rawResp, err := d.client.Get(ctx, fmt.Sprintf("/compute/hypervisors/groups/%d", data.ID.ValueInt64())) if err != nil { resp.Diagnostics.AddError("Error Reading Hypervisor Group", err.Error()) return } var groupResp client.HypervisorGroupResponse if err := json.Unmarshal(rawResp, &groupResp); err != nil { resp.Diagnostics.AddError("Error Parsing Hypervisor Group Response", err.Error()) return } data.ID = types.Int64Value(groupResp.Data.ID) data.Name = types.StringValue(groupResp.Data.Name) data.Enabled = types.BoolValue(groupResp.Data.Enabled) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }