// 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 = &PackageDataSource{} _ datasource.DataSourceWithConfigure = &PackageDataSource{} ) // NewPackageDataSource returns a new package data source. func NewPackageDataSource() datasource.DataSource { return &PackageDataSource{} } // PackageDataSource defines the data source implementation. type PackageDataSource struct { client *client.Client } // PackageDataSourceModel describes the data source data model. type PackageDataSourceModel 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 *PackageDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_package" } func (d *PackageDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches a single VirtFusion package by ID.", Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The package ID.", Required: 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 *PackageDataSource) 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 *PackageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data PackageDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } rawResp, err := d.client.Get(ctx, fmt.Sprintf("/packages/%d", data.ID.ValueInt64())) if err != nil { resp.Diagnostics.AddError("Error Reading Package", err.Error()) return } var pkgResp client.PackageResponse if err := json.Unmarshal(rawResp, &pkgResp); err != nil { resp.Diagnostics.AddError("Error Parsing Package Response", err.Error()) return } data.ID = types.Int64Value(pkgResp.Data.ID) data.Name = types.StringValue(pkgResp.Data.Name) data.Enabled = types.BoolValue(pkgResp.Data.Enabled) data.CPUCores = types.Int64Value(pkgResp.Data.CPUCores) data.Memory = types.Int64Value(pkgResp.Data.Memory) data.Storage = types.Int64Value(pkgResp.Data.Storage) data.Traffic = types.Int64Value(pkgResp.Data.Traffic) data.NetworkSpeedInbound = types.Int64Value(pkgResp.Data.NetworkSpeedInbound) data.NetworkSpeedOutbound = types.Int64Value(pkgResp.Data.NetworkSpeedOutbound) data.Ipv4 = types.Int64Value(pkgResp.Data.Ipv4) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }