com.ecwid.consul.v1.OperationException Java Examples

The following examples show how to use com.ecwid.consul.v1.OperationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsulInboundMessageProducerTests.java    From spring-cloud-consul with Apache License 2.0 6 votes vote down vote up
@Test
public void getEventsShouldNotThrowException() {
	EventService eventService = mock(EventService.class);
	when(eventService.watch()).thenThrow(new OperationException(500, "error", ""));

	ConsulInboundMessageProducer producer = new ConsulInboundMessageProducer(
			eventService);

	try {
		producer.getEvents();
	}
	catch (Exception e) {
		fail("ConsulInboundMessageProducer threw unexpected exception: " + e);
	}

}
 
Example #2
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 6 votes vote down vote up
@Override
public Response<Session> renewSession(String session, QueryParams queryParams, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
	HttpResponse httpResponse = rawClient.makePutRequest("/v1/session/renew/" + session, "", queryParams, tokenParam);

	if (httpResponse.getStatusCode() == 200) {
		List<Session> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Session>>() {
		}.getType());

		if (value.size() == 1) {
			return new Response<Session>(value.get(0), httpResponse);
		} else {
			throw new ConsulException("Strange response (list size=" + value.size() + ")");
		}
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #3
Source File: AclConsulClient.java    From consul-api with Apache License 2.0 6 votes vote down vote up
@Override
public Response<Acl> getAcl(String id) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/acl/info/" + id);

	if (httpResponse.getStatusCode() == 200) {
		List<Acl> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Acl>>() {
		}.getType());

		if (value.isEmpty()) {
			return new Response<Acl>(null, httpResponse);
		} else if (value.size() == 1) {
			return new Response<Acl>(value.get(0), httpResponse);
		} else {
			throw new ConsulException("Strange response (list size=" + value.size() + ")");
		}
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #4
Source File: SessionConsulClient.java    From consul-api with Apache License 2.0 6 votes vote down vote up
@Override
public Response<Session> getSessionInfo(String session, QueryParams queryParams, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/session/info/" + session, queryParams, tokenParam);

	if (httpResponse.getStatusCode() == 200) {
		List<Session> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Session>>() {
		}.getType());

		if (value == null || value.isEmpty()) {
			return new Response<Session>(null, httpResponse);
		} else if (value.size() == 1) {
			return new Response<Session>(value.get(0), httpResponse);
		} else {
			throw new ConsulException("Strange response (list size=" + value.size() + ")");
		}
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #5
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentSetMaintenance(boolean maintenanceEnabled, String reason) {
	UrlParameters maintenanceParameter = new SingleUrlParameters("enable", Boolean.toString(maintenanceEnabled));
	UrlParameters reasonParamenter = reason != null ? new SingleUrlParameters("reason", reason) : null;

	HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/maintenance", "", maintenanceParameter, reasonParamenter);

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}

}
 
Example #6
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Member>> getAgentMembers() {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/agent/members");

	if (httpResponse.getStatusCode() == 200) {
		List<Member> members = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Member>>() {
		}.getType());
		return new Response<List<Member>>(members, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #7
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentJoin(String address, boolean wan) {
	UrlParameters wanParams = wan ? new SingleUrlParameters("wan", "1") : null;
	HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/join/" + address, "", wanParams);

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #8
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentForceLeave(String node) {
	HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/force-leave/" + node, "");

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #9
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentCheckRegister(NewCheck newCheck, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

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

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #10
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentCheckDeregister(String checkId, String token) {
	UrlParameters tokenParameter = token != null ? new SingleUrlParameters("token", token) : null;

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

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #11
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> 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;

	HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/check/pass/" + checkId, "", noteParameter, tokenParameter);
   
	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #12
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> 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;

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

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #13
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> 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;

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

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #14
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentServiceRegister(NewService newService, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

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

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #15
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentServiceDeregister(String serviceId, String token) {
	UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;

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

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #16
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentServiceSetMaintenance(String serviceId, boolean maintenanceEnabled, String reason) {
	UrlParameters maintenanceParameter = new SingleUrlParameters("enable", Boolean.toString(maintenanceEnabled));
	UrlParameters reasonParameter = reason != null ? new SingleUrlParameters("reason", reason) : null;

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

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #17
Source File: AgentConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Void> agentReload() {
	HttpResponse httpResponse = rawClient.makePutRequest("/v1/agent/reload", "");

	if (httpResponse.getStatusCode() == 200) {
		return new Response<Void>(null, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}

}
 
Example #18
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksForNode(String nodeName, QueryParams queryParams) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/node/" + nodeName, queryParams);

	if (httpResponse.getStatusCode() == 200) {
		List<Check> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Check>>() {
		}.getType());
		return new Response<List<Check>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #19
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksForService(String serviceName, HealthChecksForServiceRequest healthChecksForServiceRequest) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/checks/" + serviceName, healthChecksForServiceRequest.asUrlParameters());

	if (httpResponse.getStatusCode() == 200) {
		List<Check> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Check>>() {
		}.getType());
		return new Response<List<Check>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #20
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<HealthService>> getHealthServices(String serviceName, HealthServicesRequest healthServicesRequest) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/service/" + serviceName, healthServicesRequest.asUrlParameters());

	if (httpResponse.getStatusCode() == 200) {
		List<com.ecwid.consul.v1.health.model.HealthService> value = GsonFactory.getGson().fromJson(httpResponse.getContent(),
				new TypeToken<List<com.ecwid.consul.v1.health.model.HealthService>>() {
				}.getType());
		return new Response<List<com.ecwid.consul.v1.health.model.HealthService>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #21
Source File: HealthConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Check>> getHealthChecksState(Check.CheckStatus checkStatus, QueryParams queryParams) {
	String status = checkStatus == null ? "any" : checkStatus.name().toLowerCase();
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/health/state/" + status, queryParams);

	if (httpResponse.getStatusCode() == 200) {
		List<Check> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Check>>() {
		}.getType());
		return new Response<List<Check>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #22
Source File: EventConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<Event> eventFire(String event, String payload, EventParams eventParams, QueryParams queryParams) {
	HttpResponse httpResponse = rawClient.makePutRequest("/v1/event/fire/" + event, payload, eventParams, queryParams);

	if (httpResponse.getStatusCode() == 200) {
		Event value = GsonFactory.getGson().fromJson(httpResponse.getContent(), Event.class);
		return new Response<Event>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #23
Source File: EventConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Event>> eventList(EventListRequest eventListRequest) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/event/list", eventListRequest.asUrlParameters());

	if (httpResponse.getStatusCode() == 200) {
		List<Event> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Event>>() {
		}.getType());
		return new Response<List<Event>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #24
Source File: CoordinateConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Datacenter>> getDatacenters() {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/coordinate/datacenters");

	if (httpResponse.getStatusCode() == 200) {
		List<Datacenter> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Datacenter>>() {
		}.getType());
		return new Response<List<Datacenter>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #25
Source File: CoordinateConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<List<Node>> getNodes(QueryParams queryParams) {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/coordinate/nodes", queryParams);

	if (httpResponse.getStatusCode() == 200) {
		List<Node> value = GsonFactory.getGson().fromJson(httpResponse.getContent(), new TypeToken<List<Node>>() {
		}.getType());
		return new Response<List<Node>>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #26
Source File: StatusConsulClient.java    From consul-api with Apache License 2.0 5 votes vote down vote up
@Override
public Response<String> getStatusLeader() {
	HttpResponse httpResponse = rawClient.makeGetRequest("/v1/status/leader");

	if (httpResponse.getStatusCode() == 200) {
		String value = GsonFactory.getGson().fromJson(httpResponse.getContent(), String.class);
		return new Response<String>(value, httpResponse);
	} else {
		throw new OperationException(httpResponse);
	}
}
 
Example #27
Source File: ConfigWatchTests.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
private void setupWatchThrowException(ApplicationEventPublisher eventPublisher, String context) {
    ConsulClient consul = mock(ConsulClient.class);
    OperationException operationException = new OperationException(403, null, null);
    when(consul.getKVValues(ArgumentMatchers.eq(context), nullable(String.class),
            ArgumentMatchers.any(QueryParams.class))).thenThrow(operationException);

    LinkedHashMap<String, Long> initialIndexes = new LinkedHashMap<>();
    initialIndexes.put(context, 0L);
    startWatch(eventPublisher, consul, initialIndexes);
}
 
Example #28
Source File: ConsulRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isAvailable() {
	try {
		Response<List<Member>> servicesMap = consulClient.getAgentMembers();
		return true;
	} catch (OperationException e) {
		logger.info("Consul agent connect failed");
		return false;
	}
}
 
Example #29
Source File: ConsulRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void doNotify(URL url, Collection<NotifyListener> listeners) {
	List<URL> result = new ArrayList<URL>();
	String consumerService = url.getServiceInterface();
	try {
		Response<List<HealthService>> response = this.consulClient.getHealthServices(consumerService, true, QueryParams.DEFAULT);
		List<HealthService> healthServices = (List<HealthService>) response.getValue();
		Iterator<HealthService> iterator = healthServices.iterator();
		while (iterator.hasNext()) {
			HealthService healthService = (HealthService) iterator.next();
			HealthService.Service service = healthService.getService();
			List<URL> urls = new ArrayList<URL>();
			String serviceURL = URL.decode(service.getAddress());
			URL u = URL.valueOf(serviceURL);
			if (UrlUtils.isMatch(url, u)) {
				urls.add(u);
			}
			result.addAll(urls);
			if (logger.isInfoEnabled()) {
				logger.info("Consul notify:  = " + urls);
			}
		}
		if (result == null || result.size() == 0) {
			return;
		}
		for (NotifyListener listener : listeners) {
			notify(url, listener, result);
		}
	} catch (OperationException e) {
		throw e;
	}
}
 
Example #30
Source File: ConsulRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void keepAlive(URL url) {
	try {
		String checkId = getCheckId(toCategoryPath(url));
		this.consulClient.agentCheckPass(checkId);
		logger.info("keep alive success, serviceId: " + checkId);
	} catch (OperationException e) {
		throw e;
	}
}