// 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 = &SelfServiceCurrenciesDataSource{} _ datasource.DataSourceWithConfigure = &SelfServiceCurrenciesDataSource{} ) // NewSelfServiceCurrenciesDataSource returns a new self-service currencies data source. func NewSelfServiceCurrenciesDataSource() datasource.DataSource { return &SelfServiceCurrenciesDataSource{} } // SelfServiceCurrenciesDataSource defines the data source implementation. type SelfServiceCurrenciesDataSource struct { client *client.Client } // SelfServiceCurrenciesDataSourceModel describes the data source data model. type SelfServiceCurrenciesDataSourceModel struct { Results types.Int64 `tfsdk:"results"` Currencies []CurrencyItemModel `tfsdk:"currencies"` } // CurrencyItemModel describes a single currency in the list. type CurrencyItemModel struct { ID types.Int64 `tfsdk:"id"` Code types.String `tfsdk:"code"` Name types.String `tfsdk:"name"` } func (d *SelfServiceCurrenciesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_self_service_currencies" } func (d *SelfServiceCurrenciesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches all available VirtFusion self-service currencies.", Attributes: map[string]schema.Attribute{ "results": resultsSchemaAttribute(), "currencies": schema.ListNestedAttribute{ MarkdownDescription: "List of available currencies.", Computed: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The currency ID.", Computed: true, }, "code": schema.StringAttribute{ MarkdownDescription: "The currency code.", Computed: true, }, "name": schema.StringAttribute{ MarkdownDescription: "The currency name.", Computed: true, }, }, }, }, }, } } func (d *SelfServiceCurrenciesDataSource) 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 *SelfServiceCurrenciesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data SelfServiceCurrenciesDataSourceModel rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/selfService/currencies?%s", resultsQueryParam(data.Results))) if err != nil { resp.Diagnostics.AddError("Error Reading Self-Service Currencies", err.Error()) return } var currencyResp client.CurrencyResponse if err := json.Unmarshal(rawResp, ¤cyResp); err != nil { resp.Diagnostics.AddError("Error Parsing Self-Service Currencies Response", err.Error()) return } data.Currencies = make([]CurrencyItemModel, len(currencyResp.Data)) for i, c := range currencyResp.Data { data.Currencies[i] = CurrencyItemModel{ ID: types.Int64Value(c.ID), Code: types.StringValue(c.Code), Name: types.StringValue(c.Name), } } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }