Merge pull request #1171 from dralves/gce-machinetypes

gce - machine types api
This commit is contained in:
Adrian Cole 2013-01-13 12:07:57 -08:00
commit 9b61cfacc8
13 changed files with 1032 additions and 0 deletions

View File

@ -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
*/

View File

@ -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
*/

View File

@ -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<GoogleComput
public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, 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)

View File

@ -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 <a href="https://developers.google.com/compute/docs/reference/v1beta13/machineTypes"/>
*/
@Beta
public final class MachineType extends Resource {
private final Integer guestCpus;
private final Integer memoryMb;
private final Integer imageSpaceGb;
private final List<EphemeralDisk> ephemeralDisks;
private final Integer maximumPersistentDisks;
private final Long maximumPersistentDisksSizeGb;
private final Set<String> 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<EphemeralDisk> ephemeralDisks,
int maximumPersistentDisks, long maximumPersistentDisksSizeGb, Set<String> 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.<EphemeralDisk>of() : ephemeralDisks;
this.maximumPersistentDisks = checkNotNull(maximumPersistentDisks, "maximumPersistentDisks of %s", name);
this.maximumPersistentDisksSizeGb = maximumPersistentDisksSizeGb;
this.availableZone = availableZone == null ? ImmutableSet.<String>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<EphemeralDisk> 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<String> 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<Builder> {
private Integer guestCpus;
private Integer memoryMb;
private Integer imageSpaceGb;
private ImmutableList.Builder<EphemeralDisk> ephemeralDisks = ImmutableList.builder();
private Integer maximumPersistentDisks;
private Long maximumPersistentDisksSizeGb;
private ImmutableSet.Builder<String> 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<EphemeralDisk> 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<String> 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());
}
}
}
}

View File

@ -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.
* <p/>
*
* @author David Alves
* @see <a href="https://developers.google.com/compute/docs/reference/v1beta13/machineTypes"/>
*/
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<MachineType> listFirstPage();
/**
* @see MachineTypeApi#listAtMarker(String, org.jclouds.googlecompute.options.ListOptions)
*/
ListPage<MachineType> 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<MachineType> listAtMarker(@Nullable String marker, ListOptions listOptions);
/**
* @see MachineTypeApi#list(org.jclouds.googlecompute.options.ListOptions)
*/
PagedIterable<MachineType> 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<MachineType> list(ListOptions listOptions);
}

View File

@ -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<MachineType> get(@PathParam("machineType") String machineTypeName);
/**
* @see MachineTypeApi#listFirstPage()
*/
@GET
@Path("/machineTypes")
@OAuthScopes(COMPUTE_READONLY_SCOPE)
@ResponseParser(ParseMachineTypes.class)
@Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class)
ListenableFuture<ListPage<MachineType>> listFirstPage();
/**
* @see MachineTypeApi#listAtMarker(String)
*/
@GET
@Path("/machineTypes")
@OAuthScopes(COMPUTE_READONLY_SCOPE)
@ResponseParser(ParseMachineTypes.class)
@Fallback(EmptyIterableWithMarkerOnNotFoundOr404.class)
ListenableFuture<ListPage<MachineType>> 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<ListPage<MachineType>> 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<? extends PagedIterable<MachineType>> 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<? extends PagedIterable<MachineType>> list(ListOptions listOptions);
}

View File

@ -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<ListPage<MachineType>> {
@Inject
public ParseMachineTypes(Json json) {
super(json, new TypeLiteral<ListPage<MachineType>>() {});
}
public static class ToPagedIterable extends BaseToPagedIterable<MachineType, ToPagedIterable> {
private final GoogleComputeApi api;
@Inject
protected ToPagedIterable(GoogleComputeApi api) {
this.api = checkNotNull(api, "api");
}
@Override
protected Function<Object, IterableWithMarker<MachineType>> fetchNextPage(final String projectName,
final String marker,
final ListOptions options) {
return new Function<Object, IterableWithMarker<MachineType>>() {
@Override
public IterableWithMarker<MachineType> apply(Object input) {
return api.getMachineTypeApiForProject(projectName).listAtMarker(marker, options);
}
};
}
}
}

View File

@ -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<GoogleComputeApi> {
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());
}
}

View File

@ -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<MachineType> machineTypes = api().list(new ListOptions.Builder()
.maxResults(1));
Iterator<IterableWithMarker<MachineType>> pageIterator = machineTypes.iterator();
assertTrue(pageIterator.hasNext());
IterableWithMarker<MachineType> singlePageIterator = pageIterator.next();
List<MachineType> 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());
}
}

View File

@ -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<ListPage<MachineType>> {
@Override
public String resource() {
return "/machinetype_list.json";
}
@Override
@Consumes(MediaType.APPLICATION_JSON)
public ListPage<MachineType> expected() {
SimpleDateFormatDateService dateService = new SimpleDateFormatDateService();
return ListPage.<MachineType>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();
}
}

View File

@ -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<MachineType> {
@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();
}
}

View File

@ -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"
}

View File

@ -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"
}
]
}