From 91339b200ee19b38dfea217191da315415d38b74 Mon Sep 17 00:00:00 2001 From: Duncan Grant Date: Thu, 9 Jun 2016 09:04:56 +0100 Subject: [PATCH] Node json should be optional The Node element of the container json is optional as well as nullable. --- .../functions/ContainerToNodeMetadata.java | 5 +-- .../org/jclouds/docker/domain/Container.java | 11 +++--- .../ContainerToNodeMetadataTest.java | 1 + .../jclouds/docker/domain/ContainerTest.java | 37 +++++++++++++++++++ .../docker/parse/ContainerParseTest.java | 3 ++ .../parse/ContainerVersionMajor1Minor21.java | 1 + apis/docker/src/test/resources/container.json | 3 ++ 7 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 apis/docker/src/test/java/org/jclouds/docker/domain/ContainerTest.java diff --git a/apis/docker/src/main/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadata.java b/apis/docker/src/main/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadata.java index d2133a6d5d..53b70f126e 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadata.java +++ b/apis/docker/src/main/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadata.java @@ -36,7 +36,6 @@ import org.jclouds.domain.Location; import org.jclouds.providers.ProviderMetadata; import com.google.common.base.Function; -import com.google.common.base.Strings; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -113,8 +112,8 @@ public class ContainerToNodeMetadata implements Function getPublicIpAddresses(Container container) { String dockerIpAddress; - if (container.node() != null && !Strings.isNullOrEmpty(container.node().ip())) { - dockerIpAddress = container.node().ip(); + if (container.node().isPresent()) { + dockerIpAddress = container.node().get().ip(); } else { dockerIpAddress = URI.create(providerMetadata.getEndpoint()).getHost(); } diff --git a/apis/docker/src/main/java/org/jclouds/docker/domain/Container.java b/apis/docker/src/main/java/org/jclouds/docker/domain/Container.java index b53cdee287..d0c10c9a82 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/domain/Container.java +++ b/apis/docker/src/main/java/org/jclouds/docker/domain/Container.java @@ -26,6 +26,7 @@ import org.jclouds.javax.annotation.Nullable; import org.jclouds.json.SerializedNames; import com.google.auto.value.AutoValue; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -77,7 +78,7 @@ public abstract class Container { @Nullable public abstract String processLabel(); - @Nullable public abstract Node node(); + public abstract Optional node(); Container() { } @@ -93,7 +94,7 @@ public abstract class Container { String resolvConfPath, Map volumes, HostConfig hostConfig, String driver, String execDriver, Map volumesRW, String command, String status, List ports, String hostnamePath, String hostsPath, - String mountLabel, String processLabel, Node node) { + String mountLabel, String processLabel, Optional node) { return new AutoValue_Container(id, created, path, name, copyOf(args), config, state, image, networkSettings, sysInitPath, resolvConfPath, copyOf(volumes), hostConfig, driver, execDriver, copyOf(volumesRW), command, status, copyOf(ports), hostnamePath, hostsPath, mountLabel, processLabel, node); @@ -132,7 +133,7 @@ public abstract class Container { private String hostsPath; private String mountLabel; private String processLabel; - private Node node; + private Optional node = Optional.absent(); public Builder id(String id) { this.id = id; @@ -250,7 +251,7 @@ public abstract class Container { } public Builder node(Node node) { - this.node = node; + this.node = Optional.fromNullable(node); return this; } @@ -266,7 +267,7 @@ public abstract class Container { .sysInitPath(in.sysInitPath()).resolvConfPath(in.resolvConfPath()).driver(in.driver()) .execDriver(in.execDriver()).volumes(in.volumes()).hostConfig(in.hostConfig()).volumesRW(in.volumesRW()) .command(in.command()).status(in.status()).ports(in.ports()).hostnamePath(in.hostnamePath()) - .hostsPath(in.hostsPath()).mountLabel(in.mountLabel()).processLabel(in.processLabel()).node(in.node()); + .hostsPath(in.hostsPath()).mountLabel(in.mountLabel()).processLabel(in.processLabel()).node(in.node().orNull()); } } } diff --git a/apis/docker/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java b/apis/docker/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java index 53ba6277e7..7588174671 100644 --- a/apis/docker/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java +++ b/apis/docker/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java @@ -128,6 +128,7 @@ public class ContainerToNodeMetadataTest { .status("") .hostConfig(HostConfig.builder().publishAllPorts(true).build()) .ports(ImmutableList.of()) + .node(null) .build(); ProviderMetadata providerMetadata = EasyMock.createMock(ProviderMetadata.class); expect(providerMetadata.getEndpoint()).andReturn("http://127.0.0.1:4243"); diff --git a/apis/docker/src/test/java/org/jclouds/docker/domain/ContainerTest.java b/apis/docker/src/test/java/org/jclouds/docker/domain/ContainerTest.java new file mode 100644 index 0000000000..7be6e3c233 --- /dev/null +++ b/apis/docker/src/test/java/org/jclouds/docker/domain/ContainerTest.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.docker.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "ContainerTest") +public class ContainerTest { + + @Test + public void testFromContainer() { + Container testContainer = Container.builder() + .id("testcontainer") + .build(); + + + Container newTestContainer = testContainer.toBuilder().build(); + assertThat(newTestContainer).isEqualTo(testContainer); + } +} + diff --git a/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java b/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java index 53ef7dcf81..884733ea79 100644 --- a/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java +++ b/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java @@ -27,6 +27,7 @@ import org.jclouds.docker.domain.Config; import org.jclouds.docker.domain.Container; import org.jclouds.docker.domain.HostConfig; import org.jclouds.docker.domain.NetworkSettings; +import org.jclouds.docker.domain.Node; import org.jclouds.docker.domain.State; import org.jclouds.docker.internal.BaseDockerParseTest; import org.testng.annotations.Test; @@ -125,6 +126,8 @@ public class ContainerParseTest extends BaseDockerParseTest { .hostsPath("/var/lib/docker/containers/6c9932f478bd761f32ddb54ed28ab42ab6fac6f2a279f561ea31503ee9d39524/hosts") .mountLabel("") .processLabel("") + .node(Node.builder(). + ip("10.10.10.10").build()) .build(); } } diff --git a/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerVersionMajor1Minor21.java b/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerVersionMajor1Minor21.java index 2b990e14c7..8bca8c83cf 100644 --- a/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerVersionMajor1Minor21.java +++ b/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerVersionMajor1Minor21.java @@ -239,6 +239,7 @@ public class ContainerVersionMajor1Minor21 { "bridge", NetworkSettings.Details.create("", "", "", 0, "", "", 0, ""))) .build()) .path("/bin/sh") + .node(null) .processLabel("") .resolvConfPath("/var/lib/docker/containers/ba033ac4401106a3b513bc9d639eee123ad78ca3616b921167cd74b20e25ed39/resolv.conf") // "RestartCount": 1, diff --git a/apis/docker/src/test/resources/container.json b/apis/docker/src/test/resources/container.json index bc18e270d1..37c5db4fd8 100644 --- a/apis/docker/src/test/resources/container.json +++ b/apis/docker/src/test/resources/container.json @@ -134,6 +134,9 @@ } } }, + "Node": { + "IP": "10.10.10.10" + }, "Path": "/home/weave/weaver", "ProcessLabel": "", "ResolvConfPath": "/var/lib/docker/containers/6c9932f478bd761f32ddb54ed28ab42ab6fac6f2a279f561ea31503ee9d39524/resolv.conf",