Skip to content

Commit

Permalink
Search configured project image families (#9243)
Browse files Browse the repository at this point in the history
* Search configured project image families

* Clarify documentation around google_compute_instance image families

* Acceptance test for private instance family creation
  • Loading branch information
cblecker authored and stack72 committed Nov 1, 2016
1 parent 76772ab commit af7cd57
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
8 changes: 7 additions & 1 deletion builtin/providers/google/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
57 changes: 57 additions & 0 deletions builtin/providers/google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit af7cd57

Please sign in to comment.