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