File: shareaccessrules.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (73 lines) | stat: -rw-r--r-- 2,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package v2

import (
	"fmt"
	"testing"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shareaccessrules"
	"github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shares"
)

func ShareAccessRuleGet(t *testing.T, client *gophercloud.ServiceClient, accessID string) (*shareaccessrules.ShareAccess, error) {
	accessRule, err := shareaccessrules.Get(client, accessID).Extract()
	if err != nil {
		t.Logf("Failed to get share access rule %s: %v", accessID, err)
		return nil, err
	}

	return accessRule, nil
}

// AccessRightToShareAccess is a helper function that converts
// shares.AccessRight into shareaccessrules.ShareAccess struct.
func AccessRightToShareAccess(accessRight *shares.AccessRight) *shareaccessrules.ShareAccess {
	return &shareaccessrules.ShareAccess{
		ShareID:     accessRight.ShareID,
		AccessType:  accessRight.AccessType,
		AccessTo:    accessRight.AccessTo,
		AccessKey:   accessRight.AccessKey,
		AccessLevel: accessRight.AccessLevel,
		State:       accessRight.State,
		ID:          accessRight.ID,
	}
}

func WaitForShareAccessRule(t *testing.T, client *gophercloud.ServiceClient, accessRule *shareaccessrules.ShareAccess, status string) error {
	if accessRule.State == status {
		return nil
	}

	return tools.WaitFor(func() (bool, error) {
		latest, err := ShareAccessRuleGet(t, client, accessRule.ID)
		if err != nil {
			if _, ok := err.(gophercloud.ErrDefault404); ok {
				return false, nil
			}

			return false, err
		}

		if latest.State == status {
			*accessRule = *latest
			return true, nil
		}

		if latest.State == "error" {
			return false, fmt.Errorf("share access rule %s for share %s is in error state", accessRule.ID, accessRule.ShareID)
		}

		return false, nil
	})
}

func ShareAccessRuleList(t *testing.T, client *gophercloud.ServiceClient, shareID string) ([]shareaccessrules.ShareAccess, error) {
	accessRules, err := shareaccessrules.List(client, shareID).Extract()
	if err != nil {
		t.Logf("Failed to list share access rules for share %s: %v", shareID, err)
		return nil, err
	}

	return accessRules, nil
}