github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/sharedfilesystems/v2/replicas.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/internal/acceptance/tools" 11 12 "github.com/vnpaycloud-console/gophercloud/v2" 13 "github.com/vnpaycloud-console/gophercloud/v2/openstack/sharedfilesystems/v2/replicas" 14 "github.com/vnpaycloud-console/gophercloud/v2/openstack/sharedfilesystems/v2/shares" 15 ) 16 17 // CreateReplica will create a replica from shareID. An error will be returned 18 // if the replica could not be created. 19 func CreateReplica(t *testing.T, client *gophercloud.ServiceClient, share *shares.Share) (*replicas.Replica, error) { 20 createOpts := replicas.CreateOpts{ 21 ShareID: share.ID, 22 AvailabilityZone: share.AvailabilityZone, 23 } 24 25 replica, err := replicas.Create(context.TODO(), client, createOpts).Extract() 26 if err != nil { 27 t.Logf("Failed to create replica") 28 return nil, err 29 } 30 31 err = waitForReplicaStatus(t, client, replica.ID, "available") 32 if err != nil { 33 t.Logf("Failed to get %s replica status", replica.ID) 34 DeleteReplica(t, client, replica) 35 return replica, err 36 } 37 38 return replica, nil 39 } 40 41 // DeleteReplica will delete a replica. A fatal error will occur if the replica 42 // failed to be deleted. This works best when used as a deferred function. 43 func DeleteReplica(t *testing.T, client *gophercloud.ServiceClient, replica *replicas.Replica) { 44 err := replicas.Delete(context.TODO(), client, replica.ID).ExtractErr() 45 if err != nil { 46 if gophercloud.ResponseCodeIs(err, http.StatusNotFound) { 47 return 48 } 49 t.Errorf("Unable to delete replica %s: %v", replica.ID, err) 50 } 51 52 err = waitForReplicaStatus(t, client, replica.ID, "deleted") 53 if err != nil { 54 t.Errorf("Failed to wait for 'deleted' status for %s replica: %v", replica.ID, err) 55 } else { 56 t.Logf("Deleted replica: %s", replica.ID) 57 } 58 } 59 60 // ListShareReplicas lists all replicas that belong to shareID. 61 // An error will be returned if the replicas could not be listed.. 62 func ListShareReplicas(t *testing.T, client *gophercloud.ServiceClient, shareID string) ([]replicas.Replica, error) { 63 opts := replicas.ListOpts{ 64 ShareID: shareID, 65 } 66 pages, err := replicas.List(client, opts).AllPages(context.TODO()) 67 if err != nil { 68 t.Errorf("Unable to list %q share replicas: %v", shareID, err) 69 } 70 71 return replicas.ExtractReplicas(pages) 72 } 73 74 func waitForReplicaStatus(t *testing.T, c *gophercloud.ServiceClient, id, status string) error { 75 var current *replicas.Replica 76 77 err := tools.WaitFor(func(ctx context.Context) (bool, error) { 78 var err error 79 80 current, err = replicas.Get(ctx, c, id).Extract() 81 if err != nil { 82 if gophercloud.ResponseCodeIs(err, http.StatusNotFound) { 83 switch status { 84 case "deleted": 85 return true, nil 86 default: 87 return false, err 88 } 89 } 90 return false, err 91 } 92 93 if current.Status == status { 94 return true, nil 95 } 96 97 if strings.Contains(current.Status, "error") { 98 return true, fmt.Errorf("An error occurred, wrong status: %s", current.Status) 99 } 100 101 return false, nil 102 }) 103 104 if err != nil { 105 mErr := PrintMessages(t, c, id) 106 if mErr != nil { 107 return fmt.Errorf("Replica status is '%s' and unable to get manila messages: %s", err, mErr) 108 } 109 } 110 111 return err 112 } 113 114 func waitForReplicaState(t *testing.T, c *gophercloud.ServiceClient, id, state string) error { 115 var current *replicas.Replica 116 117 err := tools.WaitFor(func(ctx context.Context) (bool, error) { 118 var err error 119 120 current, err = replicas.Get(ctx, c, id).Extract() 121 if err != nil { 122 return false, err 123 } 124 125 if current.State == state { 126 return true, nil 127 } 128 129 if strings.Contains(current.State, "error") { 130 return true, fmt.Errorf("An error occurred, wrong state: %s", current.State) 131 } 132 133 return false, nil 134 }) 135 136 if err != nil { 137 mErr := PrintMessages(t, c, id) 138 if mErr != nil { 139 return fmt.Errorf("Replica state is '%s' and unable to get manila messages: %s", err, mErr) 140 } 141 } 142 143 return err 144 }