github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/integration/helpers/login.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  	"strings"
     8  
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  func SetAPI() (string, string) {
    14  	apiURL := GetAPI()
    15  	skipSSLValidation := skipSSLValidation()
    16  	Eventually(CF("api", apiURL, skipSSLValidation)).Should(Exit(0))
    17  	return apiURL, skipSSLValidation
    18  }
    19  
    20  func UnsetAPI() {
    21  	Eventually(CF("api", "--unset")).Should(Exit(0))
    22  }
    23  
    24  func skipSSLValidation() string {
    25  	if skip, err := strconv.ParseBool(os.Getenv("SKIP_SSL_VALIDATION")); err == nil && !skip {
    26  		return ""
    27  	}
    28  	return "--skip-ssl-validation"
    29  }
    30  
    31  func GetAPI() string {
    32  	apiURL := os.Getenv("CF_INT_API")
    33  	if apiURL == "" {
    34  		return "https://api.bosh-lite.com"
    35  	}
    36  	if !strings.HasPrefix(apiURL, "http") {
    37  		apiURL = fmt.Sprintf("https://%s", apiURL)
    38  	}
    39  
    40  	return apiURL
    41  }
    42  
    43  func LoginCF() string {
    44  	username, password := GetCredentials()
    45  	env := map[string]string{
    46  		"CF_USERNAME": username,
    47  		"CF_PASSWORD": password,
    48  	}
    49  	Eventually(CFWithEnv(env, "auth")).Should(Exit(0))
    50  
    51  	return username
    52  }
    53  
    54  func LoginCFWithClientCredentials() string {
    55  	username, password := SkipIfClientCredentialsNotSet()
    56  	env := map[string]string{
    57  		"CF_USERNAME": username,
    58  		"CF_PASSWORD": password,
    59  	}
    60  	Eventually(CFWithEnv(env, "auth", "--client-credentials")).Should(Exit(0))
    61  
    62  	return username
    63  }
    64  
    65  // GetCredentials returns back the username and the password.
    66  func GetCredentials() (string, string) {
    67  	username := os.Getenv("CF_INT_USERNAME")
    68  	if username == "" {
    69  		username = "admin"
    70  	}
    71  	password := os.Getenv("CF_INT_PASSWORD")
    72  	if password == "" {
    73  		password = "admin"
    74  	}
    75  	return username, password
    76  }
    77  
    78  func LogoutCF() {
    79  	Eventually(CF("logout")).Should(Exit(0))
    80  }
    81  
    82  func TargetOrgAndSpace(org string, space string) {
    83  	Eventually(CF("target", "-o", org, "-s", space)).Should(Exit(0))
    84  }
    85  
    86  func TargetOrg(org string) {
    87  	Eventually(CF("target", "-o", org)).Should(Exit(0))
    88  }
    89  
    90  func ClearTarget() {
    91  	LogoutCF()
    92  	LoginCF()
    93  }
    94  
    95  func SetupCF(org string, space string) {
    96  	LoginCF()
    97  	CreateOrgAndSpace(org, space)
    98  	TargetOrgAndSpace(org, space)
    99  }