Minimalize code around hosted service creation.
Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/f5688259 Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/f5688259 Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/f5688259 Branch: refs/heads/master Commit: f56882597254769bd029955fabfc5a3f9571280c Parents: 21ef291 Author: Adrian Cole <[email protected]> Authored: Tue Oct 21 21:40:44 2014 -0700 Committer: Adrian Cole <[email protected]> Committed: Wed Oct 22 17:11:19 2014 -0700 ---------------------------------------------------------------------- .../BindCreateHostedServiceToXmlPayload.java | 79 ------------- .../azurecompute/features/HostedServiceApi.java | 29 ++--- .../functions/Base64EncodeLabel.java | 28 +++++ .../options/CreateHostedServiceOptions.java | 116 ------------------- .../features/HostedServiceApiLiveTest.java | 2 +- .../features/HostedServiceApiMockTest.java | 22 +--- .../create_hostedservice_location_options.xml | 1 - 7 files changed, 38 insertions(+), 239 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f5688259/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindCreateHostedServiceToXmlPayload.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindCreateHostedServiceToXmlPayload.java b/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindCreateHostedServiceToXmlPayload.java deleted file mode 100644 index 900c9a7..0000000 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindCreateHostedServiceToXmlPayload.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.azurecompute.binders; - -import com.google.common.base.Optional; -import com.google.common.base.Throwables; -import com.jamesmurty.utils.XMLBuilder; -import java.util.Map; -import java.util.Map.Entry; -import javax.inject.Singleton; -import org.jclouds.azurecompute.options.CreateHostedServiceOptions; -import org.jclouds.http.HttpRequest; -import org.jclouds.rest.MapBinder; - -import static com.google.common.base.Charsets.UTF_8; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.io.BaseEncoding.base64; - -@Singleton -public class BindCreateHostedServiceToXmlPayload implements MapBinder { - - private static final CreateHostedServiceOptions NO_OPTIONS = new CreateHostedServiceOptions(); - - @Override - public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) { - String serviceName = checkNotNull(postParams.get("name"), "name").toString(); - String label = base64().encode(checkNotNull(postParams.get("label"), "label").toString().getBytes(UTF_8)); - - Optional<String> location = Optional.fromNullable((String) postParams.get("location")); - Optional<String> affinityGroup = Optional.fromNullable((String) postParams.get("affinityGroup")); - CreateHostedServiceOptions options = Optional - .fromNullable((CreateHostedServiceOptions) postParams.get("options")).or(NO_OPTIONS); - try { - XMLBuilder createHostedService = XMLBuilder.create("CreateHostedService") - .a("xmlns", "http://schemas.microsoft.com/windowsazure").e("ServiceName").t(serviceName).up() - .e("Label").t(label).up(); - - if (options.getDescription().isPresent()) - createHostedService.e("Description").t(options.getDescription().get()).up(); - - if (location.isPresent()) - createHostedService.e("Location").t(location.get()).up(); - else if (affinityGroup.isPresent()) - createHostedService.e("AffinityGroup").t(affinityGroup.get()).up(); - else - throw new IllegalArgumentException("you must specify either Location or AffinityGroup!"); - - if (options.getExtendedProperties().isPresent() && options.getExtendedProperties().get().size() > 0) { - XMLBuilder extendedProperties = createHostedService.e("ExtendedProperties"); - for (Entry<String, String> entry : options.getExtendedProperties().get().entrySet()) - extendedProperties.e("ExtendedProperty").e("Name").t(entry.getKey()).up().e("Value").t(entry.getValue()); - } - return (R) request.toBuilder().payload(createHostedService.asString()).build(); - } catch (Exception e) { - throw Throwables.propagate(e); - } - - } - - @Override - public <R extends HttpRequest> R bindToRequest(R request, Object input) { - throw new IllegalStateException("BindCreateHostedServiceToXmlPayload is needs parameters"); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f5688259/azurecompute/src/main/java/org/jclouds/azurecompute/features/HostedServiceApi.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/features/HostedServiceApi.java b/azurecompute/src/main/java/org/jclouds/azurecompute/features/HostedServiceApi.java index bdfeda6..f8d1502 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/features/HostedServiceApi.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/features/HostedServiceApi.java @@ -31,16 +31,16 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import org.jclouds.azurecompute.binders.BindCreateHostedServiceToXmlPayload; import org.jclouds.azurecompute.domain.HostedService; +import org.jclouds.azurecompute.functions.Base64EncodeLabel; import org.jclouds.azurecompute.functions.ParseRequestIdHeader; -import org.jclouds.azurecompute.options.CreateHostedServiceOptions; import org.jclouds.azurecompute.xml.HostedServiceHandler; import org.jclouds.azurecompute.xml.ListHostedServicesHandler; import org.jclouds.javax.annotation.Nullable; import org.jclouds.rest.annotations.Fallback; import org.jclouds.rest.annotations.Headers; -import org.jclouds.rest.annotations.MapBinder; +import org.jclouds.rest.annotations.ParamParser; +import org.jclouds.rest.annotations.Payload; import org.jclouds.rest.annotations.PayloadParam; import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.ResponseParser; @@ -89,27 +89,12 @@ public interface HostedServiceApi { */ @Named("CreateHostedService") @POST - @MapBinder(BindCreateHostedServiceToXmlPayload.class) @Produces(APPLICATION_XML) @ResponseParser(ParseRequestIdHeader.class) - String createServiceWithLabelInLocation(@PayloadParam("name") String name, - @PayloadParam("label") String label, @PayloadParam("location") String location); - - /** - * same as {@link #createServiceWithLabelInLocation(String, String, String)} , except you can - * specify optional parameters such as extended properties or a description. - * - * @param options - * parameters such as extended properties or a description. - */ - @Named("CreateHostedService") - @POST - @MapBinder(BindCreateHostedServiceToXmlPayload.class) - @Produces(APPLICATION_XML) - @ResponseParser(ParseRequestIdHeader.class) - String createServiceWithLabelInLocation(@PayloadParam("name") String name, - @PayloadParam("label") String label, @PayloadParam("location") String location, - @PayloadParam("options") CreateHostedServiceOptions options); + @Payload("<CreateHostedService xmlns=\"http://schemas.microsoft.com/windowsazure\"><ServiceName>{name}</ServiceName><Label>{label}</Label><Location>{location}</Location></CreateHostedService>") + String createWithLabelInLocation(@PayloadParam("name") String name, + @PayloadParam("label") @ParamParser(Base64EncodeLabel.class) String label, + @PayloadParam("location") String location); /** * The Get Hosted Service Properties operation retrieves system properties for the specified http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f5688259/azurecompute/src/main/java/org/jclouds/azurecompute/functions/Base64EncodeLabel.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/functions/Base64EncodeLabel.java b/azurecompute/src/main/java/org/jclouds/azurecompute/functions/Base64EncodeLabel.java new file mode 100644 index 0000000..a8d0513 --- /dev/null +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/functions/Base64EncodeLabel.java @@ -0,0 +1,28 @@ +/* + * 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.azurecompute.functions; + +import static com.google.common.base.Charsets.UTF_8; +import static com.google.common.io.BaseEncoding.base64; + +import com.google.common.base.Function; + +public final class Base64EncodeLabel implements Function<Object, String> { + @Override public String apply(Object label) { + return base64().encode(label.toString().getBytes(UTF_8)); + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f5688259/azurecompute/src/main/java/org/jclouds/azurecompute/options/CreateHostedServiceOptions.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/options/CreateHostedServiceOptions.java b/azurecompute/src/main/java/org/jclouds/azurecompute/options/CreateHostedServiceOptions.java deleted file mode 100644 index b4c49eb..0000000 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/options/CreateHostedServiceOptions.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.azurecompute.options; - -import com.google.common.base.MoreObjects; -import com.google.common.base.Objects; -import com.google.common.base.Optional; -import java.util.Map; - -/** - * Optional parameters for creating a hosted service - * - * @see <a href="http://msdn.microsoft.com/en-us/library/gg441304" >docs</a> - */ -public class CreateHostedServiceOptions implements Cloneable { - - private Optional<String> description = Optional.absent(); - private Optional<Map<String, String>> extendedProperties = Optional.absent(); - - /** - * @see CreateHostedServiceOptions#getDescription() - */ - public CreateHostedServiceOptions description(String description) { - this.description = Optional.fromNullable(description); - return this; - } - - /** - * @see CreateHostedServiceOptions#getExtendedProperties() - */ - public CreateHostedServiceOptions extendedProperties(Map<String, String> extendedProperties) { - this.extendedProperties = Optional.fromNullable(extendedProperties); - return this; - } - - /** - * A description for the hosted service. The description can be up to 1024 characters in length. - */ - public Optional<String> getDescription() { - return description; - } - - /** - * Represents the name of an extended hosted service property. Each extended property must have - * both a defined name and value. You can have a maximum of 50 extended property name/value - * pairs. - * - * The maximum length of the Name element is 64 characters, only alphanumeric characters and - * underscores are valid in the Name, and the name must start with a letter. Each extended - * property value has a maximum length of 255 characters. - */ - public Optional<Map<String, String>> getExtendedProperties() { - return extendedProperties; - } - - public static class Builder { - - /** - * @see CreateHostedServiceOptions#getDescription() - */ - public static CreateHostedServiceOptions description(String description) { - return new CreateHostedServiceOptions().description(description); - } - - /** - * @see CreateHostedServiceOptions#getExtendedProperties() - */ - public static CreateHostedServiceOptions extendedProperties(Map<String, String> extendedProperties) { - return new CreateHostedServiceOptions().extendedProperties(extendedProperties); - } - } - - @Override - public CreateHostedServiceOptions clone() { - return new CreateHostedServiceOptions().description(description.orNull()).extendedProperties( - extendedProperties.orNull()); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - CreateHostedServiceOptions other = CreateHostedServiceOptions.class.cast(obj); - return Objects.equal(this.description, other.description) - && Objects.equal(this.extendedProperties, other.extendedProperties); - } - - @Override - public int hashCode() { - return Objects.hashCode(description, extendedProperties); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper("").omitNullValues().add("description", description.orNull()) - .add("extendedProperties", extendedProperties.orNull()).toString(); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f5688259/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java index 894c0de..08f53b4 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiLiveTest.java @@ -76,7 +76,7 @@ public class HostedServiceApiLiveTest extends BaseAzureComputeApiLiveTest { public void testCreateHostedService() { - String requestId = api().createServiceWithLabelInLocation(HOSTED_SERVICE, HOSTED_SERVICE, location); + String requestId = api().createWithLabelInLocation(HOSTED_SERVICE, HOSTED_SERVICE, location); assertTrue(operationSucceeded.apply(requestId), requestId); Logger.getAnonymousLogger().info("operation succeeded: " + requestId); http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f5688259/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiMockTest.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiMockTest.java b/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiMockTest.java index 1ac320d..9d0cf80 100644 --- a/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiMockTest.java +++ b/azurecompute/src/test/java/org/jclouds/azurecompute/features/HostedServiceApiMockTest.java @@ -16,7 +16,6 @@ */ package org.jclouds.azurecompute.features; -import static org.jclouds.azurecompute.options.CreateHostedServiceOptions.Builder.description; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; @@ -26,7 +25,6 @@ import org.jclouds.azurecompute.xml.HostedServiceHandlerTest; import org.jclouds.azurecompute.xml.ListHostedServicesHandlerTest; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableMap; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; @@ -93,14 +91,14 @@ public class HostedServiceApiMockTest extends BaseAzureComputeApiMockTest { } } - public void createServiceWithLabelInLocation() throws Exception { + public void createWithLabelInLocation() throws Exception { MockWebServer server = mockAzureManagementServer(); server.enqueue(requestIdResponse("request-1")); try { HostedServiceApi api = api(server.getUrl("/")).getHostedServiceApi(); - assertEquals(api.createServiceWithLabelInLocation("myservice", "service mine", "West US"), "request-1"); + assertEquals(api.createWithLabelInLocation("myservice", "service mine", "West US"), "request-1"); assertSent(server, "POST", "/services/hostedservices", "/create_hostedservice_location.xml"); } finally { @@ -108,22 +106,6 @@ public class HostedServiceApiMockTest extends BaseAzureComputeApiMockTest { } } - public void createServiceWithLabelInLocationOptions() throws Exception { - MockWebServer server = mockAzureManagementServer(); - server.enqueue(requestIdResponse("request-1")); - - try { - HostedServiceApi api = api(server.getUrl("/")).getHostedServiceApi(); - - assertEquals(api.createServiceWithLabelInLocation("myservice", "service mine", "West US", - description("my description").extendedProperties(ImmutableMap.of("Role", "Production"))), "request-1"); - - assertSent(server, "POST", "/services/hostedservices", "/create_hostedservice_location_options.xml"); - } finally { - server.shutdown(); - } - } - public void deleteWhenFound() throws Exception { MockWebServer server = mockAzureManagementServer(); server.enqueue(requestIdResponse("request-1")); http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/f5688259/azurecompute/src/test/resources/create_hostedservice_location_options.xml ---------------------------------------------------------------------- diff --git a/azurecompute/src/test/resources/create_hostedservice_location_options.xml b/azurecompute/src/test/resources/create_hostedservice_location_options.xml deleted file mode 100644 index 9f7f8f0..0000000 --- a/azurecompute/src/test/resources/create_hostedservice_location_options.xml +++ /dev/null @@ -1 +0,0 @@ -<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>myservice</ServiceName><Label>c2VydmljZSBtaW5l</Label><Description>my description</Description><Location>West US</Location><ExtendedProperties><ExtendedProperty><Name>Role</Name><Value>Production</Value></ExtendedProperty></ExtendedProperties></CreateHostedService> \ No newline at end of file
