|
| 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_images_image_v2.go), adapted for the |
| 6 | +// terraform-plugin-framework and PCD. |
| 7 | + |
| 8 | +package images |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + "sort" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/gophercloud/gophercloud/v2/openstack/image/v2/images" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 20 | + |
| 21 | + "github.com/platform9/terraform-provider-pcd/internal/clients" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + _ datasource.DataSource = (*imageDataSource)(nil) |
| 26 | + _ datasource.DataSourceWithConfigure = (*imageDataSource)(nil) |
| 27 | +) |
| 28 | + |
| 29 | +// NewImageDataSource is the factory registered with the provider. |
| 30 | +func NewImageDataSource() datasource.DataSource { |
| 31 | + return &imageDataSource{} |
| 32 | +} |
| 33 | + |
| 34 | +type imageDataSource struct { |
| 35 | + config *clients.Config |
| 36 | +} |
| 37 | + |
| 38 | +type imageDataSourceModel struct { |
| 39 | + ID types.String `tfsdk:"id"` |
| 40 | + ImageID types.String `tfsdk:"image_id"` |
| 41 | + Name types.String `tfsdk:"name"` |
| 42 | + Owner types.String `tfsdk:"owner"` |
| 43 | + Tag types.String `tfsdk:"tag"` |
| 44 | + Visibility types.String `tfsdk:"visibility"` |
| 45 | + MostRecent types.Bool `tfsdk:"most_recent"` |
| 46 | + ContainerFormat types.String `tfsdk:"container_format"` |
| 47 | + DiskFormat types.String `tfsdk:"disk_format"` |
| 48 | + MinDiskGB types.Int64 `tfsdk:"min_disk_gb"` |
| 49 | + MinRAMMB types.Int64 `tfsdk:"min_ram_mb"` |
| 50 | + Protected types.Bool `tfsdk:"protected"` |
| 51 | + Checksum types.String `tfsdk:"checksum"` |
| 52 | + SizeBytes types.Int64 `tfsdk:"size_bytes"` |
| 53 | + CreatedAt types.String `tfsdk:"created_at"` |
| 54 | + UpdatedAt types.String `tfsdk:"updated_at"` |
| 55 | + Tags types.Set `tfsdk:"tags"` |
| 56 | + Region types.String `tfsdk:"region"` |
| 57 | +} |
| 58 | + |
| 59 | +func (d *imageDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 60 | + resp.TypeName = req.ProviderTypeName + "_images_image" |
| 61 | +} |
| 62 | + |
| 63 | +func (d *imageDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 64 | + resp.Schema = schema.Schema{ |
| 65 | + MarkdownDescription: "Look up a single image in PCD's Glance service by id or by filters.", |
| 66 | + Attributes: map[string]schema.Attribute{ |
| 67 | + "id": schema.StringAttribute{Computed: true, MarkdownDescription: "The image ID."}, |
| 68 | + "image_id": schema.StringAttribute{Optional: true, MarkdownDescription: "Look up by image ID (takes precedence over filters)."}, |
| 69 | + "name": schema.StringAttribute{Optional: true, MarkdownDescription: "Filter by exact image name."}, |
| 70 | + "owner": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) the owning project."}, |
| 71 | + "tag": schema.StringAttribute{Optional: true, MarkdownDescription: "Filter by a required tag."}, |
| 72 | + "visibility": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "Filter by (and report) visibility."}, |
| 73 | + "most_recent": schema.BoolAttribute{Optional: true, MarkdownDescription: "If multiple images match, select the most recently created."}, |
| 74 | + "container_format": schema.StringAttribute{Computed: true, MarkdownDescription: "The container format."}, |
| 75 | + "disk_format": schema.StringAttribute{Computed: true, MarkdownDescription: "The disk format."}, |
| 76 | + "min_disk_gb": schema.Int64Attribute{Computed: true, MarkdownDescription: "Minimum disk (GB)."}, |
| 77 | + "min_ram_mb": schema.Int64Attribute{Computed: true, MarkdownDescription: "Minimum RAM (MB)."}, |
| 78 | + "protected": schema.BoolAttribute{Computed: true, MarkdownDescription: "Whether the image is protected."}, |
| 79 | + "checksum": schema.StringAttribute{Computed: true, MarkdownDescription: "md5 checksum."}, |
| 80 | + "size_bytes": schema.Int64Attribute{Computed: true, MarkdownDescription: "Size in bytes."}, |
| 81 | + "created_at": schema.StringAttribute{Computed: true, MarkdownDescription: "Creation timestamp (RFC3339)."}, |
| 82 | + "updated_at": schema.StringAttribute{Computed: true, MarkdownDescription: "Last-update timestamp (RFC3339)."}, |
| 83 | + "tags": schema.SetAttribute{Computed: true, ElementType: types.StringType, MarkdownDescription: "Image tags."}, |
| 84 | + "region": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: "The region. Defaults to the provider's region."}, |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func (d *imageDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 90 | + if req.ProviderData == nil { |
| 91 | + return |
| 92 | + } |
| 93 | + config, ok := req.ProviderData.(*clients.Config) |
| 94 | + if !ok { |
| 95 | + resp.Diagnostics.AddError("Unexpected provider data type", |
| 96 | + fmt.Sprintf("Expected *clients.Config, got %T.", req.ProviderData)) |
| 97 | + return |
| 98 | + } |
| 99 | + d.config = config |
| 100 | +} |
| 101 | + |
| 102 | +func (d *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 103 | + var data imageDataSourceModel |
| 104 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 105 | + if resp.Diagnostics.HasError() { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + client, err := d.config.ImageV2Client() |
| 110 | + if err != nil { |
| 111 | + resp.Diagnostics.AddError("images: building v2 client", err.Error()) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + var img *images.Image |
| 116 | + if v := data.ImageID.ValueString(); v != "" { |
| 117 | + img, err = images.Get(ctx, client, v).Extract() |
| 118 | + if err != nil { |
| 119 | + resp.Diagnostics.AddError("images: getting image by id", err.Error()) |
| 120 | + return |
| 121 | + } |
| 122 | + } else { |
| 123 | + listOpts := images.ListOpts{ |
| 124 | + Name: data.Name.ValueString(), |
| 125 | + Owner: data.Owner.ValueString(), |
| 126 | + } |
| 127 | + if v := data.Visibility.ValueString(); v != "" { |
| 128 | + listOpts.Visibility = images.ImageVisibility(v) |
| 129 | + } |
| 130 | + if v := data.Tag.ValueString(); v != "" { |
| 131 | + listOpts.Tags = []string{v} |
| 132 | + } |
| 133 | + pages, err := images.List(client, listOpts).AllPages(ctx) |
| 134 | + if err != nil { |
| 135 | + resp.Diagnostics.AddError("images: listing images", err.Error()) |
| 136 | + return |
| 137 | + } |
| 138 | + all, err := images.ExtractImages(pages) |
| 139 | + if err != nil { |
| 140 | + resp.Diagnostics.AddError("images: extracting images", err.Error()) |
| 141 | + return |
| 142 | + } |
| 143 | + switch { |
| 144 | + case len(all) == 0: |
| 145 | + resp.Diagnostics.AddError("No image found", "No image matched the given criteria.") |
| 146 | + return |
| 147 | + case len(all) == 1: |
| 148 | + img = &all[0] |
| 149 | + case data.MostRecent.ValueBool(): |
| 150 | + sort.Slice(all, func(i, j int) bool { return all[i].CreatedAt.After(all[j].CreatedAt) }) |
| 151 | + img = &all[0] |
| 152 | + default: |
| 153 | + resp.Diagnostics.AddError("Multiple images found", |
| 154 | + fmt.Sprintf("%d images matched; set most_recent or refine the filters.", len(all))) |
| 155 | + return |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + data.ID = types.StringValue(img.ID) |
| 160 | + data.Name = types.StringValue(img.Name) |
| 161 | + data.Owner = types.StringValue(img.Owner) |
| 162 | + data.Visibility = types.StringValue(string(img.Visibility)) |
| 163 | + data.ContainerFormat = types.StringValue(img.ContainerFormat) |
| 164 | + data.DiskFormat = types.StringValue(img.DiskFormat) |
| 165 | + data.MinDiskGB = types.Int64Value(int64(img.MinDiskGigabytes)) |
| 166 | + data.MinRAMMB = types.Int64Value(int64(img.MinRAMMegabytes)) |
| 167 | + data.Protected = types.BoolValue(img.Protected) |
| 168 | + data.Checksum = types.StringValue(img.Checksum) |
| 169 | + data.SizeBytes = types.Int64Value(img.SizeBytes) |
| 170 | + data.CreatedAt = types.StringValue(img.CreatedAt.Format(time.RFC3339)) |
| 171 | + data.UpdatedAt = types.StringValue(img.UpdatedAt.Format(time.RFC3339)) |
| 172 | + |
| 173 | + tagVals := img.Tags |
| 174 | + if tagVals == nil { |
| 175 | + tagVals = []string{} |
| 176 | + } |
| 177 | + tags, diags := types.SetValueFrom(ctx, types.StringType, tagVals) |
| 178 | + resp.Diagnostics.Append(diags...) |
| 179 | + data.Tags = tags |
| 180 | + |
| 181 | + if data.Region.IsNull() || data.Region.IsUnknown() { |
| 182 | + data.Region = types.StringValue(d.config.Region) |
| 183 | + } |
| 184 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 185 | +} |
0 commit comments