// 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 = &ServerTrafficDataSource{} var _ datasource.DataSourceWithConfigure = &ServerTrafficDataSource{} // NewServerTrafficDataSource returns a new data source for reading server traffic. func NewServerTrafficDataSource() datasource.DataSource { return &ServerTrafficDataSource{} } // ServerTrafficDataSource defines the data source implementation. type ServerTrafficDataSource struct { client *client.Client } // ServerTrafficDataSourceModel describes the data source data model. type ServerTrafficDataSourceModel struct { ServerID types.Int64 `tfsdk:"server_id"` Used types.Int64 `tfsdk:"used"` Limit types.Int64 `tfsdk:"limit"` } func (d *ServerTrafficDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_server_traffic" } func (d *ServerTrafficDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Use this data source to read traffic usage for a VirtFusion server.", Attributes: map[string]schema.Attribute{ "server_id": schema.Int64Attribute{ MarkdownDescription: "The server ID to read traffic for.", Required: true, }, "used": schema.Int64Attribute{ MarkdownDescription: "The amount of traffic used.", Computed: true, }, "limit": schema.Int64Attribute{ MarkdownDescription: "The traffic limit.", Computed: true, }, }, } } func (d *ServerTrafficDataSource) 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 *ServerTrafficDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data ServerTrafficDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } result, err := d.client.Get(ctx, fmt.Sprintf("/servers/%d/traffic", data.ServerID.ValueInt64())) if err != nil { resp.Diagnostics.AddError("Error Reading Server Traffic", err.Error()) return } var trafficResp client.TrafficResponse if err := json.Unmarshal(result, &trafficResp); err != nil { resp.Diagnostics.AddError("Error Parsing Server Traffic Response", err.Error()) return } data.Used = types.Int64Value(trafficResp.Data.Used) data.Limit = types.Int64Value(trafficResp.Data.Limit) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }