github.com/loafoe/cli@v7.1.0+incompatible/integration/helpers/org_and_space.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "strings" 6 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/gexec" 9 ) 10 11 // SetupReadOnlyOrgAndSpace creates a randomly-named org containing two randomly-named 12 // spaces. It creates a new CF_HOME directory to run these commands, then deletes it 13 // afterwards. 14 func SetupReadOnlyOrgAndSpace() (string, string) { 15 homeDir := SetHomeDir() 16 SetAPI() 17 LoginCF() 18 orgName := NewOrgName() 19 spaceName1 := NewSpaceName() 20 spaceName2 := NewSpaceName() 21 Eventually(CF("create-org", orgName)).Should(Exit(0)) 22 Eventually(CF("create-space", spaceName1, "-o", orgName)).Should(Exit(0)) 23 Eventually(CF("create-space", spaceName2, "-o", orgName)).Should(Exit(0)) 24 DestroyHomeDir(homeDir) 25 return orgName, spaceName1 26 } 27 28 // CreateAndTargetOrg creates a randomly-named org and targets it. 29 func CreateAndTargetOrg() string { 30 org := NewOrgName() 31 CreateOrg(org) 32 TargetOrg(org) 33 return org 34 } 35 36 // CreateOrgAndSpace creates an org and a space in that org with specified names. 37 func CreateOrgAndSpace(org string, space string) { 38 CreateOrg(org) 39 TargetOrg(org) 40 CreateSpace(space) 41 } 42 43 // CreateOrgAndSpaceUnlessExists creates an org and a space in that org with 44 // specified names only if these don't exist yet. 45 func CreateOrgAndSpaceUnlessExists(org string, space string) { 46 WithRandomHomeDir(func() { 47 SetAPI() 48 LoginCF() 49 50 session := CF("org", org) 51 Eventually(session).Should(Exit()) 52 if session.ExitCode() != 0 { 53 CreateOrgAndSpace(org, space) 54 return 55 } 56 57 TargetOrg(org) 58 59 session = CF("space", space) 60 Eventually(session).Should(Exit()) 61 if session.ExitCode() != 0 { 62 CreateSpace(space) 63 } 64 }) 65 } 66 67 // CreateOrg creates an org with the given name using 'cf create-org'. 68 func CreateOrg(org string) { 69 Eventually(CF("create-org", org)).Should(Exit(0)) 70 } 71 72 // CreateSpace creates a space with the given name using 'cf create-space'. 73 func CreateSpace(space string) { 74 Eventually(CF("create-space", space)).Should(Exit(0)) 75 } 76 77 // SetOrgRole sets the org role with `cf set-org-role`. 78 func SetOrgRole(username, org, role string, isClient bool) { 79 if isClient { 80 Eventually(CF("set-org-role", username, org, role, "--client")).Should(Exit(0)) 81 } else { 82 Eventually(CF("set-org-role", username, org, role)).Should(Exit(0)) 83 } 84 } 85 86 // SetSpaceRole sets the space role with `cf set-org-role`. 87 func SetSpaceRole(username, org, space, role string, isClient bool) { 88 if isClient { 89 Eventually(CF("set-space-role", username, org, space, role, "--client")).Should(Exit(0)) 90 } else { 91 Eventually(CF("set-space-role", username, org, space, role)).Should(Exit(0)) 92 } 93 } 94 95 // GetOrgGUID gets the GUID of an org with the given name. 96 func GetOrgGUID(orgName string) string { 97 session := CF("org", "--guid", orgName) 98 Eventually(session).Should(Exit(0)) 99 return strings.TrimSpace(string(session.Out.Contents())) 100 } 101 102 // GetSpaceGUID gets the GUID of a space with the given name. 103 func GetSpaceGUID(spaceName string) string { 104 session := CF("space", "--guid", spaceName) 105 Eventually(session).Should(Exit(0)) 106 return strings.TrimSpace(string(session.Out.Contents())) 107 } 108 109 // QuickDeleteOrg deletes the org with the given name, if provided, using 110 // 'cf curl /v2/organizations... -X DELETE'. 111 func QuickDeleteOrg(orgName string) { 112 // If orgName is empty, the BeforeSuite has failed and attempting to delete 113 // will produce a meaningless error. 114 if orgName == "" { 115 fmt.Println("Empty org name. Skipping deletion.") 116 return 117 } 118 119 guid := GetOrgGUID(orgName) 120 url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid) 121 session := CF("curl", "-X", "DELETE", url) 122 Eventually(session).Should(Exit(0)) 123 } 124 125 // QuickDeleteOrgIfExists deletes the org with the given name, if it exists, using 126 // 'cf curl /v2/organizations... -X DELETE'. 127 func QuickDeleteOrgIfExists(orgName string) { 128 session := CF("org", "--guid", orgName) 129 Eventually(session).Should(Exit()) 130 if session.ExitCode() != 0 { 131 return 132 } 133 guid := strings.TrimSpace(string(session.Out.Contents())) 134 url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid) 135 session = CF("curl", "-X", "DELETE", url) 136 Eventually(session).Should(Exit()) 137 } 138 139 // QuickDeleteSpace deletes the space with the given name, if it exists, using 140 // 'cf curl /v2/spaces... -X DELETE'. 141 func QuickDeleteSpace(spaceName string) { 142 guid := GetSpaceGUID(spaceName) 143 url := fmt.Sprintf("/v2/spaces/%s?recursive=true&async=true", guid) 144 session := CF("curl", "-X", "DELETE", url) 145 Eventually(session).Should(Exit(0)) 146 }