// 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 = &IPBlocksDataSource{} _ datasource.DataSourceWithConfigure = &IPBlocksDataSource{} ) // NewIPBlocksDataSource returns a new IP blocks data source. func NewIPBlocksDataSource() datasource.DataSource { return &IPBlocksDataSource{} } // IPBlocksDataSource defines the data source implementation. type IPBlocksDataSource struct { client *client.Client } // IPBlocksDataSourceModel describes the data source data model. type IPBlocksDataSourceModel struct { Results types.Int64 `tfsdk:"results"` IPBlocks []IPBlockItemModel `tfsdk:"ip_blocks"` } // IPBlockItemModel describes a single IP block in the list. type IPBlockItemModel 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 *IPBlocksDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_ip_blocks" } func (d *IPBlocksDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ MarkdownDescription: "Fetches all VirtFusion IP blocks.", Attributes: map[string]schema.Attribute{ "results": resultsSchemaAttribute(), "ip_blocks": schema.ListNestedAttribute{ MarkdownDescription: "List of IP blocks.", Computed: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ "id": schema.Int64Attribute{ MarkdownDescription: "The IP block ID.", Computed: 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 *IPBlocksDataSource) 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 *IPBlocksDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data IPBlocksDataSourceModel rawResp, err := d.client.GetAllPages(ctx, fmt.Sprintf("/connectivity/ipblocks?%s", resultsQueryParam(data.Results))) if err != nil { resp.Diagnostics.AddError("Error Reading IP Blocks", err.Error()) return } var listResp client.IPBlockListResponse if err := json.Unmarshal(rawResp, &listResp); err != nil { resp.Diagnostics.AddError("Error Parsing IP Blocks Response", err.Error()) return } data.IPBlocks = make([]IPBlockItemModel, len(listResp.Data)) for i, b := range listResp.Data { data.IPBlocks[i] = IPBlockItemModel{ ID: types.Int64Value(b.ID), Name: types.StringValue(b.Name), Type: types.Int64Value(b.Type), Gateway: types.StringValue(b.IPv4.Gateway), Netmask: types.StringValue(b.IPv4.Netmask), Enabled: types.BoolValue(b.Enabled), } } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }