Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add most_recent option to google_compute_image datasource #15187

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/8309.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `most_recent` argument to `google_compute_image` datasource
```
48 changes: 48 additions & 0 deletions google/data_source_google_compute_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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"),
),
},
},
})
}
Expand Down Expand Up @@ -165,3 +178,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)
}
19 changes: 19 additions & 0 deletions google/services/compute/data_source_google_compute_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
Expand Down Expand Up @@ -111,6 +112,11 @@ func DataSourceGoogleComputeImage() *schema.Resource {
Optional: true,
ForceNew: true,
},
"most_recent": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand Down Expand Up @@ -146,6 +152,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")
}
Expand Down
6 changes: 5 additions & 1 deletion website/docs/d/compute_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ 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.

- - -

* `project` - (Optional) The project in which the resource belongs. If it is not
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
Expand Down