Esempio n. 1
0
// PUT /:director_id/backends
bool ApiRequest::createBackend(Director* director)
{
	if (!director) {
		request_->status = x0::HttpStatus::NotFound;
		request_->finish();
		return true;
	}

	std::string name;
	if (!loadParam("name", name))
		return false;

	if (name.empty())
		return badRequest("Failed parsing attribute 'name'. value is empty.");

	BackendRole role = BackendRole::Active;
	if (!loadParam("role", role))
		return false;

	bool enabled = false;
	if (!loadParam("enabled", enabled))
		return false;

	size_t capacity = 0;
	if (!loadParam("capacity", capacity))
		return false;

	bool terminateProtection = false;
	if (hasParam("terminate-protection"))
		if (!loadParam("terminate-protection", terminateProtection))
			return false;

	std::string protocol;
	if (!loadParam("protocol", protocol))
		return false;

	if (protocol != "fastcgi" && protocol != "http")
		return false;

	SocketSpec socketSpec;
	std::string path;
	if (loadParam("path", path)) {
		socketSpec = SocketSpec::fromLocal(path);
	} else {
		std::string hostname;
		if (!loadParam("hostname", hostname))
			return false;

		int port;
		if (!loadParam("port", port))
			return false;

		socketSpec = SocketSpec::fromInet(IPAddress(hostname), port);
	}

	TimeSpan hcInterval;
	if (!loadParam("health-check-interval", hcInterval))
		return false;

	HealthMonitor::Mode hcMode;
	if (!loadParam("health-check-mode", hcMode))
		return false;

	if (!director->isMutable()) {
		request_->log(Severity::error, "director: Could not create backend '%s' at director '%s'. Director immutable.",
			name.c_str(), director->name().c_str());

		request_->status = x0::HttpStatus::Forbidden;
		request_->finish();
		return true;
	}

	Backend* backend = director->createBackend(name, protocol, socketSpec, capacity, role);
	if (!backend)
		return badRequest("Creating backend failed.");

	backend->setTerminateProtection(terminateProtection);
	backend->setEnabled(enabled);
	backend->healthMonitor()->setInterval(hcInterval);
	backend->healthMonitor()->setMode(hcMode);
	director->save();
	request_->status = x0::HttpStatus::Created;
	request_->log(Severity::info, "director: %s created backend: %s.", director->name().c_str(), backend->name().c_str());
	request_->finish();

	return true;
}