github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/helpers/service_broker.go (about)

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