github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/helpers/service_broker.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"io/ioutil"
     9  
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  const (
    16  	DefaultMemoryLimit = "256M"
    17  	DefaultDiskLimit   = "1G"
    18  )
    19  
    20  type Plan struct {
    21  	Name string `json:"name"`
    22  	ID   string `json:"id"`
    23  }
    24  
    25  type ServiceBroker struct {
    26  	Name       string
    27  	Path       string
    28  	AppsDomain string
    29  	Service    struct {
    30  		Name            string `json:"name"`
    31  		ID              string `json:"id"`
    32  		DashboardClient struct {
    33  			ID          string `json:"id"`
    34  			Secret      string `json:"secret"`
    35  			RedirectUri string `json:"redirect_uri"`
    36  		}
    37  	}
    38  	SyncPlans  []Plan
    39  	AsyncPlans []Plan
    40  }
    41  
    42  func NewServiceBroker(name string, path string, appsDomain string, serviceName string, planName string) ServiceBroker {
    43  	b := ServiceBroker{}
    44  	b.Path = path
    45  	b.Name = name
    46  	b.AppsDomain = appsDomain
    47  	b.Service.Name = serviceName
    48  	b.Service.ID = RandomName()
    49  	b.SyncPlans = []Plan{
    50  		{Name: planName, ID: RandomName()},
    51  		{Name: RandomName(), ID: RandomName()},
    52  	}
    53  	b.AsyncPlans = []Plan{
    54  		{Name: RandomName(), ID: RandomName()},
    55  		{Name: RandomName(), ID: RandomName()},
    56  	}
    57  	b.Service.DashboardClient.ID = RandomName()
    58  	b.Service.DashboardClient.Secret = RandomName()
    59  	b.Service.DashboardClient.RedirectUri = RandomName()
    60  	return b
    61  }
    62  
    63  func (b ServiceBroker) Push() {
    64  	Eventually(CF(
    65  		"push", b.Name,
    66  		"--no-start",
    67  		"-m", DefaultMemoryLimit,
    68  		"-p", b.Path,
    69  		"-d", b.AppsDomain,
    70  	)).Should(Exit(0))
    71  
    72  	Eventually(CF("start", b.Name)).Should(Exit(0))
    73  }
    74  
    75  func (b ServiceBroker) Configure() {
    76  	uri := fmt.Sprintf("http://%s.%s%s", b.Name, b.AppsDomain, "/config")
    77  	body := strings.NewReader(b.ToJSON())
    78  	req, err := http.NewRequest("POST", uri, body)
    79  	Expect(err).ToNot(HaveOccurred())
    80  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    81  
    82  	resp, err := http.DefaultClient.Do(req)
    83  	Expect(err).ToNot(HaveOccurred())
    84  	defer resp.Body.Close()
    85  }
    86  
    87  func (b ServiceBroker) Create() {
    88  	appURI := fmt.Sprintf("http://%s.%s", b.Name, b.AppsDomain)
    89  	Eventually(CF("create-service-broker", b.Name, "username", "password", appURI)).Should(Exit(0))
    90  	Eventually(CF("service-brokers")).Should(And(Exit(0), Say(b.Name)))
    91  }
    92  
    93  func (b ServiceBroker) Delete() {
    94  	Eventually(CF("delete-service-broker", b.Name, "-f")).Should(Exit(0))
    95  	Eventually(CF("service-brokers")).Should(And(Exit(0), Not(Say(b.Name))))
    96  }
    97  
    98  func (b ServiceBroker) Destroy() {
    99  	Eventually(CF("purge-service-offering", b.Service.Name, "-f")).Should(Exit(0))
   100  	b.Delete()
   101  	Eventually(CF("delete", b.Name, "-f", "-r")).Should(Exit(0))
   102  }
   103  
   104  func (b ServiceBroker) ToJSON() string {
   105  	bytes, err := ioutil.ReadFile(NewAssets().ServiceBroker + "/cats.json")
   106  	Expect(err).To(BeNil())
   107  
   108  	replacer := strings.NewReplacer(
   109  		"<fake-service>", b.Service.Name,
   110  		"<fake-service-guid>", b.Service.ID,
   111  		"<sso-test>", b.Service.DashboardClient.ID,
   112  		"<sso-secret>", b.Service.DashboardClient.Secret,
   113  		"<sso-redirect-uri>", b.Service.DashboardClient.RedirectUri,
   114  		"<fake-plan>", b.SyncPlans[0].Name,
   115  		"<fake-plan-guid>", b.SyncPlans[0].ID,
   116  		"<fake-plan-2>", b.SyncPlans[1].Name,
   117  		"<fake-plan-2-guid>", b.SyncPlans[1].ID,
   118  		"<fake-async-plan>", b.AsyncPlans[0].Name,
   119  		"<fake-async-plan-guid>", b.AsyncPlans[0].ID,
   120  		"<fake-async-plan-2>", b.AsyncPlans[1].Name,
   121  		"<fake-async-plan-2-guid>", b.AsyncPlans[1].ID,
   122  	)
   123  
   124  	return replacer.Replace(string(bytes))
   125  }
   126  
   127  func GetAppGuid(appName string) string {
   128  	session := CF("app", appName, "--guid")
   129  	Eventually(session).Should(Exit(0))
   130  
   131  	appGuid := strings.TrimSpace(string(session.Out.Contents()))
   132  	Expect(appGuid).NotTo(Equal(""))
   133  	return appGuid
   134  }
   135  
   136  type Assets struct {
   137  	ServiceBroker string
   138  }
   139  
   140  func NewAssets() Assets {
   141  	return Assets{
   142  		ServiceBroker: "../assets/service_broker",
   143  	}
   144  }