Skip to content

Commit

Permalink
Add region disk support to attached disk
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisst authored and modular-magician committed Dec 6, 2018
1 parent 8d37975 commit c457caa
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
4 changes: 4 additions & 0 deletions google/resource_bigtable_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ func resourceBigtableInstance() *schema.Resource {
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"num_nodes": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(3),
},
"storage_type": {
Type: schema.TypeString,
Optional: true,
Default: "SSD",
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"SSD", "HDD"}, false),
},
},
Expand Down
15 changes: 11 additions & 4 deletions google/resource_bigtable_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ func TestAccBigtableInstance_basic(t *testing.T) {
CheckDestroy: testAccCheckBigtableInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccBigtableInstance(instanceName),
Config: testAccBigtableInstance(instanceName, 3),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance"),
),
},
{
Config: testAccBigtableInstance(instanceName, 4),
Check: resource.ComposeTestCheckFunc(
testAccBigtableInstanceExists(
"google_bigtable_instance.instance"),
Expand Down Expand Up @@ -125,18 +132,18 @@ func testAccBigtableInstanceExists(n string) resource.TestCheckFunc {
}
}

func testAccBigtableInstance(instanceName string) string {
func testAccBigtableInstance(instanceName string, numNodes int) string {
return fmt.Sprintf(`
resource "google_bigtable_instance" "instance" {
name = "%s"
cluster {
cluster_id = "%s"
zone = "us-central1-b"
num_nodes = 3
num_nodes = %d
storage_type = "HDD"
}
}
`, instanceName, instanceName)
`, instanceName, instanceName, numNodes)
}

func testAccBigtableInstance_cluster(instanceName string) string {
Expand Down
15 changes: 13 additions & 2 deletions google/resource_compute_attached_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,21 @@ func resourceAttachedDiskCreate(d *schema.ResourceData, meta interface{}) error
return err
}

diskName := GetResourceNameFromSelfLink(d.Get("disk").(string))
disk := d.Get("disk").(string)
diskName := GetResourceNameFromSelfLink(disk)
diskSrc := fmt.Sprintf("projects/%s/zones/%s/disks/%s", zv.Project, zv.Zone, diskName)

// Check if the disk is a regional disk
if strings.Contains(disk, "regions") {
rv, err := ParseRegionDiskFieldValue(disk, d, config)
if err != nil {
return err
}
diskSrc = fmt.Sprintf("projects/%s/regions/%s/disks/%s", rv.Project, rv.Region, diskName)
}

attachedDisk := compute.AttachedDisk{
Source: fmt.Sprintf("projects/%s/zones/%s/disks/%s", zv.Project, zv.Zone, diskName),
Source: diskSrc,
Mode: d.Get("mode").(string),
DeviceName: d.Get("device_name").(string),
}
Expand Down
64 changes: 64 additions & 0 deletions google/resource_compute_attached_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ func TestAccComputeAttachedDisk_full(t *testing.T) {

}

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

diskName := acctest.RandomWithPrefix("tf-test")
instanceName := acctest.RandomWithPrefix("tf-test")
importID := fmt.Sprintf("%s/us-central1-a/%s:%s", getTestProjectFromEnv(), instanceName, diskName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
// Check destroy isn't a good test here, see comment on testCheckAttachedDiskIsNowDetached
CheckDestroy: nil,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAttachedDiskResource_region(diskName, instanceName),
},
resource.TestStep{
ResourceName: "google_compute_attached_disk.test",
ImportStateId: importID,
ImportState: true,
ImportStateVerify: true,
},
},
})

}

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

Expand Down Expand Up @@ -152,6 +179,43 @@ resource "google_compute_attached_disk" "test" {
}`)
}

func testAttachedDiskResource_region(diskName, instanceName string) string {
return fmt.Sprintf(`
resource "google_compute_attached_disk" "test" {
disk = "${google_compute_region_disk.region.self_link}"
instance = "${google_compute_instance.test.self_link}"
}
resource "google_compute_region_disk" "region" {
name = "%s"
region = "us-central1"
size = 10
replica_zones = ["us-central1-b", "us-central1-a"]
}
resource "google_compute_instance" "test" {
name = "%s"
machine_type = "f1-micro"
zone = "us-central1-a"
lifecycle {
ignore_changes = [
"attached_disk",
]
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
}
}`, diskName, instanceName)
}

func testAttachedDiskResource(diskName, instanceName string) string {
return fmt.Sprintf(`
resource "google_compute_disk" "test1" {
Expand Down

0 comments on commit c457caa

Please sign in to comment.