Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Add Ipam.options() #843

Merged
merged 2 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/main/java/com/spotify/docker/client/messages/Ipam.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

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

import javax.annotation.Nullable;

@AutoValue
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
Expand All @@ -38,9 +43,14 @@ public abstract class Ipam {
@JsonProperty("Driver")
public abstract String driver();

@Nullable
@JsonProperty("Config")
public abstract ImmutableList<IpamConfig> config();

@Nullable
@JsonProperty("Options")
public abstract ImmutableMap<String, String> options();

public static Builder builder() {
return new AutoValue_Ipam.Builder();
}
Expand All @@ -50,6 +60,8 @@ public abstract static class Builder {

public abstract Builder driver(String driver);

public abstract Builder options(Map<String, String> options);

public abstract Builder config(List<IpamConfig> config);

/**
Expand All @@ -63,13 +75,23 @@ public Builder config(final String subnet, final String ipRange, final String ga
public abstract Ipam build();
}

public static Ipam create(final String driver, final List<IpamConfig> config) {
return builder()
.driver(driver)
.config(config)
.options(null)
.build();
}

@JsonCreator
public static Ipam create(
@JsonProperty("Driver") final String driver,
@JsonProperty("Config") final List<IpamConfig> config) {
@JsonProperty("Config") final List<IpamConfig> config,
@JsonProperty("Options") final Map<String, String> options) {
return builder()
.driver(driver)
.config(config)
.options(options)
.build();
}
}
56 changes: 56 additions & 0 deletions src/test/java/com/spotify/docker/client/messages/IpamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*-
* -\-\-
* docker-client
* --
* Copyright (C) 2016 - 2017 Spotify AB
* --
* Licensed 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 com.spotify.docker.client.messages;

import static com.spotify.docker.FixtureUtil.fixture;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.spotify.docker.client.ObjectMapperProvider;

import org.junit.Test;

public class IpamTest {

private static final ObjectMapper objectMapper = ObjectMapperProvider.objectMapper();

@Test
public void testDeserialize() throws Exception {
final Ipam ipam =
objectMapper.readValue(fixture("fixtures/1.29/ipam.json"), Ipam.class);
assertThat(ipam.driver(), equalTo("default"));
assertThat(ipam.config(), contains(IpamConfig.create("172.17.0.0/16", null, null)));
assertThat(ipam.options(), equalTo(ImmutableMap.of("foo", "bar")));
}

@Test
public void testDeserialize_nullConfig() throws Exception {
final Ipam ipam =
objectMapper.readValue(fixture("fixtures/1.29/ipam-null-config.json"), Ipam.class);
assertThat(ipam.driver(), equalTo("default"));
assertThat(ipam.config(), is(nullValue()));
}
}
3 changes: 3 additions & 0 deletions src/test/resources/fixtures/1.29/ipam-null-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Driver": "default"
}
11 changes: 11 additions & 0 deletions src/test/resources/fixtures/1.29/ipam.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Driver": "default",
"Config": [
{
"Subnet": "172.17.0.0/16"
}
],
"Options": {
"foo": "bar"
}
}