// 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 = &ServerTemplatesDataSource{} var _ datasource.DataSourceWithConfigure = &ServerTemplatesDataSource{} // NewServerTemplatesDataSource returns a new data source for listing server templates. func NewServerTemplatesDataSource() datasource.DataSource { return &ServerTemplatesDataSource{} } // ServerTemplatesDataSource defines the data source implementation. type ServerTemplatesDataSource struct { client *client.Client } // ServerTemplatesDataSourceModel describes the data source data model. type ServerTemplatesDataSourceModel struct { ServerID types.Int64 `tfsdk:"server_id"` Results types.Int64 `tfsdk:"results"` Templates types.List `tfsdk:"templates"` } func (d *ServerTemplatesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_server_templates" } func (d *ServerTemplatesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Use this data source to list available templates for a VirtFusion server.", Attributes: map[string]schema.Attribute{ "server_id": schema.Int64Attribute{ MarkdownDescription: "The server ID to list templates for.", Required: true, }, "results": resultsSchemaAttribute(), "templates": schema.ListNestedAttribute{ MarkdownDescription: "The list of available templates.", Computed: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The template ID.", Computed: true, }, "name": schema.StringAttribute{ MarkdownDescription: "The template name.", Computed: true, }, }, }, }, }, } } func (d *ServerTemplatesDataSource) 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 *ServerTemplatesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data ServerTemplatesDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } result, err := d.client.GetAllPages(ctx, fmt.Sprintf("/servers/%d/templates?%s", data.ServerID.ValueInt64(), resultsQueryParam(data.Results))) if err != nil { resp.Diagnostics.AddError("Error Reading Server Templates", err.Error()) return } var templateResp client.TemplateResponse if err := json.Unmarshal(result, &templateResp); err != nil { resp.Diagnostics.AddError("Error Parsing Server Templates Response", err.Error()) return } templateAttrTypes := map[string]attr.Type{ "id": types.Int64Type, "name": types.StringType, } templateObjects := make([]attr.Value, len(templateResp.Data)) for i, t := range templateResp.Data { obj, diags := types.ObjectValue( templateAttrTypes, map[string]attr.Value{ "id": types.Int64Value(t.ID), "name": types.StringValue(t.Name), }, ) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } templateObjects[i] = obj } templatesList, diags := types.ListValue(types.ObjectType{AttrTypes: templateAttrTypes}, templateObjects) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } data.Templates = templatesList resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }