Search in sources :

Example 1 with NotFoundException

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

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

the class DefaultDockerClient method inspectConfig.

@Override
public Config inspectConfig(final String configId) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.30");
    final WebTarget resource = resource().path("configs").path(configId);
    try {
        return request(GET, Config.class, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (final DockerRequestException ex) {
        switch(ex.status()) {
            case 404:
                throw new NotFoundException("Config " + configId + " not found.", ex);
            case 503:
                throw new NonSwarmNodeException("Config not part of swarm.", ex);
            default:
                throw ex;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) TaskNotFoundException(com.spotify.docker.client.exceptions.TaskNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) NodeNotFoundException(com.spotify.docker.client.exceptions.NodeNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) ServiceNotFoundException(com.spotify.docker.client.exceptions.ServiceNotFoundException) ExecNotFoundException(com.spotify.docker.client.exceptions.ExecNotFoundException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 3 with NotFoundException

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

the class DefaultDockerClient method deleteConfig.

@Override
public void deleteConfig(final String configId) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.30");
    final WebTarget resource = resource().path("configs").path(configId);
    try {
        request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (final DockerRequestException ex) {
        switch(ex.status()) {
            case 404:
                throw new NotFoundException("Config " + configId + " not found.", ex);
            case 503:
                throw new NonSwarmNodeException("Config not part of a swarm.", ex);
            default:
                throw ex;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) TaskNotFoundException(com.spotify.docker.client.exceptions.TaskNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) NodeNotFoundException(com.spotify.docker.client.exceptions.NodeNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) ServiceNotFoundException(com.spotify.docker.client.exceptions.ServiceNotFoundException) ExecNotFoundException(com.spotify.docker.client.exceptions.ExecNotFoundException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 4 with NotFoundException

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

the class DefaultDockerClient method updateConfig.

@Override
public void updateConfig(final String configId, final Long version, final ConfigSpec nodeSpec) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.30");
    final WebTarget resource = resource().path("configs").path(configId).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 NotFoundException("Config " + configId + " not found.");
            case 503:
                throw new NonSwarmNodeException("Config not part of a swarm.", e);
            default:
                throw e;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) TaskNotFoundException(com.spotify.docker.client.exceptions.TaskNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) NodeNotFoundException(com.spotify.docker.client.exceptions.NodeNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) ServiceNotFoundException(com.spotify.docker.client.exceptions.ServiceNotFoundException) ExecNotFoundException(com.spotify.docker.client.exceptions.ExecNotFoundException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Example 5 with NotFoundException

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

the class DefaultDockerClient method inspectSecret.

@Override
public Secret inspectSecret(final String secretId) throws DockerException, InterruptedException {
    assertApiVersionIsAbove("1.25");
    final WebTarget resource = resource().path("secrets").path(secretId);
    try {
        return request(GET, Secret.class, resource, resource.request(APPLICATION_JSON_TYPE));
    } catch (final DockerRequestException ex) {
        switch(ex.status()) {
            case 404:
                throw new NotFoundException("Secret " + secretId + " not found.", ex);
            case 406:
                throw new NonSwarmNodeException("Server not part of swarm.", ex);
            default:
                throw ex;
        }
    }
}
Also used : DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) TaskNotFoundException(com.spotify.docker.client.exceptions.TaskNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) NotFoundException(com.spotify.docker.client.exceptions.NotFoundException) NodeNotFoundException(com.spotify.docker.client.exceptions.NodeNotFoundException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) ServiceNotFoundException(com.spotify.docker.client.exceptions.ServiceNotFoundException) ExecNotFoundException(com.spotify.docker.client.exceptions.ExecNotFoundException) VolumeNotFoundException(com.spotify.docker.client.exceptions.VolumeNotFoundException) NetworkNotFoundException(com.spotify.docker.client.exceptions.NetworkNotFoundException) WebTarget(javax.ws.rs.client.WebTarget) NonSwarmNodeException(com.spotify.docker.client.exceptions.NonSwarmNodeException)

Aggregations

ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)7 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)7 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)7 NetworkNotFoundException (com.spotify.docker.client.exceptions.NetworkNotFoundException)7 NotFoundException (com.spotify.docker.client.exceptions.NotFoundException)7 VolumeNotFoundException (com.spotify.docker.client.exceptions.VolumeNotFoundException)7 ExecNotFoundException (com.spotify.docker.client.exceptions.ExecNotFoundException)6 NodeNotFoundException (com.spotify.docker.client.exceptions.NodeNotFoundException)6 ServiceNotFoundException (com.spotify.docker.client.exceptions.ServiceNotFoundException)6 TaskNotFoundException (com.spotify.docker.client.exceptions.TaskNotFoundException)6 WebTarget (javax.ws.rs.client.WebTarget)6 NonSwarmNodeException (com.spotify.docker.client.exceptions.NonSwarmNodeException)4 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 ConflictException (com.spotify.docker.client.exceptions.ConflictException)1 ContainerRenameConflictException (com.spotify.docker.client.exceptions.ContainerRenameConflictException)1 DockerException (com.spotify.docker.client.exceptions.DockerException)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