// 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()}}) }
// 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()}}) }
// 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.GetName()}} err = conn.ServiceInstances().Update(bson.M{"name": si.Name, "units": unit.GetName()}, 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.GetName()}}) if rollbackErr != nil { log.Errorf("[unbind unit] could not add binded unit back to db after failure: %s", rollbackErr) } return err } return nil }