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

Add support for region boot disk to instance #15597

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
3 changes: 3 additions & 0 deletions .changelog/8718.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added support for specifying regional disks for `google_compute_instance` `boot_disk.source`
```
10 changes: 9 additions & 1 deletion google/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2483,7 +2483,15 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
}

if v, ok := d.GetOk("boot_disk.0.source"); ok {
source, err := tpgresource.ParseDiskFieldValue(v.(string), d, config)
var err error
var source interface {
RelativeLink() string
}
if strings.Contains(v.(string), "regions/") {
source, err = tpgresource.ParseRegionDiskFieldValue(v.(string), d, config)
} else {
source, err = tpgresource.ParseDiskFieldValue(v.(string), d, config)
}
if err != nil {
return nil, err
}
Expand Down
74 changes: 74 additions & 0 deletions google/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,31 @@ func TestAccComputeInstance_metadataStartupScript_update(t *testing.T) {
})
}

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

var instance compute.Instance
var instanceName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
var diskName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
var suffix = acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_regionBootDisk(instanceName, diskName, suffix),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.regional_vm_instance", &instance),
testAccCheckComputeInstanceBootDisk(&instance, diskName),
),
},
},
})
}

func testAccCheckComputeInstanceUpdateMachineType(t *testing.T, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -6656,3 +6681,52 @@ resource "google_compute_instance" "foobar" {
}
`, instance, machineType, metadata)
}

func testAccComputeInstance_regionBootDisk(instance, diskName, suffix string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "regional_vm_instance" {
name = "%s"
machine_type = "e2-medium"
zone = "us-central1-c"

boot_disk {
source = google_compute_region_disk.regionaldisk.self_link
}
network_interface {
network = google_compute_network.vpc_network.name
access_config {}
}
}

resource "google_compute_region_disk" "regionaldisk" {
name = "%s"
type = "pd-ssd"
region = "us-central1"
replica_zones = ["us-central1-c", "us-central1-a"]
size = 50
snapshot = google_compute_snapshot.debian.id
}

resource "google_compute_network" "vpc_network" {
name = "tf-test-%s"
}

data "google_compute_image" "debian" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_snapshot" "debian" {
name = "tf-test-%s"
source_disk = google_compute_disk.debian.id
}

resource "google_compute_disk" "debian" {
name = "tf-test-%s"
image = data.google_compute_image.debian.self_link
size = 10
type = "pd-ssd"
zone = "us-central1-c"
}
`, instance, diskName, suffix, suffix, suffix)
}