All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gesellix.docker.client.network.ManageNetworkClient Maven / Gradle / Ivy

The newest version!
package de.gesellix.docker.client.network;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.gesellix.docker.client.EngineResponseContent;
import de.gesellix.docker.remote.api.EngineApiClient;
import de.gesellix.docker.remote.api.IPAM;
import de.gesellix.docker.remote.api.Network;
import de.gesellix.docker.remote.api.NetworkConnectRequest;
import de.gesellix.docker.remote.api.NetworkCreateRequest;
import de.gesellix.docker.remote.api.NetworkCreateResponse;
import de.gesellix.docker.remote.api.NetworkDisconnectRequest;
import de.gesellix.docker.remote.api.NetworkPruneResponse;
import de.gesellix.util.QueryParameterEncoder;

public class ManageNetworkClient implements ManageNetwork {

  private final Logger log = LoggerFactory.getLogger(ManageNetworkClient.class);
  private final EngineApiClient client;
  private final QueryParameterEncoder queryParameterEncoder;

  public ManageNetworkClient(EngineApiClient client) {
    this.client = client;
    this.queryParameterEncoder = new QueryParameterEncoder();
  }

  /**
   * @see #networks(String)
   * @deprecated use {@link #networks(String)}
   */
  @Deprecated
  @Override
  public EngineResponseContent> networks(Map query) {
    log.info("docker network ls");
    Map actualQuery = new HashMap<>();
    if (query != null) {
      actualQuery.putAll(query);
    }

    queryParameterEncoder.jsonEncodeQueryParameter(actualQuery, "filters");
    return networks((String) actualQuery.get("filters"));
  }

  @Override
  public EngineResponseContent> networks(String filters) {
    log.info("docker network ls");
    List networks = client.getNetworkApi().networkList(filters);
    return new EngineResponseContent<>(networks);
  }

  @Override
  public EngineResponseContent> networks() {
    return networks((String) null);
  }

  @Override
  public EngineResponseContent inspectNetwork(String name) {
    log.info("docker network inspect");
    Network network = client.getNetworkApi().networkInspect(name, null, null);
    return new EngineResponseContent<>(network);
  }

  /**
   * @see #createNetwork(NetworkCreateRequest)
   * @deprecated use {@link #createNetwork(NetworkCreateRequest)}
   */
  @Deprecated
  @Override
  public EngineResponseContent createNetwork(String name, Map config) {
    Map actualConfig = new HashMap<>();
    if (config != null) {
      actualConfig.putAll(config);
    }

    Map ipam = (Map) actualConfig.get("IPAM");
    NetworkCreateRequest request = new NetworkCreateRequest(
        name, true,
        (String) actualConfig.get("Driver"),
        (Boolean) actualConfig.get("Internal"),
        (Boolean) actualConfig.get("Attachable"),
        (Boolean) actualConfig.get("Ingress"),
        ipam == null ? null : new IPAM(
            ipam == null ? null : (String) ipam.get("Driver"),
            ipam == null ? null : (List) ipam.get("Config"),
            ipam == null ? null : (Map) ipam.get("Options")),
        (Boolean) actualConfig.get("EnableIPv6"),
        (Map) actualConfig.get("Options"),
        (Map) actualConfig.get("Labels"));
    return createNetwork(request);
  }

  @Override
  public EngineResponseContent createNetwork(String name) {
    NetworkCreateRequest request = new NetworkCreateRequest(
        name,
        true,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null);
    return createNetwork(request);
  }

  @Override
  public EngineResponseContent createNetwork(NetworkCreateRequest networkCreateRequest) {
    log.info("docker network create");
//    if (networkCreateRequest.name == null) {
//      throw new IllegalArgumentException("Name is null")
//    }
    // TODO set defaults?
//    if (networkCreateRequest.checkDuplicate == null) {
//      networkCreateRequest.checkDuplicate = true
//    }
    NetworkCreateResponse networkCreate = client.getNetworkApi().networkCreate(networkCreateRequest);
    return new EngineResponseContent<>(networkCreate);
  }

  @Override
  public void connectNetwork(String network, String container) {
    log.info("docker network connect");
    client.getNetworkApi().networkConnect(network, new NetworkConnectRequest(container, null));
  }

  @Override
  public void disconnectNetwork(String network, String container) {
    log.info("docker network disconnect");
    client.getNetworkApi().networkDisconnect(network, new NetworkDisconnectRequest(container, null));
  }

  @Override
  public void rmNetwork(String name) {
    log.info("docker network rm");
    client.getNetworkApi().networkDelete(name);
  }

  /**
   * @see #pruneNetworks(String)
   * @deprecated use {@link #pruneNetworks(String)}
   */
  @Deprecated
  @Override
  public EngineResponseContent pruneNetworks(Map query) {
    log.info("docker network prune");
    Map actualQuery = new HashMap<>();
    if (query != null) {
      actualQuery.putAll(query);
    }
    queryParameterEncoder.jsonEncodeQueryParameter(actualQuery, "filters");
    return pruneNetworks((String) actualQuery.get("filters"));
  }

  @Override
  public EngineResponseContent pruneNetworks(String filters) {
    log.info("docker network prune");
    NetworkPruneResponse networkPrune = client.getNetworkApi().networkPrune(filters);
    return new EngineResponseContent<>(networkPrune);
  }

  @Override
  public EngineResponseContent pruneNetworks() {
    return pruneNetworks((String) null);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy