github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/sharedfilesystems/v2/replicas.go (about)

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