Search in sources :

Example 1 with ConflictException

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

the class DefaultDockerClientTest method testSecretOperations.

@Test
public void testSecretOperations() throws Exception {
    requireDockerApiVersionAtLeast("1.25", "secret support");
    for (final Secret secret : sut.listSecrets()) {
        sut.deleteSecret(secret.id());
    }
    assertThat(sut.listSecrets().size(), equalTo(0));
    final String secretData = Base64.encodeAsString("testdata".getBytes());
    final Map<String, String> labels = ImmutableMap.of("foo", "bar", "1", "a");
    final SecretSpec secretSpec = SecretSpec.builder().name("asecret").data(secretData).labels(labels).build();
    final SecretCreateResponse response = sut.createSecret(secretSpec);
    final String secretId = response.id();
    assertThat(secretId, is(notNullValue()));
    final SecretSpec secretSpecConflict = SecretSpec.builder().name("asecret").data(secretData).labels(labels).build();
    try {
        sut.createSecret(secretSpecConflict);
        fail("Should fail due to secret name conflict");
    } catch (ConflictException | DockerRequestException ex) {
    // Ignored; Docker should return status code 409 but doesn't in some earlier versions.
    // Recent versions return 409 which translates into ConflictException.
    }
    final SecretSpec secretSpecInvalidData = SecretSpec.builder().name("asecret2").data("plainData").labels(labels).build();
    try {
        sut.createSecret(secretSpecInvalidData);
        fail("Should fail due to non base64 data");
    } catch (DockerException ex) {
    // Ignored
    }
    final Secret secret = sut.inspectSecret(secretId);
    final List<Secret> secrets = sut.listSecrets();
    assertThat(secrets.size(), equalTo(1));
    assertThat(secrets, hasItem(secret));
    sut.deleteSecret(secretId);
    assertThat(sut.listSecrets().size(), equalTo(0));
    try {
        sut.inspectSecret(secretId);
        fail("Should fail because of non-existant secret ID");
    } catch (NotFoundException ex) {
    // Ignored
    }
    try {
        sut.deleteSecret(secretId);
        fail("Should fail because of non-existant secret ID");
    } catch (DockerRequestException | NotFoundException ex) {
    // Ignored; Docker should return status code 404,
    // but it doesn't for some reason in versions < 17.04.
    // So we catch 2 different exceptions here.
    }
}
Also used : Secret(com.spotify.docker.client.messages.swarm.Secret) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerRenameConflictException(com.spotify.docker.client.exceptions.ContainerRenameConflictException) ConflictException(com.spotify.docker.client.exceptions.ConflictException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) SecretSpec(com.spotify.docker.client.messages.swarm.SecretSpec) SecretCreateResponse(com.spotify.docker.client.messages.swarm.SecretCreateResponse) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with ConflictException

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

the class DefaultDockerClient method createSecret.

@Override
public SecretCreateResponse createSecret(final SecretSpec secret) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.25");
    final WebTarget resource = resource().path("secrets").path("create");
    try {
        return request(POST, SecretCreateResponse.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(secret));
    } catch (final DockerRequestException ex) {
        switch(ex.status()) {
            case 406:
                throw new NonSwarmNodeException("Server not part of swarm.", ex);
            case 409:
                throw new ConflictException("Name conflicts with an existing object.", ex);
            default:
                throw ex;
        }
    }
}
Also used : 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) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 3 with ConflictException

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

the class DefaultDockerClient method createConfig.

@Override
public ConfigCreateResponse createConfig(final ConfigSpec config) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.30");
    final WebTarget resource = resource().path("configs").path("create");
    try {
        return request(POST, ConfigCreateResponse.class, resource, resource.request(APPLICATION_JSON_TYPE), Entity.json(config));
    } catch (final DockerRequestException ex) {
        switch(ex.status()) {
            case 503:
                throw new NonSwarmNodeException("Server not part of swarm.", ex);
            case 409:
                throw new ConflictException("Name conflicts with an existing object.", ex);
            default:
                throw ex;
        }
    }
}
Also used : 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) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 4 with ConflictException

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

Aggregations

ConflictException (com.spotify.docker.client.exceptions.ConflictException)4 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)4 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)4 ExecCreateConflictException (com.spotify.docker.client.exceptions.ExecCreateConflictException)3 ExecStartConflictException (com.spotify.docker.client.exceptions.ExecStartConflictException)3 WebTarget (javax.ws.rs.client.WebTarget)3 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)2 NonSwarmNodeException (com.spotify.docker.client.exceptions.NonSwarmNodeException)2 BadParamException (com.spotify.docker.client.exceptions.BadParamException)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 DockerException (com.spotify.docker.client.exceptions.DockerException)1 NetworkNotFoundException (com.spotify.docker.client.exceptions.NetworkNotFoundException)1 NotFoundException (com.spotify.docker.client.exceptions.NotFoundException)1 VolumeNotFoundException (com.spotify.docker.client.exceptions.VolumeNotFoundException)1 Secret (com.spotify.docker.client.messages.swarm.Secret)1 SecretCreateResponse (com.spotify.docker.client.messages.swarm.SecretCreateResponse)1 SecretSpec (com.spotify.docker.client.messages.swarm.SecretSpec)1 Long.toHexString (java.lang.Long.toHexString)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1