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.
}
}
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;
}
}
}
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;
}
}
}
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;
}
}
}
Aggregations