From 9ec0bca204719d92680ec25d15e2e6f3f71b2e39 Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Sun, 13 Jan 2013 13:39:42 -0600 Subject: [PATCH] gce - machine types api --- .../googlecompute/GoogleComputeApi.java | 11 + .../googlecompute/GoogleComputeAsyncApi.java | 10 + .../config/GoogleComputeRestClientModule.java | 3 + .../googlecompute/domain/MachineType.java | 339 ++++++++++++++++++ .../features/MachineTypeApi.java | 83 +++++ .../features/MachineTypeAsyncApi.java | 122 +++++++ .../functions/internal/ParseMachineTypes.java | 68 ++++ .../features/MachineTypeApiExpectTest.java | 112 ++++++ .../features/MachineTypeApiLiveTest.java | 78 ++++ .../parse/ParseMachineTypeListTest.java | 83 +++++ .../parse/ParseMachineTypeTest.java | 61 ++++ .../src/test/resources/machinetype.json | 21 ++ .../src/test/resources/machinetype_list.json | 41 +++ 13 files changed, 1032 insertions(+) create mode 100644 labs/google-compute/src/main/java/org/jclouds/googlecompute/domain/MachineType.java create mode 100644 labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeApi.java create mode 100644 labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeAsyncApi.java create mode 100644 labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/ParseMachineTypes.java create mode 100644 labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiExpectTest.java create mode 100644 labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiLiveTest.java create mode 100644 labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeListTest.java create mode 100644 labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeTest.java create mode 100644 labs/google-compute/src/test/resources/machinetype.json create mode 100644 labs/google-compute/src/test/resources/machinetype_list.json 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 88c149b9cf..258f4895d6 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 @@ -21,6 +21,7 @@ package org.jclouds.googlecompute; 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.OperationApi; import org.jclouds.googlecompute.features.ProjectApi; import org.jclouds.googlecompute.features.ZoneApi; @@ -58,6 +59,16 @@ public interface GoogleComputeApi { @Path("/projects/{project}") KernelApi getKernelApiForProject(@PathParam("project") String projectName); + /** + * Provides synchronous access to MachineType features + * + * @param projectName the name of the project + */ + @Delegate + @Path("/projects/{project}") + MachineTypeApi getMachineTypeApiForProject(@PathParam("project") String projectName); + + /** * Provides synchronous access to Project 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 0578eb8bea..16d62ffb21 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 @@ -21,6 +21,7 @@ package org.jclouds.googlecompute; 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.OperationAsyncApi; import org.jclouds.googlecompute.features.ProjectAsyncApi; import org.jclouds.googlecompute.features.ZoneAsyncApi; @@ -57,6 +58,15 @@ public interface GoogleComputeAsyncApi { @Path("/projects/{project}") KernelAsyncApi getKernelApiForProject(@PathParam("project") String projectName); + /** + * Provides asynchronous access to MachineType features + * + * @param projectName the name of the project + */ + @Delegate + @Path("/projects/{project}") + MachineTypeAsyncApi getMachineTypeApiForProject(@PathParam("project") String projectName); + /** * Provides asynchronous access to Project 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 2ddf9e69cf..126f5c4982 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 @@ -31,6 +31,8 @@ import org.jclouds.googlecompute.features.DiskApi; import org.jclouds.googlecompute.features.DiskAsyncApi; 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.OperationApi; import org.jclouds.googlecompute.features.OperationAsyncApi; import org.jclouds.googlecompute.features.ProjectApi; @@ -64,6 +66,7 @@ public class GoogleComputeRestClientModule extends RestClientModule, Class> DELEGATE_MAP = ImmutableMap., Class>builder() .put(DiskApi.class, DiskAsyncApi.class) .put(KernelApi.class, KernelAsyncApi.class) + .put(MachineTypeApi.class, MachineTypeAsyncApi.class) .put(OperationApi.class, OperationAsyncApi.class) .put(ProjectApi.class, ProjectAsyncApi.class) .put(ZoneApi.class, ZoneAsyncApi.class) diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/domain/MachineType.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/domain/MachineType.java new file mode 100644 index 0000000000..7a7a1e79ee --- /dev/null +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/domain/MachineType.java @@ -0,0 +1,339 @@ +/* + * 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.domain; + +import com.google.common.annotations.Beta; +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; + +import java.beans.ConstructorProperties; +import java.net.URI; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Objects.toStringHelper; +import static com.google.common.base.Optional.fromNullable; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Represents a machine type used to host an instance. + * + * @author David Alves + * @see + */ +@Beta +public final class MachineType extends Resource { + + private final Integer guestCpus; + private final Integer memoryMb; + private final Integer imageSpaceGb; + private final List ephemeralDisks; + private final Integer maximumPersistentDisks; + private final Long maximumPersistentDisksSizeGb; + private final Set availableZone; + + @ConstructorProperties({ + "id", "creationTimestamp", "selfLink", "name", "description", "guestCpus", "memoryMb", + "imageSpaceGb", "ephemeralDisks", "maximumPersistentDisks", "maximumPersistentDisksSizeGb", "availableZone" + }) + private MachineType(String id, Date creationTimestamp, URI selfLink, String name, String description, + int guestCpus, int memoryMb, int imageSpaceGb, List ephemeralDisks, + int maximumPersistentDisks, long maximumPersistentDisksSizeGb, Set availableZone) { + super(Kind.MACHINE_TYPE, checkNotNull(id, "id of %s", name), fromNullable(creationTimestamp), + checkNotNull(selfLink, "selfLink of %s", name), checkNotNull(name, "name"), fromNullable(description)); + this.guestCpus = checkNotNull(guestCpus, "guestCpus of %s", name); + this.memoryMb = checkNotNull(memoryMb, "memoryMb of %s", name); + this.imageSpaceGb = checkNotNull(imageSpaceGb, "imageSpaceGb of %s", name); + this.ephemeralDisks = ephemeralDisks == null ? ImmutableList.of() : ephemeralDisks; + this.maximumPersistentDisks = checkNotNull(maximumPersistentDisks, "maximumPersistentDisks of %s", name); + this.maximumPersistentDisksSizeGb = maximumPersistentDisksSizeGb; + this.availableZone = availableZone == null ? ImmutableSet.of() : availableZone; + } + + /** + * @return count of CPUs exposed to the instance. + */ + public int getGuestCpus() { + return guestCpus; + } + + /** + * @return physical memory assigned to the instance, defined in MB. + */ + public int getMemoryMb() { + return memoryMb; + } + + /** + * @return space allotted for the image, defined in GB. + */ + public int getImageSpaceGb() { + return imageSpaceGb; + } + + /** + * @return extended ephemeral disks assigned to the instance. + */ + public List getEphemeralDisks() { + return ephemeralDisks; + } + + /** + * @return maximum persistent disks allowed. + */ + public int getMaximumPersistentDisks() { + return maximumPersistentDisks; + } + + /** + * @return maximum total persistent disks size (GB) allowed. + */ + public long getMaximumPersistentDisksSizeGb() { + return maximumPersistentDisksSizeGb; + } + + /** + * @return the zones that this machine type can run in. + */ + public Set getAvailableZone() { + return availableZone; + } + + /** + * {@inheritDoc} + */ + protected Objects.ToStringHelper string() { + return super.string() + .add("guestCpus", guestCpus) + .add("memoryMb", memoryMb) + .add("imageSpaceGb", imageSpaceGb) + .add("ephemeralDisks", ephemeralDisks) + .add("maximumPersistentDisks", maximumPersistentDisks) + .add("maximumPersistentDisksSizeGb", maximumPersistentDisksSizeGb) + .add("availableZone", availableZone); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return string().toString(); + } + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder().fromMachineType(this); + } + + public static final class Builder extends Resource.Builder { + + private Integer guestCpus; + private Integer memoryMb; + private Integer imageSpaceGb; + private ImmutableList.Builder ephemeralDisks = ImmutableList.builder(); + private Integer maximumPersistentDisks; + private Long maximumPersistentDisksSizeGb; + private ImmutableSet.Builder availableZone = ImmutableSet.builder(); + + /** + * @see MachineType#getGuestCpus() + */ + public Builder guestCpus(int guesCpus) { + this.guestCpus = guesCpus; + return this; + } + + /** + * @see MachineType#getMemoryMb() + */ + public Builder memoryMb(int memoryMb) { + this.memoryMb = memoryMb; + return this; + } + + /** + * @see MachineType#getImageSpaceGb() + */ + public Builder imageSpaceGb(int imageSpaceGb) { + this.imageSpaceGb = imageSpaceGb; + return this; + } + + /** + * @see MachineType#getEphemeralDisks() + */ + public Builder addEphemeralDisk(int diskGb) { + this.ephemeralDisks.add(EphemeralDisk.builder().diskGb(diskGb).build()); + return this; + } + + /** + * @see MachineType#getEphemeralDisks() + */ + public Builder ephemeralDisks(List ephemeralDisks) { + this.ephemeralDisks.addAll(ephemeralDisks); + return this; + } + + /** + * @see MachineType#getMaximumPersistentDisks() + */ + public Builder maximumPersistentDisks(int maximumPersistentDisks) { + this.maximumPersistentDisks = maximumPersistentDisks; + return this; + } + + /** + * @see MachineType#getMaximumPersistentDisksSizeGb() + */ + public Builder maximumPersistentDisksSizeGb(long maximumPersistentDisksSizeGb) { + this.maximumPersistentDisksSizeGb = maximumPersistentDisksSizeGb; + return this; + } + + /** + * @see MachineType#getAvailableZone() + */ + public Builder addAvailableZone(String availableZone) { + this.availableZone.add(availableZone); + return this; + } + + /** + * @see MachineType#getAvailableZone() + */ + public Builder availableZones(Set availableZone) { + this.availableZone.addAll(availableZone); + return this; + } + + @Override + protected Builder self() { + return this; + } + + public MachineType build() { + return new MachineType(id, creationTimestamp, selfLink, name, description, guestCpus, memoryMb, + imageSpaceGb, ephemeralDisks.build(), maximumPersistentDisks, maximumPersistentDisksSizeGb, + availableZone.build()); + } + + + public Builder fromMachineType(MachineType in) { + return super.fromResource(in).memoryMb(in.getMemoryMb()).imageSpaceGb(in.getImageSpaceGb()).ephemeralDisks(in + .getEphemeralDisks()).maximumPersistentDisks(in.getMaximumPersistentDisks()) + .maximumPersistentDisksSizeGb(in.getMaximumPersistentDisksSizeGb()).availableZones(in + .getAvailableZone()); + } + } + + /** + * An ephemeral disk of a MachineType + */ + public static final class EphemeralDisk { + + private final int diskGb; + + @ConstructorProperties({ + "diskGb" + }) + private EphemeralDisk(int diskGb) { + this.diskGb = diskGb; + } + + /** + * @return size of the ephemeral disk, defined in GB. + */ + public int getDiskGb() { + return diskGb; + } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + return Objects.hashCode(diskGb); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + EphemeralDisk that = EphemeralDisk.class.cast(obj); + return equal(this.diskGb, that.diskGb); + } + + /** + * {@inheritDoc} + */ + protected Objects.ToStringHelper string() { + return toStringHelper(this) + .add("diskGb", diskGb); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return string().toString(); + } + + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return builder().fromEphemeralDisk(this); + } + + public static class Builder { + + private int diskGb; + + /** + * @see org.jclouds.googlecompute.domain.MachineType.EphemeralDisk#getDiskGb() + */ + public Builder diskGb(int diskGb) { + this.diskGb = diskGb; + return this; + } + + public EphemeralDisk build() { + return new EphemeralDisk(diskGb); + } + + public Builder fromEphemeralDisk(EphemeralDisk in) { + return new Builder().diskGb(in.getDiskGb()); + } + } + } +} diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeApi.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeApi.java new file mode 100644 index 0000000000..fb34c09734 --- /dev/null +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeApi.java @@ -0,0 +1,83 @@ +/* + * 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.MachineType; +import org.jclouds.googlecompute.options.ListOptions; +import org.jclouds.javax.annotation.Nullable; + +/** + * Provides synchronous access to MachineTypes via their REST API. + *

+ * + * @author David Alves + * @see + */ +public interface MachineTypeApi { + + /** + * Returns the specified machine type resource + * + * @param machineTypeName name of the machine type resource to return. + * @return If successful, this method returns a MachineType resource + */ + MachineType get(String machineTypeName); + + /** + * @see MachineTypeApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + ListPage listFirstPage(); + + /** + * @see MachineTypeApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + ListPage listAtMarker(@Nullable String marker); + + /** + * Retrieves the list of machine type resources available to 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 MachineTypeApi#list(org.jclouds.googlecompute.options.ListOptions) + */ + PagedIterable list(); + + /** + * A paged version of MachineTypeApi#list() + * + * @return a Paged, Fluent Iterable that is able to fetch additional pages when required + * @see PagedIterable + * @see MachineTypeApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + PagedIterable list(ListOptions listOptions); + +} diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeAsyncApi.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeAsyncApi.java new file mode 100644 index 0000000000..2de3ca8c1d --- /dev/null +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/features/MachineTypeAsyncApi.java @@ -0,0 +1,122 @@ +/* + * 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.MachineType; +import org.jclouds.googlecompute.functions.internal.ParseMachineTypes; +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.RequestFilters; +import org.jclouds.rest.annotations.ResponseParser; +import org.jclouds.rest.annotations.SkipEncoding; +import org.jclouds.rest.annotations.Transform; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +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; + +/** + * Provides asynchronous access to MachineTypes via their REST API. + * + * @author David Alves + * @see MachineTypeApi + */ +@SkipEncoding({'/', '='}) +@RequestFilters(OAuthAuthenticator.class) +@Consumes(MediaType.APPLICATION_JSON) +public interface MachineTypeAsyncApi { + + /** + * @see MachineTypeApi#get(String) + */ + @GET + @Path("/machineTypes/{machineType}") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @Fallback(NullOnNotFoundOr404.class) + ListenableFuture get(@PathParam("machineType") String machineTypeName); + + /** + * @see MachineTypeApi#listFirstPage() + */ + @GET + @Path("/machineTypes") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseMachineTypes.class) + @Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class) + ListenableFuture> listFirstPage(); + + /** + * @see MachineTypeApi#listAtMarker(String) + */ + @GET + @Path("/machineTypes") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseMachineTypes.class) + @Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class) + ListenableFuture> listAtMarker(@QueryParam("pageToken") @Nullable String marker); + + /** + * @see MachineTypeApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions) + */ + @GET + @Path("/machineTypes") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseMachineTypes.class) + @Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class) + ListenableFuture> listAtMarker(@QueryParam("pageToken") @Nullable String marker, + ListOptions listOptions); + + /** + * @see org.jclouds.googlecompute.features.MachineTypeApi#list() + */ + @GET + @Path("/machineTypes") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseMachineTypes.class) + @Transform(ParseMachineTypes.ToPagedIterable.class) + @Fallback(EmptyPagedIterableOnNotFoundOr404.class) + ListenableFuture> list(); + + /** + * @see MachineTypeApi#list(org.jclouds.googlecompute.options.ListOptions) + */ + @GET + @Path("/machineTypes") + @OAuthScopes(COMPUTE_READONLY_SCOPE) + @ResponseParser(ParseMachineTypes.class) + @Transform(ParseMachineTypes.ToPagedIterable.class) + @Fallback(EmptyPagedIterableOnNotFoundOr404.class) + ListenableFuture> list(ListOptions listOptions); + +} diff --git a/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/ParseMachineTypes.java b/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/ParseMachineTypes.java new file mode 100644 index 0000000000..03b4c35cfa --- /dev/null +++ b/labs/google-compute/src/main/java/org/jclouds/googlecompute/functions/internal/ParseMachineTypes.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.MachineType; +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 ParseMachineTypes extends ParseJson> { + + @Inject + public ParseMachineTypes(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.getMachineTypeApiForProject(projectName).listAtMarker(marker, options); + } + }; + } + } +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiExpectTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiExpectTest.java new file mode 100644 index 0000000000..5818dfb959 --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiExpectTest.java @@ -0,0 +1,112 @@ +/* + * 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.GoogleComputeApi; +import org.jclouds.googlecompute.internal.BaseGoogleComputeExpectTest; +import org.jclouds.googlecompute.parse.ParseMachineTypeListTest; +import org.jclouds.googlecompute.parse.ParseMachineTypeTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +import static org.jclouds.googlecompute.GoogleComputeConstants.COMPUTE_READONLY_SCOPE; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * @author David Alves + */ +@Test(groups = "unit") +public class MachineTypeApiExpectTest extends BaseGoogleComputeExpectTest { + + public void testGetMachineTypeResponseIs2xx() throws Exception { + HttpRequest get = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/machineTypes/n1-standard-1") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/machinetype.json")).build(); + + MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, get, operationResponse).getMachineTypeApiForProject("myproject"); + + assertEquals(machineTypeApi.get("n1-standard-1"), + new ParseMachineTypeTest().expected()); + } + + public void testGetMachineTypeResponseIs4xx() throws Exception { + HttpRequest get = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/machineTypes/n1-standard-1") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(404).build(); + + MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, get, operationResponse).getMachineTypeApiForProject("myproject"); + + assertNull(machineTypeApi.get("n1-standard-1")); + } + + public void testListMachineTypeNoOptionsResponseIs2xx() throws Exception { + HttpRequest list = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/machineTypes") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/machinetype_list.json")).build(); + + MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, list, operationResponse).getMachineTypeApiForProject("myproject"); + + assertEquals(machineTypeApi.listFirstPage().toString(), + new ParseMachineTypeListTest().expected().toString()); + } + + public void testLisOperationWithPaginationOptionsResponseIs4xx() { + HttpRequest list = HttpRequest + .builder() + .method("GET") + .endpoint("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/machineTypes") + .addHeader("Accept", "application/json") + .addHeader("Authorization", "Bearer " + TOKEN).build(); + + HttpResponse operationResponse = HttpResponse.builder().statusCode(404).build(); + + MachineTypeApi machineTypeApi = requestsSendResponses(requestForScopes(COMPUTE_READONLY_SCOPE), + TOKEN_RESPONSE, list, operationResponse).getMachineTypeApiForProject("myproject"); + + assertTrue(machineTypeApi.list().concat().isEmpty()); + } +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiLiveTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiLiveTest.java new file mode 100644 index 0000000000..f88cf517a0 --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/features/MachineTypeApiLiveTest.java @@ -0,0 +1,78 @@ +/* + * 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.IterableWithMarker; +import org.jclouds.collect.PagedIterable; +import org.jclouds.googlecompute.domain.MachineType; +import org.jclouds.googlecompute.internal.BaseGoogleComputeApiLiveTest; +import org.jclouds.googlecompute.options.ListOptions; +import org.testng.annotations.Test; + +import java.util.Iterator; +import java.util.List; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertSame; +import static org.testng.Assert.assertTrue; + +/** + * @author David Alves + */ +public class MachineTypeApiLiveTest extends BaseGoogleComputeApiLiveTest { + + private MachineType machineType; + + private MachineTypeApi api() { + return context.getApi().getMachineTypeApiForProject(getUserProject()); + } + + @Test(groups = "live") + public void testListMachineType() { + + PagedIterable machineTypes = api().list(new ListOptions.Builder() + .maxResults(1)); + + Iterator> pageIterator = machineTypes.iterator(); + assertTrue(pageIterator.hasNext()); + + IterableWithMarker singlePageIterator = pageIterator.next(); + List machineTypeAsList = Lists.newArrayList(singlePageIterator); + + assertSame(machineTypeAsList.size(), 1); + + this.machineType = Iterables.getOnlyElement(machineTypeAsList); + } + + + @Test(groups = "live", dependsOnMethods = "testListMachineType") + public void testGetMachineType() { + MachineType machineType = api().get(this.machineType.getName()); + assertNotNull(machineType); + assertMachineTypeEquals(machineType, this.machineType); + } + + private void assertMachineTypeEquals(MachineType result, MachineType expected) { + assertEquals(result.getName(), expected.getName()); + } +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeListTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeListTest.java new file mode 100644 index 0000000000..d3bc60e1be --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeListTest.java @@ -0,0 +1,83 @@ +/* + * 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.ListPage; +import org.jclouds.googlecompute.domain.MachineType; +import org.jclouds.googlecompute.internal.BaseGoogleComputeParseTest; + +import javax.ws.rs.Consumes; +import javax.ws.rs.core.MediaType; +import java.net.URI; + +import static org.jclouds.googlecompute.domain.Resource.Kind.MACHINE_TYPE_LIST; + +/** + * @author David Alves + */ +public class ParseMachineTypeListTest extends BaseGoogleComputeParseTest> { + + + @Override + public String resource() { + return "/machinetype_list.json"; + } + + @Override + @Consumes(MediaType.APPLICATION_JSON) + public ListPage expected() { + SimpleDateFormatDateService dateService = new SimpleDateFormatDateService(); + return ListPage.builder() + .kind(MACHINE_TYPE_LIST) + .id("projects/myproject/machineTypes") + .selfLink(URI.create("https://www.googleapis.com/compute/v1beta13/projects/myproject/machineTypes")) + .addItem(MachineType.builder() + .id("12907738072351752276") + .creationTimestamp(dateService.iso8601DateParse("2012-06-07T20:48:14.670")) + .selfLink(URI.create("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/machineTypes/n1-standard-1")) + .name("n1-standard-1") + .description("1 vCPU, 3.75 GB RAM, and a 10 GB ephemeral root disk") + .guestCpus(1) + .memoryMb(3840) + .imageSpaceGb(10) + .maximumPersistentDisks(16) + .maximumPersistentDisksSizeGb(128) + .build()) + .addItem(MachineType.builder() + .id("12908560709887590691") + .creationTimestamp(dateService.iso8601DateParse("2012-06-07T20:51:19.936")) + .selfLink(URI.create("https://www.googleapis" + + ".com/compute/v1beta13/projects/myproject/machineTypes/n1-standard-8-d")) + .name("n1-standard-8-d") + .description("8 vCPUs, 30 GB RAM, a 10 GB ephemeral root disk, " + + "and 2 extra 1770 GB ephemeral disks") + .guestCpus(8) + .memoryMb(30720) + .imageSpaceGb(10) + .addEphemeralDisk(1770) + .addEphemeralDisk(1770) + .maximumPersistentDisks(16) + .maximumPersistentDisksSizeGb(1024) + .build()) + .build(); + } +} diff --git a/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeTest.java b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeTest.java new file mode 100644 index 0000000000..6a999f29f7 --- /dev/null +++ b/labs/google-compute/src/test/java/org/jclouds/googlecompute/parse/ParseMachineTypeTest.java @@ -0,0 +1,61 @@ +/* + * 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.MachineType; +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 ParseMachineTypeTest extends BaseGoogleComputeParseTest { + + + @Override + public String resource() { + return "/machinetype.json"; + } + + @Override + @Consumes(MediaType.APPLICATION_JSON) + public MachineType expected() { + SimpleDateFormatDateService dateService = new SimpleDateFormatDateService(); + return MachineType.builder() + .id("12907738072351752276") + .creationTimestamp(dateService.iso8601DateParse("2012-06-07T20:48:14.670")) + .selfLink(URI.create("https://www.googleapis.com/compute/v1beta13/projects/myproject/machineTypes/n1" + + "-standard-1")) + .name("n1-standard-1") + .description("1 vCPU, 3.75 GB RAM, and a 10 GB ephemeral root disk") + .guestCpus(1) + .memoryMb(3840) + .imageSpaceGb(10) + .addEphemeralDisk(1770) + .addEphemeralDisk(1770) + .maximumPersistentDisks(16) + .maximumPersistentDisksSizeGb(128) + .build(); + } +} diff --git a/labs/google-compute/src/test/resources/machinetype.json b/labs/google-compute/src/test/resources/machinetype.json new file mode 100644 index 0000000000..6f225723a7 --- /dev/null +++ b/labs/google-compute/src/test/resources/machinetype.json @@ -0,0 +1,21 @@ +{ + "kind": "compute#machineType", + "id": "12907738072351752276", + "creationTimestamp": "2012-06-07T20:48:14.670", + "selfLink": "https://www.googleapis.com/compute/v1beta13/projects/myproject/machineTypes/n1-standard-1", + "name": "n1-standard-1", + "description": "1 vCPU, 3.75 GB RAM, and a 10 GB ephemeral root disk", + "guestCpus": 1, + "memoryMb": 3840, + "imageSpaceGb": 10, + "ephemeralDisks": [ + { + "diskGb": 1770 + }, + { + "diskGb": 1770 + } + ], + "maximumPersistentDisks": 16, + "maximumPersistentDisksSizeGb": "128" +} \ No newline at end of file diff --git a/labs/google-compute/src/test/resources/machinetype_list.json b/labs/google-compute/src/test/resources/machinetype_list.json new file mode 100644 index 0000000000..14e7318ac9 --- /dev/null +++ b/labs/google-compute/src/test/resources/machinetype_list.json @@ -0,0 +1,41 @@ +{ + "kind": "compute#machineTypeList", + "id": "projects/myproject/machineTypes", + "selfLink": "https://www.googleapis.com/compute/v1beta13/projects/myproject/machineTypes", + "items": [ + { + "kind": "compute#machineType", + "id": "12907738072351752276", + "creationTimestamp": "2012-06-07T20:48:14.670", + "selfLink": "https://www.googleapis.com/compute/v1beta13/projects/myproject/machineTypes/n1-standard-1", + "name": "n1-standard-1", + "description": "1 vCPU, 3.75 GB RAM, and a 10 GB ephemeral root disk", + "guestCpus": 1, + "memoryMb": 3840, + "imageSpaceGb": 10, + "maximumPersistentDisks": 16, + "maximumPersistentDisksSizeGb": "128" + }, + { + "kind": "compute#machineType", + "id": "12908560709887590691", + "creationTimestamp": "2012-06-07T20:51:19.936", + "selfLink": "https://www.googleapis.com/compute/v1beta13/projects/myproject/machineTypes/n1-standard-8-d", + "name": "n1-standard-8-d", + "description": "8 vCPUs, 30 GB RAM, a 10 GB ephemeral root disk, and 2 extra 1770 GB ephemeral disks", + "guestCpus": 8, + "memoryMb": 30720, + "imageSpaceGb": 10, + "ephemeralDisks": [ + { + "diskGb": 1770 + }, + { + "diskGb": 1770 + } + ], + "maximumPersistentDisks": 16, + "maximumPersistentDisksSizeGb": "1024" + } + ] +} \ No newline at end of file