func volumeBind(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	var bindInfo struct {
		App        string
		MountPoint string
		ReadOnly   bool
		NoRestart  bool
	}
	err := ParseInput(r, &bindInfo)
	if err != nil {
		return err
	}
	dbVolume, err := volume.Load(r.URL.Query().Get(":name"))
	if err != nil {
		if err == volume.ErrVolumeNotFound {
			return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
		}
		return err
	}
	canBindVolume := permission.Check(t, permission.PermVolumeUpdateBind, contextsForVolume(dbVolume)...)
	if !canBindVolume {
		return permission.ErrUnauthorized
	}
	a, err := getAppFromContext(bindInfo.App, r)
	if err != nil {
		return err
	}
	canBindApp := permission.Check(t, permission.PermAppUpdateBindVolume, contextsForApp(&a)...)
	if !canBindApp {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     event.Target{Type: event.TargetTypeVolume, Value: dbVolume.Name},
		Kind:       permission.PermVolumeUpdateBind,
		Owner:      t,
		CustomData: event.FormToCustomData(InputFields(r)),
		Allowed:    event.Allowed(permission.PermVolumeReadEvents, contextsForVolume(dbVolume)...),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	err = dbVolume.BindApp(bindInfo.App, bindInfo.MountPoint, bindInfo.ReadOnly)
	if err != nil || bindInfo.NoRestart {
		if err == volume.ErrVolumeAlreadyBound {
			return &errors.HTTP{Code: http.StatusConflict, Message: err.Error()}
		}
		return err
	}
	w.Header().Set("Content-Type", "application/x-json-stream")
	keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 30*time.Second, "")
	defer keepAliveWriter.Stop()
	writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
	evt.SetLogWriter(writer)
	return a.Restart("", evt)
}
func volumeBind(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	var bindInfo struct {
		App        string
		MountPoint string
		ReadOnly   bool
		NoRestart  bool
	}
	err := ParseInput(r, &bindInfo)
	if err != nil {
		return err
	}
	dbVolume, err := volume.Load(r.URL.Query().Get(":name"))
	if err != nil {
		if err == volume.ErrVolumeNotFound {
			return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
		}
		return err
	}
	canBindVolume := permission.Check(t, permission.PermVolumeUpdateBind, contextsForVolume(dbVolume)...)
	if !canBindVolume {
		return permission.ErrUnauthorized
	}
	a, err := getAppFromContext(bindInfo.App, r)
	if err != nil {
		return err
	}
	canBindApp := permission.Check(t, permission.PermAppUpdateBindVolume, contextsForApp(&a)...)
	if !canBindApp {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     event.Target{Type: event.TargetTypeVolume, Value: dbVolume.Name},
		Kind:       permission.PermVolumeUpdateBind,
		Owner:      t,
		CustomData: event.FormToCustomData(InputFields(r)),
		Allowed:    event.Allowed(permission.PermVolumeReadEvents, contextsForVolume(dbVolume)...),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	err = dbVolume.BindApp(bindInfo.App, bindInfo.MountPoint, bindInfo.ReadOnly)
	if err != nil || bindInfo.NoRestart {
		if err == volume.ErrVolumeAlreadyBound {
			return &errors.HTTP{Code: http.StatusConflict, Message: err.Error()}
		}
		return err
	}
	w.Header().Set("Content-Type", "application/x-json-stream")
	keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 30*time.Second, "")
	defer keepAliveWriter.Stop()
	writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
	evt.SetLogWriter(writer)
	return a.Restart("", evt)
}
// title: volume bind
// path: /volumes/{name}/bind
// method: POST
// produce: application/json
// responses:
//   200: Volume binded
//   401: Unauthorized
//   404: Volume not found
//   409: Volume bind already exists