From ec52bdbfb90e1b0136e28b68501f669a40c54360 Mon Sep 17 00:00:00 2001 From: Daniel Broudy Date: Fri, 30 Jan 2015 18:00:27 -0800 Subject: [PATCH] Updated ForwardingRuleCreationOptions to AutoValue + Builder --- .../binders/ForwardingRuleCreationBinder.java | 14 +- .../ForwardingRuleCreationOptions.java | 147 +++++++++--------- .../ForwardingRuleCreationBinderTest.java | 5 +- .../features/ForwardingRuleApiLiveTest.java | 5 +- .../features/ForwardingRuleApiMockTest.java | 4 +- .../GlobalForwardingRuleApiLiveTest.java | 17 +- .../GlobalForwardingRuleApiMockTest.java | 4 +- .../features/TargetPoolApiLiveTest.java | 5 +- 8 files changed, 99 insertions(+), 102 deletions(-) diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinder.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinder.java index 0b1f006f95..df0e6fc125 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinder.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinder.java @@ -45,8 +45,8 @@ public class ForwardingRuleCreationBinder extends BindToJsonPayload { /** * Values used to bind ForwardingRuleOptions to json request. - * Note: Two break convention of starting with lower case letters due to - * attributes on GCE starting with upper case letters. + * Note: Two break convention of starting with lower case letters due to + * attributes on GCE starting with upper case letters. */ @SuppressWarnings("unused") private String name; @@ -63,11 +63,11 @@ public class ForwardingRuleCreationBinder extends BindToJsonPayload { private ForwardingRuleCreationBinderHelper(String name, ForwardingRuleCreationOptions forwardingRuleCreationOptions){ this.name = name; - this.description = forwardingRuleCreationOptions.getDescription(); - this.IPAddress = forwardingRuleCreationOptions.getIPAddress(); - this.IPProtocol = forwardingRuleCreationOptions.getIPProtocol(); - this.portRange = forwardingRuleCreationOptions.getPortRange(); - this.target = forwardingRuleCreationOptions.getTarget(); + this.description = forwardingRuleCreationOptions.description(); + this.IPAddress = forwardingRuleCreationOptions.ipAddress(); + this.IPProtocol = forwardingRuleCreationOptions.ipProtocol(); + this.portRange = forwardingRuleCreationOptions.portRange(); + this.target = forwardingRuleCreationOptions.target(); } } } diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/ForwardingRuleCreationOptions.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/ForwardingRuleCreationOptions.java index 1594cee68d..7391e837aa 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/ForwardingRuleCreationOptions.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/options/ForwardingRuleCreationOptions.java @@ -19,98 +19,91 @@ package org.jclouds.googlecomputeengine.options; import java.net.URI; import org.jclouds.googlecomputeengine.domain.ForwardingRule; +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.json.SerializedNames; + +import com.google.auto.value.AutoValue; /** * Options for creating a Forwarding Rule */ -public class ForwardingRuleCreationOptions{ +@AutoValue +public abstract class ForwardingRuleCreationOptions{ - private String description; - private String ipAddress; - private ForwardingRule.IPProtocol ipProtocol; - private String portRange; - private URI target; - - /** - * An optional textual description of the TargetPool. - * @return description, provided by the client. - */ - public String getDescription(){ - return description; + @Nullable public abstract String description(); + @Nullable public abstract String ipAddress(); + @Nullable public abstract ForwardingRule.IPProtocol ipProtocol(); + @Nullable public abstract String portRange(); + @Nullable public abstract URI target(); + + + @SerializedNames({"description", "ipAddress", "ipProtocol", "portRange", "target"}) + static ForwardingRuleCreationOptions create( + String description, String ipAddress, ForwardingRule.IPProtocol ipProtocol, + String portRange, URI target){ + return new AutoValue_ForwardingRuleCreationOptions(description, ipAddress, ipProtocol, portRange, target); } - /** - * The external IP address that this forwarding rule is serving on behalf of - * @return ipAddress - */ - public String getIPAddress(){ - return ipAddress; + ForwardingRuleCreationOptions(){ } - /** - * The IP protocol to which this rule applies - * @return ipProtocol - */ - public ForwardingRule.IPProtocol getIPProtocol(){ - return ipProtocol; - } + public static class Builder { - /** - * If IPProtocol is TCP or UDP, packets addressed to ports in the specified range - * will be forwarded to backend. By default, this is empty and all ports are allowed. - * @return portRange - */ - public String getPortRange(){ - return portRange; - } + private String description; + private String ipAddress; + private ForwardingRule.IPProtocol ipProtocol; + private String portRange; + private URI target; - /** - * The URL of the target resource to receive the matched traffic. - * The target resource must live in the same region as this forwarding rule. - * @return target - */ - public URI getTarget(){ - return target; - } + /** + * An optional textual description of the TargetPool. + * @return description, provided by the client. + */ + public Builder description(String description){ + this.description = description; + return this; + } - /** - * @see ForwardingRuleCreationOptions#getDescription() - */ - public ForwardingRuleCreationOptions description(String description){ - this.description = description; - return this; - } + /** + * The external IP address that this forwarding rule is serving on behalf of + * @return ipAddress + */ + public Builder ipAddress(String ipAddress){ + this.ipAddress = ipAddress; + return this; + } - /** - * @see ForwardingRuleCreationOptions#getIPAddress() - */ - public ForwardingRuleCreationOptions ipAddress(String ipAddress){ - this.ipAddress = ipAddress; - return this; - } + /** + * The IP protocol to which this rule applies + * @return ipProtocol + */ + public Builder ipProtocol(ForwardingRule.IPProtocol ipProtocol){ + this.ipProtocol = ipProtocol; + return this; + } - /** - * @see ForwardingRuleCreationOptions#getIPProtocol() - */ - public ForwardingRuleCreationOptions ipProtocol(ForwardingRule.IPProtocol ipProtocol){ - this.ipProtocol = ipProtocol; - return this; - } + /** + * If IPProtocol is TCP or UDP, packets addressed to ports in the specified range + * will be forwarded to backend. By default, this is empty and all ports are allowed. + * @return portRange + */ + public Builder portRange(String portRange){ + this.portRange = portRange; + return this; + } - /** - * @see ForwardingRuleCreationOptions#getPortRange() - */ - public ForwardingRuleCreationOptions portRange(String portRange){ - this.portRange = portRange; - return this; - } + /** + * The URL of the target resource to receive the matched traffic. + * The target resource must live in the same region as this forwarding rule. + * @return target + */ + public Builder target(URI target){ + this.target = target; + return this; + } - /** - * @see ForwardingRuleCreationOptions#getTarget() - */ - public ForwardingRuleCreationOptions target(URI target){ - this.target = target; - return this; + public ForwardingRuleCreationOptions build() { + return create(description, ipAddress, ipProtocol, portRange, target); + } } - } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinderTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinderTest.java index d45a6c3ac1..5190129979 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinderTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/binders/ForwardingRuleCreationBinderTest.java @@ -45,12 +45,13 @@ public class ForwardingRuleCreationBinderTest extends BaseGoogleComputeEngineExp @Test public void testMap() throws SecurityException, NoSuchMethodException { ForwardingRuleCreationBinder binder = new ForwardingRuleCreationBinder(json); - ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() + ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder() .description(DESCRIPTION) .ipAddress(IP_ADDRESS) .ipProtocol(ForwardingRule.IPProtocol.SCTP) .portRange(PORT_RANGE) - .target(TARGET); + .target(TARGET) + .build(); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build(); Map postParams = ImmutableMap.of("name", "testForwardingRuleName", "options", forwardingRuleCreationOptions); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiLiveTest.java index 2f036238cd..625c958fa5 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiLiveTest.java @@ -82,11 +82,12 @@ public class ForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiLiveTes @Test(groups = "live") public void testInsertForwardingRule() { - ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() + ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder() .description(DESCRIPTION) .ipAddress(address.address()) .ipProtocol(ForwardingRule.IPProtocol.TCP) - .target(targetPool.selfLink()); + .target(targetPool.selfLink()) + .build(); assertOperationDoneSuccessfully(api().create(FORWARDING_RULE_NAME, forwardingRuleCreationOptions)); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java index 8c90711d4c..7924e1b725 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/ForwardingRuleApiMockTest.java @@ -50,8 +50,8 @@ public class ForwardingRuleApiMockTest extends BaseGoogleComputeEngineApiMockTes public void insert() throws Exception { server.enqueue(jsonResponse("/region_operation.json")); - ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() - .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))); + ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder() + .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))).build(); assertEquals(forwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions), new ParseRegionOperationTest().expected(url("/projects"))); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java index 3aa012b317..624530960d 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiLiveTest.java @@ -75,18 +75,19 @@ public class GlobalForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiL getUrlMapUrl(GLOBAL_FORWARDING_RULE_URL_MAP_NAME))); assertOperationDoneSuccessfully( api().create(GLOBAL_FORWARDING_RULE_NAME, - new ForwardingRuleCreationOptions().target(getTargetHttpProxyUrl(GLOBAL_FORWARDING_RULE_TARGET_HTTP_PROXY_NAME)) - .portRange(PORT_RANGE))); + new ForwardingRuleCreationOptions.Builder().target(getTargetHttpProxyUrl(GLOBAL_FORWARDING_RULE_TARGET_HTTP_PROXY_NAME)) + .portRange(PORT_RANGE).build())); } @Test(groups = "live", dependsOnMethods = "testInsertGlobalForwardingRule") public void testGetGlobalForwardingRule() { ForwardingRule forwardingRule = api().get(GLOBAL_FORWARDING_RULE_NAME); assertNotNull(forwardingRule); - ForwardingRuleCreationOptions expected = new ForwardingRuleCreationOptions() + ForwardingRuleCreationOptions expected = new ForwardingRuleCreationOptions.Builder() .target(getTargetHttpProxyUrl(GLOBAL_FORWARDING_RULE_TARGET_HTTP_PROXY_NAME)) .portRange("80-80") - .ipProtocol(IPProtocol.TCP); + .ipProtocol(IPProtocol.TCP) + .build(); assertGlobalForwardingRuleEquals(forwardingRule, expected); } @@ -127,10 +128,10 @@ public class GlobalForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiL } private void assertGlobalForwardingRuleEquals(ForwardingRule result, ForwardingRuleCreationOptions expected) { - assertEquals(result.target(), expected.getTarget()); - assertEquals(result.ipProtocol(), expected.getIPProtocol()); - assertEquals(result.description(), expected.getDescription()); - assertEquals(result.portRange(), expected.getPortRange()); + assertEquals(result.target(), expected.target()); + assertEquals(result.ipProtocol(), expected.ipProtocol()); + assertEquals(result.description(), expected.description()); + assertEquals(result.portRange(), expected.portRange()); assertTrue(result.ipAddress() != null); } diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java index 8c0d31820d..065619371c 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/GlobalForwardingRuleApiMockTest.java @@ -50,8 +50,8 @@ public class GlobalForwardingRuleApiMockTest extends BaseGoogleComputeEngineApiM public void insert() throws Exception { server.enqueue(jsonResponse("/region_operation.json")); - ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() - .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))); + ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder() + .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))).build(); assertEquals(globalForwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions), new ParseRegionOperationTest().expected(url("/projects"))); diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetPoolApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetPoolApiLiveTest.java index c2423bc93a..b5957c20f4 100644 --- a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetPoolApiLiveTest.java +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/TargetPoolApiLiveTest.java @@ -115,10 +115,11 @@ public class TargetPoolApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { TargetPool targetPool = api().get(TARGETPOOL_NAME); URI target = targetPool.selfLink(); - ForwardingRuleCreationOptions forwardingRuleOptions = new ForwardingRuleCreationOptions() + ForwardingRuleCreationOptions forwardingRuleOptions = new ForwardingRuleCreationOptions.Builder() .ipProtocol(IPProtocol.TCP) .portRange("80-80") - .target(target); + .target(target) + .build(); assertOperationDoneSuccessfully(api.forwardingRulesInRegion(DEFAULT_REGION_NAME) .create(FORWARDING_RULE_NAME, forwardingRuleOptions));