|
| 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_compute_availability_zones_v2.go), adapted |
| 6 | +// for the terraform-plugin-framework and PCD. |
| 7 | + |
| 8 | +package compute |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + |
| 13 | + "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/availabilityzones" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + |
| 18 | + "github.com/platform9/terraform-provider-pcd/internal/clients" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + _ datasource.DataSource = (*azDataSource)(nil) |
| 23 | + _ datasource.DataSourceWithConfigure = (*azDataSource)(nil) |
| 24 | +) |
| 25 | + |
| 26 | +// NewAvailabilityZonesDataSource is the factory registered with the provider. |
| 27 | +func NewAvailabilityZonesDataSource() datasource.DataSource { |
| 28 | + return &azDataSource{} |
| 29 | +} |
| 30 | + |
| 31 | +type azDataSource struct { |
| 32 | + config *clients.Config |
| 33 | +} |
| 34 | + |
| 35 | +type azDataSourceModel struct { |
| 36 | + ID types.String `tfsdk:"id"` |
| 37 | + State types.String `tfsdk:"state"` |
| 38 | + Names types.List `tfsdk:"names"` |
| 39 | + Region types.String `tfsdk:"region"` |
| 40 | +} |
| 41 | + |
| 42 | +func (d *azDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 43 | + resp.TypeName = req.ProviderTypeName + "_compute_availability_zones" |
| 44 | +} |
| 45 | + |
| 46 | +func (d *azDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 47 | + resp.Schema = schema.Schema{ |
| 48 | + MarkdownDescription: "Return the compute availability zones.", |
| 49 | + Attributes: map[string]schema.Attribute{ |
| 50 | + "id": schema.StringAttribute{Computed: true, MarkdownDescription: "Data source identifier."}, |
| 51 | + "state": schema.StringAttribute{Optional: true, MarkdownDescription: "Filter by state: available (default) or unavailable."}, |
| 52 | + "names": schema.ListAttribute{Computed: true, ElementType: types.StringType, MarkdownDescription: "The matching availability zone names."}, |
| 53 | + "region": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "The region. Defaults to the provider's region."}, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (d *azDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 59 | + d.config = configureClient(req.ProviderData, &resp.Diagnostics) |
| 60 | +} |
| 61 | + |
| 62 | +func (d *azDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 63 | + var data azDataSourceModel |
| 64 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 65 | + if resp.Diagnostics.HasError() { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + client, err := d.config.ComputeV2Client() |
| 70 | + if err != nil { |
| 71 | + resp.Diagnostics.AddError("compute: building v2 client", err.Error()) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + pages, err := availabilityzones.List(client).AllPages(ctx) |
| 76 | + if err != nil { |
| 77 | + resp.Diagnostics.AddError("compute: listing availability zones", err.Error()) |
| 78 | + return |
| 79 | + } |
| 80 | + zones, err := availabilityzones.ExtractAvailabilityZones(pages) |
| 81 | + if err != nil { |
| 82 | + resp.Diagnostics.AddError("compute: extracting availability zones", err.Error()) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + wantAvailable := data.State.ValueString() != "unavailable" |
| 87 | + names := make([]string, 0, len(zones)) |
| 88 | + for _, z := range zones { |
| 89 | + if z.ZoneState.Available == wantAvailable { |
| 90 | + names = append(names, z.ZoneName) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + nameList, diags := types.ListValueFrom(ctx, types.StringType, names) |
| 95 | + resp.Diagnostics.Append(diags...) |
| 96 | + data.Names = nameList |
| 97 | + if data.Region.IsNull() || data.Region.IsUnknown() { |
| 98 | + data.Region = types.StringValue(d.config.Region) |
| 99 | + } |
| 100 | + data.ID = types.StringValue(d.config.Region + ":azs") |
| 101 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 102 | +} |
0 commit comments