All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ecwid.consul.v1.agent.AgentConsulClient Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
package com.ecwid.consul.v1.agent;

import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.json.GsonFactory;
import com.ecwid.consul.transport.RawResponse;
import com.ecwid.consul.transport.TLSConfig;
import com.ecwid.consul.v1.ConsulRawClient;
import com.ecwid.consul.v1.OperationException;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.*;
import com.google.gson.reflect.TypeToken;

import java.util.List;
import java.util.Map;

/**
 * @author Vasily Vasilkov ([email protected])
 */
public final class AgentConsulClient implements AgentClient {

	private final ConsulRawClient rawClient;

	public AgentConsulClient(ConsulRawClient rawClient) {
		this.rawClient = rawClient;
	}

	public AgentConsulClient() {
		this(new ConsulRawClient());
	}

	public AgentConsulClient(TLSConfig tlsConfig) {
		this(new ConsulRawClient(tlsConfig));
	}

	public AgentConsulClient(String agentHost) {
		this(new ConsulRawClient(agentHost));
	}

	public AgentConsulClient(String agentHost, TLSConfig tlsConfig) {
		this(new ConsulRawClient(agentHost, tlsConfig));
	}

	public AgentConsulClient(String agentHost, int agentPort) {
		this(new ConsulRawClient(agentHost, agentPort));
	}

	public AgentConsulClient(String agentHost, int agentPort, TLSConfig tlsConfig) {
		this(new ConsulRawClient(agentHost, agentPort, tlsConfig));
	}

	@Override
	public Response> getAgentChecks() {
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/agent/checks");

		if (rawResponse.getStatusCode() == 200) {
			Map value = GsonFactory.getGson().fromJson(rawResponse.getContent(), new TypeToken>() {
			}.getType());
			return new Response>(value, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response> getAgentServices() {
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/agent/services");

		if (rawResponse.getStatusCode() == 200) {
			Map agentServices = GsonFactory.getGson().fromJson(rawResponse.getContent(),
					new TypeToken>() {
					}.getType());
			return new Response>(agentServices, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response> getAgentMembers() {
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/agent/members");

		if (rawResponse.getStatusCode() == 200) {
			List members = GsonFactory.getGson().fromJson(rawResponse.getContent(), new TypeToken>() {
			}.getType());
			return new Response>(members, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response getAgentSelf() {
		return getAgentSelf(null);
	}

	@Override
	public Response getAgentSelf(String token) {
		UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

		RawResponse rawResponse = rawClient.makeGetRequest("/v1/agent/self", tokenParam);

		if (rawResponse.getStatusCode() == 200) {
			Self self = GsonFactory.getGson().fromJson(rawResponse.getContent(), Self.class);
			return new Response(self, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentSetMaintenance(boolean maintenanceEnabled) {
		return agentSetMaintenance(maintenanceEnabled, null);
	}

	@Override
	public Response agentSetMaintenance(boolean maintenanceEnabled, String reason) {
		UrlParameters maintenanceParameter = new SingleUrlParameters("enable", Boolean.toString(maintenanceEnabled));
		UrlParameters reasonParamenter = reason != null ? new SingleUrlParameters("reason", reason) : null;

		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/maintenance", "", maintenanceParameter, reasonParamenter);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}

	}

	@Override
	public Response agentJoin(String address, boolean wan) {
		UrlParameters wanParams = wan ? new SingleUrlParameters("wan", "1") : null;
		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/join/" + address, "", wanParams);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentForceLeave(String node) {
		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/force-leave/" + node, "");

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentCheckRegister(NewCheck newCheck) {
		return agentCheckRegister(newCheck, null);
	}

	@Override
	public Response agentCheckRegister(NewCheck newCheck, String token) {
		UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

		String json = GsonFactory.getGson().toJson(newCheck);
		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/check/register", json, tokenParam);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentCheckDeregister(String checkId) {
		return agentCheckDeregister(checkId, null);
	}

	@Override
	public Response agentCheckDeregister(String checkId, String token) {
		UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/check/deregister/" + checkId, "", tokenParameter);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentCheckPass(String checkId) {
		return agentCheckPass(checkId, null);
	}

	@Override
	public Response agentCheckPass(String checkId, String note) {
		return agentCheckPass(checkId, note, null);
	}

	@Override
	public Response agentCheckPass(String checkId, String note, String token) {
		UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
		UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/check/pass/" + checkId, "", noteParameter, tokenParameter);
    
		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentCheckWarn(String checkId) {
		return agentCheckWarn(checkId, null);
	}

	@Override
	public Response agentCheckWarn(String checkId, String note) {
		return agentCheckWarn(checkId, note, null);
	}

	@Override
	public Response agentCheckWarn(String checkId, String note, String token) {
		UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
		UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/check/warn/" + checkId, "", noteParameter, tokenParameter);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentCheckFail(String checkId) {
		return agentCheckFail(checkId, null);
	}

	@Override
	public Response agentCheckFail(String checkId, String note) {
		return agentCheckFail(checkId, note, null);
	}

	@Override
	public Response agentCheckFail(String checkId, String note, String token) {
		UrlParameters noteParameter = note != null ? new SingleUrlParameters("note", note) : null;
		UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/check/fail/" + checkId, "", noteParameter, tokenParameter);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentServiceRegister(NewService newService) {
		return agentServiceRegister(newService, null);
	}

	@Override
	public Response agentServiceRegister(NewService newService, String token) {
		UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

		String json = GsonFactory.getGson().toJson(newService);
		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/service/register", json, tokenParam);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentServiceDeregister(String serviceId) {
		return agentServiceDeregister(serviceId, null);
	}

	@Override
	public Response agentServiceDeregister(String serviceId, String token) {
		UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/service/deregister/" + serviceId, "", tokenParam);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentServiceSetMaintenance(String serviceId, boolean maintenanceEnabled) {
		return agentServiceSetMaintenance(serviceId, maintenanceEnabled, null);
	}

	@Override
	public Response agentServiceSetMaintenance(String serviceId, boolean maintenanceEnabled, String reason) {
		UrlParameters maintenanceParameter = new SingleUrlParameters("enable", Boolean.toString(maintenanceEnabled));
		UrlParameters reasonParameter = reason != null ? new SingleUrlParameters("reason", reason) : null;

		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/service/maintenance/" + serviceId, "", maintenanceParameter, reasonParameter);

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response agentReload() {
		RawResponse rawResponse = rawClient.makePutRequest("/v1/agent/reload", "");

		if (rawResponse.getStatusCode() == 200) {
			return new Response(null, rawResponse);
		} else {
			throw new OperationException(rawResponse);
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy