github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/sharedfilesystems/v2/snapshots.go (about) 1 package v2 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/gophercloud/gophercloud" 9 "github.com/gophercloud/gophercloud/internal/acceptance/tools" 10 "github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/snapshots" 11 ) 12 13 // CreateSnapshot will create a snapshot from the share ID with a name. An error will 14 // be returned if the snapshot could not be created 15 func CreateSnapshot(t *testing.T, client *gophercloud.ServiceClient, shareID string) (*snapshots.Snapshot, error) { 16 if testing.Short() { 17 t.Skip("Skipping test that requres share creation in short mode.") 18 } 19 20 createOpts := snapshots.CreateOpts{ 21 ShareID: shareID, 22 Name: "My Test Snapshot", 23 Description: "My Test Description", 24 } 25 26 snapshot, err := snapshots.Create(client, createOpts).Extract() 27 if err != nil { 28 t.Logf("Failed to create snapshot") 29 return nil, err 30 } 31 32 err = waitForSnapshotStatus(t, client, snapshot.ID, "available") 33 if err != nil { 34 t.Logf("Failed to get %s snapshot status", snapshot.ID) 35 return snapshot, err 36 } 37 38 return snapshot, nil 39 } 40 41 // ListSnapshots lists all snapshots that belong to this tenant's project. 42 // An error will be returned if the snapshots could not be listed.. 43 func ListSnapshots(t *testing.T, client *gophercloud.ServiceClient) ([]snapshots.Snapshot, error) { 44 r, err := snapshots.ListDetail(client, &snapshots.ListOpts{}).AllPages() 45 if err != nil { 46 return nil, err 47 } 48 49 return snapshots.ExtractSnapshots(r) 50 } 51 52 // DeleteSnapshot will delete a snapshot. A fatal error will occur if the snapshot 53 // failed to be deleted. This works best when used as a deferred function. 54 func DeleteSnapshot(t *testing.T, client *gophercloud.ServiceClient, snapshot *snapshots.Snapshot) { 55 err := snapshots.Delete(client, snapshot.ID).ExtractErr() 56 if err != nil { 57 if _, ok := err.(gophercloud.ErrDefault404); ok { 58 return 59 } 60 t.Errorf("Unable to delete snapshot %s: %v", snapshot.ID, err) 61 } 62 63 err = waitForSnapshotStatus(t, client, snapshot.ID, "deleted") 64 if err != nil { 65 t.Errorf("Failed to wait for 'deleted' status for %s snapshot: %v", snapshot.ID, err) 66 } else { 67 t.Logf("Deleted snapshot: %s", snapshot.ID) 68 } 69 } 70 71 func waitForSnapshotStatus(t *testing.T, c *gophercloud.ServiceClient, id, status string) error { 72 err := tools.WaitFor(func() (bool, error) { 73 current, err := snapshots.Get(c, id).Extract() 74 if err != nil { 75 if _, ok := err.(gophercloud.ErrDefault404); ok { 76 switch status { 77 case "deleted": 78 return true, nil 79 default: 80 return false, err 81 } 82 } 83 return false, err 84 } 85 86 if current.Status == status { 87 return true, nil 88 } 89 90 if strings.Contains(current.Status, "error") { 91 return true, fmt.Errorf("An error occurred, wrong status: %s", current.Status) 92 } 93 94 return false, nil 95 }) 96 97 if err != nil { 98 mErr := PrintMessages(t, c, id) 99 if mErr != nil { 100 return fmt.Errorf("Snapshot status is '%s' and unable to get manila messages: %s", err, mErr) 101 } 102 } 103 104 return err 105 }