func handleAcquireByState(r *ranch.Ranch) http.HandlerFunc {
	return func(res http.ResponseWriter, req *http.Request) {
		logrus.WithField("handler", "handleStart").Infof("From %v", req.RemoteAddr)

		if req.Method != http.MethodPost {
			msg := fmt.Sprintf("Method %v, /acquire only accepts POST.", req.Method)
			logrus.Warning(msg)
			http.Error(res, msg, http.StatusMethodNotAllowed)
			return
		}

		// TODO(krzyzacy) - sanitize user input
		state := req.URL.Query().Get("state")
		dest := req.URL.Query().Get("dest")
		owner := req.URL.Query().Get("owner")
		names := req.URL.Query().Get("names")
		if state == "" || dest == "" || owner == "" || names == "" {
			msg := fmt.Sprintf(
				"state: %v, dest: %v, owner: %v, names: %v - all of them must be set in the request.",
				state, dest, owner, names)
			logrus.Warning(msg)
			http.Error(res, msg, http.StatusBadRequest)
			return
		}
		rNames := strings.Split(names, ",")
		logrus.Infof("Request resources %s at state %v from %v, to state %v",
			strings.Join(rNames, ", "), state, owner, dest)

		resources, err := r.AcquireByState(state, dest, owner, rNames)

		if err != nil {
			logrus.WithError(err).Errorf("No available resources")
			http.Error(res, err.Error(), ErrorToStatus(err))
			return
		}

		resBytes := new(bytes.Buffer)

		if err := json.NewEncoder(resBytes).Encode(resources); err != nil {
			logrus.WithError(err).Errorf("json.Marshal failed: %v, resources will be released", resources)
			http.Error(res, err.Error(), ErrorToStatus(err))
			for _, resource := range resources {
				err := r.Release(resource.Name, state, owner)
				if err != nil {
					logrus.WithError(err).Warningf("unable to release resource %s", resource.Name)
				}
			}
			return
		}
		logrus.Infof("Resource leased: %v", resBytes.String())
		fmt.Fprint(res, resBytes.String())
	}
}
func handleAcquireByState(r *ranch.Ranch) http.HandlerFunc {
	return func(res http.ResponseWriter, req *http.Request) {
		logrus.WithField("handler", "handleStart").Infof("From %v", req.RemoteAddr)

		if req.Method != http.MethodPost {
			msg := fmt.Sprintf("Method %v, /acquire only accepts POST.", req.Method)
			logrus.Warning(msg)
			http.Error(res, msg, http.StatusMethodNotAllowed)
			return
		}

		// TODO(krzyzacy) - sanitize user input
		state := req.URL.Query().Get("state")
		dest := req.URL.Query().Get("dest")
		owner := req.URL.Query().Get("owner")
		names := req.URL.Query().Get("names")
		if state == "" || dest == "" || owner == "" || names == "" {
			msg := fmt.Sprintf(
				"state: %v, dest: %v, owner: %v, names: %v - all of them must be set in the request.",
				state, dest, owner, names)
			logrus.Warning(msg)
			http.Error(res, msg, http.StatusBadRequest)
			return
		}
		rNames := strings.Split(names, ",")
		logrus.Infof("Request resources %s at state %v from %v, to state %v",
			strings.Join(rNames, ", "), state, owner, dest)

		resources, err := r.AcquireByState(state, dest, owner, rNames)

		if err != nil {
			logrus.WithError(err).Errorf("No available resources")
			http.Error(res, err.Error(), ErrorToStatus(err))
			return
		}

		resBytes := new(bytes.Buffer)

		if err := json.NewEncoder(resBytes).Encode(resources); err != nil {
			logrus.WithError(err).Errorf("json.Marshal failed: %v, resources will be released", resources)
			http.Error(res, err.Error(), ErrorToStatus(err))
			for _, resource := range resources {
				err := r.Release(resource.Name, state, owner)
				if err != nil {
					logrus.WithError(err).Warningf("unable to release resource %s", resource.Name)
				}
			}
			return
		}
		logrus.Infof("Resource leased: %v", resBytes.String())
		fmt.Fprint(res, resBytes.String())
	}
}
//  handleAcquireByState: Handler for /acquirebystate
//  Method: POST
// 	URLParams:
//		Required: state=[string] : current state of the requested resource
//		Required: dest=[string]  : destination state of the requested resource
//		Required: owner=[string] : requester of the resource
//		Required: names=[string] : expected resources names