Java Code Examples for com.spotify.docker.client.exceptions.DockerRequestException#status()

The following examples show how to use com.spotify.docker.client.exceptions.DockerRequestException#status() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public Service inspectService(final String serviceId)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  try {
    final WebTarget resource = resource().path("services").path(serviceId);
    return request(GET, Service.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ServiceNotFoundException(serviceId);
      default:
        throw e;
    }
  }
}
 
Example 2
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public ContainerUpdate updateContainer(final String containerId, final HostConfig config)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.22");
  try {
    WebTarget resource = resource().path("containers").path(containerId).path("update");
    return request(POST, ContainerUpdate.class, resource, resource.request(APPLICATION_JSON_TYPE),
            Entity.json(config));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId);
      default:
        throw e;
    }
  }
}
 
Example 3
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public List<Config> listConfigs() throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.30");

  final WebTarget resource = resource().path("configs");

  try {
    return request(GET, CONFIG_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 503:
        throw new NonSwarmNodeException("node is not part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example 4
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteNode(final String nodeId, final boolean force)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");

  final WebTarget resource = resource().path("nodes")
      .path(nodeId)
      .queryParam("force", String.valueOf(force));

  try {
    request(DELETE, resource, resource.request(APPLICATION_JSON_TYPE));
  } 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;
    }
  }
}
 
Example 5
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void disconnectFromNetwork(String containerId, String networkId, boolean force)
        throws DockerException, InterruptedException {
  final WebTarget resource = resource().path("networks").path(networkId).path("disconnect");

  final Map<String, Object> request = new HashMap<>();
  request.put("Container", containerId);
  request.put("Force", force);

  try {
    request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE),
            Entity.json(request));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        final String message = String.format("Container %s or network %s not found.",
                                             containerId, networkId);
        throw new NotFoundException(message, e);
      default:
        throw e;
    }
  }
}
 
Example 6
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public List<ContainerChange> inspectContainerChanges(final String containerId)
    throws DockerException, InterruptedException {
  try {
    final WebTarget resource = resource().path("containers").path(containerId).path("changes");
    return request(GET, CONTAINER_CHANGE_LIST, resource,
                   resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example 7
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example 8
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void updateService(final String serviceId, final Long version, final ServiceSpec spec,
                                           final RegistryAuth config)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  try {
    WebTarget resource = resource().path("services").path(serviceId).path("update");
    resource = resource.queryParam("version", version);
    request(POST, String.class, resource, resource.request(APPLICATION_JSON_TYPE)
            .header("X-Registry-Auth", authHeader(config)),
            Entity.json(spec));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ServiceNotFoundException(serviceId);
      default:
        throw e;
    }
  }
}
 
Example 9
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public UnlockKey unlockKey() throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.24");
  try {
    final WebTarget resource = resource().path("swarm").path("unlockkey");

    return request(GET, UnlockKey.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 500:
        throw new DockerException("server error", e);
      case 503:
        throw new DockerException("node is not part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example 10
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example 11
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void resizeTty(final String containerId, final Integer height, final Integer width)
    throws DockerException, InterruptedException {
  checkTtyParams(height, width);

  WebTarget resource = resource().path("containers").path(containerId).path("resize");
  if (height != null && height > 0) {
    resource = resource.queryParam("h", height);
  }
  if (width != null && width > 0) {
    resource = resource.queryParam("w", width);
  }

  try {
    request(POST, resource, resource.request(TEXT_PLAIN_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example 12
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public void stopContainer(final String containerId, final int secondsToWaitBeforeKilling)
    throws DockerException, InterruptedException {
  try {
    final WebTarget resource = noTimeoutResource()
        .path("containers").path(containerId).path("stop")
        .queryParam("t", String.valueOf(secondsToWaitBeforeKilling));
    request(POST, resource, resource.request());
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 304: // already stopped, so we're cool
        return;
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example 13
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
private void containerAction(final String containerId, final String action,
                             final MultivaluedMap<String, String> queryParameters)
        throws DockerException, InterruptedException {
  try {
    WebTarget resource = resource()
            .path("containers").path(containerId).path(action);

    for (Map.Entry<String, List<String>> queryParameter : queryParameters.entrySet()) {
      for (String parameterValue : queryParameter.getValue()) {
        resource = resource.queryParam(queryParameter.getKey(), parameterValue);
      }
    }
    request(POST, resource, resource.request());
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example 14
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@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;
    }
  }
}
 
Example 15
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream archiveContainer(String containerId, String path)
    throws DockerException, InterruptedException {
  final String apiVersion = version().apiVersion();
  final int versionComparison = compareVersion(apiVersion, "1.20");

  // Version below 1.20
  if (versionComparison < 0) {
    throw new UnsupportedApiVersionException(apiVersion);
  }

  final WebTarget resource = resource()
      .path("containers").path(containerId).path("archive")
      .queryParam("path", path);

  try {
    return request(GET, InputStream.class, resource,
                   resource.request(APPLICATION_OCTET_STREAM_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ContainerNotFoundException(containerId, e);
      default:
        throw e;
    }
  }
}
 
Example 16
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public List<Config> listConfigs(final Config.Criteria criteria)
    throws DockerException, InterruptedException {
  assertApiVersionIsAbove("1.30");

  final Map<String, List<String>> filters = new HashMap<>();

  if (criteria.configId() != null) {
    filters.put("id", Collections.singletonList(criteria.configId()));
  }
  if (criteria.label() != null) {
    filters.put("label", Collections.singletonList(criteria.label()));
  }
  if (criteria.name() != null) {
    filters.put("name", Collections.singletonList(criteria.name()));
  }

  final WebTarget resource = resource().path("configs")
      .queryParam("filters", urlEncodeFilters(filters));

  try {
    return request(GET, CONFIG_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 503:
        throw new NonSwarmNodeException("node is not part of a swarm", e);
      default:
        throw e;
    }
  }
}
 
Example 17
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public void pull(final String image, final RegistryAuth registryAuth,
                 final ProgressHandler handler)
    throws DockerException, InterruptedException {
  final ImageRef imageRef = new ImageRef(image);

  WebTarget resource = resource().path("images").path("create");

  resource = resource.queryParam("fromImage", imageRef.getImage());
  if (imageRef.getTag() != null) {
    resource = resource.queryParam("tag", imageRef.getTag());
  }

  try {
    requestAndTail(POST, handler, resource,
            resource
                .request(APPLICATION_JSON_TYPE)
                .header("X-Registry-Auth", authHeader(registryAuth)));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ImageNotFoundException(image, e);
      default:
        throw e;
    }
  }
}
 
Example 18
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@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;
    }
  }
}
 
Example 19
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@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;
    }
  }
}
 
Example 20
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public ImageInfo inspectImage(final String image) throws DockerException, InterruptedException {
  try {
    final WebTarget resource = resource().path("images").path(image).path("json");
    return request(GET, ImageInfo.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new ImageNotFoundException(image, e);
      default:
        throw e;
    }
  }
}