github.com/arunkumar7540/cli@v6.45.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 // TODO: remove when create-space works with CF_CLI_EXPERIMENTAL=true 23 Eventually(CFWithEnv(map[string]string{"CF_CLI_EXPERIMENTAL": "false"}, "create-space", spaceName1, "-o", orgName)).Should(Exit(0)) 24 Eventually(CFWithEnv(map[string]string{"CF_CLI_EXPERIMENTAL": "false"}, "create-space", spaceName2, "-o", orgName)).Should(Exit(0)) 25 DestroyHomeDir(homeDir) 26 return orgName, spaceName1 27 } 28 29 // CreateAndTargetOrg creates a randomly-named org and targets it. 30 func CreateAndTargetOrg() string { 31 org := NewOrgName() 32 CreateOrg(org) 33 TargetOrg(org) 34 return org 35 } 36 37 // CreateOrgAndSpace creates a randomly-named org and a randomly-named space in that org. 38 func CreateOrgAndSpace(org string, space string) { 39 CreateOrg(org) 40 TargetOrg(org) 41 CreateSpace(space) 42 } 43 44 // CreateOrg creates an org with the given name using 'cf create-org'. 45 func CreateOrg(org string) { 46 Eventually(CF("create-org", org)).Should(Exit(0)) 47 } 48 49 // CreateSpace creates a space with the given name using 'cf create-space'. 50 func CreateSpace(space string) { 51 // TODO: remove when create-space works with CF_CLI_EXPERIMENTAL=true 52 Eventually(CFWithEnv(map[string]string{"CF_CLI_EXPERIMENTAL": "false"}, "create-space", space)).Should(Exit(0)) 53 } 54 55 // GetOrgGUID gets the GUID of an org with the given name. 56 func GetOrgGUID(orgName string) string { 57 session := CF("org", "--guid", orgName) 58 Eventually(session).Should(Exit(0)) 59 return strings.TrimSpace(string(session.Out.Contents())) 60 } 61 62 // GetSpaceGUID gets the GUID of a space with the given name. 63 func GetSpaceGUID(spaceName string) string { 64 session := CF("space", "--guid", spaceName) 65 Eventually(session).Should(Exit(0)) 66 return strings.TrimSpace(string(session.Out.Contents())) 67 } 68 69 // QuickDeleteOrg deletes the org with the given name, if provided, using 70 // 'cf curl /v2/organizations... -X DELETE'. 71 func QuickDeleteOrg(orgName string) { 72 // If orgName is empty, the BeforeSuite has failed and attempting to delete 73 // will produce a meaningless error. 74 if orgName == "" { 75 fmt.Println("Empty org name. Skipping deletion.") 76 return 77 } 78 79 guid := GetOrgGUID(orgName) 80 url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid) 81 session := CF("curl", "-X", "DELETE", url) 82 Eventually(session).Should(Exit(0)) 83 } 84 85 // QuickDeleteOrgIfExists deletes the org with the given name, if it exists, using 86 // 'cf curl /v2/organizations... -X DELETE'. 87 func QuickDeleteOrgIfExists(orgName string) { 88 session := CF("org", "--guid", orgName) 89 Eventually(session).Should(Exit()) 90 if session.ExitCode() != 0 { 91 return 92 } 93 guid := strings.TrimSpace(string(session.Out.Contents())) 94 url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid) 95 session = CF("curl", "-X", "DELETE", url) 96 Eventually(session).Should(Exit()) 97 } 98 99 // QuickDeleteSpace deletes the space with the given name, if it exists, using 100 // 'cf curl /v2/spaces... -X DELETE'. 101 func QuickDeleteSpace(spaceName string) { 102 guid := GetSpaceGUID(spaceName) 103 url := fmt.Sprintf("/v2/spaces/%s?recursive=true&async=true", guid) 104 session := CF("curl", "-X", "DELETE", url) 105 Eventually(session).Should(Exit(0)) 106 }