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

Search configured project image families #9243

Merged
merged 3 commits into from
Nov 1, 2016
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
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