Skip to content

Commit

Permalink
Fixed error in provider
Browse files Browse the repository at this point in the history
Fixed error in volume attachment cmd
Added the test for volume attachment

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jun 30, 2020
1 parent b73725b commit 0c87d35
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions civo/resource_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func resourceVolumeAttachmentCreate(d *schema.ResourceData, m interface{}) error
if volume.InstanceID == "" || volume.InstanceID != instanceID {
// Only one volume can be attached at one time to a single droplet.
log.Printf("[INFO] attaching the volume %s to instance %s", volumeID, instanceID)
_, err := apiClient.AttachVolume(volumeID, volumeID)
_, err := apiClient.AttachVolume(volumeID, instanceID)
if err != nil {
return fmt.Errorf("[ERR] error attaching volume to instance %s", err)
}
}

d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-%s-", instanceID, volumeID)))

return resourceNetworkRead(d, m)
return resourceVolumeAttachmentRead(d, m)
}

// function to read the volume
Expand Down
70 changes: 70 additions & 0 deletions civo/resource_volume_attachment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package civo

import (
"fmt"
"testing"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

// example.Widget represents a concrete Go type that represents an API resource
func TestAccCivoVolumeAttachment_basic(t *testing.T) {
var volume civogo.Volume

// generate a random name for each test run
resName := "civo_volume_attachment.foobar"
var VolumeAttachmentName = acctest.RandomWithPrefix("tf-test")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCivoVolumeAttachmentDestroy,
Steps: []resource.TestStep{
{
// use a dynamic configuration with the random name from above
Config: testAccCheckCivoVolumeAttachmentConfigBasic(VolumeAttachmentName),
// compose a basic test, checking both remote and local values
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the widget object
testAccCheckCivoVolumeResourceExists("civo_volume.foo", &volume),
// verify local values
resource.TestCheckResourceAttrSet(resName, "id"),
resource.TestCheckResourceAttrSet(resName, "instance_id"),
resource.TestCheckResourceAttrSet(resName, "volume_id"),
),
},
},
})
}

func testAccCheckCivoVolumeAttachmentDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "civo_volume_attachment" {
continue
}
}

return nil
}

func testAccCheckCivoVolumeAttachmentConfigBasic(name string) string {
return fmt.Sprintf(`
resource "civo_instance" "vm" {
hostname = "instance-%s"
}
resource "civo_volume" "foo" {
name = "%s"
size_gb = 60
bootable = false
}
resource "civo_volume_attachment" "foobar" {
instance_id = civo_instance.vm.id
volume_id = civo_volume.foo.id
}
`, name, name)
}

0 comments on commit 0c87d35

Please sign in to comment.