forked from cloudfoundry/cli
/
org_and_space.go
151 lines (131 loc) · 4.57 KB
/
org_and_space.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package helpers
import (
"fmt"
"strings"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
// SetupReadOnlyOrgAndSpace creates a randomly-named org containing two randomly-named
// spaces. It creates a new CF_HOME directory to run these commands, then deletes it
// afterwards.
func SetupReadOnlyOrgAndSpace() (string, string) {
homeDir := SetHomeDir()
SetAPI()
LoginCF()
orgName := NewOrgName()
spaceName1 := NewSpaceName()
spaceName2 := NewSpaceName()
Eventually(CF("create-org", orgName)).Should(Exit(0))
Eventually(CF("create-space", spaceName1, "-o", orgName)).Should(Exit(0))
Eventually(CF("create-space", spaceName2, "-o", orgName)).Should(Exit(0))
DestroyHomeDir(homeDir)
return orgName, spaceName1
}
// CreateAndTargetOrg creates a randomly-named org and targets it.
func CreateAndTargetOrg() string {
org := NewOrgName()
CreateOrg(org)
TargetOrg(org)
return org
}
// CreateOrgAndSpace creates an org and a space in that org with specified names.
func CreateOrgAndSpace(org string, space string) {
CreateOrg(org)
TargetOrg(org)
CreateSpace(space)
}
// CreateOrgAndSpaceUnlessExists creates an org and a space in that org with
// specified names only if these don't exist yet.
func CreateOrgAndSpaceUnlessExists(org string, space string) {
WithRandomHomeDir(func() {
SetAPI()
LoginCF()
session := CF("org", org)
Eventually(session).Should(Exit())
if session.ExitCode() != 0 {
CreateOrgAndSpace(org, space)
return
}
TargetOrg(org)
session = CF("space", space)
Eventually(session).Should(Exit())
if session.ExitCode() != 0 {
CreateSpace(space)
}
})
}
// CreateOrg creates an org with the given name using 'cf create-org'.
func CreateOrg(org string) {
Eventually(CF("create-org", org)).Should(Exit(0))
}
// CreateSpace creates a space with the given name using 'cf create-space'.
func CreateSpace(space string) {
Eventually(CF("create-space", space)).Should(Exit(0))
}
// CreateSpaceInOrg creates a space with the given name using 'cf create-space'.
func CreateSpaceInOrg(space string, org string) {
Eventually(CF("create-space", space, "-o", org)).Should(Exit(0))
}
// SetOrgRole sets the org role with `cf set-org-role`.
func SetOrgRole(username, org, role string, isClient bool) {
if isClient {
Eventually(CF("set-org-role", username, org, role, "--client")).Should(Exit(0))
} else {
Eventually(CF("set-org-role", username, org, role)).Should(Exit(0))
}
}
// SetSpaceRole sets the space role with `cf set-org-role`.
func SetSpaceRole(username, org, space, role string, isClient bool) {
if isClient {
Eventually(CF("set-space-role", username, org, space, role, "--client")).Should(Exit(0))
} else {
Eventually(CF("set-space-role", username, org, space, role)).Should(Exit(0))
}
}
// GetOrgGUID gets the GUID of an org with the given name.
func GetOrgGUID(orgName string) string {
session := CF("org", "--guid", orgName)
Eventually(session).Should(Exit(0))
return strings.TrimSpace(string(session.Out.Contents()))
}
// GetSpaceGUID gets the GUID of a space with the given name.
func GetSpaceGUID(spaceName string) string {
session := CF("space", "--guid", spaceName)
Eventually(session).Should(Exit(0))
return strings.TrimSpace(string(session.Out.Contents()))
}
// QuickDeleteOrg deletes the org with the given name, if provided, using
// 'cf curl /v2/organizations... -X DELETE'.
func QuickDeleteOrg(orgName string) {
// If orgName is empty, the BeforeSuite has failed and attempting to delete
// will produce a meaningless error.
if orgName == "" {
fmt.Println("Empty org name. Skipping deletion.")
return
}
guid := GetOrgGUID(orgName)
url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid)
session := CF("curl", "-X", "DELETE", url)
Eventually(session).Should(Exit(0))
}
// QuickDeleteOrgIfExists deletes the org with the given name, if it exists, using
// 'cf curl /v2/organizations... -X DELETE'.
func QuickDeleteOrgIfExists(orgName string) {
session := CF("org", "--guid", orgName)
Eventually(session).Should(Exit())
if session.ExitCode() != 0 {
return
}
guid := strings.TrimSpace(string(session.Out.Contents()))
url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid)
session = CF("curl", "-X", "DELETE", url)
Eventually(session).Should(Exit())
}
// QuickDeleteSpace deletes the space with the given name, if it exists, using
// 'cf curl /v2/spaces... -X DELETE'.
func QuickDeleteSpace(spaceName string) {
guid := GetSpaceGUID(spaceName)
url := fmt.Sprintf("/v2/spaces/%s?recursive=true&async=true", guid)
session := CF("curl", "-X", "DELETE", url)
Eventually(session).Should(Exit(0))
}