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
This commit is contained in:
Christoph Blecker 2016-11-01 14:00:12 -07:00 committed by Paul Stack
parent 76772ab929
commit af7cd57a4a
3 changed files with 68 additions and 3 deletions

View File

@ -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.

View File

@ -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)
}

View File

@ -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.