github.com/leeclow-ops/gophercloud@v1.2.1/acceptance/openstack/sharedfilesystems/v2/snapshots.go (about) 1 package v2 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/leeclow-ops/gophercloud" 9 "github.com/leeclow-ops/gophercloud/acceptance/tools" 10 "github.com/leeclow-ops/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 t.Errorf("Unable to delete snapshot %s: %v", snapshot.ID, err) 58 } 59 60 err = waitForSnapshotStatus(t, client, snapshot.ID, "deleted") 61 if err != nil { 62 t.Errorf("Failed to wait for 'deleted' status for %s snapshot: %v", snapshot.ID, err) 63 } else { 64 t.Logf("Deleted snapshot: %s", snapshot.ID) 65 } 66 } 67 68 func waitForSnapshotStatus(t *testing.T, c *gophercloud.ServiceClient, id, status string) error { 69 err := tools.WaitFor(func() (bool, error) { 70 current, err := snapshots.Get(c, id).Extract() 71 if err != nil { 72 if _, ok := err.(gophercloud.ErrDefault404); ok { 73 switch status { 74 case "deleted": 75 return true, nil 76 default: 77 return false, err 78 } 79 } 80 return false, err 81 } 82 83 if current.Status == status { 84 return true, nil 85 } 86 87 if strings.Contains(current.Status, "error") { 88 return true, fmt.Errorf("An error occurred, wrong status: %s", current.Status) 89 } 90 91 return false, nil 92 }) 93 94 if err != nil { 95 mErr := PrintMessages(t, c, id) 96 if mErr != nil { 97 return fmt.Errorf("Snapshot status is '%s' and unable to get manila messages: %s", err, mErr) 98 } 99 } 100 101 return err 102 }