|
| 1 | +// Copyright (c) Platform9 Systems, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// |
| 4 | +// Ported from terraform-provider-openstack v3.4.0 |
| 5 | +// (openstack/data_source_openstack_networking_floatingip_v2.go), adapted for the |
| 6 | +// terraform-plugin-framework and PCD. |
| 7 | + |
| 8 | +package networking |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + |
| 14 | + "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | + |
| 19 | + "github.com/platform9/terraform-provider-pcd/internal/clients" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + _ datasource.DataSource = (*floatingIPDataSource)(nil) |
| 24 | + _ datasource.DataSourceWithConfigure = (*floatingIPDataSource)(nil) |
| 25 | +) |
| 26 | + |
| 27 | +// NewFloatingIPDataSource is the factory registered with the provider. |
| 28 | +func NewFloatingIPDataSource() datasource.DataSource { |
| 29 | + return &floatingIPDataSource{} |
| 30 | +} |
| 31 | + |
| 32 | +type floatingIPDataSource struct { |
| 33 | + config *clients.Config |
| 34 | +} |
| 35 | + |
| 36 | +type floatingIPDataSourceModel struct { |
| 37 | + ID types.String `tfsdk:"id"` |
| 38 | + Address types.String `tfsdk:"address"` |
| 39 | + Description types.String `tfsdk:"description"` |
| 40 | + FloatingNetworkID types.String `tfsdk:"floating_network_id"` |
| 41 | + PortID types.String `tfsdk:"port_id"` |
| 42 | + FixedIP types.String `tfsdk:"fixed_ip"` |
| 43 | + Status types.String `tfsdk:"status"` |
| 44 | + RouterID types.String `tfsdk:"router_id"` |
| 45 | + TenantID types.String `tfsdk:"tenant_id"` |
| 46 | + Region types.String `tfsdk:"region"` |
| 47 | +} |
| 48 | + |
| 49 | +func (d *floatingIPDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 50 | + resp.TypeName = req.ProviderTypeName + "_networking_floatingip" |
| 51 | +} |
| 52 | + |
| 53 | +func (d *floatingIPDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 54 | + resp.Schema = schema.Schema{ |
| 55 | + MarkdownDescription: "Look up a floating IP by address or filters. Exactly one floating IP must match.", |
| 56 | + Attributes: map[string]schema.Attribute{ |
| 57 | + "id": schema.StringAttribute{Computed: true, MarkdownDescription: "The floating IP ID."}, |
| 58 | + "address": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the floating IP address."}, |
| 59 | + "description": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the description."}, |
| 60 | + "floating_network_id": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the external network ID."}, |
| 61 | + "port_id": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the associated port."}, |
| 62 | + "fixed_ip": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the mapped fixed IP."}, |
| 63 | + "status": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the operational status."}, |
| 64 | + "router_id": schema.StringAttribute{Computed: true, MarkdownDescription: "The router through which the floating IP is routed."}, |
| 65 | + "tenant_id": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the owning project."}, |
| 66 | + "region": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "The region. Defaults to the provider's region."}, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (d *floatingIPDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 72 | + d.config = configureClient(req.ProviderData, &resp.Diagnostics) |
| 73 | +} |
| 74 | + |
| 75 | +func (d *floatingIPDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 76 | + var data floatingIPDataSourceModel |
| 77 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 78 | + if resp.Diagnostics.HasError() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + client, err := d.config.NetworkV2Client() |
| 83 | + if err != nil { |
| 84 | + resp.Diagnostics.AddError("networking: building v2 client", err.Error()) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + listOpts := floatingips.ListOpts{ |
| 89 | + FloatingIP: data.Address.ValueString(), |
| 90 | + Description: data.Description.ValueString(), |
| 91 | + FloatingNetworkID: data.FloatingNetworkID.ValueString(), |
| 92 | + PortID: data.PortID.ValueString(), |
| 93 | + FixedIP: data.FixedIP.ValueString(), |
| 94 | + Status: data.Status.ValueString(), |
| 95 | + TenantID: data.TenantID.ValueString(), |
| 96 | + } |
| 97 | + pages, err := floatingips.List(client, listOpts).AllPages(ctx) |
| 98 | + if err != nil { |
| 99 | + resp.Diagnostics.AddError("networking: listing floating IPs", err.Error()) |
| 100 | + return |
| 101 | + } |
| 102 | + all, err := floatingips.ExtractFloatingIPs(pages) |
| 103 | + if err != nil { |
| 104 | + resp.Diagnostics.AddError("networking: extracting floating IPs", err.Error()) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + var fip *floatingips.FloatingIP |
| 109 | + switch len(all) { |
| 110 | + case 0: |
| 111 | + resp.Diagnostics.AddError("No floating IP found", "No floating IP matched the given criteria.") |
| 112 | + return |
| 113 | + case 1: |
| 114 | + fip = &all[0] |
| 115 | + default: |
| 116 | + resp.Diagnostics.AddError("Multiple floating IPs found", |
| 117 | + fmt.Sprintf("%d floating IPs matched; refine the filters.", len(all))) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + data.ID = types.StringValue(fip.ID) |
| 122 | + data.Address = types.StringValue(fip.FloatingIP) |
| 123 | + data.Description = types.StringValue(fip.Description) |
| 124 | + data.FloatingNetworkID = types.StringValue(fip.FloatingNetworkID) |
| 125 | + data.PortID = types.StringValue(fip.PortID) |
| 126 | + data.FixedIP = types.StringValue(fip.FixedIP) |
| 127 | + data.Status = types.StringValue(fip.Status) |
| 128 | + data.RouterID = types.StringValue(fip.RouterID) |
| 129 | + data.TenantID = types.StringValue(fip.TenantID) |
| 130 | + if data.Region.IsNull() || data.Region.IsUnknown() { |
| 131 | + data.Region = types.StringValue(d.config.Region) |
| 132 | + } |
| 133 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 134 | +} |
0 commit comments