Updated ForwardingRuleCreationOptions to AutoValue + Builder

This commit is contained in:
Daniel Broudy 2015-01-30 18:00:27 -08:00 committed by Ignasi Barrera
parent a26a721575
commit ec52bdbfb9
8 changed files with 99 additions and 102 deletions

View File

@ -63,11 +63,11 @@ public class ForwardingRuleCreationBinder extends BindToJsonPayload {
private ForwardingRuleCreationBinderHelper(String name, ForwardingRuleCreationOptions forwardingRuleCreationOptions){ private ForwardingRuleCreationBinderHelper(String name, ForwardingRuleCreationOptions forwardingRuleCreationOptions){
this.name = name; this.name = name;
this.description = forwardingRuleCreationOptions.getDescription(); this.description = forwardingRuleCreationOptions.description();
this.IPAddress = forwardingRuleCreationOptions.getIPAddress(); this.IPAddress = forwardingRuleCreationOptions.ipAddress();
this.IPProtocol = forwardingRuleCreationOptions.getIPProtocol(); this.IPProtocol = forwardingRuleCreationOptions.ipProtocol();
this.portRange = forwardingRuleCreationOptions.getPortRange(); this.portRange = forwardingRuleCreationOptions.portRange();
this.target = forwardingRuleCreationOptions.getTarget(); this.target = forwardingRuleCreationOptions.target();
} }
} }
} }

View File

@ -19,11 +19,35 @@ package org.jclouds.googlecomputeengine.options;
import java.net.URI; import java.net.URI;
import org.jclouds.googlecomputeengine.domain.ForwardingRule; 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 * Options for creating a Forwarding Rule
*/ */
public class ForwardingRuleCreationOptions{ @AutoValue
public abstract class ForwardingRuleCreationOptions{
@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);
}
ForwardingRuleCreationOptions(){
}
public static class Builder {
private String description; private String description;
private String ipAddress; private String ipAddress;
@ -35,24 +59,27 @@ public class ForwardingRuleCreationOptions{
* An optional textual description of the TargetPool. * An optional textual description of the TargetPool.
* @return description, provided by the client. * @return description, provided by the client.
*/ */
public String getDescription(){ public Builder description(String description){
return description; this.description = description;
return this;
} }
/** /**
* The external IP address that this forwarding rule is serving on behalf of * The external IP address that this forwarding rule is serving on behalf of
* @return ipAddress * @return ipAddress
*/ */
public String getIPAddress(){ public Builder ipAddress(String ipAddress){
return ipAddress; this.ipAddress = ipAddress;
return this;
} }
/** /**
* The IP protocol to which this rule applies * The IP protocol to which this rule applies
* @return ipProtocol * @return ipProtocol
*/ */
public ForwardingRule.IPProtocol getIPProtocol(){ public Builder ipProtocol(ForwardingRule.IPProtocol ipProtocol){
return ipProtocol; this.ipProtocol = ipProtocol;
return this;
} }
/** /**
@ -60,8 +87,9 @@ public class ForwardingRuleCreationOptions{
* will be forwarded to backend. By default, this is empty and all ports are allowed. * will be forwarded to backend. By default, this is empty and all ports are allowed.
* @return portRange * @return portRange
*/ */
public String getPortRange(){ public Builder portRange(String portRange){
return portRange; this.portRange = portRange;
return this;
} }
/** /**
@ -69,48 +97,13 @@ public class ForwardingRuleCreationOptions{
* The target resource must live in the same region as this forwarding rule. * The target resource must live in the same region as this forwarding rule.
* @return target * @return target
*/ */
public URI getTarget(){ public Builder target(URI target){
return target;
}
/**
* @see ForwardingRuleCreationOptions#getDescription()
*/
public ForwardingRuleCreationOptions description(String description){
this.description = description;
return this;
}
/**
* @see ForwardingRuleCreationOptions#getIPAddress()
*/
public ForwardingRuleCreationOptions ipAddress(String ipAddress){
this.ipAddress = ipAddress;
return this;
}
/**
* @see ForwardingRuleCreationOptions#getIPProtocol()
*/
public ForwardingRuleCreationOptions ipProtocol(ForwardingRule.IPProtocol ipProtocol){
this.ipProtocol = ipProtocol;
return this;
}
/**
* @see ForwardingRuleCreationOptions#getPortRange()
*/
public ForwardingRuleCreationOptions portRange(String portRange){
this.portRange = portRange;
return this;
}
/**
* @see ForwardingRuleCreationOptions#getTarget()
*/
public ForwardingRuleCreationOptions target(URI target){
this.target = target; this.target = target;
return this; return this;
} }
public ForwardingRuleCreationOptions build() {
return create(description, ipAddress, ipProtocol, portRange, target);
}
}
} }

View File

@ -45,12 +45,13 @@ public class ForwardingRuleCreationBinderTest extends BaseGoogleComputeEngineExp
@Test @Test
public void testMap() throws SecurityException, NoSuchMethodException { public void testMap() throws SecurityException, NoSuchMethodException {
ForwardingRuleCreationBinder binder = new ForwardingRuleCreationBinder(json); ForwardingRuleCreationBinder binder = new ForwardingRuleCreationBinder(json);
ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder()
.description(DESCRIPTION) .description(DESCRIPTION)
.ipAddress(IP_ADDRESS) .ipAddress(IP_ADDRESS)
.ipProtocol(ForwardingRule.IPProtocol.SCTP) .ipProtocol(ForwardingRule.IPProtocol.SCTP)
.portRange(PORT_RANGE) .portRange(PORT_RANGE)
.target(TARGET); .target(TARGET)
.build();
HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build(); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://momma").build();
Map<String, Object> postParams = ImmutableMap.of("name", "testForwardingRuleName", "options", forwardingRuleCreationOptions); Map<String, Object> postParams = ImmutableMap.of("name", "testForwardingRuleName", "options", forwardingRuleCreationOptions);

View File

@ -82,11 +82,12 @@ public class ForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiLiveTes
@Test(groups = "live") @Test(groups = "live")
public void testInsertForwardingRule() { public void testInsertForwardingRule() {
ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder()
.description(DESCRIPTION) .description(DESCRIPTION)
.ipAddress(address.address()) .ipAddress(address.address())
.ipProtocol(ForwardingRule.IPProtocol.TCP) .ipProtocol(ForwardingRule.IPProtocol.TCP)
.target(targetPool.selfLink()); .target(targetPool.selfLink())
.build();
assertOperationDoneSuccessfully(api().create(FORWARDING_RULE_NAME, forwardingRuleCreationOptions)); assertOperationDoneSuccessfully(api().create(FORWARDING_RULE_NAME, forwardingRuleCreationOptions));
} }

View File

@ -50,8 +50,8 @@ public class ForwardingRuleApiMockTest extends BaseGoogleComputeEngineApiMockTes
public void insert() throws Exception { public void insert() throws Exception {
server.enqueue(jsonResponse("/region_operation.json")); server.enqueue(jsonResponse("/region_operation.json"));
ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder()
.target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))); .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))).build();
assertEquals(forwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions), assertEquals(forwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions),
new ParseRegionOperationTest().expected(url("/projects"))); new ParseRegionOperationTest().expected(url("/projects")));

View File

@ -75,18 +75,19 @@ public class GlobalForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiL
getUrlMapUrl(GLOBAL_FORWARDING_RULE_URL_MAP_NAME))); getUrlMapUrl(GLOBAL_FORWARDING_RULE_URL_MAP_NAME)));
assertOperationDoneSuccessfully( assertOperationDoneSuccessfully(
api().create(GLOBAL_FORWARDING_RULE_NAME, api().create(GLOBAL_FORWARDING_RULE_NAME,
new ForwardingRuleCreationOptions().target(getTargetHttpProxyUrl(GLOBAL_FORWARDING_RULE_TARGET_HTTP_PROXY_NAME)) new ForwardingRuleCreationOptions.Builder().target(getTargetHttpProxyUrl(GLOBAL_FORWARDING_RULE_TARGET_HTTP_PROXY_NAME))
.portRange(PORT_RANGE))); .portRange(PORT_RANGE).build()));
} }
@Test(groups = "live", dependsOnMethods = "testInsertGlobalForwardingRule") @Test(groups = "live", dependsOnMethods = "testInsertGlobalForwardingRule")
public void testGetGlobalForwardingRule() { public void testGetGlobalForwardingRule() {
ForwardingRule forwardingRule = api().get(GLOBAL_FORWARDING_RULE_NAME); ForwardingRule forwardingRule = api().get(GLOBAL_FORWARDING_RULE_NAME);
assertNotNull(forwardingRule); assertNotNull(forwardingRule);
ForwardingRuleCreationOptions expected = new ForwardingRuleCreationOptions() ForwardingRuleCreationOptions expected = new ForwardingRuleCreationOptions.Builder()
.target(getTargetHttpProxyUrl(GLOBAL_FORWARDING_RULE_TARGET_HTTP_PROXY_NAME)) .target(getTargetHttpProxyUrl(GLOBAL_FORWARDING_RULE_TARGET_HTTP_PROXY_NAME))
.portRange("80-80") .portRange("80-80")
.ipProtocol(IPProtocol.TCP); .ipProtocol(IPProtocol.TCP)
.build();
assertGlobalForwardingRuleEquals(forwardingRule, expected); assertGlobalForwardingRuleEquals(forwardingRule, expected);
} }
@ -127,10 +128,10 @@ public class GlobalForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiL
} }
private void assertGlobalForwardingRuleEquals(ForwardingRule result, ForwardingRuleCreationOptions expected) { private void assertGlobalForwardingRuleEquals(ForwardingRule result, ForwardingRuleCreationOptions expected) {
assertEquals(result.target(), expected.getTarget()); assertEquals(result.target(), expected.target());
assertEquals(result.ipProtocol(), expected.getIPProtocol()); assertEquals(result.ipProtocol(), expected.ipProtocol());
assertEquals(result.description(), expected.getDescription()); assertEquals(result.description(), expected.description());
assertEquals(result.portRange(), expected.getPortRange()); assertEquals(result.portRange(), expected.portRange());
assertTrue(result.ipAddress() != null); assertTrue(result.ipAddress() != null);
} }

View File

@ -50,8 +50,8 @@ public class GlobalForwardingRuleApiMockTest extends BaseGoogleComputeEngineApiM
public void insert() throws Exception { public void insert() throws Exception {
server.enqueue(jsonResponse("/region_operation.json")); server.enqueue(jsonResponse("/region_operation.json"));
ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions() ForwardingRuleCreationOptions forwardingRuleCreationOptions = new ForwardingRuleCreationOptions.Builder()
.target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))); .target(URI.create(url("/projects/party/regions/europe-west1/targetPools/test-target-pool"))).build();
assertEquals(globalForwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions), assertEquals(globalForwardingRuleApi().create("test-forwarding-rule", forwardingRuleCreationOptions),
new ParseRegionOperationTest().expected(url("/projects"))); new ParseRegionOperationTest().expected(url("/projects")));

View File

@ -115,10 +115,11 @@ public class TargetPoolApiLiveTest extends BaseGoogleComputeEngineApiLiveTest {
TargetPool targetPool = api().get(TARGETPOOL_NAME); TargetPool targetPool = api().get(TARGETPOOL_NAME);
URI target = targetPool.selfLink(); URI target = targetPool.selfLink();
ForwardingRuleCreationOptions forwardingRuleOptions = new ForwardingRuleCreationOptions() ForwardingRuleCreationOptions forwardingRuleOptions = new ForwardingRuleCreationOptions.Builder()
.ipProtocol(IPProtocol.TCP) .ipProtocol(IPProtocol.TCP)
.portRange("80-80") .portRange("80-80")
.target(target); .target(target)
.build();
assertOperationDoneSuccessfully(api.forwardingRulesInRegion(DEFAULT_REGION_NAME) assertOperationDoneSuccessfully(api.forwardingRulesInRegion(DEFAULT_REGION_NAME)
.create(FORWARDING_RULE_NAME, forwardingRuleOptions)); .create(FORWARDING_RULE_NAME, forwardingRuleOptions));