Search in sources :

Example 1 with BadParamException

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

the class DefaultDockerClient method listContainers.

@Override
public List<Container> listContainers(final ListContainersParam... params) throws DockerException, InterruptedException {
    WebTarget resource = resource().path("containers").path("json");
    resource = addParameters(resource, params);
    try {
        return request(GET, CONTAINER_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 400:
                throw new BadParamException(getQueryParamMap(resource), e);
            default:
                throw e;
        }
    }
}
Also used : BadParamException(com.spotify.docker.client.exceptions.BadParamException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with BadParamException

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

the class DefaultDockerClient method removeContainer.

@Override
public void removeContainer(final String containerId, final RemoveContainerParam... params) throws DockerException, InterruptedException {
    try {
        WebTarget resource = resource().path("containers").path(containerId);
        for (final RemoveContainerParam param : params) {
            resource = resource.queryParam(param.name(), param.value());
        }
        request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (DockerRequestException e) {
        switch(e.status()) {
            case 400:
                throw new BadParamException(getQueryParamMap(resource()), e);
            case 404:
                throw new ContainerNotFoundException(containerId, e);
            default:
                throw e;
        }
    }
}
Also used : BadParamException(com.spotify.docker.client.exceptions.BadParamException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) WebTarget(javax.ws.rs.client.WebTarget) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException)

Example 3 with BadParamException

use of com.spotify.docker.client.exceptions.BadParamException 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 4 with BadParamException

use of com.spotify.docker.client.exceptions.BadParamException 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)

Aggregations

BadParamException (com.spotify.docker.client.exceptions.BadParamException)4 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)4 WebTarget (javax.ws.rs.client.WebTarget)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ConflictException (com.spotify.docker.client.exceptions.ConflictException)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)1 ExecCreateConflictException (com.spotify.docker.client.exceptions.ExecCreateConflictException)1 ExecStartConflictException (com.spotify.docker.client.exceptions.ExecStartConflictException)1 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 Long.toHexString (java.lang.Long.toHexString)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1 Test (org.junit.Test)1