From 3e9612a0bca98cb75a58e3bd7b93a290ba5a87a0 Mon Sep 17 00:00:00 2001 From: Daniel Broudy Date: Wed, 26 Nov 2014 17:16:06 -0800 Subject: [PATCH] Added LicenseApi --- .../GoogleComputeEngineApi.java | 5 ++ .../googlecomputeengine/domain/License.java | 40 ++++++++++++++++ .../features/LicenseApi.java | 48 +++++++++++++++++++ .../features/LicenseApiLiveTest.java | 47 ++++++++++++++++++ .../features/LicenseApiMockTest.java | 46 ++++++++++++++++++ .../parse/ParseLicenseTest.java | 46 ++++++++++++++++++ .../src/test/resources/license_get.json | 6 +++ 7 files changed, 238 insertions(+) create mode 100644 providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/License.java create mode 100644 providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/LicenseApi.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiLiveTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiMockTest.java create mode 100644 providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseLicenseTest.java create mode 100644 providers/google-compute-engine/src/test/resources/license_get.json diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/GoogleComputeEngineApi.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/GoogleComputeEngineApi.java index 14a3961801..21c0764fdc 100644 --- a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/GoogleComputeEngineApi.java +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/GoogleComputeEngineApi.java @@ -32,6 +32,7 @@ import org.jclouds.googlecomputeengine.features.ForwardingRuleApi; import org.jclouds.googlecomputeengine.features.HttpHealthCheckApi; import org.jclouds.googlecomputeengine.features.ImageApi; import org.jclouds.googlecomputeengine.features.InstanceApi; +import org.jclouds.googlecomputeengine.features.LicenseApi; import org.jclouds.googlecomputeengine.features.MachineTypeApi; import org.jclouds.googlecomputeengine.features.NetworkApi; import org.jclouds.googlecomputeengine.features.OperationApi; @@ -101,6 +102,10 @@ public interface GoogleComputeEngineApi extends Closeable { @Path("/zones/{zone}") InstanceApi instancesInZone(@PathParam("zone") String zone); + @Delegate + @Path("/projects/{project}/global") + LicenseApi licensesInProject(@PathParam("project") String project); + @Delegate @Endpoint(CurrentProject.class) @Path("/zones/{zone}") diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/License.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/License.java new file mode 100644 index 0000000000..68277e5197 --- /dev/null +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/domain/License.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.googlecomputeengine.domain; + +import java.net.URI; + +import org.jclouds.json.SerializedNames; + +import com.google.auto.value.AutoValue; + +/** Parameter to {@linkplain org.jclouds.googlecomputeengine.features.InstanceApi#create(NewInstance)}. */ +@AutoValue +public abstract class License { + + public abstract URI selfLink(); + public abstract String name(); + public abstract boolean chargesUseFee(); + + @SerializedNames({"selfLink", "name", "chargesUseFee"}) + public static License create(URI selfLink, String name, boolean chargesUseFee) { + return new AutoValue_License(selfLink, name, chargesUseFee); + } + + License() { + } +} diff --git a/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/LicenseApi.java b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/LicenseApi.java new file mode 100644 index 0000000000..9301fc55fa --- /dev/null +++ b/providers/google-compute-engine/src/main/java/org/jclouds/googlecomputeengine/features/LicenseApi.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.googlecomputeengine.features; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; + +import javax.inject.Named; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +import org.jclouds.Fallbacks.NullOnNotFoundOr404; +import org.jclouds.googlecomputeengine.domain.License; +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.oauth.v2.filters.OAuthFilter; +import org.jclouds.rest.annotations.Fallback; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.SkipEncoding; + +@SkipEncoding({'/', '='}) +@RequestFilters(OAuthFilter.class) +@Consumes(APPLICATION_JSON) +public interface LicenseApi { + + /** Returns the specified License resource. */ + @Named("License:get") + @GET + @Path("/licenses/{license}") + @Fallback(NullOnNotFoundOr404.class) + @Nullable + License get(@PathParam("license") String license); + +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiLiveTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiLiveTest.java new file mode 100644 index 0000000000..247bb4fe28 --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiLiveTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +import java.net.URI; + +import org.jclouds.googlecomputeengine.domain.License; +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiLiveTest; +import org.testng.annotations.Test; + +public class LicenseApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { + + public static final String PROJECT = "suse-cloud"; + public static final String LICENSE = "sles-12"; + + + private LicenseApi api() { + return api.licensesInProject(PROJECT); + } + + @Test(groups = "live") + public void testGetLicense() { + License license = api().get(LICENSE); + assertNotNull(license); + assertEquals(license.name(), LICENSE); + URI selfLink = URI.create("https://www.googleapis.com/compute/v1/projects/" + + PROJECT + "/global/licenses/" + LICENSE); + assertEquals(license.selfLink(), selfLink); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiMockTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiMockTest.java new file mode 100644 index 0000000000..cc88192613 --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/LicenseApiMockTest.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.googlecomputeengine.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiMockTest; +import org.jclouds.googlecomputeengine.parse.ParseLicenseTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "LicenseApiMockTest", singleThreaded = true) +public class LicenseApiMockTest extends BaseGoogleComputeEngineApiMockTest { + + public void get() throws Exception { + server.enqueue(jsonResponse("/license_get.json")); + + assertEquals(licenseApi().get("sles-12"), new ParseLicenseTest().expected(url("/projects"))); + assertSent(server, "GET", "/projects/suse-cloud/global/licenses/sles-12"); + } + + public void get_4xx() throws Exception { + server.enqueue(response404()); + + assertNull(licenseApi().get("sles-12")); + assertSent(server, "GET", "/projects/suse-cloud/global/licenses/sles-12"); + } + + public LicenseApi licenseApi(){ + return api().licensesInProject("suse-cloud"); + } +} diff --git a/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseLicenseTest.java b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseLicenseTest.java new file mode 100644 index 0000000000..519c4d50da --- /dev/null +++ b/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseLicenseTest.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.googlecomputeengine.parse; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; + +import java.net.URI; + +import javax.ws.rs.Consumes; + +import org.jclouds.googlecomputeengine.domain.License; +import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineParseTest; +import org.testng.annotations.Test; + +@Test(groups = "unit", testName = "ParseLicenseTest") +public class ParseLicenseTest extends BaseGoogleComputeEngineParseTest { + + @Override + public String resource() { + return "/license_get.json"; + } + + @Override @Consumes(APPLICATION_JSON) + public License expected() { + return expected(BASE_URL); + } + + @Consumes(APPLICATION_JSON) + public License expected(String baseUrl) { + return License.create(URI.create(baseUrl + "/suse-cloud/global/licenses/sles-12"), "sles-12", true); + } +} diff --git a/providers/google-compute-engine/src/test/resources/license_get.json b/providers/google-compute-engine/src/test/resources/license_get.json new file mode 100644 index 0000000000..fffdf6ccaf --- /dev/null +++ b/providers/google-compute-engine/src/test/resources/license_get.json @@ -0,0 +1,6 @@ +{ + "kind": "compute#license", + "selfLink": "https://www.googleapis.com/compute/v1/projects/suse-cloud/global/licenses/sles-12", + "name": "sles-12", + "chargesUseFee": true +} \ No newline at end of file