Search in sources :

Example 1 with Filters

use of org.eclipse.che.plugin.docker.client.json.Filters in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToGetListContainersByFiltersInTheListContainersParams.

@Test
public void shouldBeAbleToGetListContainersByFiltersInTheListContainersParams() throws IOException, JsonParseException {
    Filters filters = new Filters().withFilter("testKey", "testValue");
    ListContainersParams listContainersParams = ListContainersParams.create().withFilters(filters);
    ContainerListEntry containerListEntry = mock(ContainerListEntry.class);
    List<ContainerListEntry> expectedListContainers = singletonList(containerListEntry);
    doReturn(expectedListContainers).when(dockerConnector).parseResponseStreamAndClose(eq(inputStream), Matchers.<TypeToken<List<ContainerListEntry>>>any());
    List<ContainerListEntry> containers = dockerConnector.listContainers(listContainersParams);
    verify(dockerConnection).query(eq("filters"), anyObject());
    assertEquals(containers, expectedListContainers);
}
Also used : Filters(org.eclipse.che.plugin.docker.client.json.Filters) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) ListContainersParams(org.eclipse.che.plugin.docker.client.params.ListContainersParams) Test(org.testng.annotations.Test)

Example 2 with Filters

use of org.eclipse.che.plugin.docker.client.json.Filters in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToGetNetworksWithParams.

@Test
public void shouldBeAbleToGetNetworksWithParams() throws Exception {
    // given
    Network network = createNetwork();
    List<Network> originNetworks = singletonList(network);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(GSON.toJson(originNetworks).getBytes());
    doReturn(inputStream).when(dockerResponse).getInputStream();
    GetNetworksParams getNetworksParams = GetNetworksParams.create().withFilters(new Filters().withFilter("key", "value1", "value2"));
    // when
    List<Network> actual = dockerConnector.getNetworks(getNetworksParams);
    // then
    assertEquals(actual, originNetworks);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_GET);
    verify(dockerConnection).path("/networks");
    verify(dockerConnection).query(eq("filters"), anyObject());
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
}
Also used : Filters(org.eclipse.che.plugin.docker.client.json.Filters) ByteArrayInputStream(java.io.ByteArrayInputStream) GetNetworksParams(org.eclipse.che.plugin.docker.client.params.network.GetNetworksParams) Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) NewNetwork(org.eclipse.che.plugin.docker.client.json.network.NewNetwork) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 3 with Filters

use of org.eclipse.che.plugin.docker.client.json.Filters in project che by eclipse.

the class OpenShiftConnector method getNetworks.

/**
     * In OpenShift, there is only one network in the Docker sense, and it is similar
     * to the default bridge network. Rather than implementing all of the filters
     * available in the Docker API, we only implement {@code type=["custom"|"builtin"]}.
     *
     * <p> If type is "custom", null is returned. Otherwise, the default network is returned,
     * and the result is effectively the same as {@link DockerConnector#inspectNetwork(String)}
     * where the network is "bridge".
     *
     * @see DockerConnector#getNetworks()
     */
@Override
public List<Network> getNetworks(GetNetworksParams params) throws IOException {
    Filters filters = params.getFilters();
    List<Network> networks = new ArrayList<>();
    List<String> typeFilters = filters.getFilter("type");
    if (typeFilters == null || !typeFilters.contains("custom")) {
        Network network = inspectNetwork("openshift");
        networks.add(network);
    }
    return networks;
}
Also used : Filters(org.eclipse.che.plugin.docker.client.json.Filters) Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) ArrayList(java.util.ArrayList)

Example 4 with Filters

use of org.eclipse.che.plugin.docker.client.json.Filters in project che by eclipse.

the class DockerConnector method listContainers.

/**
     * Method returns list of docker containers which was filtered by {@link ListContainersParams}
     *
     * @throws IOException
     *         when problems occurs with docker api calls
     */
public List<ContainerListEntry> listContainers(ListContainersParams params) throws IOException {
    final Filters filters = params.getFilters();
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/containers/json")) {
        addQueryParamIfNotNull(connection, "all", params.isAll());
        addQueryParamIfNotNull(connection, "size", params.isSize());
        addQueryParamIfNotNull(connection, "limit", params.getLimit());
        addQueryParamIfNotNull(connection, "since", params.getSince());
        addQueryParamIfNotNull(connection, "before", params.getBefore());
        if (filters != null) {
            connection.query("filters", urlPathSegmentEscaper().escape(toJson(filters.getFilters())));
        }
        DockerResponse response = connection.request();
        final int status = response.getStatus();
        if (OK.getStatusCode() != status) {
            throw getDockerException(response);
        }
        return parseResponseStreamAndClose(response.getInputStream(), new TypeToken<List<ContainerListEntry>>() {
        });
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) Filters(org.eclipse.che.plugin.docker.client.json.Filters) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) List(java.util.List)

Example 5 with Filters

use of org.eclipse.che.plugin.docker.client.json.Filters in project che by eclipse.

the class DockerConnector method getEvents.

/**
     * Get docker events.
     * Parameter {@code untilSecond} does nothing if {@code sinceSecond} is 0.<br>
     * If {@code untilSecond} and {@code sinceSecond} are 0 method gets new events only (streaming mode).<br>
     * If {@code untilSecond} and {@code sinceSecond} are not 0 (but less that current date)
     * methods get events that were generated between specified dates.<br>
     * If {@code untilSecond} is 0 but {@code sinceSecond} is not method gets old events and streams new ones.<br>
     * If {@code sinceSecond} is 0 no old events will be got.<br>
     * With some connection implementations method can fail due to connection timeout in streaming mode.
     *
     * @param messageProcessor
     *         processor of all found events that satisfy specified parameters
     * @throws IOException
     *          when a problem occurs with docker api calls
     */
public void getEvents(final GetEventsParams params, MessageProcessor<Event> messageProcessor) throws IOException {
    final Filters filters = params.getFilters();
    try (DockerConnection connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/events")) {
        addQueryParamIfNotNull(connection, "since", params.getSinceSecond());
        addQueryParamIfNotNull(connection, "until", params.getUntilSecond());
        if (filters != null) {
            connection.query("filters", urlPathSegmentEscaper().escape(toJson(filters.getFilters())));
        }
        final DockerResponse response = connection.request();
        if (OK.getStatusCode() != response.getStatus()) {
            throw getDockerException(response);
        }
        try (InputStream responseStream = response.getInputStream()) {
            new MessagePumper<>(new JsonMessageReader<>(responseStream, Event.class), messageProcessor).start();
        }
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) Filters(org.eclipse.che.plugin.docker.client.json.Filters) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) BufferedInputStream(java.io.BufferedInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Aggregations

Filters (org.eclipse.che.plugin.docker.client.json.Filters)5 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DockerConnection (org.eclipse.che.plugin.docker.client.connection.DockerConnection)2 DockerResponse (org.eclipse.che.plugin.docker.client.connection.DockerResponse)2 ContainerInNetwork (org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork)2 Network (org.eclipse.che.plugin.docker.client.json.network.Network)2 Test (org.testng.annotations.Test)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Collections.singletonList (java.util.Collections.singletonList)1 CloseConnectionInputStream (org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream)1 ContainerListEntry (org.eclipse.che.plugin.docker.client.json.ContainerListEntry)1 NewNetwork (org.eclipse.che.plugin.docker.client.json.network.NewNetwork)1 ListContainersParams (org.eclipse.che.plugin.docker.client.params.ListContainersParams)1 GetNetworksParams (org.eclipse.che.plugin.docker.client.params.network.GetNetworksParams)1