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