func TestDetach(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() MockDetachResponse(t) err := volumeactions.Detach(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", &volumeactions.DetachOpts{}).ExtractErr() th.AssertNoErr(t, err) }
// DeleteVolumeAttach will detach a volume from an instance. A fatal error will // occur if the snapshot failed to be deleted. This works best when used as a // deferred function. func DeleteVolumeAttach(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) { t.Logf("Attepting to detach volume volume: %s", volume.ID) detachOpts := volumeactions.DetachOpts{ AttachmentID: volume.Attachments[0].AttachmentID, } if err := volumeactions.Detach(client, volume.ID, detachOpts).ExtractErr(); err != nil { t.Fatalf("Unable to detach volume %s: %v", volume.ID, err) } if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil { t.Fatalf("Volume %s failed to become unavailable in 60 seconds: %v", volume.ID, err) } t.Logf("Detached volume: %s", volume.ID) }
func resourceBlockStorageVolumeAttachV2Delete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) client, err := config.blockStorageV2Client(GetRegion(d)) if err != nil { return fmt.Errorf("Error creating OpenStack block storage client: %s", err) } volumeId, attachmentId, err := blockStorageVolumeAttachV2ParseId(d.Id()) if err != nil { return err } detachOpts := volumeactions.DetachOpts{ AttachmentID: attachmentId, } log.Printf("[DEBUG] Detachment Options: %#v", detachOpts) if err := volumeactions.Detach(client, volumeId, detachOpts).ExtractErr(); err != nil { return err } stateConf := &resource.StateChangeConf{ Pending: []string{"in-use", "attaching", "detaching"}, Target: []string{"available"}, Refresh: VolumeV2StateRefreshFunc(client, volumeId), Timeout: 10 * time.Minute, Delay: 10 * time.Second, MinTimeout: 3 * time.Second, } _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf("Error waiting for volume (%s) to become available: %s", volumeId, err) } return nil }