Search in sources :

Example 41 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method updateNode.

@Override
public void updateNode(final String nodeId, final Long version, final NodeSpec nodeSpec) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.24");
    WebTarget resource = resource().path("nodes").path(nodeId).path("update").queryParam("version", version);
    try {
        request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(nodeSpec));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new NodeNotFoundException(nodeId);
            case 503:
                throw new NonSwarmNodeException("Node " + nodeId + " is not a swarm node", e);
            default:
                throw e;
        }
    }
}
Also used : NodeNotFoundException(com.spotify.docker.client.exceptions.NodeNotFoundException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 42 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method tag.

@Override
public void tag(final String image, final String name, final boolean force) throws DockerException, InterruptedException {
    final ImageRef imageRef = new ImageRef(name);
    WebTarget resource = resource().path("images").path(image).path("tag");
    resource = resource.queryParam("repo", imageRef.getImage());
    if (imageRef.getTag() != null) {
        resource = resource.queryParam("tag", imageRef.getTag());
    }
    if (force) {
        resource = resource.queryParam("force", true);
    }
    try {
        request(POST, resource, resource.request());
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 400:
                throw new BadParamException(getQueryParamMap(resource), e);
            case 404:
                throw new ImageNotFoundException(image, e);
            case 409:
                throw new ConflictException(e);
            default:
                throw e;
        }
    }
}
Also used : BadParamException(com.spotify.docker.client.exceptions.BadParamException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) ExecStartConflictException(com.spotify.docker.client.exceptions.ExecStartConflictException) ExecCreateConflictException(com.spotify.docker.client.exceptions.ExecCreateConflictException) WebTarget(javax.ws.rs.client.WebTarget) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Example 43 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClient method removeNetwork.

@Override
public void removeNetwork(String networkId) throws DockerException, InterruptedException {
    try {
        final WebTarget resource = resource().path("networks").path(networkId);
        request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 404:
                throw new NetworkNotFoundException(networkId, e);
            default:
                throw e;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException)

Example 44 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project docker-client by spotify.

the class DefaultDockerClientTest method testResizeTty.

@Test
public void testResizeTty() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("/bin/sh", "-c", "while :; do sleep 1; done").build();
    final ContainerCreation creation = sut.createContainer(config, randomName());
    final String id = creation.id();
    try {
        sut.resizeTty(id, 100, 0);
        fail("Should get an exception resizing TTY with width=0");
    } catch (BadParamException e) {
        final Map<String, String> params = e.getParams();
        assertThat(params, hasKey("w"));
        assertEquals("0", params.get("w"));
    }
    try {
        sut.resizeTty(id, 100, 80);
        fail("Should get an exception resizing TTY for non-running container");
    } catch (DockerRequestException e) {
        if (dockerApiVersionLessThan("1.20")) {
            assertEquals(String.format("Cannot resize container %s, container is not running\n", id), e.getResponseBody());
        } else if (dockerApiVersionLessThan("1.24")) {
            assertEquals(String.format("Container %s is not running\n", id), e.getResponseBody());
        } else {
            final ObjectMapper mapper = ObjectMapperProvider.objectMapper();
            final Map<String, String> jsonMessage = mapper.readValue(e.getResponseBody(), new TypeReference<Map<String, String>>() {
            });
            assertThat(jsonMessage, hasKey("message"));
            assertEquals(String.format("Container %s is not running", id), jsonMessage.get("message"));
        }
    }
    sut.startContainer(id);
    sut.resizeTty(id, 100, 80);
// We didn't get an exception, so everything went fine
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) BadParamException(com.spotify.docker.client.exceptions.BadParamException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 45 with DockerRequestException

use of com.spotify.docker.client.exceptions.DockerRequestException in project helios by spotify.

the class DockerDaemonHealthCheckerTest method testUnhealthy.

@Test
public void testUnhealthy() throws Exception {
    when(dockerClient.ping()).thenThrow(new DockerRequestException("GET", new URI("/ping"), 500, null, null));
    final HealthCheck.Result result = sut.check();
    assertThat(result.isHealthy(), is(false));
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) HealthCheck(com.codahale.metrics.health.HealthCheck) URI(java.net.URI) Test(org.junit.Test)

Aggregations

DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)45 WebTarget (javax.ws.rs.client.WebTarget)37 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)18 DockerException (com.spotify.docker.client.exceptions.DockerException)14 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)12 NonSwarmNodeException (com.spotify.docker.client.exceptions.NonSwarmNodeException)11 NetworkNotFoundException (com.spotify.docker.client.exceptions.NetworkNotFoundException)9 NodeNotFoundException (com.spotify.docker.client.exceptions.NodeNotFoundException)9 ExecNotFoundException (com.spotify.docker.client.exceptions.ExecNotFoundException)8 NotFoundException (com.spotify.docker.client.exceptions.NotFoundException)8 ServiceNotFoundException (com.spotify.docker.client.exceptions.ServiceNotFoundException)8 VolumeNotFoundException (com.spotify.docker.client.exceptions.VolumeNotFoundException)8 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)7 TaskNotFoundException (com.spotify.docker.client.exceptions.TaskNotFoundException)6 Test (org.junit.Test)6 BadParamException (com.spotify.docker.client.exceptions.BadParamException)5 ConflictException (com.spotify.docker.client.exceptions.ConflictException)5 IOException (java.io.IOException)5 ExecCreateConflictException (com.spotify.docker.client.exceptions.ExecCreateConflictException)4 ExecStartConflictException (com.spotify.docker.client.exceptions.ExecStartConflictException)4