github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/integration/helpers/domains.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  // DomainName returns a random domain name, with a given prefix if provided.
    14  func DomainName(prefix ...string) string {
    15  	if len(prefix) > 0 {
    16  		return fmt.Sprintf("integration-%s.com", PrefixedRandomName(prefix[0]))
    17  	}
    18  	return fmt.Sprintf("integration%s.com", PrefixedRandomName(""))
    19  }
    20  
    21  // Domain represents a domain scoped to an organization.
    22  type Domain struct {
    23  	Org  string
    24  	Name string
    25  }
    26  
    27  // NewDomain constructs a new Domain with given owning organization and name.
    28  func NewDomain(org string, name string) Domain {
    29  	return Domain{
    30  		Org:  org,
    31  		Name: name,
    32  	}
    33  }
    34  
    35  // globally cached
    36  var foundDefaultDomain string
    37  
    38  // DefaultSharedDomain runs 'cf domains' to find the default domain, caching
    39  // the result so that the same domain is returned each time it is called.
    40  func DefaultSharedDomain() string {
    41  	if foundDefaultDomain == "" {
    42  		session := CF("domains")
    43  		Eventually(session).Should(Exit(0))
    44  
    45  		output := strings.Split(string(session.Out.Contents()), "\n")
    46  		for _, line := range output {
    47  			if line == "" {
    48  				// Skip empty lines
    49  				continue
    50  			}
    51  
    52  			if strings.HasPrefix(line, "integration-") {
    53  				// Skip domains created as part of integration tests
    54  				continue
    55  			}
    56  
    57  			if strings.HasSuffix(line, "tcp") {
    58  				// Skip domains with protocol "tcp"
    59  				continue
    60  			}
    61  
    62  			regex := regexp.MustCompile(`(.+?)\s+shared.*`)
    63  			matches := regex.FindStringSubmatch(line)
    64  			if len(matches) == 2 && !strings.Contains(matches[0], "true") && !strings.Contains(matches[0], "internal") {
    65  				foundDefaultDomain = matches[1]
    66  				break
    67  			}
    68  		}
    69  
    70  		Expect(foundDefaultDomain).ToNot(BeEmpty())
    71  	}
    72  	return foundDefaultDomain
    73  }
    74  
    75  // Create uses 'cf create-domain' to create the domain in org d.Org with name
    76  // d.Name.
    77  func (d Domain) Create() {
    78  	Eventually(CF("create-domain", d.Org, d.Name)).Should(Exit(0))
    79  	Eventually(CF("domains")).Should(And(Exit(0), Say(d.Name)))
    80  }
    81  
    82  // CreatePrivate uses 'cf create-private-domain' to create the domain in org
    83  // d.Org with name d.Name.
    84  func (d Domain) CreatePrivate() {
    85  	Eventually(CF("create-private-domain", d.Org, d.Name)).Should(Exit(0))
    86  }
    87  
    88  // CreateShared uses 'cf create-shared-domain' to create an shared domain
    89  // with name d.Name.
    90  func (d Domain) CreateShared() {
    91  	Eventually(CF("create-shared-domain", d.Name)).Should(Exit(0))
    92  }
    93  
    94  // CreateWithRouterGroup uses 'cf create-shared-domain' to create a shared
    95  // domain with name d.Name and given router group.
    96  func (d Domain) CreateWithRouterGroup(routerGroup string) {
    97  	Eventually(CF("create-shared-domain", d.Name, "--router-group", routerGroup)).Should(Exit(0))
    98  }
    99  
   100  // CreateInternal uses 'cf create-shared-domain' to create an shared,
   101  // internal domain with name d.Name.
   102  func (d Domain) CreateInternal() {
   103  	Eventually(CF("create-shared-domain", d.Name, "--internal")).Should(Exit(0))
   104  }
   105  
   106  // V7Share uses 'cf share-private-domain' to share the domain with the given
   107  // org.
   108  func (d Domain) V7Share(orgName string) {
   109  	Eventually(CF("share-private-domain", orgName, d.Name)).Should(Exit(0))
   110  }
   111  
   112  // Delete uses 'cf delete-domain' to delete the domain without asking for
   113  // confirmation.
   114  func (d Domain) Delete() {
   115  	Eventually(CF("delete-domain", d.Name, "-f")).Should(Exit(0))
   116  }
   117  
   118  // DeletePrivate uses 'cf delete-private-domain' to delete the domain without asking for
   119  // confirmation.
   120  func (d Domain) DeletePrivate() {
   121  	Eventually(CF("delete-private-domain", d.Name, "-f")).Should(Exit(0))
   122  }
   123  
   124  // DeleteShared uses 'cf delete-shared-domain' to delete the shared domain
   125  // without asking for confirmation.
   126  func (d Domain) DeleteShared() {
   127  	Eventually(CF("delete-shared-domain", d.Name, "-f")).Should(Exit(0))
   128  }