|
| 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_flavor_v2.go), adapted for the |
| 6 | +// terraform-plugin-framework and PCD. |
| 7 | + |
| 8 | +package compute |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + |
| 14 | + "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/flavors" |
| 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 = (*flavorDataSource)(nil) |
| 24 | + _ datasource.DataSourceWithConfigure = (*flavorDataSource)(nil) |
| 25 | +) |
| 26 | + |
| 27 | +// NewFlavorDataSource is the factory registered with the provider. |
| 28 | +func NewFlavorDataSource() datasource.DataSource { |
| 29 | + return &flavorDataSource{} |
| 30 | +} |
| 31 | + |
| 32 | +type flavorDataSource struct { |
| 33 | + config *clients.Config |
| 34 | +} |
| 35 | + |
| 36 | +type flavorDataSourceModel struct { |
| 37 | + ID types.String `tfsdk:"id"` |
| 38 | + FlavorID types.String `tfsdk:"flavor_id"` |
| 39 | + Name types.String `tfsdk:"name"` |
| 40 | + RAM types.Int64 `tfsdk:"ram"` |
| 41 | + VCPUs types.Int64 `tfsdk:"vcpus"` |
| 42 | + Disk types.Int64 `tfsdk:"disk"` |
| 43 | + Swap types.Int64 `tfsdk:"swap"` |
| 44 | + RxTxFactor types.Float64 `tfsdk:"rx_tx_factor"` |
| 45 | + IsPublic types.Bool `tfsdk:"is_public"` |
| 46 | + Region types.String `tfsdk:"region"` |
| 47 | +} |
| 48 | + |
| 49 | +func (d *flavorDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 50 | + resp.TypeName = req.ProviderTypeName + "_compute_flavor" |
| 51 | +} |
| 52 | + |
| 53 | +func (d *flavorDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 54 | + resp.Schema = schema.Schema{ |
| 55 | + MarkdownDescription: "Look up a compute flavor by name or ID.", |
| 56 | + Attributes: map[string]schema.Attribute{ |
| 57 | + "id": schema.StringAttribute{Computed: true, MarkdownDescription: "The flavor ID."}, |
| 58 | + "flavor_id": schema.StringAttribute{Optional: true, MarkdownDescription: "Look up by flavor ID (takes precedence over name)."}, |
| 59 | + "name": schema.StringAttribute{Optional: true, MarkdownDescription: "Look up by exact name."}, |
| 60 | + "ram": schema.Int64Attribute{Computed: true, MarkdownDescription: "Memory in MB."}, |
| 61 | + "vcpus": schema.Int64Attribute{Computed: true, MarkdownDescription: "Number of vCPUs."}, |
| 62 | + "disk": schema.Int64Attribute{Computed: true, MarkdownDescription: "Root disk in GB."}, |
| 63 | + "swap": schema.Int64Attribute{Computed: true, MarkdownDescription: "Swap in MB."}, |
| 64 | + "rx_tx_factor": schema.Float64Attribute{Computed: true, MarkdownDescription: "RX/TX factor."}, |
| 65 | + "is_public": schema.BoolAttribute{Computed: true, MarkdownDescription: "Whether the flavor is public."}, |
| 66 | + "region": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "The region. Defaults to the provider's region."}, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (d *flavorDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 72 | + d.config = configureClient(req.ProviderData, &resp.Diagnostics) |
| 73 | +} |
| 74 | + |
| 75 | +func (d *flavorDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 76 | + var data flavorDataSourceModel |
| 77 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 78 | + if resp.Diagnostics.HasError() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + client, err := d.config.ComputeV2Client() |
| 83 | + if err != nil { |
| 84 | + resp.Diagnostics.AddError("compute: building v2 client", err.Error()) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + var flavor *flavors.Flavor |
| 89 | + if v := data.FlavorID.ValueString(); v != "" { |
| 90 | + flavor, err = flavors.Get(ctx, client, v).Extract() |
| 91 | + if err != nil { |
| 92 | + resp.Diagnostics.AddError("compute: getting flavor by id", err.Error()) |
| 93 | + return |
| 94 | + } |
| 95 | + } else { |
| 96 | + pages, err := flavors.ListDetail(client, flavors.ListOpts{}).AllPages(ctx) |
| 97 | + if err != nil { |
| 98 | + resp.Diagnostics.AddError("compute: listing flavors", err.Error()) |
| 99 | + return |
| 100 | + } |
| 101 | + all, err := flavors.ExtractFlavors(pages) |
| 102 | + if err != nil { |
| 103 | + resp.Diagnostics.AddError("compute: extracting flavors", err.Error()) |
| 104 | + return |
| 105 | + } |
| 106 | + name := data.Name.ValueString() |
| 107 | + var matches []flavors.Flavor |
| 108 | + for _, f := range all { |
| 109 | + if f.Name == name { |
| 110 | + matches = append(matches, f) |
| 111 | + } |
| 112 | + } |
| 113 | + switch len(matches) { |
| 114 | + case 0: |
| 115 | + resp.Diagnostics.AddError("No flavor found", fmt.Sprintf("No flavor named %q.", name)) |
| 116 | + return |
| 117 | + case 1: |
| 118 | + flavor = &matches[0] |
| 119 | + default: |
| 120 | + resp.Diagnostics.AddError("Multiple flavors found", fmt.Sprintf("%d flavors named %q.", len(matches), name)) |
| 121 | + return |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + data.ID = types.StringValue(flavor.ID) |
| 126 | + data.FlavorID = types.StringValue(flavor.ID) |
| 127 | + data.Name = types.StringValue(flavor.Name) |
| 128 | + data.RAM = types.Int64Value(int64(flavor.RAM)) |
| 129 | + data.VCPUs = types.Int64Value(int64(flavor.VCPUs)) |
| 130 | + data.Disk = types.Int64Value(int64(flavor.Disk)) |
| 131 | + data.Swap = types.Int64Value(int64(flavor.Swap)) |
| 132 | + data.RxTxFactor = types.Float64Value(flavor.RxTxFactor) |
| 133 | + data.IsPublic = types.BoolValue(flavor.IsPublic) |
| 134 | + if data.Region.IsNull() || data.Region.IsUnknown() { |
| 135 | + data.Region = types.StringValue(d.config.Region) |
| 136 | + } |
| 137 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 138 | +} |
0 commit comments