From af7cd57a4a6d934640b64cc1f204b7ef2851f1f6 Mon Sep 17 00:00:00 2001 From: Christoph Blecker Date: Tue, 1 Nov 2016 14:00:12 -0700 Subject: [PATCH] Search configured project image families (#9243) * Search configured project image families * Clarify documentation around google_compute_instance image families * Acceptance test for private instance family creation --- builtin/providers/google/image.go | 8 ++- .../google/resource_compute_instance_test.go | 57 +++++++++++++++++++ .../google/r/compute_instance.html.markdown | 6 +- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/builtin/providers/google/image.go b/builtin/providers/google/image.go index 5a006eb9a16e..e4a50905eff7 100644 --- a/builtin/providers/google/image.go +++ b/builtin/providers/google/image.go @@ -21,12 +21,18 @@ func resolveImage(c *Config, name string) (string, error) { // Must infer the project name: - // First, try the configured project. + // First, try the configured project for a specific image: image, err := c.clientCompute.Images.Get(c.Project, name).Do() if err == nil { return image.SelfLink, nil } + // If it doesn't exist, try to see if it works as an image family: + image, err = c.clientCompute.Images.GetFromFamily(c.Project, name).Do() + if err == nil { + return image.SelfLink, nil + } + // If we match a lookup for an alternate project, then try that next. // If not, we return the original error. diff --git a/builtin/providers/google/resource_compute_instance_test.go b/builtin/providers/google/resource_compute_instance_test.go index 1caf8f01f4ec..7ea120e2efeb 100644 --- a/builtin/providers/google/resource_compute_instance_test.go +++ b/builtin/providers/google/resource_compute_instance_test.go @@ -458,6 +458,30 @@ func TestAccComputeInstance_address_custom(t *testing.T) { }, }) } + +func TestAccComputeInstance_private_image_family(t *testing.T) { + var instance compute.Instance + var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10)) + var diskName = fmt.Sprintf("instance-testd-%s", acctest.RandString(10)) + var imageName = fmt.Sprintf("instance-testi-%s", acctest.RandString(10)) + var familyName = fmt.Sprintf("instance-testf-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeInstance_private_image_family(diskName, imageName, familyName, instanceName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists( + "google_compute_instance.foobar", &instance), + ), + }, + }, + }) +} + func testAccCheckComputeInstanceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -1095,3 +1119,36 @@ func testAccComputeInstance_address_custom(instance, address string) string { }`, acctest.RandString(10), acctest.RandString(10), instance, address) } + +func testAccComputeInstance_private_image_family(disk, image, family, instance string) string { + return fmt.Sprintf(` + resource "google_compute_disk" "foobar" { + name = "%s" + zone = "us-central1-a" + image = "debian-8-jessie-v20160803" + } + + resource "google_compute_image" "foobar" { + name = "%s" + source_disk = "${google_compute_disk.foobar.self_link}" + family = "%s" + } + + resource "google_compute_instance" "foobar" { + name = "%s" + machine_type = "n1-standard-1" + zone = "us-central1-a" + + disk { + image = "${google_compute_image.foobar.family}" + } + + network_interface { + network = "default" + } + + metadata { + foo = "bar" + } + }`, disk, image, family, instance) +} diff --git a/website/source/docs/providers/google/r/compute_instance.html.markdown b/website/source/docs/providers/google/r/compute_instance.html.markdown index dd9b325b9440..39d0ceb44147 100644 --- a/website/source/docs/providers/google/r/compute_instance.html.markdown +++ b/website/source/docs/providers/google/r/compute_instance.html.markdown @@ -112,9 +112,11 @@ the type is "local-ssd", in which case scratch must be true). `google_compute_disk`) to attach. * `image` - The image from which to initialize this - disk. Either the full URL, a contraction of the form "project/name", an + disk. Either the full URL, a contraction of the form "project/name", the + name of a Google-supported [image family](https://cloud.google.com/compute/docs/images#image_families), - or just a name (in which case the current project is used). + or simple the name of an image or image family (in which case the current + project is used). * `auto_delete` - (Optional) Whether or not the disk should be auto-deleted. This defaults to true. Leave true for local SSDs.