diff --git a/mmv1/third_party/terraform/services/compute/data_source_google_compute_image.go.erb b/mmv1/third_party/terraform/services/compute/data_source_google_compute_image.go.erb index 5852144e5429..5fece43bc8e7 100644 --- a/mmv1/third_party/terraform/services/compute/data_source_google_compute_image.go.erb +++ b/mmv1/third_party/terraform/services/compute/data_source_google_compute_image.go.erb @@ -5,6 +5,7 @@ import ( "fmt" "log" "strconv" + "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-google/google/tpgresource" @@ -114,6 +115,11 @@ func DataSourceGoogleComputeImage() *schema.Resource { Optional: true, ForceNew: true, }, + "most_recent": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, }, } } @@ -149,6 +155,19 @@ func dataSourceGoogleComputeImageRead(d *schema.ResourceData, meta interface{}) for _, im := range images.Items { image = im } + } else if mr, ok := d.GetOk("most_recent"); len(images.Items) >= 1 && ok && mr.(bool) { + most_recent := time.UnixMicro(0) + for _, im := range images.Items { + parsedTS, err := time.Parse(time.RFC3339, im.CreationTimestamp) + if err != nil { + return fmt.Errorf("error parsing creation timestamp: %w", err) + } + + if parsedTS.After(most_recent) { + most_recent = parsedTS + image = im + } + } } else { return fmt.Errorf("your filter has returned more than one image or no image. Please refine your filter to return exactly one image") } diff --git a/mmv1/third_party/terraform/tests/data_source_google_compute_image_test.go b/mmv1/third_party/terraform/tests/data_source_google_compute_image_test.go index 166a823ca51b..a4124eb868e6 100644 --- a/mmv1/third_party/terraform/tests/data_source_google_compute_image_test.go +++ b/mmv1/third_party/terraform/tests/data_source_google_compute_image_test.go @@ -70,6 +70,19 @@ func TestAccDataSourceComputeImageFilter(t *testing.T) { "self_link"), ), }, + { + Config: testAccDataSourceCustomImageFilterWithMostRecent(family, name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.google_compute_image.from_filter", + "name", name+"-latest"), + resource.TestCheckResourceAttr("data.google_compute_image.from_filter", + "family", family), + resource.TestCheckResourceAttr("data.google_compute_image.from_filter", + "most_recent", "true"), + resource.TestCheckResourceAttrSet("data.google_compute_image.from_filter", + "self_link"), + ), + }, }, }) } @@ -163,3 +176,38 @@ data "google_compute_image" "from_filter" { `, family, name, name, name, name) } + +func testAccDataSourceCustomImageFilterWithMostRecent(family, name string) string { + return fmt.Sprintf(` +resource "google_compute_image" "image-first" { + family = "%s" + name = "%s-first" + source_disk = google_compute_disk.disk.self_link + labels = { + test = "%s" + } +} + +resource "google_compute_image" "image-latest" { + depends_on = [ google_compute_image.image-first ] + family = "%s" + name = "%s-latest" + source_disk = google_compute_disk.disk.self_link + labels = { + test = "%s" + } +} + +resource "google_compute_disk" "disk" { + name = "%s-disk" + zone = "us-central1-b" +} + +data "google_compute_image" "from_filter" { + project = google_compute_image.image-latest.project + filter = "labels.test = %s" + most_recent = true +} + +`, family, name, name, family, name, name, name, name) +} diff --git a/mmv1/third_party/terraform/website/docs/d/compute_image.html.markdown b/mmv1/third_party/terraform/website/docs/d/compute_image.html.markdown index 72707c9bd6cc..e9cdc9616ad6 100644 --- a/mmv1/third_party/terraform/website/docs/d/compute_image.html.markdown +++ b/mmv1/third_party/terraform/website/docs/d/compute_image.html.markdown @@ -36,7 +36,8 @@ The following arguments are supported: Exactly one of `name`, `family` or `filter` must be specified. If `name` is specified, it will fetch the corresponding image. If `family` is specified, it will return the latest image that is part of an image family and is not deprecated. If you specify `filter`, your -filter must return exactly one image. Filter syntax can be found [here](https://cloud.google.com/compute/docs/reference/rest/v1/images/list) in the filter section. +filter must return exactly one image unless you use `most_recent`. +Filter syntax can be found [here](https://cloud.google.com/compute/docs/reference/rest/v1/images/list) in the filter section. - - - @@ -44,6 +45,9 @@ filter must return exactly one image. Filter syntax can be found [here](https:// provided, the provider project is used. If you are using a [public base image][pubimg], be sure to specify the correct Image Project. +* `most_recent` - (Optional) A boolean to indicate either to take to most recent image if your filter + returns more than one image. + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are