github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+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 Bindable bool `json:"bindable"` 51 DashboardClient struct { 52 ID string `json:"id"` 53 Secret string `json:"secret"` 54 RedirectUri string `json:"redirect_uri"` 55 } 56 } 57 SyncPlans []Plan 58 AsyncPlans []Plan 59 } 60 61 func NewServiceBroker(name string, path string, appsDomain string, serviceName string, planName string) ServiceBroker { 62 b := ServiceBroker{} 63 b.Path = path 64 b.Name = name 65 b.AppsDomain = appsDomain 66 b.Service.Name = serviceName 67 b.Service.ID = RandomName() 68 b.Service.Bindable = true 69 b.SyncPlans = []Plan{ 70 {Name: planName, ID: RandomName()}, 71 {Name: RandomName(), ID: RandomName()}, 72 } 73 b.AsyncPlans = []Plan{ 74 {Name: RandomName(), ID: RandomName()}, 75 {Name: RandomName(), ID: RandomName()}, 76 {Name: RandomName(), ID: RandomName()}, // accepts_incomplete = true 77 } 78 b.Service.DashboardClient.ID = RandomName() 79 b.Service.DashboardClient.Secret = RandomName() 80 b.Service.DashboardClient.RedirectUri = RandomName() 81 return b 82 } 83 84 func NewAsynchServiceBroker(name string, path string, appsDomain string, serviceName string, planName string) ServiceBroker { 85 b := ServiceBroker{} 86 b.Path = path 87 b.Name = name 88 b.AppsDomain = appsDomain 89 b.Service.Name = serviceName 90 b.Service.ID = RandomName() 91 b.Service.Bindable = true 92 b.SyncPlans = []Plan{ 93 {Name: RandomName(), ID: RandomName()}, 94 {Name: RandomName(), ID: RandomName()}, 95 } 96 b.AsyncPlans = []Plan{ 97 {Name: RandomName(), ID: RandomName()}, 98 {Name: RandomName(), ID: RandomName()}, 99 {Name: planName, ID: RandomName()}, // accepts_incomplete = true 100 } 101 b.Service.DashboardClient.ID = RandomName() 102 b.Service.DashboardClient.Secret = RandomName() 103 b.Service.DashboardClient.RedirectUri = RandomName() 104 return b 105 } 106 107 func (b ServiceBroker) Push() { 108 Eventually(CF( 109 "push", b.Name, 110 "--no-start", 111 "-m", DefaultMemoryLimit, 112 "-p", b.Path, 113 "-d", b.AppsDomain, 114 )).Should(Exit(0)) 115 116 Eventually(CF("start", b.Name)).Should(Exit(0)) 117 } 118 119 func (b ServiceBroker) Configure(shareable bool) { 120 uri := fmt.Sprintf("http://%s.%s%s", b.Name, b.AppsDomain, "/config") 121 body := strings.NewReader(b.ToJSON(shareable)) 122 req, err := http.NewRequest("POST", uri, body) 123 Expect(err).ToNot(HaveOccurred()) 124 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 125 126 resp, err := http.DefaultClient.Do(req) 127 Expect(err).ToNot(HaveOccurred()) 128 defer resp.Body.Close() 129 } 130 131 func (b ServiceBroker) Create() { 132 appURI := fmt.Sprintf("http://%s.%s", b.Name, b.AppsDomain) 133 Eventually(CF("create-service-broker", b.Name, "username", "password", appURI)).Should(Exit(0)) 134 Eventually(CF("service-brokers")).Should(And(Exit(0), Say(b.Name))) 135 } 136 137 func (b ServiceBroker) Update() { 138 appURI := fmt.Sprintf("http://%s.%s", b.Name, b.AppsDomain) 139 Eventually(CF("update-service-broker", b.Name, "username", "password", appURI)).Should(Exit(0)) 140 Eventually(CF("service-brokers")).Should(And(Exit(0), Say(b.Name))) 141 } 142 143 func (b ServiceBroker) Delete() { 144 Eventually(CF("delete-service-broker", b.Name, "-f")).Should(Exit(0)) 145 Eventually(CF("service-brokers")).Should(And(Exit(0), Not(Say(b.Name)))) 146 } 147 148 func (b ServiceBroker) Destroy() { 149 Eventually(CF("purge-service-offering", b.Service.Name, "-f")).Should(Exit(0)) 150 b.Delete() 151 Eventually(CF("delete", b.Name, "-f", "-r")).Should(Exit(0)) 152 } 153 154 func (b ServiceBroker) ToJSON(shareable bool) string { 155 bytes, err := ioutil.ReadFile(NewAssets().ServiceBroker + "/cats.json") 156 Expect(err).To(BeNil()) 157 158 planSchema, err := json.Marshal(b.SyncPlans[0].Schemas) 159 Expect(err).To(BeNil()) 160 161 replacer := strings.NewReplacer( 162 "<fake-service>", b.Service.Name, 163 "<fake-service-guid>", b.Service.ID, 164 "<sso-test>", b.Service.DashboardClient.ID, 165 "<sso-secret>", b.Service.DashboardClient.Secret, 166 "<sso-redirect-uri>", b.Service.DashboardClient.RedirectUri, 167 "<fake-plan>", b.SyncPlans[0].Name, 168 "<fake-plan-guid>", b.SyncPlans[0].ID, 169 "<fake-plan-2>", b.SyncPlans[1].Name, 170 "<fake-plan-2-guid>", b.SyncPlans[1].ID, 171 "<fake-async-plan>", b.AsyncPlans[0].Name, 172 "<fake-async-plan-guid>", b.AsyncPlans[0].ID, 173 "<fake-async-plan-2>", b.AsyncPlans[1].Name, 174 "<fake-async-plan-2-guid>", b.AsyncPlans[1].ID, 175 "<fake-async-plan-3>", b.AsyncPlans[2].Name, 176 "<fake-async-plan-3-guid>", b.AsyncPlans[2].ID, 177 "\"<fake-plan-schema>\"", string(planSchema), 178 "\"<shareable-service>\"", fmt.Sprintf("%t", shareable), 179 "\"<bindable>\"", fmt.Sprintf("%t", b.Service.Bindable), 180 ) 181 182 return replacer.Replace(string(bytes)) 183 } 184 185 type Assets struct { 186 ServiceBroker string 187 } 188 189 func NewAssets() Assets { 190 return Assets{ 191 ServiceBroker: "../../assets/service_broker", 192 } 193 }