// 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 = &IPBlockDataSource{} _ datasource.DataSourceWithConfigure = &IPBlockDataSource{} ) // NewIPBlockDataSource returns a new IP block data source. func NewIPBlockDataSource() datasource.DataSource { return &IPBlockDataSource{} } // IPBlockDataSource defines the data source implementation. type IPBlockDataSource struct { client *client.Client } // IPBlockDataSourceModel describes the data source data model. type IPBlockDataSourceModel struct { ID types.Int64 `tfsdk:"id"` Name types.String `tfsdk:"name"` Type types.Int64 `tfsdk:"type"` Gateway types.String `tfsdk:"gateway"` Netmask types.String `tfsdk:"netmask"` Enabled types.Bool `tfsdk:"enabled"` } func (d *IPBlockDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_ip_block" } func (d *IPBlockDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches a single VirtFusion IP block by ID.", Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The IP block ID.", Required: true, }, "name": schema.StringAttribute{ MarkdownDescription: "The IP block name.", Computed: true, }, "type": schema.Int64Attribute{ MarkdownDescription: "The IP block type (4 = IPv4, 6 = IPv6).", Computed: true, }, "gateway": schema.StringAttribute{ MarkdownDescription: "The IPv4 gateway address.", Computed: true, }, "netmask": schema.StringAttribute{ MarkdownDescription: "The IPv4 netmask.", Computed: true, }, "enabled": schema.BoolAttribute{ MarkdownDescription: "Whether the IP block is enabled.", Computed: true, }, }, } } func (d *IPBlockDataSource) 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 *IPBlockDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data IPBlockDataSourceModel resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } rawResp, err := d.client.Get(ctx, fmt.Sprintf("/connectivity/ipblocks/%d", data.ID.ValueInt64())) if err != nil { resp.Diagnostics.AddError("Error Reading IP Block", err.Error()) return } var blockResp client.IPBlockResponse if err := json.Unmarshal(rawResp, &blockResp); err != nil { resp.Diagnostics.AddError("Error Parsing IP Block Response", err.Error()) return } data.ID = types.Int64Value(blockResp.Data.ID) data.Name = types.StringValue(blockResp.Data.Name) data.Type = types.Int64Value(blockResp.Data.Type) data.Gateway = types.StringValue(blockResp.Data.IPv4.Gateway) data.Netmask = types.StringValue(blockResp.Data.IPv4.Netmask) data.Enabled = types.BoolValue(blockResp.Data.Enabled) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }