github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/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  // CreateSpaceInOrg creates a space with the given name using 'cf create-space'.
    78  func CreateSpaceInOrg(space string, org string) {
    79  	Eventually(CF("create-space", space, "-o", org)).Should(Exit(0))
    80  }
    81  
    82  // SetOrgRole sets the org role with `cf set-org-role`.
    83  func SetOrgRole(username, org, role string, isClient bool) {
    84  	if isClient {
    85  		Eventually(CF("set-org-role", username, org, role, "--client")).Should(Exit(0))
    86  	} else {
    87  		Eventually(CF("set-org-role", username, org, role)).Should(Exit(0))
    88  	}
    89  }
    90  
    91  // SetSpaceRole sets the space role with `cf set-org-role`.
    92  func SetSpaceRole(username, org, space, role string, isClient bool) {
    93  	if isClient {
    94  		Eventually(CF("set-space-role", username, org, space, role, "--client")).Should(Exit(0))
    95  	} else {
    96  		Eventually(CF("set-space-role", username, org, space, role)).Should(Exit(0))
    97  	}
    98  }
    99  
   100  // GetOrgGUID gets the GUID of an org with the given name.
   101  func GetOrgGUID(orgName string) string {
   102  	session := CF("org", "--guid", orgName)
   103  	Eventually(session).Should(Exit(0))
   104  	return strings.TrimSpace(string(session.Out.Contents()))
   105  }
   106  
   107  // GetSpaceGUID gets the GUID of a space with the given name.
   108  func GetSpaceGUID(spaceName string) string {
   109  	session := CF("space", "--guid", spaceName)
   110  	Eventually(session).Should(Exit(0))
   111  	return strings.TrimSpace(string(session.Out.Contents()))
   112  }
   113  
   114  // QuickDeleteOrg deletes the org with the given name, if provided, using
   115  // 'cf curl /v2/organizations... -X DELETE'.
   116  func QuickDeleteOrg(orgName string) {
   117  	// If orgName is empty, the BeforeSuite has failed and attempting to delete
   118  	// will produce a meaningless error.
   119  	if orgName == "" {
   120  		fmt.Println("Empty org name. Skipping deletion.")
   121  		return
   122  	}
   123  
   124  	guid := GetOrgGUID(orgName)
   125  	url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid)
   126  	session := CF("curl", "-X", "DELETE", url)
   127  	Eventually(session).Should(Exit(0))
   128  }
   129  
   130  // QuickDeleteOrgIfExists deletes the org with the given name, if it exists, using
   131  // 'cf curl /v2/organizations... -X DELETE'.
   132  func QuickDeleteOrgIfExists(orgName string) {
   133  	session := CF("org", "--guid", orgName)
   134  	Eventually(session).Should(Exit())
   135  	if session.ExitCode() != 0 {
   136  		return
   137  	}
   138  	guid := strings.TrimSpace(string(session.Out.Contents()))
   139  	url := fmt.Sprintf("/v2/organizations/%s?recursive=true&async=true", guid)
   140  	session = CF("curl", "-X", "DELETE", url)
   141  	Eventually(session).Should(Exit())
   142  }
   143  
   144  // QuickDeleteSpace deletes the space with the given name, if it exists, using
   145  // 'cf curl /v2/spaces... -X DELETE'.
   146  func QuickDeleteSpace(spaceName string) {
   147  	guid := GetSpaceGUID(spaceName)
   148  	url := fmt.Sprintf("/v2/spaces/%s?recursive=true&async=true", guid)
   149  	session := CF("curl", "-X", "DELETE", url)
   150  	Eventually(session).Should(Exit(0))
   151  }