Updated BackendServiceOptions to AutoValue + Builder

This commit is contained in:
Daniel Broudy 2015-01-27 16:47:16 -08:00 committed by Ignasi Barrera
parent 7ac7197e83
commit a9eecfad81
6 changed files with 135 additions and 157 deletions

View File

@ -21,11 +21,38 @@ import java.util.List;
import org.jclouds.googlecomputeengine.domain.BackendService.Backend; import org.jclouds.googlecomputeengine.domain.BackendService.Backend;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;
public class BackendServiceOptions { import com.google.auto.value.AutoValue;
@AutoValue
public abstract class BackendServiceOptions {
@Nullable public abstract String name();
@Nullable public abstract String description();
@Nullable public abstract List<URI> healthChecks();
@Nullable public abstract List<Backend> backends();
@Nullable public abstract Integer timeoutSec();
@Nullable public abstract Integer port();
@Nullable public abstract String protocol();
@Nullable public abstract String fingerprint();
@Nullable public abstract String portName();
@SerializedNames({"name", "description", "healthChecks", "backends", "timeoutSec",
"port", "protocol", "fingerprint", "portName"})
static BackendServiceOptions create(String name, String description, List<URI> healthChecks,
List<Backend> backends, Integer timeoutSec, Integer port, String protocol, String fingerprint, String portName){
return new AutoValue_BackendServiceOptions(name, description, healthChecks,
backends, timeoutSec, port, protocol, fingerprint, portName);
}
BackendServiceOptions(){
}
public static class Builder {
private String name; private String name;
@Nullable private String description; private String description;
private List<URI> healthChecks; private List<URI> healthChecks;
private List<Backend> backends; private List<Backend> backends;
private Integer timeoutSec; private Integer timeoutSec;
@ -35,49 +62,37 @@ public class BackendServiceOptions {
private String portName; private String portName;
/** /**
* Name of the BackendService resource. * @param name, provided by the client.
* @return name, provided by the client. * @param healthChecks The list of {@link HttpHealthCheck#selfLink Links} to the HttpHealthCheck
* resource for health checking this BackendService.
* Currently at most one health check can be specified, and a health check is required.
*/ */
public String getName(){ public Builder(String name, List<URI> healthChecks){
return name; this.name = name;
this.healthChecks = healthChecks;
} }
/** /**
* @see BackendServiceOptions#getName() * Empty builder for use when patching or updating and existing BackendService
* Otherwise use the other {@link #Builder(String, List) builder}
*/ */
public BackendServiceOptions name(String name) { public Builder(){
this.name = name;
return this;
} }
/** /**
* An optional textual description of the BackendService. * An optional textual description of the BackendService.
* @return description, provided by the client.
*/ */
public String getDescription(){ public Builder description(String description){
return description;
}
/**
* @see BackendServiceOptions#getDescription()
*/
public BackendServiceOptions description(String description) {
this.description = description; this.description = description;
return this; return this;
} }
/** /**
* The list of {@link HttpHealthCheck#selfLink Links} to the HttpHealthCheck resource for health checking this BackendService. * HealthChecks - The list of {@link HttpHealthCheck#selfLink Links} to the HttpHealthCheck
* resource for health checking this BackendService.
* Currently at most one health check can be specified, and a health check is required. * Currently at most one health check can be specified, and a health check is required.
*/ */
public List<URI> getHealthChecks() { public Builder healthChecks(List<URI> healthChecks){
return healthChecks;
}
/**
* @see BackendServiceOptions#getHealthChecks()
*/
public BackendServiceOptions healthChecks(List<URI> healthChecks) {
this.healthChecks = healthChecks; this.healthChecks = healthChecks;
return this; return this;
} }
@ -85,14 +100,7 @@ public class BackendServiceOptions {
/** /**
* The list of backends that serve this BackendService. * The list of backends that serve this BackendService.
*/ */
public List<Backend> getBackends() { public Builder backends(List<Backend> backends){
return backends;
}
/**
* @see BackendServiceOptions#getBackends()
*/
public BackendServiceOptions backends(List<Backend> backends){
this.backends = backends; this.backends = backends;
return this; return this;
} }
@ -101,14 +109,7 @@ public class BackendServiceOptions {
* How many seconds to wait for the backend before considering it a failed request. * How many seconds to wait for the backend before considering it a failed request.
* Default is 30 seconds. * Default is 30 seconds.
*/ */
public Integer getTimeoutSec() { public Builder timeoutSec(Integer timeoutSec) {
return timeoutSec;
}
/**
* @see BackendServiceOptions#getTimeoutSec()
*/
public BackendServiceOptions timeoutSec(Integer timeoutSec) {
this.timeoutSec = timeoutSec; this.timeoutSec = timeoutSec;
return this; return this;
} }
@ -117,30 +118,15 @@ public class BackendServiceOptions {
* The TCP port to connect on the backend. * The TCP port to connect on the backend.
* The default value is 80. * The default value is 80.
*/ */
public Integer getPort() { public Builder port(Integer port) {
return port;
}
/**
* @see BackendServiceOptions#getPort()
*/
public BackendServiceOptions port(Integer port) {
this.port = port; this.port = port;
return this; return this;
} }
/** /**
* The protocol for incoming requests. * The protocol for incoming requests.
*/ */
public String getProtocol() { public Builder protocol(String protocol) {
return protocol;
}
/**
* @see BackendServiceOptions#getProtocol()
*/
public BackendServiceOptions protocol(String protocol) {
this.protocol = protocol; this.protocol = protocol;
return this; return this;
} }
@ -151,24 +137,19 @@ public class BackendServiceOptions {
* inserting a BackendService. An up-to-date fingerprint must be provided in * inserting a BackendService. An up-to-date fingerprint must be provided in
* order to update the BackendService. * order to update the BackendService.
*/ */
public String getFingerprint() { public Builder fingerprint(String fingerprint) {
return fingerprint;
}
/**
* @see BackendServiceOptions#getFingerprint()
*/
public BackendServiceOptions fingerprint(String fingerprint) {
this.fingerprint = fingerprint; this.fingerprint = fingerprint;
return this; return this;
} }
public String getPortName() { public Builder portName(String portName) {
return portName;
}
public BackendServiceOptions portName(String portName) {
this.portName = portName; this.portName = portName;
return this; return this;
} }
public BackendServiceOptions build(){
return create(name, description, healthChecks,
backends, timeoutSec, port, protocol, fingerprint, portName);
}
}
} }

View File

@ -47,7 +47,8 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes
assertOperationDoneSuccessfully(api.httpHeathChecks().insert(BACKEND_SERVICE_HEALTH_CHECK_NAME)); assertOperationDoneSuccessfully(api.httpHeathChecks().insert(BACKEND_SERVICE_HEALTH_CHECK_NAME));
List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME)); List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME));
BackendServiceOptions b = new BackendServiceOptions().name(BACKEND_SERVICE_NAME).healthChecks(healthChecks);
BackendServiceOptions b = new BackendServiceOptions.Builder(BACKEND_SERVICE_NAME, healthChecks).build();
assertOperationDoneSuccessfully(api().create(b)); assertOperationDoneSuccessfully(api().create(b));
} }
@ -61,11 +62,10 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes
@Test(groups = "live", dependsOnMethods = "testGetBackendService") @Test(groups = "live", dependsOnMethods = "testGetBackendService")
public void testPatchBackendService() { public void testPatchBackendService() {
String fingerprint = api().get(BACKEND_SERVICE_NAME).fingerprint(); String fingerprint = api().get(BACKEND_SERVICE_NAME).fingerprint();
BackendServiceOptions backendService = new BackendServiceOptions() BackendServiceOptions backendService = new BackendServiceOptions.Builder(BACKEND_SERVICE_NAME, ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME)))
.name(BACKEND_SERVICE_NAME)
.healthChecks(ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME)))
.timeoutSec(10) .timeoutSec(10)
.fingerprint(fingerprint); .fingerprint(fingerprint)
.build();
assertOperationDoneSuccessfully(api().update(BACKEND_SERVICE_NAME, backendService)); assertOperationDoneSuccessfully(api().update(BACKEND_SERVICE_NAME, backendService));
assertBackendServiceEquals(api().get(BACKEND_SERVICE_NAME), backendService); assertBackendServiceEquals(api().get(BACKEND_SERVICE_NAME), backendService);
@ -75,12 +75,11 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes
public void testUpdateBackendService() { public void testUpdateBackendService() {
String fingerprint = api().get(BACKEND_SERVICE_NAME).fingerprint(); String fingerprint = api().get(BACKEND_SERVICE_NAME).fingerprint();
BackendServiceOptions backendService = new BackendServiceOptions() BackendServiceOptions backendService = new BackendServiceOptions.Builder(BACKEND_SERVICE_NAME, ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME)))
.name(BACKEND_SERVICE_NAME)
.healthChecks(ImmutableList.of(getHealthCheckUrl(BACKEND_SERVICE_HEALTH_CHECK_NAME)))
.timeoutSec(45) .timeoutSec(45)
.port(8080) .port(8080)
.fingerprint(fingerprint); .fingerprint(fingerprint)
.build();
assertOperationDoneSuccessfully(api().update(BACKEND_SERVICE_NAME, backendService)); assertOperationDoneSuccessfully(api().update(BACKEND_SERVICE_NAME, backendService));
assertBackendServiceEquals(api().get(BACKEND_SERVICE_NAME), assertBackendServiceEquals(api().get(BACKEND_SERVICE_NAME),
@ -119,13 +118,13 @@ public class BackendServiceApiLiveTest extends BaseGoogleComputeEngineApiLiveTes
} }
private void assertBackendServiceEquals(BackendService result, BackendServiceOptions expected) { private void assertBackendServiceEquals(BackendService result, BackendServiceOptions expected) {
assertEquals(result.name(), expected.getName()); assertEquals(result.name(), expected.name());
assertEquals(result.healthChecks(), expected.getHealthChecks()); assertEquals(result.healthChecks(), expected.healthChecks());
if (expected.getTimeoutSec() != null) { if (expected.timeoutSec() != null) {
org.testng.Assert.assertEquals(result.timeoutSec(), expected.getTimeoutSec().intValue()); org.testng.Assert.assertEquals(result.timeoutSec(), expected.timeoutSec().intValue());
} }
if (expected.getPort() != null) { if (expected.port() != null) {
org.testng.Assert.assertEquals(result.port(), expected.getPort().intValue()); org.testng.Assert.assertEquals(result.port(), expected.port().intValue());
} }
} }

View File

@ -57,11 +57,11 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes
List<URI> healthChecks = ImmutableList.of(URI.create(url("/projects/" List<URI> healthChecks = ImmutableList.of(URI.create(url("/projects/"
+ "myproject/global/httpHealthChecks/jclouds-test"))); + "myproject/global/httpHealthChecks/jclouds-test")));
assertEquals(backendServiceApi().create( new BackendServiceOptions().name("jclouds-test") assertEquals(backendServiceApi().create( new BackendServiceOptions.Builder("jclouds-test", healthChecks)
.protocol("HTTP") .protocol("HTTP")
.port(80) .port(80)
.timeoutSec(30) .timeoutSec(30)
.healthChecks(healthChecks)), .build()),
new ParseOperationTest().expected(url("/projects"))); new ParseOperationTest().expected(url("/projects")));
assertSent(server, "POST", "/projects/party/global/backendServices", assertSent(server, "POST", "/projects/party/global/backendServices",
@ -75,11 +75,11 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes
+ "myproject/global/httpHealthChecks/jclouds-test"))); + "myproject/global/httpHealthChecks/jclouds-test")));
assertEquals(backendServiceApi().update("jclouds-test", assertEquals(backendServiceApi().update("jclouds-test",
new BackendServiceOptions().name("jclouds-test") new BackendServiceOptions.Builder("jclouds-test", healthChecks)
.protocol("HTTP") .protocol("HTTP")
.port(80) .port(80)
.timeoutSec(30) .timeoutSec(30)
.healthChecks(healthChecks)), .build()),
new ParseOperationTest().expected(url("/projects"))); new ParseOperationTest().expected(url("/projects")));
assertSent(server, "PUT", "/projects/party/global/backendServices/jclouds-test", assertSent(server, "PUT", "/projects/party/global/backendServices/jclouds-test",
stringFromResource("/backend_service_insert.json")); stringFromResource("/backend_service_insert.json"));
@ -92,11 +92,11 @@ public class BackendServiceApiMockTest extends BaseGoogleComputeEngineApiMockTes
+ "myproject/global/httpHealthChecks/jclouds-test"))); + "myproject/global/httpHealthChecks/jclouds-test")));
assertEquals(backendServiceApi().patch("jclouds-test", assertEquals(backendServiceApi().patch("jclouds-test",
new BackendServiceOptions().name("jclouds-test") new BackendServiceOptions.Builder("jclouds-test", healthChecks)
.protocol("HTTP") .protocol("HTTP")
.port(80) .port(80)
.timeoutSec(30) .timeoutSec(30)
.healthChecks(healthChecks)), .build()),
new ParseOperationTest().expected(url("/projects"))); new ParseOperationTest().expected(url("/projects")));
assertSent(server, "PATCH", "/projects/party/global/backendServices/jclouds-test", assertSent(server, "PATCH", "/projects/party/global/backendServices/jclouds-test",
stringFromResource("/backend_service_insert.json")); stringFromResource("/backend_service_insert.json"));

View File

@ -61,8 +61,7 @@ public class GlobalForwardingRuleApiLiveTest extends BaseGoogleComputeEngineApiL
List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(GLOBAL_FORWARDING_RULE_HEALTH_CHECK_NAME)); List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(GLOBAL_FORWARDING_RULE_HEALTH_CHECK_NAME));
BackendServiceOptions b = new BackendServiceOptions().name(GLOBAL_FORWARDING_RULE_BACKEND_SERVICE_NAME) BackendServiceOptions b = new BackendServiceOptions.Builder(GLOBAL_FORWARDING_RULE_BACKEND_SERVICE_NAME, healthChecks).build();
.healthChecks(healthChecks);
assertOperationDoneSuccessfully(api.backendServices() assertOperationDoneSuccessfully(api.backendServices()
.create(b)); .create(b));

View File

@ -52,8 +52,8 @@ public class TargetHttpProxyApiLiveTest extends BaseGoogleComputeEngineApiLiveTe
assertOperationDoneSuccessfully(api.httpHeathChecks().insert(HEALTH_CHECK_NAME)); assertOperationDoneSuccessfully(api.httpHeathChecks().insert(HEALTH_CHECK_NAME));
List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(HEALTH_CHECK_NAME)); List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(HEALTH_CHECK_NAME));
BackendServiceOptions b = new BackendServiceOptions().name(URL_MAP_DEFAULT_SERVICE_NAME) BackendServiceOptions b = new BackendServiceOptions.Builder(URL_MAP_DEFAULT_SERVICE_NAME, healthChecks)
.healthChecks(healthChecks); .build();
assertOperationDoneSuccessfully(api.backendServices().create(b)); assertOperationDoneSuccessfully(api.backendServices().create(b));

View File

@ -54,8 +54,7 @@ public class UrlMapApiLiveTest extends BaseGoogleComputeEngineApiLiveTest {
assertOperationDoneSuccessfully(api.httpHeathChecks().insert(HEALTH_CHECK_NAME)); assertOperationDoneSuccessfully(api.httpHeathChecks().insert(HEALTH_CHECK_NAME));
List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(HEALTH_CHECK_NAME)); List<URI> healthChecks = ImmutableList.of(getHealthCheckUrl(HEALTH_CHECK_NAME));
BackendServiceOptions b = new BackendServiceOptions().name(URL_MAP_BACKEND_SERVICE_NAME) BackendServiceOptions b = new BackendServiceOptions.Builder(URL_MAP_BACKEND_SERVICE_NAME, healthChecks).build();
.healthChecks(healthChecks);
assertOperationDoneSuccessfully(api.backendServices().create(b)); assertOperationDoneSuccessfully(api.backendServices().create(b));
UrlMapOptions map = new UrlMapOptions.Builder().name(URL_MAP_NAME).description("simple url map") UrlMapOptions map = new UrlMapOptions.Builder().name(URL_MAP_NAME).description("simple url map")