Skip to content

Commit f792923

Browse files
authored
Merge pull request #238 from z1nkum/snapshot-fix-url-to-name
Fix google_compute_disk created from snapshot, forces new resource then apply once more
2 parents eca4c3e + d0b7fbc commit f792923

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

google/resource_compute_disk.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ func resourceComputeDisk() *schema.Resource {
5555
},
5656

5757
"image": &schema.Schema{
58-
Type: schema.TypeString,
59-
Optional: true,
60-
ForceNew: true,
58+
Type: schema.TypeString,
59+
Optional: true,
60+
ForceNew: true,
61+
DiffSuppressFunc: linkDiffSuppress,
6162
},
6263

6364
"project": &schema.Schema{
@@ -78,9 +79,10 @@ func resourceComputeDisk() *schema.Resource {
7879
},
7980

8081
"snapshot": &schema.Schema{
81-
Type: schema.TypeString,
82-
Optional: true,
83-
ForceNew: true,
82+
Type: schema.TypeString,
83+
Optional: true,
84+
ForceNew: true,
85+
DiffSuppressFunc: linkDiffSuppress,
8486
},
8587

8688
"type": &schema.Schema{
@@ -261,10 +263,8 @@ func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error {
261263
if disk.DiskEncryptionKey != nil && disk.DiskEncryptionKey.Sha256 != "" {
262264
d.Set("disk_encryption_key_sha256", disk.DiskEncryptionKey.Sha256)
263265
}
264-
if disk.SourceImage != "" {
265-
imageUrlParts := strings.Split(disk.SourceImage, "/")
266-
d.Set("image", imageUrlParts[len(imageUrlParts)-1])
267-
}
266+
267+
d.Set("image", disk.SourceImage)
268268
d.Set("snapshot", disk.SourceSnapshot)
269269

270270
return nil

google/resource_compute_disk_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestAccComputeDisk_updateSize(t *testing.T) {
6060
})
6161
}
6262

63-
func TestAccComputeDisk_fromSnapshotURI(t *testing.T) {
63+
func TestAccComputeDisk_fromSnapshot(t *testing.T) {
6464
diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
6565
firstDiskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
6666
snapshotName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
@@ -74,7 +74,14 @@ func TestAccComputeDisk_fromSnapshotURI(t *testing.T) {
7474
CheckDestroy: testAccCheckComputeDiskDestroy,
7575
Steps: []resource.TestStep{
7676
resource.TestStep{
77-
Config: testAccComputeDisk_fromSnapshotURI(firstDiskName, snapshotName, diskName, xpn_host),
77+
Config: testAccComputeDisk_fromSnapshot(firstDiskName, snapshotName, diskName, xpn_host, "self_link"),
78+
Check: resource.ComposeTestCheckFunc(
79+
testAccCheckComputeDiskExists(
80+
"google_compute_disk.seconddisk", &disk),
81+
),
82+
},
83+
resource.TestStep{
84+
Config: testAccComputeDisk_fromSnapshot(firstDiskName, snapshotName, diskName, xpn_host, "name"),
7885
Check: resource.ComposeTestCheckFunc(
7986
testAccCheckComputeDiskExists(
8087
"google_compute_disk.seconddisk", &disk),
@@ -251,7 +258,7 @@ resource "google_compute_disk" "foobar" {
251258
}`, diskName)
252259
}
253260

254-
func testAccComputeDisk_fromSnapshotURI(firstDiskName, snapshotName, diskName, xpn_host string) string {
261+
func testAccComputeDisk_fromSnapshot(firstDiskName, snapshotName, diskName, xpn_host string, ref_selector string) string {
255262
return fmt.Sprintf(`
256263
resource "google_compute_disk" "foobar" {
257264
name = "%s"
@@ -270,10 +277,10 @@ resource "google_compute_snapshot" "snapdisk" {
270277
}
271278
resource "google_compute_disk" "seconddisk" {
272279
name = "%s"
273-
snapshot = "${google_compute_snapshot.snapdisk.self_link}"
280+
snapshot = "${google_compute_snapshot.snapdisk.%s}"
274281
type = "pd-ssd"
275282
zone = "us-central1-a"
276-
}`, firstDiskName, xpn_host, snapshotName, xpn_host, diskName)
283+
}`, firstDiskName, xpn_host, snapshotName, xpn_host, diskName, ref_selector)
277284
}
278285

279286
func testAccComputeDisk_encryption(diskName string) string {

0 commit comments

Comments
 (0)