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

Support for clone a persistent disk #6637

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions mmv1/products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3507,6 +3507,25 @@ objects:
deprecation_message: This field is no longer in use, disk interfaces will be automatically determined on attachment. To resolve this issue, remove this field from your config.
description: |
Specifies the disk interface to use for attaching this disk, which is either SCSI or NVME. The default is SCSI.
- !ruby/object:Api::Type::String
name: 'sourceDisk'
description: |
The source disk used to create this disk. You can provide this as a partial or full URL to the resource.
For example, the following are valid values:

* https://www.googleapis.com/compute/v1/projects/project/zones/zone/disks/disk
* https://www.googleapis.com/compute/v1/projects/project/regions/region/disks/disk
* projects/project/zones/zone/disks/disk
* projects/project/regions/region/disks/disk
* zones/zone/disks/disk
* regions/region/disks/disk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better formatting, can we surround the variable segments with {}? like projects/{project}/zones/{zone}/disks/{disk} for both versions of the field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- !ruby/object:Api::Type::String
name: 'sourceDiskId'
description: |
The ID value of the disk used to create this image. This value may
be used to determine whether the image was taken from the current
or a previous instance of a given disk name.
output: true
- !ruby/object:Api::Type::ResourceRef
name: 'type'
resource: 'DiskType'
Expand Down Expand Up @@ -10542,6 +10561,25 @@ objects:
deprecation_message: This field is no longer in use, disk interfaces will be automatically determined on attachment. To resolve this issue, remove this field from your config.
description: |
Specifies the disk interface to use for attaching this disk, which is either SCSI or NVME. The default is SCSI.
- !ruby/object:Api::Type::String
name: 'sourceDisk'
description: |
The source disk used to create this disk. You can provide this as a partial or full URL to the resource.
For example, the following are valid values:

* https://www.googleapis.com/compute/v1/projects/project/zones/zone/disks/disk
* https://www.googleapis.com/compute/v1/projects/project/regions/region/disks/disk
* projects/project/zones/zone/disks/disk
* projects/project/regions/region/disks/disk
* zones/zone/disks/disk
* regions/region/disks/disk
- !ruby/object:Api::Type::String
name: 'sourceDiskId'
description: |
The ID value of the disk used to create this image. This value may
be used to determine whether the image was taken from the current
or a previous instance of a given disk name.
output: true
- !ruby/object:Api::Resource
name: 'RegionUrlMap'
kind: 'compute#urlMap'
Expand Down
4 changes: 4 additions & 0 deletions mmv1/products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
to allow for updating the resource policy attached to the disk.
interface: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'alwaysDiffSuppress'
sourceDisk: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'sourceDiskDiffSupress'
custom_code: !ruby/object:Provider::Terraform::CustomCode
pre_delete: templates/terraform/pre_delete/detach_disk.erb
constants: templates/terraform/constants/disk.erb
Expand Down Expand Up @@ -2104,6 +2106,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
sensitive: true
interface: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'alwaysDiffSuppress'
sourceDisk: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'sourceDiskDiffSupress'
custom_code: !ruby/object:Provider::Terraform::CustomCode
pre_delete: templates/terraform/pre_delete/detach_disk.erb
encoder: templates/terraform/encoders/disk.erb
Expand Down
10 changes: 10 additions & 0 deletions mmv1/templates/terraform/constants/disk.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// diffsupress for beta and to check change in source_disk attribute
func sourceDiskDiffSupress(_, old, new string, _ *schema.ResourceData) bool {
s1 := strings.TrimPrefix(old, "https://www.googleapis.com/compute/beta")
s2 := strings.TrimPrefix(new, "https://www.googleapis.com/compute/v1")
if strings.HasSuffix(s1, s2) {
return true
}
return false
}

// Is the new disk size smaller than the old one?
func isDiskShrinkage(_ context.Context, old, new, _ interface{}) bool {
// It's okay to remove size entirely.
Expand Down
58 changes: 58 additions & 0 deletions mmv1/third_party/terraform/tests/resource_compute_disk_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,34 @@ func testAccCheckEncryptionKey(t *testing.T, n string, disk *compute.Disk) resou
}
}

func TestAccComputeDisk_cloneDisk(t *testing.T) {
t.Parallel()
pid := getTestProjectFromEnv()
diskName := fmt.Sprintf("tf-test-%s", randString(t, 10))

var disk compute.Disk

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeDiskDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeDisk_diskClone(diskName, "self_link"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeDiskExists(
t, "google_compute_disk.disk-clone", pid, &disk),
),
},
{
ResourceName: "google_compute_disk.disk-clone",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeDisk_basic(diskName string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down Expand Up @@ -963,3 +991,33 @@ resource "google_compute_instance" "foobar" {
`, diskName, enableMultiwriter, instance)
}
<% end -%>

func testAccComputeDisk_diskClone(diskName, refSelector string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_disk" "foobar" {
name = "%s"
image = data.google_compute_image.my_image.self_link
size = 50
type = "pd-ssd"
zone = "us-central1-a"
labels = {
my-label = "my-label-value"
}
}

resource "google_compute_disk" "disk-clone" {
name = "%s"
source_disk = google_compute_disk.foobar.%s
type = "pd-ssd"
zone = "us-central1-a"
labels = {
my-label = "my-label-value"
}
}
`, diskName, diskName+"-clone", refSelector)
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ func TestAccComputeRegionDisk_deleteDetach(t *testing.T) {
})
}

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

diskName := fmt.Sprintf("tf-test-%s", randString(t, 10))

var disk compute.Disk

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRegionDiskDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRegionDisk_diskClone(diskName, "self_link"),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionDiskExists(
t, "google_compute_region_disk.regiondisk-clone", &disk),
),
},
{
ResourceName: "google_compute_region_disk.regiondisk-clone",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckComputeRegionDiskExists(t *testing.T, n string, disk *compute.Disk) resource.TestCheckFunc {
return func(s *terraform.State) error {
p := getTestProjectFromEnv()
Expand Down Expand Up @@ -415,3 +443,41 @@ resource "google_compute_instance" "inst" {
}
`, diskName, diskName, regionDiskName, instanceName)
}

func testAccComputeRegionDisk_diskClone(diskName, refSelector string) string {
return fmt.Sprintf(`
resource "google_compute_region_disk" "regiondisk" {
name = "%s"
snapshot = google_compute_snapshot.snapdisk.id
type = "pd-ssd"
region = "us-central1"
physical_block_size_bytes = 4096

replica_zones = ["us-central1-a", "us-central1-f"]
}

resource "google_compute_disk" "disk" {
name = "%s"
image = "debian-11-bullseye-v20220719"
size = 50
type = "pd-ssd"
zone = "us-central1-a"
}

resource "google_compute_snapshot" "snapdisk" {
name = "%s"
source_disk = google_compute_disk.disk.name
zone = "us-central1-a"
}

resource "google_compute_region_disk" "regiondisk-clone" {
name = "%s"
source_disk = google_compute_region_disk.regiondisk.%s
type = "pd-ssd"
region = "us-central1"
physical_block_size_bytes = 4096

replica_zones = ["us-central1-a", "us-central1-f"]
}
`, diskName, diskName, diskName, diskName+"-clone", refSelector)
}