github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/sharedfilesystems/v2/shareaccessrules.go (about)

     1  package v2
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/vnpaycloud-console/gophercloud/v2"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/tools"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/sharedfilesystems/v2/shareaccessrules"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/sharedfilesystems/v2/shares"
    13  )
    14  
    15  func ShareAccessRuleGet(t *testing.T, client *gophercloud.ServiceClient, accessID string) (*shareaccessrules.ShareAccess, error) {
    16  	accessRule, err := shareaccessrules.Get(context.TODO(), client, accessID).Extract()
    17  	if err != nil {
    18  		t.Logf("Failed to get share access rule %s: %v", accessID, err)
    19  		return nil, err
    20  	}
    21  
    22  	return accessRule, nil
    23  }
    24  
    25  // AccessRightToShareAccess is a helper function that converts
    26  // shares.AccessRight into shareaccessrules.ShareAccess struct.
    27  func AccessRightToShareAccess(accessRight *shares.AccessRight) *shareaccessrules.ShareAccess {
    28  	return &shareaccessrules.ShareAccess{
    29  		ShareID:     accessRight.ShareID,
    30  		AccessType:  accessRight.AccessType,
    31  		AccessTo:    accessRight.AccessTo,
    32  		AccessKey:   accessRight.AccessKey,
    33  		AccessLevel: accessRight.AccessLevel,
    34  		State:       accessRight.State,
    35  		ID:          accessRight.ID,
    36  	}
    37  }
    38  
    39  func WaitForShareAccessRule(t *testing.T, client *gophercloud.ServiceClient, accessRule *shareaccessrules.ShareAccess, status string) error {
    40  	if accessRule.State == status {
    41  		return nil
    42  	}
    43  
    44  	return tools.WaitFor(func(context.Context) (bool, error) {
    45  		latest, err := ShareAccessRuleGet(t, client, accessRule.ID)
    46  		if err != nil {
    47  			if gophercloud.ResponseCodeIs(err, http.StatusNotFound) {
    48  				return false, nil
    49  			}
    50  
    51  			return false, err
    52  		}
    53  
    54  		if latest.State == status {
    55  			*accessRule = *latest
    56  			return true, nil
    57  		}
    58  
    59  		if latest.State == "error" {
    60  			return false, fmt.Errorf("share access rule %s for share %s is in error state", accessRule.ID, accessRule.ShareID)
    61  		}
    62  
    63  		return false, nil
    64  	})
    65  }
    66  
    67  func ShareAccessRuleList(t *testing.T, client *gophercloud.ServiceClient, shareID string) ([]shareaccessrules.ShareAccess, error) {
    68  	accessRules, err := shareaccessrules.List(context.TODO(), client, shareID).Extract()
    69  	if err != nil {
    70  		t.Logf("Failed to list share access rules for share %s: %v", shareID, err)
    71  		return nil, err
    72  	}
    73  
    74  	return accessRules, nil
    75  }