func (si *ServiceInstance) Bind(app bind.App) error { err := si.AddApp(app.GetName()) if err != nil { return &errors.Http{Code: http.StatusConflict, Message: "This app is already binded to this service instance."} } var envVars []bind.EnvVar var setEnv = func(env map[string]string) { for k, v := range env { envVars = append(envVars, bind.EnvVar{ Name: k, Value: v, Public: false, InstanceName: si.Name, }) } } cli := si.Service().ProductionEndpoint() if len(app.GetUnits()) == 0 { return &errors.Http{Code: http.StatusPreconditionFailed, Message: "This app does not have an IP yet."} } env, err := cli.Bind(si, app) if err != nil { return err } setEnv(env) err = si.update() if err != nil { cli.Unbind(si, app) return err } return app.SetEnvs(envVars, false) }
func (c *Client) Unbind(instance *ServiceInstance, app bind.App) (err error) { log.Print("Attempting to call unbind of service instance " + instance.Name + " and app " + app.GetName() + " at " + instance.ServiceName + " api") var resp *http.Response url := "/resources/" + instance.Name + "/hostname/" + app.GetUnits()[0].GetIp() if resp, err = c.issueRequest(url, "DELETE", nil); err == nil && resp.StatusCode > 299 { msg := "Failed to unbind instance " + instance.Name + " from the app " + app.GetName() + ": " + c.buildErrorMessage(err, resp) log.Print(msg) err = &errors.Http{Code: http.StatusInternalServerError, Message: msg} } return }
func (c *Client) Bind(instance *ServiceInstance, app bind.App) (envVars map[string]string, err error) { log.Print("Attempting to call bind of service instance " + instance.Name + " and app " + app.GetName() + " at " + instance.ServiceName + " api") var resp *http.Response params := map[string][]string{ "hostname": {app.GetUnits()[0].GetIp()}, } if resp, err = c.issueRequest("/resources/"+instance.Name, "POST", params); err == nil && resp.StatusCode < 300 { return c.jsonFromResponse(resp) } else if resp.StatusCode == http.StatusPreconditionFailed { err = &errors.Http{Code: resp.StatusCode, Message: "You cannot bind any app to this service instance because it is not ready yet."} } else { msg := "Failed to bind instance " + instance.Name + " to the app " + app.GetName() + ": " + c.buildErrorMessage(err, resp) log.Print(msg) err = &errors.Http{Code: http.StatusInternalServerError, Message: msg} } return }
func (si *ServiceInstance) Unbind(app bind.App) error { err := si.RemoveApp(app.GetName()) if err != nil { return &errors.Http{Code: http.StatusPreconditionFailed, Message: "This app is not binded to this service instance."} } err = si.update() if err != nil { return err } go func() { si.Service().ProductionEndpoint().Unbind(si, app) }() var envVars []string for k := range app.InstanceEnv(si.Name) { envVars = append(envVars, k) } return app.UnsetEnvs(envVars, false) }