// 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 = &PackagesDataSource{} _ datasource.DataSourceWithConfigure = &PackagesDataSource{} ) // NewPackagesDataSource returns a new packages data source. func NewPackagesDataSource() datasource.DataSource { return &PackagesDataSource{} } // PackagesDataSource defines the data source implementation. type PackagesDataSource struct { client *client.Client } // PackagesDataSourceModel describes the data source data model. type PackagesDataSourceModel struct { Results types.Int64 `tfsdk:"results"` Packages []PackageItemModel `tfsdk:"packages"` } // PackageItemModel describes a single package in the list. type PackageItemModel struct { ID types.Int64 `tfsdk:"id"` Name types.String `tfsdk:"name"` Enabled types.Bool `tfsdk:"enabled"` CPUCores types.Int64 `tfsdk:"cpu_cores"` Memory types.Int64 `tfsdk:"memory"` Storage types.Int64 `tfsdk:"storage"` Traffic types.Int64 `tfsdk:"traffic"` NetworkSpeedInbound types.Int64 `tfsdk:"network_speed_inbound"` NetworkSpeedOutbound types.Int64 `tfsdk:"network_speed_outbound"` Ipv4 types.Int64 `tfsdk:"ipv4"` } func (d *PackagesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_packages" } func (d *PackagesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches all VirtFusion packages.", Attributes: map[string]schema.Attribute{ "results": resultsSchemaAttribute(), "packages": schema.ListNestedAttribute{ MarkdownDescription: "List of packages.", Computed: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The package ID.", Computed: true, }, "name": schema.StringAttribute{ MarkdownDescription: "The package name.", Computed: true, }, "enabled": schema.BoolAttribute{ MarkdownDescription: "Whether the package is enabled.", Computed: true, }, "cpu_cores": schema.Int64Attribute{ MarkdownDescription: "The number of CPU cores in the package.", Computed: true, }, "memory": schema.Int64Attribute{ MarkdownDescription: "The amount of memory in the package.", Computed: true, }, "storage": schema.Int64Attribute{ MarkdownDescription: "The amount of storage in the package.", Computed: true, }, "traffic": schema.Int64Attribute{ MarkdownDescription: "The traffic limit in the package.", Computed: true, }, "network_speed_inbound": schema.Int64Attribute{ MarkdownDescription: "The inbound network speed in the package.", Computed: true, }, "network_speed_outbound": schema.Int64Attribute{ MarkdownDescription: "The outbound network speed in the package.", Computed: true, }, "ipv4": schema.Int64Attribute{ MarkdownDescription: "The number of IPv4 addresses in the package.", Computed: true, }, }, }, }, }, } } func (d *PackagesDataSource) 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 *PackagesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data PackagesDataSourceModel rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/packages?%s", resultsQueryParam(data.Results))) if err != nil { resp.Diagnostics.AddError("Error Reading Packages", err.Error()) return } var listResp client.PackageListResponse if err := json.Unmarshal(rawResp, &listResp); err != nil { resp.Diagnostics.AddError("Error Parsing Packages Response", err.Error()) return } data.Packages = make([]PackageItemModel, len(listResp.Data)) for i, p := range listResp.Data { data.Packages[i] = PackageItemModel{ ID: types.Int64Value(p.ID), Name: types.StringValue(p.Name), Enabled: types.BoolValue(p.Enabled), CPUCores: types.Int64Value(p.CPUCores), Memory: types.Int64Value(p.Memory), Storage: types.Int64Value(p.Storage), Traffic: types.Int64Value(p.Traffic), NetworkSpeedInbound: types.Int64Value(p.NetworkSpeedInbound), NetworkSpeedOutbound: types.Int64Value(p.NetworkSpeedOutbound), Ipv4: types.Int64Value(p.Ipv4), } } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }