diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeApi.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeApi.java index 258f4895d6..f9f05cc8f7 100644 --- a/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeApi.java +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeApi.java @@ -22,6 +22,7 @@ import com.google.common.annotations.Beta; import org.jclouds.googlecompute.features.DiskApi; import org.jclouds.googlecompute.features.KernelApi; import org.jclouds.googlecompute.features.MachineTypeApi; +import org.jclouds.googlecompute.features.NetworkApi; import org.jclouds.googlecompute.features.OperationApi; import org.jclouds.googlecompute.features.ProjectApi; import org.jclouds.googlecompute.features.ZoneApi; @@ -68,12 +69,14 @@ public interface GoogleComputeApi { @Path("/projects/{project}") MachineTypeApi getMachineTypeApiForProject(@PathParam("project") String projectName); - /** - * Provides synchronous access to Project features + * Provides synchronous access to Network features + * + * @param projectName the name of the project */ @Delegate - ProjectApi getProjectApi(); + @Path("/projects/{project}") + NetworkApi getNetworkApiForProject(@PathParam("project") String projectName); /** * Provides synchronous access to Operation features @@ -84,6 +87,12 @@ public interface GoogleComputeApi { @Path("/projects/{project}") OperationApi getOperationApiForProject(@PathParam("project") String projectName); + /** + * Provides synchronous access to Project features + */ + @Delegate + ProjectApi getProjectApi(); + /** * Provides synchronous access to Zone features * diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeAsyncApi.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeAsyncApi.java index 16d62ffb21..a11bea8c9b 100644 --- a/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeAsyncApi.java +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/GoogleComputeAsyncApi.java @@ -22,6 +22,7 @@ import com.google.common.annotations.Beta; import org.jclouds.googlecompute.features.DiskAsyncApi; import org.jclouds.googlecompute.features.KernelAsyncApi; import org.jclouds.googlecompute.features.MachineTypeAsyncApi; +import org.jclouds.googlecompute.features.NetworkAsyncApi; import org.jclouds.googlecompute.features.OperationAsyncApi; import org.jclouds.googlecompute.features.ProjectAsyncApi; import org.jclouds.googlecompute.features.ZoneAsyncApi; @@ -54,8 +55,6 @@ public interface GoogleComputeAsyncApi { * * @param projectName the name of the project */ - @Delegate - @Path("/projects/{project}") KernelAsyncApi getKernelApiForProject(@PathParam("project") String projectName); /** @@ -68,10 +67,13 @@ public interface GoogleComputeAsyncApi { MachineTypeAsyncApi getMachineTypeApiForProject(@PathParam("project") String projectName); /** - * Provides asynchronous access to Project features + * Provides asynchronous access to Network features + * + * @param projectName the name of the project */ @Delegate - ProjectAsyncApi getProjectApi(); + @Path("/projects/{project}") + NetworkAsyncApi getNetworkApiForProject(@PathParam("project") String projectName); /** * Provides asynchronous access to Operation features @@ -82,6 +84,12 @@ public interface GoogleComputeAsyncApi { @Path("/projects/{project}") OperationAsyncApi getOperationApiForProject(@PathParam("project") String projectName); + /** + * Provides asynchronous access to Project features + */ + @Delegate + ProjectAsyncApi getProjectApi(); + /** * Provides asynchronous access to Zone features * diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/config/GoogleComputeRestClientModule.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/config/GoogleComputeRestClientModule.java index 126f5c4982..e7f3213baa 100644 --- a/labs/google-compute/src/main/java/org/jclouds/googlecompute/config/GoogleComputeRestClientModule.java +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/config/GoogleComputeRestClientModule.java @@ -33,6 +33,8 @@ import org.jclouds.googlecompute.features.KernelApi; import org.jclouds.googlecompute.features.KernelAsyncApi; import org.jclouds.googlecompute.features.MachineTypeApi; import org.jclouds.googlecompute.features.MachineTypeAsyncApi; +import org.jclouds.googlecompute.features.NetworkApi; +import org.jclouds.googlecompute.features.NetworkAsyncApi; import org.jclouds.googlecompute.features.OperationApi; import org.jclouds.googlecompute.features.OperationAsyncApi; import org.jclouds.googlecompute.features.ProjectApi; @@ -67,6 +69,7 @@ public class GoogleComputeRestClientModule extends RestClientModule + */ +@Beta +public final class Network extends Resource { + + private final String IPv4Range; + private final Optional gatewayIPv4; + + @ConstructorProperties({ + "id", "creationTimestamp", "selfLink", "name", "description", "IPv4Range", + "gatewayIPv4" + }) + protected Network(String id, Date creationTimestamp, URI selfLink, String name, String description, + String IPv4Range, String gatewayIPv4) { + super(Kind.NETWORK, checkNotNull(id, "id of %s", name), fromNullable(creationTimestamp), checkNotNull(selfLink, + "selfLink of %s", name), checkNotNull(name, "name"), fromNullable(description)); + this.IPv4Range = checkNotNull(IPv4Range); + this.gatewayIPv4 = fromNullable(gatewayIPv4); + } + + /** + * @return Required; The range of internal addresses that are legal on this network. This range is a CIDR + * specification, for example: 192.168.0.0/16. + */ + public String getIPv4Range() { + return IPv4Range; + } + + /** + * This must be within the range specified by IPv4Range, and is typically the first usable address in that range. + * If not specified, the default value is the first usable address in IPv4Range. + * + * @return an optional address that is used for default routing to other networks. + */ + public Optional getGatewayIPv4() { + return gatewayIPv4; + } + + /** + * {@inheritDoc} + */ + protected Objects.ToStringHelper string() { + return super.string() + .omitNullValues() + .add("IPv4Range", IPv4Range) + .add("gatewayIPv4", gatewayIPv4.orNull()); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return string().toString(); + } + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder().fromNetwork(this); + } + + public static final class Builder extends Resource.Builder { + + private String IPv4Range; + private String gatewayIPv4; + + /** + * @see Network#getIPv4Range() + */ + public Builder IPv4Range(String IPv4Range) { + this.IPv4Range = IPv4Range; + return this; + } + + /** + * @see Network#getGatewayIPv4() + */ + public Builder gatewayIPv4(String gatewayIPv4) { + this.gatewayIPv4 = gatewayIPv4; + return this; + } + + @Override + protected Builder self() { + return this; + } + + public Network build() { + return new Network(super.id, super.creationTimestamp, super.selfLink, super.name, + super.description, IPv4Range, gatewayIPv4); + } + + public Builder fromNetwork(Network in) { + return super.fromResource(in); + } + } + +} diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/NetworkApi.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/NetworkApi.java new file mode 100644 index 0000000000..c1d717c842 --- /dev/null +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/NetworkApi.java @@ -0,0 +1,113 @@ +/* + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.googlecompute.features; + +import org.jclouds.collect.PagedIterable; +import org.jclouds.googlecompute.domain.ListPage; +import org.jclouds.googlecompute.domain.Network; +import org.jclouds.googlecompute.domain.Operation; +import org.jclouds.googlecompute.options.ListOptions; +import org.jclouds.javax.annotation.Nullable; + +/** + * Provides synchronous access to Networks via their REST API. + * + * @author David Alves + * @see NetworkAsyncApi + * @see + */ +public interface NetworkApi { + + /** + * Returns the specified persistent network resource. + * + * @param networkName name of the persistent network resource to return. + * @return a Network resource. + */ + Network get(String networkName); + + /** + * Creates a persistent network resource in the specified project with the specified range. + * + * @param networkName the network name + * @param IPv4Range the range of the network to be inserted. + * @return an Operation resource. To check on the status of an operation, poll the Operations resource returned to + * you, and look for the status field. + */ + Operation createInIPv4Range(String networkName, String IPv4Range); + + /** + * Creates a persistent network resource in the specified project with the specified range and specified gateway. + * + * @param networkName the network name + * @param IPv4Range the range of the network to be inserted. + * @param gatewayIPv4 the range of the network to be inserted. + * @return an Operation resource. To check on the status of an operation, poll the Operations resource returned to + * you, and look for the status field. + */ + Operation createInIPv4RangeWithGateway(String networkName, String IPv4Range, String gatewayIPv4); + + /** + * Deletes the specified persistent network resource. + * + * @param networkName name of the persistent network resource to delete. + * @return an Operation resource. To check on the status of an operation, poll the Operations resource returned to + * you, and look for the status field. + */ + Operation delete(String networkName); + + /** + * @see NetworkApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + ListPage listFirstPage(); + + /** + * @see NetworkApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + ListPage listAtMarker(@Nullable String marker); + + /** + * Retrieves the list of persistent network resources contained within the specified project. + * By default the list as a maximum size of 100, if no options are provided or ListOptions#getMaxResults() has not + * been set. + * + * + * @param marker marks the beginning of the next list page + * @param listOptions listing options + * @return a page of the list + * @see ListOptions + * @see org.jclouds.googlecompute.domain.ListPage + */ + ListPage listAtMarker(@Nullable String marker, ListOptions listOptions); + + /** + * @see NetworkApi#list(org.jclouds.googlecompute.options.ListOptions) + */ + PagedIterable list(); + + /** + * A paged version of NetworkApi#list() + * + * @return a Paged, Fluent Iterable that is able to fetch additional pages when required + * @see PagedIterable + * @see NetworkApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + PagedIterable list(@Nullable ListOptions listOptions); +} diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/NetworkAsyncApi.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/NetworkAsyncApi.java new file mode 100644 index 0000000000..2c73ac2743 --- /dev/null +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/NetworkAsyncApi.java @@ -0,0 +1,169 @@ +/* + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.googlecompute.features; + +import com.google.common.util.concurrent.ListenableFuture; +import org.jclouds.collect.PagedIterable; +import org.jclouds.googlecompute.domain.ListPage; +import org.jclouds.googlecompute.domain.Network; +import org.jclouds.googlecompute.domain.Operation; +import org.jclouds.googlecompute.functions.internal.ParseNetworks; +import org.jclouds.googlecompute.options.ListOptions; +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.oauth.v2.config.OAuthScopes; +import org.jclouds.oauth.v2.filters.OAuthAuthenticator; +import org.jclouds.rest.annotations.Fallback; +import org.jclouds.rest.annotations.MapBinder; +import org.jclouds.rest.annotations.PayloadParam; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.ResponseParser; +import org.jclouds.rest.annotations.SkipEncoding; +import org.jclouds.rest.annotations.Transform; +import org.jclouds.rest.binders.BindToJsonPayload; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import static org.jclouds.Fallbacks.EmptyIterableWithMarkerOnNotFoundOr404; +import static org.jclouds.Fallbacks.EmptyPagedIterableOnNotFoundOr404; +import static org.jclouds.Fallbacks.NullOnNotFoundOr404; +import static org.jclouds.googlecompute.GoogleComputeConstants.COMPUTE_READONLY_SCOPE; +import static org.jclouds.googlecompute.GoogleComputeConstants.COMPUTE_SCOPE; + +/** + * Provides asynchronous access to Networks via their REST API. + * + * @author David Alves + * @see NetworkApi + */ +@SkipEncoding({'/', '='}) +@RequestFilters(OAuthAuthenticator.class) +public interface NetworkAsyncApi { + + /** + * @see NetworkApi#get(String) + */ + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/networks/{network}") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @Fallback(NullOnNotFoundOr404.class) + ListenableFuture get(@PathParam("network") String networkName); + + /** + * @see NetworkApi#createInIPv4Range(String, String) + */ + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Path("/networks") + @OAuthScopes({COMPUTE_SCOPE}) + @MapBinder(BindToJsonPayload.class) + ListenableFuture createInIPv4Range(@PayloadParam("name") String networkName, + @PayloadParam("IPv4Range") String IPv4Range); + + /** + * @see NetworkApi#createInIPv4RangeWithGateway(String, String, String) + */ + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Path("/networks") + @OAuthScopes({COMPUTE_SCOPE}) + @MapBinder(BindToJsonPayload.class) + ListenableFuture createInIPv4RangeWithGateway(@PayloadParam("name") String networkName, + @PayloadParam("IPv4Range") String IPv4Range, + @PayloadParam("gatewayIPv4") String gatewayIPv4); + + /** + * @see NetworkApi#delete(String) + */ + @DELETE + @Consumes(MediaType.APPLICATION_JSON) + @Path("/networks/{network}") + @OAuthScopes(COMPUTE_SCOPE) + @Fallback(NullOnNotFoundOr404.class) + ListenableFuture delete(@PathParam("network") String networkName); + + /** + * @see NetworkApi#listFirstPage() + */ + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/networks") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseNetworks.class) + @Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class) + ListenableFuture> listFirstPage(); + + /** + * @see NetworkApi#listAtMarker(String) + */ + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/networks") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseNetworks.class) + @Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class) + ListenableFuture> listAtMarker(@QueryParam("pageToken") @Nullable String marker); + + /** + * @see NetworkApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/networks") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseNetworks.class) + @Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class) + ListenableFuture> listAtMarker(@QueryParam("pageToken") @Nullable String marker, + ListOptions options); + + /** + * @see NetworkApi#list() + */ + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/networks") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseNetworks.class) + @Transform(ParseNetworks.ToPagedIterable.class) + @Fallback(EmptyPagedIterableOnNotFoundOr404.class) + ListenableFuture> list(); + + /** + * @see NetworkApi#list(org.jclouds.googlecompute.options.ListOptions) + */ + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/networks") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseNetworks.class) + @Transform(ParseNetworks.ToPagedIterable.class) + @Fallback(EmptyPagedIterableOnNotFoundOr404.class) + ListenableFuture> list(ListOptions options); +} diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/ParseNetworks.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/ParseNetworks.java new file mode 100644 index 0000000000..53c643273f --- /dev/null +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/ParseNetworks.java @@ -0,0 +1,68 @@ +/* + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.googlecompute.functions.internal; + +import com.google.common.base.Function; +import com.google.inject.TypeLiteral; +import org.jclouds.collect.IterableWithMarker; +import org.jclouds.googlecompute.GoogleComputeApi; +import org.jclouds.googlecompute.domain.ListPage; +import org.jclouds.googlecompute.domain.Network; +import org.jclouds.googlecompute.options.ListOptions; +import org.jclouds.http.functions.ParseJson; +import org.jclouds.json.Json; + +import javax.inject.Inject; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author David Alves + */ +public class ParseNetworks extends ParseJson> { + + @Inject + public ParseNetworks(Json json) { + super(json, new TypeLiteral>() {}); + } + + public static class ToPagedIterable extends BaseToPagedIterable { + + private final GoogleComputeApi api; + + @Inject + protected ToPagedIterable(GoogleComputeApi api) { + this.api = checkNotNull(api, "api"); + } + + @Override + protected Function> fetchNextPage(final String projectName, + final String marker, + final ListOptions options) { + return new Function>() { + + @Override + public IterableWithMarker apply(Object input) { + return api.getNetworkApiForProject(projectName).listAtMarker(marker, options); + } + }; + } + } +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/NetworkApiExpectTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/NetworkApiExpectTest.java new file mode 100644 index 0000000000..dd57d1eba0 --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/NetworkApiExpectTest.java @@ -0,0 +1,169 @@ +/* + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.googlecompute.features; + +import org.jclouds.googlecompute.internal.BaseGoogleComputeApiExpectTest; +import org.jclouds.googlecompute.parse.ParseNetworkListTest; +import org.jclouds.googlecompute.parse.ParseNetworkTest; +import org.jclouds.googlecompute.parse.ParseOperationTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +import javax.ws.rs.core.MediaType; + +import static org.jclouds.googlecompute.GoogleComputeConstants.COMPUTE_READONLY_SCOPE; +import static org.jclouds.googlecompute.GoogleComputeConstants.COMPUTE_SCOPE; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.AssertJUnit.assertNull; + +/** + * @author David Alves + */ +@Test(groups = "unit") +public class NetworkApiExpectTest extends BaseGoogleComputeApiExpectTest { + + public void testGetNetworkResponseIs2xx() throws Exception { + HttpRequest get = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis.com/compute/v1beta13/projects/myproject/networks/default") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/network_get.json")).build(); + + NetworkApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, get, operationResponse).getNetworkApiForProject("myproject"); + + assertEquals(api.get("default"), + new ParseNetworkTest().expected()); + } + + public void testGetNetworkResponseIs4xx() throws Exception { + HttpRequest get = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis.com/compute/v1beta13/projects/myproject/networks/default") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(404).build(); + + NetworkApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, get, operationResponse).getNetworkApiForProject("myproject"); + + assertNull(api.get("default")); + } + + public void testInsertNetworkResponseIs2xx() { + HttpRequest insert = HttpRequest + .builder() + .method("POST") + .endpoint("https://www.googleapis.com/compute/v1beta13/projects/myproject/networks") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN) + .payload(payloadFromResourceWithContentType("/network_insert.json", MediaType.APPLICATION_JSON)) + .build(); + + HttpResponse insertNetworkResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/operation.json")).build(); + + NetworkApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), + TOKEN_RESPONSE, insert, + insertNetworkResponse).getNetworkApiForProject("myproject"); + + assertEquals(api.createInIPv4Range("test-network", "10.0.1.0/8"), new ParseOperationTest().expected()); + } + + public void testDeleteNetworkResponseIs2xx() { + HttpRequest delete = HttpRequest + .builder() + .method("DELETE") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/networks/default") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse deleteResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/operation.json")).build(); + + NetworkApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), + TOKEN_RESPONSE, delete, deleteResponse).getNetworkApiForProject("myproject"); + + assertEquals(api.delete("default"), + new ParseOperationTest().expected()); + } + + public void testDeleteNetworkResponseIs4xx() { + HttpRequest delete = HttpRequest + .builder() + .method("DELETE") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/networks/default") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse deleteResponse = HttpResponse.builder().statusCode(404).build(); + + NetworkApi api = requestsSendResponses(requestForScopes(COMPUTE_SCOPE), + TOKEN_RESPONSE, delete, deleteResponse).getNetworkApiForProject("myproject"); + + assertNull(api.delete("default")); + } + + public void testListNetworksResponseIs2xx() { + HttpRequest list = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/networks") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/network_list.json")).build(); + + NetworkApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, list, operationResponse).getNetworkApiForProject("myproject"); + + assertEquals(api.listFirstPage().toString(), + new ParseNetworkListTest().expected().toString()); + } + + public void testListNetworksResponseIs4xx() { + HttpRequest list = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/networks") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(404).build(); + + NetworkApi api = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, list, operationResponse).getNetworkApiForProject("myproject"); + + assertTrue(api.list().concat().isEmpty()); + } +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/NetworkApiLiveTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/NetworkApiLiveTest.java new file mode 100644 index 0000000000..f55ebb3292 --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/NetworkApiLiveTest.java @@ -0,0 +1,94 @@ +/* + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.googlecompute.features; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import org.jclouds.collect.PagedIterable; +import org.jclouds.googlecompute.domain.Network; +import org.jclouds.googlecompute.internal.BaseGoogleComputeApiLiveTest; +import org.jclouds.googlecompute.options.ListOptions; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.util.List; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +/** + * @author David Alves + */ +public class NetworkApiLiveTest extends BaseGoogleComputeApiLiveTest { + + private static final String NETWORK_NAME = "network-api-live-test-network"; + private static final String IPV4_RANGE = "10.0.0.0/8"; + private static final int TIME_WAIT = 10; + + @BeforeClass(groups = {"integration", "live"}) + public void setupContext() { + super.setupContext(); + } + + private NetworkApi api() { + return context.getApi().getNetworkApiForProject(getUserProject()); + } + + @Test(groups = "live") + public void testInsertNetwork() { + + assertOperationDoneSucessfully(api().createInIPv4Range(NETWORK_NAME, IPV4_RANGE), TIME_WAIT); + + } + + @Test(groups = "live", dependsOnMethods = "testInsertNetwork") + public void testGetNetwork() { + + Network network = api().get(NETWORK_NAME); + assertNotNull(network); + assertNetworkEquals(network); + } + + @Test(groups = "live", dependsOnMethods = "testGetNetwork") + public void testListNetwork() { + + PagedIterable networks = api().list(new ListOptions.Builder() + .filter("name eq " + NETWORK_NAME)); + + List networksAsList = Lists.newArrayList(networks.concat()); + + assertEquals(networksAsList.size(), 1); + + assertNetworkEquals(Iterables.getOnlyElement(networksAsList)); + + } + + @Test(groups = "live", dependsOnMethods = "testListNetwork") + public void testDeleteNetwork() { + + assertOperationDoneSucessfully(api().delete(NETWORK_NAME), TIME_WAIT); + } + + private void assertNetworkEquals(Network result) { + assertEquals(result.getName(), NETWORK_NAME); + assertEquals(result.getIPv4Range(), IPV4_RANGE); + } + +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseNetworkListTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseNetworkListTest.java new file mode 100644 index 0000000000..b14c6980c3 --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseNetworkListTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.googlecompute.parse; + +import com.google.common.collect.ImmutableSet; +import org.jclouds.googlecompute.domain.ListPage; +import org.jclouds.googlecompute.domain.Network; +import org.jclouds.googlecompute.domain.Resource; +import org.jclouds.googlecompute.internal.BaseGoogleComputeParseTest; + +import javax.ws.rs.Consumes; +import javax.ws.rs.core.MediaType; +import java.net.URI; + +/** + * @author David Alves + */ +public class ParseNetworkListTest extends BaseGoogleComputeParseTest> { + + @Override + public String resource() { + return "/network_list.json"; + } + + @Override + @Consumes(MediaType.APPLICATION_JSON) + public ListPage expected() { + return ListPage.builder() + .kind(Resource.Kind.NETWORK_LIST) + .id("projects/myproject/networks") + .selfLink(URI.create("https://www.googleapis.com/compute/v1beta13/projects/myproject/networks")) + .items(ImmutableSet.of(new ParseNetworkTest().expected())) + .build(); + + } +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseNetworkTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseNetworkTest.java new file mode 100644 index 0000000000..0adbbd3a55 --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseNetworkTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.googlecompute.parse; + +import org.jclouds.date.internal.SimpleDateFormatDateService; +import org.jclouds.googlecompute.domain.Network; +import org.jclouds.googlecompute.internal.BaseGoogleComputeParseTest; + +import javax.ws.rs.Consumes; +import javax.ws.rs.core.MediaType; +import java.net.URI; + +/** + * @author David Alves + */ +public class ParseNetworkTest extends BaseGoogleComputeParseTest { + + @Override + public String resource() { + return "/network_get.json"; + } + + @Override + @Consumes(MediaType.APPLICATION_JSON) + public Network expected() { + return Network.builder() + .id("13024414170909937976") + .creationTimestamp(new SimpleDateFormatDateService().iso8601DateParse("2012-10-24T20:13:19.967")) + .selfLink(URI.create("https://www.googleapis.com/compute/v1beta13/projects/myproject/networks/default")) + .name("default") + .description("Default network for the project") + .IPv4Range("10.240.0.0/16") + .gatewayIPv4("10.240.0.1") + .build(); + } +} diff --git a/labs/google-compute/src/test/resources/network_get.json b/labs/google-compute/src/test/resources/network_get.json new file mode 100644 index 0000000000..bdb554a39b --- /dev/null +++ b/labs/google-compute/src/test/resources/network_get.json @@ -0,0 +1,10 @@ +{ + "kind": "compute#network", + "id": "13024414170909937976", + "creationTimestamp": "2012-10-24T20:13:19.967", + "selfLink": "https://www.googleapis.com/compute/v1beta13/projects/myproject/networks/default", + "name": "default", + "description": "Default network for the project", + "IPv4Range": "10.240.0.0/16", + "gatewayIPv4": "10.240.0.1" +} \ No newline at end of file diff --git a/labs/google-compute/src/test/resources/network_insert.json b/labs/google-compute/src/test/resources/network_insert.json new file mode 100644 index 0000000000..6da7cedc81 --- /dev/null +++ b/labs/google-compute/src/test/resources/network_insert.json @@ -0,0 +1 @@ +{"name":"test-network","IPv4Range":"10.0.1.0/8"} \ No newline at end of file diff --git a/labs/google-compute/src/test/resources/network_list.json b/labs/google-compute/src/test/resources/network_list.json new file mode 100644 index 0000000000..299c5c935c --- /dev/null +++ b/labs/google-compute/src/test/resources/network_list.json @@ -0,0 +1,18 @@ +{ + "kind": "compute#networkList", + "id": "projects/myproject/networks", + "selfLink": "https://www.googleapis.com/compute/v1beta13/projects/myproject/networks", + "items": [ + { + + "kind": "compute#network", + "id": "13024414170909937976", + "creationTimestamp": "2012-10-24T20:13:19.967", + "selfLink": "https://www.googleapis.com/compute/v1beta13/projects/myproject/networks/default", + "name": "default", + "description": "Default network for the project", + "IPv4Range": "10.240.0.0/16", + "gatewayIPv4": "10.240.0.1" + } + ] +} \ No newline at end of file