github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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 "--no-route", 114 )).Should(Exit(0)) 115 116 Eventually(CF( 117 "map-route", 118 b.Name, 119 b.AppsDomain, 120 "--hostname", b.Name, 121 )).Should(Exit(0)) 122 123 Eventually(CF("start", b.Name)).Should(Exit(0)) 124 } 125 126 func (b ServiceBroker) Configure(shareable bool) { 127 uri := fmt.Sprintf("http://%s.%s%s", b.Name, b.AppsDomain, "/config") 128 body := strings.NewReader(b.ToJSON(shareable)) 129 req, err := http.NewRequest("POST", uri, body) 130 Expect(err).ToNot(HaveOccurred()) 131 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 132 133 resp, err := http.DefaultClient.Do(req) 134 Expect(err).ToNot(HaveOccurred()) 135 defer resp.Body.Close() 136 } 137 138 func (b ServiceBroker) Create() { 139 appURI := fmt.Sprintf("http://%s.%s", b.Name, b.AppsDomain) 140 Eventually(CF("create-service-broker", b.Name, "username", "password", appURI)).Should(Exit(0)) 141 Eventually(CF("service-brokers")).Should(And(Exit(0), Say(b.Name))) 142 } 143 144 func (b ServiceBroker) Update() { 145 appURI := fmt.Sprintf("http://%s.%s", b.Name, b.AppsDomain) 146 Eventually(CF("update-service-broker", b.Name, "username", "password", appURI)).Should(Exit(0)) 147 Eventually(CF("service-brokers")).Should(And(Exit(0), Say(b.Name))) 148 } 149 150 func (b ServiceBroker) Delete() { 151 Eventually(CF("delete-service-broker", b.Name, "-f")).Should(Exit(0)) 152 Eventually(CF("service-brokers")).Should(And(Exit(0), Not(Say(b.Name)))) 153 } 154 155 func (b ServiceBroker) Destroy() { 156 Eventually(CF("purge-service-offering", b.Service.Name, "-f")).Should(Exit(0)) 157 b.Delete() 158 Eventually(CF("delete", b.Name, "-f", "-r")).Should(Exit(0)) 159 } 160 161 func (b ServiceBroker) ToJSON(shareable bool) string { 162 bytes, err := ioutil.ReadFile(NewAssets().ServiceBroker + "/broker_config.json") 163 Expect(err).To(BeNil()) 164 165 planSchema, err := json.Marshal(b.SyncPlans[0].Schemas) 166 Expect(err).To(BeNil()) 167 168 replacer := strings.NewReplacer( 169 "<fake-service>", b.Service.Name, 170 "<fake-service-guid>", b.Service.ID, 171 "<sso-test>", b.Service.DashboardClient.ID, 172 "<sso-secret>", b.Service.DashboardClient.Secret, 173 "<sso-redirect-uri>", b.Service.DashboardClient.RedirectUri, 174 "<fake-plan>", b.SyncPlans[0].Name, 175 "<fake-plan-guid>", b.SyncPlans[0].ID, 176 "<fake-plan-2>", b.SyncPlans[1].Name, 177 "<fake-plan-2-guid>", b.SyncPlans[1].ID, 178 "<fake-async-plan>", b.AsyncPlans[0].Name, 179 "<fake-async-plan-guid>", b.AsyncPlans[0].ID, 180 "<fake-async-plan-2>", b.AsyncPlans[1].Name, 181 "<fake-async-plan-2-guid>", b.AsyncPlans[1].ID, 182 "<fake-async-plan-3>", b.AsyncPlans[2].Name, 183 "<fake-async-plan-3-guid>", b.AsyncPlans[2].ID, 184 "\"<fake-plan-schema>\"", string(planSchema), 185 "\"<shareable-service>\"", fmt.Sprintf("%t", shareable), 186 "\"<bindable>\"", fmt.Sprintf("%t", b.Service.Bindable), 187 ) 188 189 return replacer.Replace(string(bytes)) 190 } 191 192 type Assets struct { 193 ServiceBroker string 194 } 195 196 func NewAssets() Assets { 197 return Assets{ 198 ServiceBroker: "../../assets/service_broker", 199 } 200 }