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

com.ecwid.consul.v1.acl.AclConsulClient Maven / Gradle / Ivy

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

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

import com.ecwid.consul.ConsulException;
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.acl.model.Acl;
import com.ecwid.consul.v1.acl.model.NewAcl;
import com.ecwid.consul.v1.acl.model.UpdateAcl;
import com.google.gson.reflect.TypeToken;

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

	private final ConsulRawClient rawClient;

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

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

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

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

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

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

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

	@Override
	public Response aclCreate(NewAcl newAcl, String token) {
		UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
		String json = GsonFactory.getGson().toJson(newAcl);
		RawResponse rawResponse = rawClient.makePutRequest("/v1/acl/create", json, tokenParams);

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

	@Override
	public Response aclUpdate(UpdateAcl updateAcl, String token) {
		UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
		String json = GsonFactory.getGson().toJson(updateAcl);
		RawResponse rawResponse = rawClient.makePutRequest("/v1/acl/update", json, tokenParams);

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

	@Override
	public Response aclDestroy(String aclId, String token) {
		UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
		RawResponse rawResponse = rawClient.makePutRequest("/v1/acl/destroy/" + aclId, "", tokenParams);

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

	@Override
	public Response getAcl(String id) {
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/acl/info/" + id);

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

			if (value.isEmpty()) {
				return new Response(null, rawResponse);
			} else if (value.size() == 1) {
				return new Response(value.get(0), rawResponse);
			} else {
				throw new ConsulException("Strange response (list size=" + value.size() + ")");
			}
		} else {
			throw new OperationException(rawResponse);
		}
	}

	@Override
	public Response aclClone(String aclId, String token) {
		UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
		RawResponse rawResponse = rawClient.makePutRequest("/v1/acl/clone/" + aclId, "", tokenParams);

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

	@Override
	public Response> getAclList(String token) {
		UrlParameters tokenParams = token != null ? new SingleUrlParameters("token", token) : null;
		RawResponse rawResponse = rawClient.makeGetRequest("/v1/acl/list", tokenParams);

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy