mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #254 from dainis/fix_gce_persistent_disk_attaching
providers/google: Fix gce persistent disk attaching
This commit is contained in:
commit
b9e693134d
builtin/providers/google
@ -36,4 +36,8 @@ func testAccPreCheck(t *testing.T) {
|
||||
if v := os.Getenv("GOOGLE_CLIENT_FILE"); v == "" {
|
||||
t.Fatal("GOOGLE_CLIENT_FILE must be set for acceptance tests")
|
||||
}
|
||||
|
||||
if v := os.Getenv("GOOGLE_PROJECT"); v == "" {
|
||||
t.Fatal("GOOGLE_PROJECT must be set for acceptance tests")
|
||||
}
|
||||
}
|
||||
|
@ -60,6 +60,10 @@ func resourceComputeInstance() *schema.Resource {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"auto_delete": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -151,12 +155,23 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
||||
for i := 0; i < disksCount; i++ {
|
||||
prefix := fmt.Sprintf("disk.%d", i)
|
||||
|
||||
var sourceLink string
|
||||
// var sourceLink string
|
||||
|
||||
// Build the disk
|
||||
var disk compute.AttachedDisk
|
||||
disk.Type = "PERSISTENT"
|
||||
disk.Mode = "READ_WRITE"
|
||||
disk.Boot = i == 0
|
||||
disk.AutoDelete = true
|
||||
|
||||
if v, ok := d.GetOk(prefix + ".auto_delete"); ok {
|
||||
disk.AutoDelete = v.(bool)
|
||||
}
|
||||
|
||||
// Load up the disk for this disk if specified
|
||||
if v, ok := d.GetOk(prefix + ".disk"); ok {
|
||||
diskName := v.(string)
|
||||
disk, err := config.clientCompute.Disks.Get(
|
||||
diskData, err := config.clientCompute.Disks.Get(
|
||||
config.Project, zone.Name, diskName).Do()
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
@ -164,7 +179,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
||||
diskName, err)
|
||||
}
|
||||
|
||||
sourceLink = disk.SelfLink
|
||||
disk.Source = diskData.SelfLink
|
||||
}
|
||||
|
||||
// Load up the image for this disk if specified
|
||||
@ -177,17 +192,9 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
||||
imageName, err)
|
||||
}
|
||||
|
||||
sourceLink = image.SelfLink
|
||||
}
|
||||
|
||||
// Build the disk
|
||||
var disk compute.AttachedDisk
|
||||
disk.Type = "PERSISTENT"
|
||||
disk.Mode = "READ_WRITE"
|
||||
disk.Boot = i == 0
|
||||
disk.AutoDelete = true
|
||||
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
|
||||
SourceImage: sourceLink,
|
||||
disk.InitializeParams = &compute.AttachedDiskInitializeParams{
|
||||
SourceImage: image.SelfLink,
|
||||
}
|
||||
}
|
||||
|
||||
disks = append(disks, &disk)
|
||||
|
@ -3,6 +3,7 @@ package google
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"strings"
|
||||
|
||||
"code.google.com/p/google-api-go-client/compute/v1"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
@ -24,6 +25,7 @@ func TestAccComputeInstance_basic(t *testing.T) {
|
||||
"google_compute_instance.foobar", &instance),
|
||||
testAccCheckComputeInstanceTag(&instance, "foo"),
|
||||
testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"),
|
||||
testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
|
||||
),
|
||||
},
|
||||
},
|
||||
@ -50,6 +52,28 @@ func TestAccComputeInstance_IP(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
//!NB requires that disk with name terraform-test-disk is present in gce,
|
||||
//if created as dependency then it tries to remove it while it is still attached
|
||||
//to instance and that fails with an error
|
||||
func TestAccComputeInstance_disks(t *testing.T) {
|
||||
var instance compute.Instance
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckComputeInstanceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccComputeInstance_disks,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true),
|
||||
testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccComputeInstance_update(t *testing.T) {
|
||||
var instance compute.Instance
|
||||
|
||||
@ -164,6 +188,22 @@ func testAccCheckComputeInstanceNetwork(instance *compute.Instance) resource.Tes
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
if instance.Disks == nil {
|
||||
return fmt.Errorf("no disks")
|
||||
}
|
||||
|
||||
for _, disk := range instance.Disks {
|
||||
if strings.LastIndex(disk.Source, "/"+source) == (len(disk.Source) - len(source) - 1) && disk.AutoDelete == delete && disk.Boot == boot{
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Disk not found: %s", source)
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
if instance.Tags == nil {
|
||||
@ -244,3 +284,27 @@ resource "google_compute_instance" "foobar" {
|
||||
foo = "bar"
|
||||
}
|
||||
}`
|
||||
|
||||
const testAccComputeInstance_disks = `
|
||||
resource "google_compute_instance" "foobar" {
|
||||
name = "terraform-test"
|
||||
machine_type = "n1-standard-1"
|
||||
zone = "us-central1-a"
|
||||
|
||||
disk {
|
||||
image = "debian-7-wheezy-v20140814"
|
||||
}
|
||||
|
||||
disk {
|
||||
disk = "terraform-test-disk"
|
||||
auto_delete = false
|
||||
}
|
||||
|
||||
network {
|
||||
source = "default"
|
||||
}
|
||||
|
||||
metadata {
|
||||
foo = "bar"
|
||||
}
|
||||
}`
|
||||
|
Loading…
Reference in New Issue
Block a user