com.spotify.docker.client.messages.Network Java Examples

The following examples show how to use com.spotify.docker.client.messages.Network. 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: Networks.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 6 votes vote down vote up
public static List<NetworkAttachmentConfig> fromString(String networkConfig, List<Network> dockerNetworks) {
    if (isBlank(networkConfig)) {
        return Collections.emptyList();
    }

    final Map<String, Network> availableNetworks = dockerNetworks.stream().collect(Collectors.toMap(o -> o.name(), o -> o));

    final List<NetworkAttachmentConfig> serviceNetworks = new ArrayList<>();
    final Collection<String> networkEntries = splitIntoLinesAndTrimSpaces(networkConfig);
    networkEntries.forEach(networkEntry -> {
        final Network availableNetwork = availableNetworks.get(networkEntry);

        if (availableNetwork == null) {
            throw new RuntimeException(format("Network with name `{0}` does not exist.", networkEntry));
        }

        LOG.debug(format("Using network `{0}` with id `{1}`.", networkEntry, availableNetwork.id()));
        serviceNetworks.add(NetworkAttachmentConfig.builder().target(networkEntry).build());
    });

    return serviceNetworks;
}
 
Example #2
Source File: NetworksTest.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnNetworkAttachmentConfigListFromString() {
    final Network swarmNetwork = mock(Network.class);
    when(swarmNetwork.name()).thenReturn("frontend");

    final List<NetworkAttachmentConfig> serviceNetworks = Networks.fromString("frontend", asList(swarmNetwork));

    assertNotNull(serviceNetworks);
    assertThat(serviceNetworks, hasSize(1));

    assertThat(serviceNetworks.get(0).target(), is("frontend"));
}
 
Example #3
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public List<Network> listNetworks(final ListNetworksParam... params)
    throws DockerException, InterruptedException {
  WebTarget resource = resource().path("networks");
  resource = addParameters(resource, params);
  return request(GET, NETWORK_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
}
 
Example #4
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public Network inspectNetwork(String networkId) throws DockerException, InterruptedException {
  final WebTarget resource = resource().path("networks").path(networkId);
  try {
    return request(GET, Network.class, resource, resource.request(APPLICATION_JSON_TYPE));
  } catch (DockerRequestException e) {
    switch (e.status()) {
      case 404:
        throw new NetworkNotFoundException(networkId, e);
      default:
        throw e;
    }
  }
}
 
Example #5
Source File: SysNetworkServiceImpl.java    From paas with Apache License 2.0 4 votes vote down vote up
@Transactional(rollbackFor = CustomException.class)
@Override
public ResultVO createPublicNetwork(String name, String driver, Map<String, String> labels, HttpServletRequest request) {
    // 参数判断
    if(StringUtils.isBlank(name, driver)) {
        return ResultVOUtils.error(ResultEnum.PARAM_ERROR);
    }
    // 名称只能由数字和字母组成
    if(StringUtils.isNotAlphaOrNumeric(name)) {
        return ResultVOUtils.error(ResultEnum.NETWORK_NAME_ILLEGAL);
    }
    // 名称判断
    if(hasExistName(name)) {
        return ResultVOUtils.error(ResultEnum.NETWORK_NAME_EXIST);
    }
    // host driver 判断
    if("host".equals(driver) && hasExistHostDriver()) {
        return ResultVOUtils.error(ResultEnum.NETWORK_HOST_EXIST);
    }

    try {
        NetworkConfig.Builder builder = NetworkConfig.builder();
        builder.name(name);
        builder.driver(driver);
        builder.checkDuplicate(true);
        builder.attachable(true);

        if(labels != null) {
            builder.labels(labels);
        }

        NetworkConfig config = builder.build();
        dockerClient.createNetwork(config);

        // 保存数据库
        List<Network> networks = dockerClient.listNetworks(DockerClient.ListNetworksParam.byNetworkName(name));
        if(networks == null || networks.size() == 0) {
            return ResultVOUtils.error(ResultEnum.PUBLIC_NETWORK_CREATE_ERROR);
        }
        SysNetwork sysNetwork = network2SysNetwork(networks.get(0));
        networkMapper.insert(sysNetwork);

        // 保存日志
        sysLogService.saveLog(request, SysLogTypeEnum.CREATE_PUBLIC_NETWORK);

        return ResultVOUtils.success();
    } catch (NotFoundException fe){
        fe.printStackTrace();
        return ResultVOUtils.error(ResultEnum.CREATE_NETWORK_ERROR_BY_DRIVER);
    } catch (Exception e) {
        log.error("创建公共网络出现错误,错误位置:{},错误栈:{}",
                "SysNetworkServiceImpl.createPublicNetwork()", HttpClientUtils.getStackTraceAsString(e));
        // 保存日志
        sysLogService.saveLog(request, SysLogTypeEnum.CREATE_PUBLIC_NETWORK_ERROR, e);

        return ResultVOUtils.error(ResultEnum.PUBLIC_NETWORK_CREATE_ERROR);
    }
}
 
Example #6
Source File: SysNetworkServiceImpl.java    From paas with Apache License 2.0 4 votes vote down vote up
@Override
public ResultVO createUserNetwork(String name, String driver, Map<String, String> labels, String uid) {
    // 参数判断
    if(StringUtils.isBlank(name, driver)) {
        return ResultVOUtils.error(ResultEnum.PARAM_ERROR);
    }
    // 名称只能由数字和字母组成
    if(StringUtils.isNotAlphaOrNumeric(name)) {
        return ResultVOUtils.error(ResultEnum.NETWORK_NAME_ILLEGAL);
    }
    // 名称判断
    if(hasExistName(name, uid)) {
        return ResultVOUtils.error(ResultEnum.NETWORK_NAME_EXIST);
    }
    // host driver 判断
    if("host".equals(driver) && hasExistHostDriver()) {
        return ResultVOUtils.error(ResultEnum.NETWORK_HOST_EXIST);
    }

    try {
        String fullName =uid + "-" + name;
        NetworkConfig.Builder builder = NetworkConfig.builder();
        builder.name(fullName);
        builder.driver(driver);
        builder.checkDuplicate(true);
        builder.attachable(true);

        if(labels != null) {
            builder.labels(labels);
        }

        NetworkConfig config = builder.build();
        dockerClient.createNetwork(config);

        // 保存数据库
        List<Network> networks = dockerClient.listNetworks(DockerClient.ListNetworksParam.byNetworkName(name));
        if(networks == null || networks.size() == 0) {
            return ResultVOUtils.error(ResultEnum.USER_NETWORK_CREATE_ERROR);
        }
        SysNetwork sysNetwork = network2SysNetwork(networks.get(0));
        networkMapper.insert(sysNetwork);

        return ResultVOUtils.success();
    } catch (Exception e) {
        log.error("创建个人网络出现错误,错误位置:{},错误栈:{}",
                "SysNetworkServiceImpl.createUserNetwork()", HttpClientUtils.getStackTraceAsString(e));
        return ResultVOUtils.error(ResultEnum.USER_NETWORK_CREATE_ERROR);
    }
}
 
Example #7
Source File: DiscoveredContainer.java    From hazelcast-docker-swarm-discovery-spi with Apache License 2.0 4 votes vote down vote up
public DiscoveredContainer(Network network, Service service, Task task, NetworkAttachment relevantNetworkAttachment) {
    this.network = network;
    this.service = service;
    this.task = task;
    this.relevantNetworkAttachment = relevantNetworkAttachment;
}
 
Example #8
Source File: SwarmDiscoveryUtil.java    From hazelcast-docker-swarm-discovery-spi with Apache License 2.0 3 votes vote down vote up
/**
 * Discover containers on the relevant networks that match the given
 * service criteria, using additionally a NullServiceFilter
 *
 * @param docker
 * @param relevantNetIds2Networks
 * @param criteria
 * @return set of DiscoveredContainer instances
 * @throws Exception
 */
private Set<DiscoveredContainer> discoverContainersViaCriteria(DockerClient docker,
                                                               Map<String, Network> relevantNetIds2Networks,
                                                               Service.Criteria criteria) throws Exception {
    // no ServiceFilter provided, so use one with no constraints
    return discoverContainersViaCriteria(docker, relevantNetIds2Networks, criteria, NullServiceFilter.getInstance());
}
 
Example #9
Source File: DockerClient.java    From docker-client with Apache License 2.0 2 votes vote down vote up
/**
 * List all or a subset of the networks.
 * Filters were added in Docker 1.10, API version 1.22.
 *
 * @return networks
 * @throws DockerException      if a server error occurred (500)
 * @throws InterruptedException If the thread is interrupted
 */
List<Network> listNetworks(ListNetworksParam... params)
    throws DockerException, InterruptedException;
 
Example #10
Source File: DockerClient.java    From docker-client with Apache License 2.0 2 votes vote down vote up
/**
 * Filter networks by network type.
 * There are two types of networks: those built-in into Docker
 * and custom networks created by users.
 * @param type The network type.
 * @return The ListNetworksParam for the given type.
 * @see #builtInNetworks()
 * @see #customNetworks()
 * @since Docker 1.10, API version 1.22
 */
public static ListNetworksParam withType(final Network.Type type) {
  return filter("type", type.getName());
}
 
Example #11
Source File: DockerClient.java    From docker-client with Apache License 2.0 2 votes vote down vote up
/**
 * Inspect a specific network.
 *
 * @param networkId The id of the network
 * @return network information
 * @throws NetworkNotFoundException
 *                              if network is not found (404)
 * @throws DockerException      if a server error occurred (500)
 * @throws InterruptedException If the thread is interrupted
 */
Network inspectNetwork(final String networkId) throws DockerException, InterruptedException;