Skip to content

fix instance template interaction with regional disks, make docs more clear about valid values #2138

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

Merged
merged 1 commit into from
Oct 1, 2018
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
2 changes: 1 addition & 1 deletion google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ func flattenDisks(disks []*computeBeta.AttachedDisk, d *schema.ResourceData) ([]
diskMap["boot"] = disk.Boot
diskMap["device_name"] = disk.DeviceName
diskMap["interface"] = disk.Interface
diskMap["source"] = disk.Source
diskMap["source"] = ConvertSelfLinkToV1(disk.Source)
diskMap["mode"] = disk.Mode
diskMap["type"] = disk.Type
result = append(result, diskMap)
Expand Down
99 changes: 63 additions & 36 deletions google/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,33 @@ func TestAccComputeInstanceTemplate_networkIPAddress(t *testing.T) {
func TestAccComputeInstanceTemplate_disks(t *testing.T) {
t.Parallel()

var instanceTemplate compute.InstanceTemplate

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstanceTemplate_disks(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
"google_compute_instance_template.foobar", &instanceTemplate),
testAccCheckComputeInstanceTemplateDisk(&instanceTemplate, "terraform-test-foobar", false, false),
),
},
resource.TestStep{
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccComputeInstanceTemplate_regionDisks(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstanceTemplate_regionDisks(),
},
resource.TestStep{
ResourceName: "google_compute_instance_template.foobar",
Expand Down Expand Up @@ -534,34 +547,6 @@ func testAccCheckComputeInstanceTemplateNetworkName(instanceTemplate *compute.In
}
}

func testAccCheckComputeInstanceTemplateDisk(instanceTemplate *compute.InstanceTemplate, source string, delete bool, boot bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instanceTemplate.Properties.Disks == nil {
return fmt.Errorf("no disks")
}

for _, disk := range instanceTemplate.Properties.Disks {
if disk.InitializeParams == nil {
// Check disk source
if disk.Source == source {
if disk.AutoDelete == delete && disk.Boot == boot {
return nil
}
}
} else {
// Check source image
if disk.InitializeParams.SourceImage == source {
if disk.AutoDelete == delete && disk.Boot == boot {
return nil
}
}
}
}

return fmt.Errorf("Disk not found: %s", source)
}
}

func testAccCheckComputeInstanceTemplateSubnetwork(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, i := range instanceTemplate.Properties.NetworkInterfaces {
Expand Down Expand Up @@ -942,7 +927,49 @@ resource "google_compute_instance_template" "foobar" {
}

disk {
source = "terraform-test-foobar"
source = "${google_compute_disk.foobar.name}"
auto_delete = false
boot = false
}

network_interface {
network = "default"
}

metadata {
foo = "bar"
}
}`, acctest.RandString(10), acctest.RandString(10))
}

func testAccComputeInstanceTemplate_regionDisks() string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}

resource "google_compute_region_disk" "foobar" {
name = "instancet-test-%s"
size = 10
type = "pd-ssd"
region = "us-central1"
replica_zones = ["us-central1-a", "us-central1-f"]
}

resource "google_compute_instance_template" "foobar" {
name = "instancet-test-%s"
machine_type = "n1-standard-1"

disk {
source_image = "${data.google_compute_image.my_image.self_link}"
auto_delete = true
disk_size_gb = 100
boot = true
}

disk {
source = "${google_compute_region_disk.foobar.name}"
auto_delete = false
boot = false
}
Expand Down
21 changes: 17 additions & 4 deletions website/docs/r/compute_instance_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ resource "google_compute_instance_template" "default" {

// Use an existing disk resource
disk {
source = "foo_existing_disk"
// Instance Templates reference disks by name, not self link
source = "${google_compute_disk.foobar.name}"
auto_delete = false
boot = false
}
Expand All @@ -62,6 +63,19 @@ resource "google_compute_instance_template" "default" {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}

data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}

resource "google_compute_disk" "foobar" {
name = "existing-disk"
image = "${data.google_compute_image.my_image.self_link}"
size = 10
type = "pd-ssd"
zone = "us-central1-a"
}
```

## Using with Instance Group Manager
Expand Down Expand Up @@ -260,9 +274,8 @@ The `disk` block supports:
or READ_ONLY. If you are attaching or creating a boot disk, this must
read-write mode.

* `source` - (Required if source_image not set) The name of the disk (such as
those managed by `google_compute_disk`) to attach. This cannot be a regional
disk.
* `source` - (Required if source_image not set) The name (**not self_link**)
of the disk (such as those managed by `google_compute_disk`) to attach.

* `disk_type` - (Optional) The GCE disk type. Can be either `"pd-ssd"`,
`"local-ssd"`, or `"pd-standard"`.
Expand Down