// UnbindUnit makes the unbind between the service instance and an unit. func (si *ServiceInstance) UnbindUnit(app bind.App, unit bind.Unit) error { endpoint, err := si.Service().getClient("production") if err != nil { return err } conn, err := db.Conn() if err != nil { return err } defer conn.Close() updateOp := bson.M{"$pull": bson.M{"units": unit.GetID()}} err = conn.ServiceInstances().Update(bson.M{"name": si.Name, "service_name": si.ServiceName, "units": unit.GetID()}, updateOp) if err != nil { if err == mgo.ErrNotFound { return ErrUnitNotBound } return err } err = endpoint.UnbindUnit(si, app, unit) if err != nil { rollbackErr := si.update(bson.M{"$addToSet": bson.M{"units": unit.GetID()}}) if rollbackErr != nil { log.Errorf("[unbind unit] could not add bound unit back to db after failure: %s", rollbackErr) } return err } return nil }
func (c *Client) BindUnit(instance *ServiceInstance, app bind.App, unit bind.Unit) error { log.Debugf("Calling bind of instance %q and %q unit at %q API", instance.Name, unit.GetIp(), instance.ServiceName) var resp *http.Response params := map[string][]string{ "app-host": {app.GetIp()}, "unit-host": {unit.GetIp()}, } resp, err := c.issueRequest("/resources/"+instance.GetIdentifier()+"/bind", "POST", params) if err != nil { if m, _ := regexp.MatchString("", err.Error()); m { return fmt.Errorf("%s api is down.", instance.Name) } return err } if resp.StatusCode == http.StatusPreconditionFailed { return &errors.HTTP{Code: resp.StatusCode, Message: "You cannot bind any app to this service instance because it is not ready yet."} } if resp.StatusCode > 299 { msg := fmt.Sprintf("Failed to bind the instance %q to the unit %q: %s", instance.Name, unit.GetIp(), c.buildErrorMessage(err, resp)) log.Error(msg) return &errors.HTTP{Code: http.StatusInternalServerError, Message: msg} } return nil }
func (c *Client) BindUnit(instance *ServiceInstance, app bind.App, unit bind.Unit) error { log.Debugf("Calling bind of instance %q and %q unit at %q API", instance.Name, unit.GetIp(), instance.ServiceName) var resp *http.Response params := map[string][]string{ "app-host": {app.GetIp()}, "unit-host": {unit.GetIp()}, } resp, err := c.issueRequest("/resources/"+instance.GetIdentifier()+"/bind", "POST", params) if err != nil { return log.WrapError(errors.Wrapf(err, `Failed to bind the instance "%s/%s" to the unit %q`, instance.ServiceName, instance.Name, unit.GetIp())) } defer resp.Body.Close() switch resp.StatusCode { case http.StatusPreconditionFailed: return ErrInstanceNotReady case http.StatusNotFound: return ErrInstanceNotFoundInAPI } if resp.StatusCode > 299 { err = errors.Wrapf(c.buildErrorMessage(err, resp), `Failed to bind the instance "%s/%s" to the unit %q`, instance.ServiceName, instance.Name, unit.GetIp()) return log.WrapError(err) } return nil }
func (c *Client) BindUnit(instance *ServiceInstance, app bind.App, unit bind.Unit) error { log.Debugf("Calling bind of instance %q and %q unit at %q API", instance.Name, unit.GetIp(), instance.ServiceName) var resp *http.Response params := map[string][]string{ "app-host": {app.GetIp()}, "unit-host": {unit.GetIp()}, } resp, err := c.issueRequest("/resources/"+instance.GetIdentifier()+"/bind", "POST", params) if err != nil { if m, _ := regexp.MatchString("", err.Error()); m { return fmt.Errorf("%s api is down.", instance.Name) } return err } switch resp.StatusCode { case http.StatusPreconditionFailed: return ErrInstanceNotReady case http.StatusNotFound: return ErrInstanceNotFoundInAPI } if resp.StatusCode > 299 { msg := fmt.Sprintf(`Failed to bind the instance "%s/%s" to the unit %q: %s`, instance.ServiceName, instance.Name, unit.GetIp(), c.buildErrorMessage(err, resp)) log.Error(msg) return errors.New(msg) } return nil }
// UnbindUnit makes the unbind between the service instance and an unit. func (si *ServiceInstance) UnbindUnit(app bind.App, unit bind.Unit) error { endpoint, err := si.Service().getClient("production") if err != nil { return err } err = si.reload() if err != nil { return err } var found bool for _, unitName := range si.Units { if unitName == unit.GetName() { found = true break } } if !found { return ErrUnitNotBound } err = endpoint.UnbindUnit(si, app, unit) if err != nil { return err } return si.update(bson.M{"$pull": bson.M{"units": unit.GetName()}}) }
func (c *Client) Unbind(instance *ServiceInstance, unit bind.Unit) error { log.Debug("Attempting to call unbind of service instance " + instance.Name + " and unit " + unit.GetIp() + " at " + instance.ServiceName + " api") var resp *http.Response url := "/resources/" + instance.Name + "/hostname/" + unit.GetIp() resp, err := c.issueRequest(url, "DELETE", nil) if err == nil && resp.StatusCode > 299 { msg := fmt.Sprintf("Failed to unbind (%q): %s", url, c.buildErrorMessage(err, resp)) log.Error(msg) return &errors.HTTP{Code: http.StatusInternalServerError, Message: msg} } return err }
func (c *Client) UnbindUnit(instance *ServiceInstance, app bind.App, unit bind.Unit) error { log.Debugf("Calling unbind of service instance %q and unit %q at %q", instance.Name, unit.GetIp(), instance.ServiceName) var resp *http.Response url := "/resources/" + instance.GetIdentifier() + "/bind" params := map[string][]string{ "app-host": {app.GetIp()}, "unit-host": {unit.GetIp()}, } resp, err := c.issueRequest(url, "DELETE", params) if err == nil && resp.StatusCode > 299 { msg := fmt.Sprintf("Failed to unbind (%q): %s", url, c.buildErrorMessage(err, resp)) log.Error(msg) return &errors.HTTP{Code: http.StatusInternalServerError, Message: msg} } return err }
// BindUnit makes the bind between the binder and an unit. func (si *ServiceInstance) BindUnit(app bind.App, unit bind.Unit) error { endpoint, err := si.Service().getClient("production") if err != nil { return err } err = si.reload() if err != nil { return err } for _, unitName := range si.Units { if unitName == unit.GetName() { return ErrUnitAlreadyBound } } err = endpoint.BindUnit(si, app, unit) if err != nil { return err } return si.update(bson.M{"$addToSet": bson.M{"units": unit.GetName()}}) }
func (c *Client) UnbindUnit(instance *ServiceInstance, app bind.App, unit bind.Unit) error { log.Debugf("Calling unbind of service instance %q and unit %q at %q", instance.Name, unit.GetIp(), instance.ServiceName) var resp *http.Response url := "/resources/" + instance.GetIdentifier() + "/bind" params := map[string][]string{ "app-host": {app.GetIp()}, "unit-host": {unit.GetIp()}, } resp, err := c.issueRequest(url, "DELETE", params) if err == nil { defer resp.Body.Close() if resp.StatusCode > 299 { if resp.StatusCode == http.StatusNotFound { return ErrInstanceNotFoundInAPI } err = errors.Wrapf(c.buildErrorMessage(err, resp), "Failed to unbind (%q)", url) return log.WrapError(err) } } return err }
func (c *Client) Bind(instance *ServiceInstance, app bind.App, unit bind.Unit) (map[string]string, error) { log.Debugf("Calling bind of instance %q and unit %q at %q API", instance.Name, unit.GetIp(), instance.ServiceName) var resp *http.Response params := map[string][]string{ "unit-host": {unit.GetIp()}, "app-host": {app.GetIp()}, } resp, err := c.issueRequest("/resources/"+instance.Name, "POST", params) if err != nil { if m, _ := regexp.MatchString("", err.Error()); m { return nil, fmt.Errorf("%s api is down.", instance.Name) } return nil, err } if err == nil && resp.StatusCode < 300 { var result map[string]string err = c.jsonFromResponse(resp, &result) if err != nil { return nil, err } return result, nil } if resp.StatusCode == http.StatusPreconditionFailed { return nil, &errors.HTTP{Code: resp.StatusCode, Message: "You cannot bind any app to this service instance because it is not ready yet."} } msg := "Failed to bind instance " + instance.Name + " to the unit " + unit.GetIp() + ": " + c.buildErrorMessage(err, resp) log.Error(msg) return nil, &errors.HTTP{Code: http.StatusInternalServerError, Message: msg} }