Cleanup Azure compute ImageParams input value type.
Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/bd8c0b9c Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/bd8c0b9c Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/bd8c0b9c Branch: refs/heads/master Commit: bd8c0b9c0435e1773d7f62d557b68a774bb05793 Parents: f568825 Author: Adrian Cole <[email protected]> Authored: Tue Oct 21 21:43:39 2014 -0700 Committer: Adrian Cole <[email protected]> Committed: Wed Oct 22 17:11:19 2014 -0700 ---------------------------------------------------------------------- .../binders/BindOSImageParamsToXmlPayload.java | 49 ------ .../azurecompute/binders/ImageParamsToXML.java | 43 +++++ .../azurecompute/domain/ImageParams.java | 165 ++++++++----------- .../jclouds/azurecompute/features/ImageApi.java | 22 +-- .../azurecompute/functions/ImageParamsName.java | 13 +- 5 files changed, 127 insertions(+), 165 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/bd8c0b9c/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindOSImageParamsToXmlPayload.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindOSImageParamsToXmlPayload.java b/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindOSImageParamsToXmlPayload.java deleted file mode 100644 index 1c1ec1e..0000000 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/binders/BindOSImageParamsToXmlPayload.java +++ /dev/null @@ -1,49 +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 static com.google.common.base.Throwables.propagate; -import static org.jclouds.azurecompute.domain.Image.OSType.LINUX; - -import javax.inject.Singleton; - -import org.jclouds.azurecompute.domain.ImageParams; -import org.jclouds.http.HttpRequest; -import org.jclouds.rest.Binder; - -import com.jamesmurty.utils.XMLBuilder; - -@Singleton -public class BindOSImageParamsToXmlPayload implements Binder { - - - @Override - public <R extends HttpRequest> R bindToRequest(R request, Object input) { - ImageParams params = ImageParams.class.cast(input); - try { - String xml = XMLBuilder.create("OSImage", "http://schemas.microsoft.com/windowsazure") - .e("Label").t(params.getLabel()).up() - .e("MediaLink").t(params.getMediaLink().toASCIIString()).up() - .e("Name").t(params.getName()).up() - .e("OS").t(params.getOS() == LINUX ? "Linux" : "Windows").up() - .up().asString(); - return (R) request.toBuilder().payload(xml).build(); - } catch (Exception e) { - throw propagate(e); - } - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/bd8c0b9c/azurecompute/src/main/java/org/jclouds/azurecompute/binders/ImageParamsToXML.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/binders/ImageParamsToXML.java b/azurecompute/src/main/java/org/jclouds/azurecompute/binders/ImageParamsToXML.java new file mode 100644 index 0000000..040184b --- /dev/null +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/binders/ImageParamsToXML.java @@ -0,0 +1,43 @@ +/* + * 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 static com.google.common.base.Throwables.propagate; +import static org.jclouds.azurecompute.domain.Image.OSType.LINUX; + +import org.jclouds.azurecompute.domain.ImageParams; +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.Binder; + +import com.jamesmurty.utils.XMLBuilder; + +public final class ImageParamsToXML implements Binder { + @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { + ImageParams params = ImageParams.class.cast(input); + try { + String xml = XMLBuilder.create("OSImage", "http://schemas.microsoft.com/windowsazure") + .e("Label").t(params.label()).up() + .e("MediaLink").t(params.mediaLink().toASCIIString()).up() + .e("Name").t(params.name()).up() + .e("OS").t(params.os() == LINUX ? "Linux" : "Windows").up() + .up().asString(); + return (R) request.toBuilder().payload(xml).build(); + } catch (Exception e) { + throw propagate(e); + } + } +} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/bd8c0b9c/azurecompute/src/main/java/org/jclouds/azurecompute/domain/ImageParams.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/ImageParams.java b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/ImageParams.java index 134c858..ed72936 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/domain/ImageParams.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/domain/ImageParams.java @@ -16,153 +16,126 @@ */ package org.jclouds.azurecompute.domain; +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Objects.toStringHelper; import static com.google.common.base.Preconditions.checkNotNull; import java.net.URI; import org.jclouds.azurecompute.domain.Image.OSType; -import com.google.common.base.MoreObjects; -import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.base.Objects; -/** - * The Add OS Image operation adds an OS image that is currently stored in a storage account in your - * subscription to the image repository. - * - * @see <a href="http://msdn.microsoft.com/en-us/library/jj157191" >api</a> - */ -public class ImageParams { +/** To create a new operating system image. */ +public final class ImageParams { - public static Builder builder() { - return new Builder(); + /** Specifies a name that is used to identify the image when you create a Virtual Machine. */ + public String name() { + return name; + } + + /** Specifies the friendly name of the image. */ + public String label() { + return label; + } + + /** Specifies the location of the vhd file for the image. */ + public URI mediaLink() { + return mediaLink; + } + + /** {@link Image#os() Os type} of the image. */ + public OSType os() { + return os; } public Builder toBuilder() { return builder().fromImageParams(this); } - public static class Builder { + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + private String name; private String label; private URI mediaLink; private OSType os; - private String name; - /** - * @see ImageParams#getLabel() - */ + public Builder name(String name) { + this.name = name; + return this; + } + public Builder label(String label) { this.label = label; return this; } - /** - * @see ImageParams#getMediaLink() - */ public Builder mediaLink(URI mediaLink) { this.mediaLink = mediaLink; return this; } - /** - * @see ImageParams#getName() - */ - public Builder name(String name) { - this.name = name; - return this; - } - - /** - * @see ImageParams#getOS() - */ public Builder os(OSType os) { this.os = os; return this; } public ImageParams build() { - return new ImageParams(label, mediaLink, name, os); + return ImageParams.create(name, label, mediaLink, os); } public Builder fromImageParams(ImageParams in) { - return this.label(in.getLabel()).mediaLink(in.getMediaLink()).name(in.getName()).os(in.getOS()); + return name(in.name()) + .label(in.label()) + .mediaLink(in.mediaLink()) + .os(in.os()); } } - private final String label; - private final URI mediaLink; - private final String name; - private final OSType os; - - private ImageParams(String label, URI mediaLink, String name, OSType os) { - this.label = checkNotNull(label, "label"); - this.name = checkNotNull(name, "name for %s", label); - this.mediaLink = checkNotNull(mediaLink, "mediaLink for %s", label); - this.os = checkNotNull(os, "os for %s", label); - } - - /** - * The operating system type of the OS image. - */ - public OSType getOS() { - return os; - } - - /** - * The name of the hosted service. This name is the DNS prefix name and can be used to access the - * hosted service. - * - * For example, if the service name is MyService you could access the access the service by - * calling: http://MyService.cloudapp.net - */ - public String getName() { - return name; + private static ImageParams create(String name, String label, URI mediaLink, OSType os) { + return new ImageParams(name, label, mediaLink, os); } - /** - * The location of the blob in the blob store in which the media for the image is located. The - * blob location belongs to a storage account in the subscription specified by the - * <subscription-id> value in the operation call. - * - * Example: - * - * http://example.blob.core.windows.net/disks/myimage.vhd - */ - public URI getMediaLink() { - return mediaLink; + // TODO: Remove from here down with @AutoValue. + private ImageParams(String name, String label, URI mediaLink, OSType os) { + this.name = checkNotNull(name, "name"); + this.label = checkNotNull(label, "label"); + this.mediaLink = checkNotNull(mediaLink, "mediaLink"); + this.os = checkNotNull(os, "os"); } - /** - * The description of the image. - */ - public String getLabel() { - return label; - } + private final String name; + private final String label; + private final URI mediaLink; + private final OSType os; - @Override - public int hashCode() { - return Objects.hashCode(name); + @Override public int hashCode() { + return Objects.hashCode(name, label, mediaLink, os); } - @Override - public boolean equals(Object obj) { - if (this == obj) + @Override public boolean equals(Object object) { + if (this == object) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) + } + if (object instanceof ImageParams) { + ImageParams that = ImageParams.class.cast(object); + return equal(name, that.name) + && equal(label, that.label) + && equal(mediaLink, that.mediaLink) + && equal(os, that.os); + } else { return false; - ImageParams other = (ImageParams) obj; - return Objects.equal(this.name, other.name); - } - - @Override - public String toString() { - return string().toString(); + } } - private ToStringHelper string() { - return MoreObjects.toStringHelper(this).add("label", label).add("mediaLink", mediaLink).add("name", name) - .add("os", os); + @Override public String toString() { + return toStringHelper(this) + .add("name", name) + .add("label", label) + .add("mediaLink", mediaLink) + .add("os", os).toString(); } } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/bd8c0b9c/azurecompute/src/main/java/org/jclouds/azurecompute/features/ImageApi.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/features/ImageApi.java b/azurecompute/src/main/java/org/jclouds/azurecompute/features/ImageApi.java index cdf7029..7ccad03 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/features/ImageApi.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/features/ImageApi.java @@ -16,7 +16,12 @@ */ package org.jclouds.azurecompute.features; +import static javax.ws.rs.core.MediaType.APPLICATION_XML; +import static org.jclouds.Fallbacks.EmptyListOnNotFoundOr404; +import static org.jclouds.Fallbacks.NullOnNotFoundOr404; + import java.util.List; + import javax.inject.Named; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -26,8 +31,8 @@ import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import org.jclouds.azurecompute.binders.BindOSImageParamsToXmlPayload; + +import org.jclouds.azurecompute.binders.ImageParamsToXML; import org.jclouds.azurecompute.domain.Image; import org.jclouds.azurecompute.domain.ImageParams; import org.jclouds.azurecompute.functions.ImageParamsName; @@ -40,9 +45,6 @@ import org.jclouds.rest.annotations.ParamParser; import org.jclouds.rest.annotations.ResponseParser; import org.jclouds.rest.annotations.XMLResponseParser; -import static org.jclouds.Fallbacks.EmptyListOnNotFoundOr404; -import static org.jclouds.Fallbacks.NullOnNotFoundOr404; - /** * The Service Management API includes operations for managing the OS images in your subscription. * @@ -50,7 +52,7 @@ import static org.jclouds.Fallbacks.NullOnNotFoundOr404; */ @Path("/services/images") @Headers(keys = "x-ms-version", values = "{jclouds.api-version}") -@Consumes(MediaType.APPLICATION_XML) +@Consumes(APPLICATION_XML) public interface ImageApi { /** @@ -69,9 +71,9 @@ public interface ImageApi { */ @Named("AddImage") @POST - @Produces(MediaType.APPLICATION_XML) + @Produces(APPLICATION_XML) @ResponseParser(ParseRequestIdHeader.class) - String add(@BinderParam(BindOSImageParamsToXmlPayload.class) ImageParams params); + String add(@BinderParam(ImageParamsToXML.class) ImageParams params); /** * The Update OS Image operation updates an OS image that in your image repository. @@ -79,10 +81,10 @@ public interface ImageApi { @Named("UpdateImage") @PUT @Path("/{imageName}") - @Produces(MediaType.APPLICATION_XML) + @Produces(APPLICATION_XML) @ResponseParser(ParseRequestIdHeader.class) String update(@PathParam("imageName") @ParamParser(ImageParamsName.class) - @BinderParam(BindOSImageParamsToXmlPayload.class) ImageParams params); + @BinderParam(ImageParamsToXML.class) ImageParams params); /** * The Delete Hosted Service operation deletes the specified hosted service from Windows Azure. http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/bd8c0b9c/azurecompute/src/main/java/org/jclouds/azurecompute/functions/ImageParamsName.java ---------------------------------------------------------------------- diff --git a/azurecompute/src/main/java/org/jclouds/azurecompute/functions/ImageParamsName.java b/azurecompute/src/main/java/org/jclouds/azurecompute/functions/ImageParamsName.java index 077b63c..3b2c458 100644 --- a/azurecompute/src/main/java/org/jclouds/azurecompute/functions/ImageParamsName.java +++ b/azurecompute/src/main/java/org/jclouds/azurecompute/functions/ImageParamsName.java @@ -16,19 +16,12 @@ */ package org.jclouds.azurecompute.functions; -import com.google.common.base.Function; -import javax.inject.Singleton; import org.jclouds.azurecompute.domain.ImageParams; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Function; -@Singleton public class ImageParamsName implements Function<Object, String> { - @Override - public String apply(Object input) { - checkArgument(checkNotNull(input, "input") instanceof ImageParams, - "this function is only valid for ImageParams!"); - return checkNotNull(ImageParams.class.cast(input), "ImageParams").getName(); + @Override public String apply(Object input) { + return ImageParams.class.cast(input).name(); } }
