Skip to content

Commit

Permalink
Refactor image configuration classes
Browse files Browse the repository at this point in the history
- Move archiveSizeBytes to ImageConfiguration
- Add Type enum to ImageConfiguration and type field
- Rename RawImageConfiguration to ComputeImageConfiguration
- Update tests
- Add getClass() equality check to ImageConfiguration.baseEquals
  • Loading branch information
mziccard committed Mar 30, 2016
1 parent 9a13683 commit 562324a
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public static <T extends ResourceId> DeprecationStatus<T> of(Status status, T re
static <T extends ResourceId> DeprecationStatus<T> fromPb(
com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb,
Function<String, T> fromUrl) {
Builder<T> builder = new Builder<T>();
Builder<T> builder = new Builder<>();
builder.deleted(deprecationStatusPb.getDeleted());
builder.deprecated(deprecationStatusPb.getDeprecated());
builder.obsolete(deprecationStatusPb.getObsolete());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ public static final class Builder
private DiskId sourceDisk;
private String sourceDiskId;

private Builder() {}
private Builder() {
super(Type.DISK);
}

private Builder(DiskImageConfiguration imageConfiguration) {
super(imageConfiguration);
super(Type.DISK, imageConfiguration);
this.sourceDisk = imageConfiguration.sourceDisk;
this.sourceDiskId = imageConfiguration.sourceDiskId;
}

private Builder(Image imagePb) {
super(imagePb);
super(Type.DISK, imagePb);
this.sourceDisk = DiskId.fromUrl(imagePb.getSourceDisk());
this.sourceDiskId = imagePb.getSourceDiskId();
}
Expand Down Expand Up @@ -124,8 +126,10 @@ public final boolean equals(Object obj) {

@Override
DiskImageConfiguration setProjectId(String projectId) {
Builder builder = toBuilder().sourceDisk(sourceDisk.setProjectId(projectId));
return builder.build();
if (sourceDisk.project() != null) {
return this;
}
return toBuilder().sourceDisk(sourceDisk.setProjectId(projectId)).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,31 @@

/**
* Base class for Google Compute Engine image configuration. Use {@link DiskImageConfiguration} to
* create an image from an existing Google Compute Engine disk. Use {@link RawImageConfiguration} to
* create an image from a file stored in Google Cloud Storage.
* create an image from an existing Google Compute Engine disk. Use
* {@link StorageImageConfiguration} to create an image from a file stored in Google Cloud Storage.
*/
public abstract class ImageConfiguration implements Serializable {

private static final long serialVersionUID = -9154332316597745316L;

private final Type type;
private final SourceType sourceType;
private final Long archiveSizeBytes;

/**
* Type of a Google Compute Engine image.
*/
public enum Type {
/**
* A Google Compute Engine image created from a Google Compute Engine disk.
*/
DISK,

/**
* A Google Compute Engine image created from a file saved in Google Cloud Storage.
*/
STORAGE
}

/**
* Image source type. The only admissible value is {@code RAW}.
Expand All @@ -48,38 +65,67 @@ public enum SourceType {
*/
public abstract static class Builder<T extends ImageConfiguration, B extends Builder<T, B>> {

private Type type;
private SourceType sourceType;
private Long archiveSizeBytes;

Builder() {}
Builder(Type type) {
this.type = type;
}

Builder(ImageConfiguration imageConfiguration) {
Builder(Type type, ImageConfiguration imageConfiguration) {
this.type = type;
this.sourceType = imageConfiguration.sourceType;
this.archiveSizeBytes = imageConfiguration.archiveSizeBytes;
}

Builder(Image imagePb) {
Builder(Type type, Image imagePb) {
this.type = type;
if (imagePb.getSourceType() != null) {
this.sourceType = SourceType.valueOf(imagePb.getSourceType());
}
this.archiveSizeBytes = imagePb.getArchiveSizeBytes();
}

@SuppressWarnings("unchecked")
B self() {
return (B) this;
}

B type(Type type) {
this.type = type;
return self();
}

B sourceType(SourceType sourceType) {
this.sourceType = sourceType;
return self();
}

B archiveSizeBytes(Long archiveSizeBytes) {
this.archiveSizeBytes = archiveSizeBytes;
return self();
}

/**
* Creates a configuration object.
*/
public abstract T build();
}

ImageConfiguration(Builder builder) {
this.type = builder.type;
this.sourceType = builder.sourceType;
this.archiveSizeBytes = builder.archiveSizeBytes;
}

/**
* Returns the image's type. This method returns {@link Type#DISK} if this image was created from
* an existing disk. This method returns {@link Type#STORAGE} if this image was created from a
* file in Google Cloud Storage.
*/
public Type type() {
return type;
}

/**
Expand All @@ -89,13 +135,23 @@ public SourceType sourceType() {
return sourceType;
}

/**
* Returns the size of the image archive stored in Google Cloud Storage (in bytes).
*/
public Long archiveSizeBytes() {
return archiveSizeBytes;
}

/**
* Returns a builder for the object.
*/
public abstract Builder toBuilder();

MoreObjects.ToStringHelper toStringHelper() {
return MoreObjects.toStringHelper(this).add("sourceType", sourceType);
return MoreObjects.toStringHelper(this)
.add("type", type)
.add("sourceType", sourceType)
.add("archiveSizeBytes", archiveSizeBytes);
}

@Override
Expand All @@ -104,11 +160,13 @@ public String toString() {
}

final int baseHashCode() {
return Objects.hash(sourceType);
return Objects.hash(type, sourceType, archiveSizeBytes);
}

final boolean baseEquals(ImageConfiguration imageConfiguration) {
return Objects.equals(toPb(), imageConfiguration.toPb());
return imageConfiguration != null
&& getClass().equals(imageConfiguration.getClass())
&& Objects.equals(toPb(), imageConfiguration.toPb());
}

abstract ImageConfiguration setProjectId(String projectId);
Expand All @@ -118,6 +176,7 @@ Image toPb() {
if (sourceType != null) {
imagePb.setSourceType(sourceType.name());
}
imagePb.setArchiveSizeBytes(archiveSizeBytes);
return imagePb;
}

Expand All @@ -126,6 +185,6 @@ static <T extends ImageConfiguration> T fromPb(Image imagePb) {
if (imagePb.getSourceDisk() != null) {
return (T) DiskImageConfiguration.fromPb(imagePb);
}
return (T) RawImageConfiguration.fromPb(imagePb);
return (T) StorageImageConfiguration.fromPb(imagePb);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* images of certain operating systems that you can use, or you can create a custom image. A custom
* image is an image created from one of your virtual machine instances that contains your specific
* instance configurations. Use {@link DiskImageConfiguration} to create an image from an existing
* disk. Use {@link RawImageConfiguration} to create an image from a raw image stored in Google
* disk. Use {@link StorageImageConfiguration} to create an image from a raw image stored in Google
* Cloud Storage.
*
* @see <a href="https://cloud.google.com/compute/docs/images">Images</a>
Expand Down Expand Up @@ -115,8 +115,8 @@ public abstract static class Builder {

/**
* Sets the image configuration. Use {@link DiskImageConfiguration} to create an image from an
* existing disk. Use {@link RawImageConfiguration} to create an image from a raw image stored
* in Google Cloud Storage.
* existing disk. Use {@link StorageImageConfiguration} to create an image from a raw image
* stored in Google Cloud Storage.
*/
public abstract Builder configuration(ImageConfiguration configuration);

Expand Down Expand Up @@ -286,8 +286,8 @@ public String description() {
/**
* Returns the image configuration. This method returns an instance of
* {@link DiskImageConfiguration} if the the image was created from a Google Compute Engine disk.
* This method returns an instance of {@link RawImageConfiguration} if the image was created from
* a raw image stored in Google Cloud Storage.
* This method returns an instance of {@link StorageImageConfiguration} if the image was created
* from a raw image stored in Google Cloud Storage.
*/
@SuppressWarnings("unchecked")
public <T extends ImageConfiguration> T configuration() {
Expand Down Expand Up @@ -393,7 +393,7 @@ Image toPb() {
/**
* Returns a builder for an {@code ImageInfo} object given the image identity and an image
* configuration. Use {@link DiskImageConfiguration} to create an image from an existing disk. Use
* {@link RawImageConfiguration} to create an image from a raw image stored in Google Cloud
* {@link StorageImageConfiguration} to create an image from a raw image stored in Google Cloud
* Storage.
*/
public static Builder builder(ImageId imageId, ImageConfiguration configuration) {
Expand All @@ -403,7 +403,7 @@ public static Builder builder(ImageId imageId, ImageConfiguration configuration)
/**
* Returns an {@code ImageInfo} object given the image identity and an image configuration. Use
* {@link DiskImageConfiguration} to create an image from an existing disk. Use
* {@link RawImageConfiguration} to create an image from a raw image stored in Google Cloud
* {@link StorageImageConfiguration} to create an image from a raw image stored in Google Cloud
* Storage.
*/
public static ImageInfo of(ImageId imageId, ImageConfiguration configuration) {
Expand Down
Loading

0 comments on commit 562324a

Please sign in to comment.