|
| 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_network_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/networks" |
| 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 = (*networkDataSource)(nil) |
| 24 | + _ datasource.DataSourceWithConfigure = (*networkDataSource)(nil) |
| 25 | +) |
| 26 | + |
| 27 | +// NewNetworkDataSource is the factory registered with the provider. |
| 28 | +func NewNetworkDataSource() datasource.DataSource { |
| 29 | + return &networkDataSource{} |
| 30 | +} |
| 31 | + |
| 32 | +type networkDataSource struct { |
| 33 | + config *clients.Config |
| 34 | +} |
| 35 | + |
| 36 | +type networkDataSourceModel struct { |
| 37 | + ID types.String `tfsdk:"id"` |
| 38 | + NetworkID types.String `tfsdk:"network_id"` |
| 39 | + Name types.String `tfsdk:"name"` |
| 40 | + Description types.String `tfsdk:"description"` |
| 41 | + AdminStateUp types.Bool `tfsdk:"admin_state_up"` |
| 42 | + Shared types.Bool `tfsdk:"shared"` |
| 43 | + External types.Bool `tfsdk:"external"` |
| 44 | + TenantID types.String `tfsdk:"tenant_id"` |
| 45 | + Region types.String `tfsdk:"region"` |
| 46 | +} |
| 47 | + |
| 48 | +func (d *networkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 49 | + resp.TypeName = req.ProviderTypeName + "_networking_network" |
| 50 | +} |
| 51 | + |
| 52 | +func (d *networkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 53 | + resp.Schema = schema.Schema{ |
| 54 | + MarkdownDescription: "Look up a Neutron network by name or ID.", |
| 55 | + Attributes: map[string]schema.Attribute{ |
| 56 | + "id": schema.StringAttribute{Computed: true, MarkdownDescription: "The network ID."}, |
| 57 | + "network_id": schema.StringAttribute{Optional: true, MarkdownDescription: "Look up by network ID (takes precedence over name)."}, |
| 58 | + "name": schema.StringAttribute{Optional: true, MarkdownDescription: "Look up by name."}, |
| 59 | + "description": schema.StringAttribute{Computed: true, MarkdownDescription: "The network description."}, |
| 60 | + "admin_state_up": schema.BoolAttribute{Computed: true, MarkdownDescription: "The administrative state."}, |
| 61 | + "shared": schema.BoolAttribute{Computed: true, MarkdownDescription: "Whether the network is shared."}, |
| 62 | + "external": schema.BoolAttribute{Computed: true, MarkdownDescription: "Whether the network is external."}, |
| 63 | + "tenant_id": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the owning project."}, |
| 64 | + "region": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "The region. Defaults to the provider's region."}, |
| 65 | + }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (d *networkDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 70 | + d.config = configureClient(req.ProviderData, &resp.Diagnostics) |
| 71 | +} |
| 72 | + |
| 73 | +func (d *networkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 74 | + var data networkDataSourceModel |
| 75 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 76 | + if resp.Diagnostics.HasError() { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + client, err := d.config.NetworkV2Client() |
| 81 | + if err != nil { |
| 82 | + resp.Diagnostics.AddError("networking: building v2 client", err.Error()) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + id := data.NetworkID.ValueString() |
| 87 | + if id == "" { |
| 88 | + pages, err := networks.List(client, networks.ListOpts{ |
| 89 | + Name: data.Name.ValueString(), |
| 90 | + TenantID: data.TenantID.ValueString(), |
| 91 | + }).AllPages(ctx) |
| 92 | + if err != nil { |
| 93 | + resp.Diagnostics.AddError("networking: listing networks", err.Error()) |
| 94 | + return |
| 95 | + } |
| 96 | + all, err := networks.ExtractNetworks(pages) |
| 97 | + if err != nil { |
| 98 | + resp.Diagnostics.AddError("networking: extracting networks", err.Error()) |
| 99 | + return |
| 100 | + } |
| 101 | + switch len(all) { |
| 102 | + case 0: |
| 103 | + resp.Diagnostics.AddError("No network found", "No network matched the given criteria.") |
| 104 | + return |
| 105 | + case 1: |
| 106 | + id = all[0].ID |
| 107 | + default: |
| 108 | + resp.Diagnostics.AddError("Multiple networks found", |
| 109 | + fmt.Sprintf("%d networks matched; refine name/tenant_id.", len(all))) |
| 110 | + return |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + var n networkExtended |
| 115 | + if err := networks.Get(ctx, client, id).ExtractInto(&n); err != nil { |
| 116 | + resp.Diagnostics.AddError("networking: getting network", err.Error()) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + data.ID = types.StringValue(n.ID) |
| 121 | + data.NetworkID = types.StringValue(n.ID) |
| 122 | + data.Name = types.StringValue(n.Name) |
| 123 | + data.Description = types.StringValue(n.Description) |
| 124 | + data.AdminStateUp = types.BoolValue(n.AdminStateUp) |
| 125 | + data.Shared = types.BoolValue(n.Shared) |
| 126 | + data.External = types.BoolValue(n.NetworkExternalExt.External) |
| 127 | + data.TenantID = types.StringValue(n.TenantID) |
| 128 | + if data.Region.IsNull() || data.Region.IsUnknown() { |
| 129 | + data.Region = types.StringValue(d.config.Region) |
| 130 | + } |
| 131 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 132 | +} |
0 commit comments