| 
 | 1 | +// This Source Code Form is subject to the terms of the Mozilla Public  | 
 | 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this  | 
 | 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/.  | 
 | 4 | + | 
 | 5 | +package provider  | 
 | 6 | + | 
 | 7 | +import (  | 
 | 8 | +	"context"  | 
 | 9 | + | 
 | 10 | +	"github.com/google/uuid"  | 
 | 11 | +	"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"  | 
 | 12 | +	"github.com/hashicorp/terraform-plugin-framework/datasource"  | 
 | 13 | +	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"  | 
 | 14 | +	"github.com/hashicorp/terraform-plugin-framework/types"  | 
 | 15 | +	"github.com/hashicorp/terraform-plugin-log/tflog"  | 
 | 16 | +	"github.com/oxidecomputer/oxide.go/oxide"  | 
 | 17 | +)  | 
 | 18 | + | 
 | 19 | +var _ datasource.DataSource = (*systemIpPoolsDataSource)(nil)  | 
 | 20 | +var _ datasource.DataSourceWithConfigure = (*systemIpPoolsDataSource)(nil)  | 
 | 21 | + | 
 | 22 | +type systemIpPoolsDataSource struct {  | 
 | 23 | +	client *oxide.Client  | 
 | 24 | +}  | 
 | 25 | + | 
 | 26 | +type systemIpPoolsDataSourceModel struct {  | 
 | 27 | +	ID       types.String        `tfsdk:"id"`  | 
 | 28 | +	Timeouts timeouts.Value      `tfsdk:"timeouts"`  | 
 | 29 | +	IpPools  []systemIpPoolModel `tfsdk:"ip_pools"`  | 
 | 30 | +}  | 
 | 31 | + | 
 | 32 | +type systemIpPoolModel struct {  | 
 | 33 | +	Description  types.String `tfsdk:"description"`  | 
 | 34 | +	ID           types.String `tfsdk:"id"`  | 
 | 35 | +	Name         types.String `tfsdk:"name"`  | 
 | 36 | +	TimeCreated  types.String `tfsdk:"time_created"`  | 
 | 37 | +	TimeModified types.String `tfsdk:"time_modified"`  | 
 | 38 | +}  | 
 | 39 | + | 
 | 40 | +// NewSystemIpPoolsDataSource initialises a system_ip_pools data source.  | 
 | 41 | +func NewSystemIpPoolsDataSource() datasource.DataSource {  | 
 | 42 | +	return &systemIpPoolsDataSource{}  | 
 | 43 | +}  | 
 | 44 | + | 
 | 45 | +func (d *systemIpPoolsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {  | 
 | 46 | +	resp.TypeName = "oxide_system_ip_pools"  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +// Configure adds the provider configured client to the data source.  | 
 | 50 | +func (d *systemIpPoolsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {  | 
 | 51 | +	if req.ProviderData == nil {  | 
 | 52 | +		return  | 
 | 53 | +	}  | 
 | 54 | + | 
 | 55 | +	d.client = req.ProviderData.(*oxide.Client)  | 
 | 56 | +}  | 
 | 57 | + | 
 | 58 | +func (d *systemIpPoolsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {  | 
 | 59 | +	resp.Schema = schema.Schema{  | 
 | 60 | +		Attributes: map[string]schema.Attribute{  | 
 | 61 | +			"id": schema.StringAttribute{  | 
 | 62 | +				Computed: true,  | 
 | 63 | +			},  | 
 | 64 | +			"timeouts": timeouts.Attributes(ctx),  | 
 | 65 | +			"ip_pools": schema.ListNestedAttribute{  | 
 | 66 | +				Computed: true,  | 
 | 67 | +				NestedObject: schema.NestedAttributeObject{  | 
 | 68 | +					Attributes: map[string]schema.Attribute{  | 
 | 69 | +						"name": schema.StringAttribute{  | 
 | 70 | +							Computed:    true,  | 
 | 71 | +							Description: "Name of the IP pool.",  | 
 | 72 | +						},  | 
 | 73 | +						"description": schema.StringAttribute{  | 
 | 74 | +							Computed:    true,  | 
 | 75 | +							Description: "Description for the IP pool.",  | 
 | 76 | +						},  | 
 | 77 | +						"id": schema.StringAttribute{  | 
 | 78 | +							Computed:    true,  | 
 | 79 | +							Description: "Unique, immutable, system-controlled identifier of the IP pool.",  | 
 | 80 | +						},  | 
 | 81 | +						"time_created": schema.StringAttribute{  | 
 | 82 | +							Computed:    true,  | 
 | 83 | +							Description: "Timestamp of when this IP pool was created.",  | 
 | 84 | +						},  | 
 | 85 | +						"time_modified": schema.StringAttribute{  | 
 | 86 | +							Computed:    true,  | 
 | 87 | +							Description: "Timestamp of when this IP pool was last modified.",  | 
 | 88 | +						},  | 
 | 89 | +					},  | 
 | 90 | +				},  | 
 | 91 | +			},  | 
 | 92 | +		},  | 
 | 93 | +	}  | 
 | 94 | +}  | 
 | 95 | + | 
 | 96 | +func (d *systemIpPoolsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {  | 
 | 97 | +	var state systemIpPoolsDataSourceModel  | 
 | 98 | + | 
 | 99 | +	// Read Terraform configuration data into the model  | 
 | 100 | +	resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)  | 
 | 101 | +	if resp.Diagnostics.HasError() {  | 
 | 102 | +		return  | 
 | 103 | +	}  | 
 | 104 | + | 
 | 105 | +	readTimeout, diags := state.Timeouts.Read(ctx, defaultTimeout())  | 
 | 106 | +	resp.Diagnostics.Append(diags...)  | 
 | 107 | +	if resp.Diagnostics.HasError() {  | 
 | 108 | +		return  | 
 | 109 | +	}  | 
 | 110 | +	ctx, cancel := context.WithTimeout(ctx, readTimeout)  | 
 | 111 | +	defer cancel()  | 
 | 112 | + | 
 | 113 | +	params := oxide.IpPoolListParams{  | 
 | 114 | +		SortBy: oxide.NameOrIdSortModeIdAscending,  | 
 | 115 | +	}  | 
 | 116 | +	ipPools, err := d.client.IpPoolListAllPages(ctx, params)  | 
 | 117 | +	if err != nil {  | 
 | 118 | +		resp.Diagnostics.AddError(  | 
 | 119 | +			"Unable to read system IP pools list:",  | 
 | 120 | +			err.Error(),  | 
 | 121 | +		)  | 
 | 122 | +		return  | 
 | 123 | +	}  | 
 | 124 | + | 
 | 125 | +	tflog.Trace(ctx, "read all pools from system")  | 
 | 126 | + | 
 | 127 | +	// Set a unique ID for the data source payload  | 
 | 128 | +	state.ID = types.StringValue(uuid.New().String())  | 
 | 129 | + | 
 | 130 | +	for _, ipPool := range ipPools {  | 
 | 131 | +		poolState := systemIpPoolModel{  | 
 | 132 | +			Description:  types.StringValue(ipPool.Description),  | 
 | 133 | +			ID:           types.StringValue(ipPool.Id),  | 
 | 134 | +			Name:         types.StringValue(string(ipPool.Name)),  | 
 | 135 | +			TimeCreated:  types.StringValue(ipPool.TimeCreated.String()),  | 
 | 136 | +			TimeModified: types.StringValue(ipPool.TimeCreated.String()),  | 
 | 137 | +		}  | 
 | 138 | + | 
 | 139 | +		state.IpPools = append(state.IpPools, poolState)  | 
 | 140 | +	}  | 
 | 141 | + | 
 | 142 | +	// Save state into Terraform state  | 
 | 143 | +	resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)  | 
 | 144 | +	if resp.Diagnostics.HasError() {  | 
 | 145 | +		return  | 
 | 146 | +	}  | 
 | 147 | +}  | 
0 commit comments