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 }
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 }
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} }