From 6e933623ebdb32e09aacf4d192f3f9dd6c2055e3 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 13 Apr 2016 16:28:23 +0200 Subject: [PATCH] Add AttachedDisk class, related configurations and tests --- .../google/gcloud/compute/AttachedDisk.java | 922 ++++++++++++++++++ .../gcloud/compute/AttachedDiskTest.java | 392 ++++++++ .../gcloud/compute/SerializationTest.java | 27 +- 3 files changed, 1333 insertions(+), 8 deletions(-) create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/AttachedDisk.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/AttachedDiskTest.java diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/AttachedDisk.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/AttachedDisk.java new file mode 100644 index 000000000000..e3f7dc2d5732 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/AttachedDisk.java @@ -0,0 +1,922 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed 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 com.google.gcloud.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.api.services.compute.model.AttachedDiskInitializeParams; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import com.google.common.collect.Lists; + +import java.io.Serializable; +import java.util.List; +import java.util.Objects; + +/** + * A disk attached to a Google Compute Engine instance. To create a new disk to attach when an image + * is being created use {@link CreateDiskConfiguration}. To attach an existing persistent disk use + * {@link PersistentDiskConfiguration}. To attach a scratch disk use + * {@link ScratchDiskConfiguration}. + */ +public class AttachedDisk implements Serializable { + + static final Function + FROM_PB_FUNCTION = + new Function() { + @Override + public AttachedDisk apply( + com.google.api.services.compute.model.AttachedDisk pb) { + return AttachedDisk.fromPb(pb); + } + }; + static final Function + TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.AttachedDisk apply( + AttachedDisk attachedDisk) { + return attachedDisk.toPb(); + } + }; + private static final long serialVersionUID = 2969789134157943798L; + + private final String deviceName; + private final Integer index; + private final AttachedDiskConfiguration configuration; + private final List licenses; + + /** + * Base class for {@code AttachedDisk} configuration. Use {@link PersistentDiskConfiguration} to + * attach an existing persistent disk. Use {@link CreateDiskConfiguration} to create a boot + * persistent disk to attach to the instance. Use {@link ScratchDiskConfiguration} to attach a + * scratch disk. + */ + public abstract static class AttachedDiskConfiguration implements Serializable { + + private static final long serialVersionUID = 8813134841283115565L; + + private final Type type; + private final InterfaceType interfaceType; + private final Boolean boot; + private final Boolean autoDelete; + + /** + * Specifies the type of the attached disk. + */ + public enum Type { + /** + * A persistent disk attached to a VM instance. Such an attached disk must already exist or + * can be created along with the instance by using {@link CreateDiskConfiguration}. A + * persistent disk can be attached to other VM instances. + */ + PERSISTENT, + + /** + * A scratch disk is created with the VM instance it is attached to. Scratch disks are only + * available to their VM instance. + */ + SCRATCH + } + + /** + * Specifies the disk interface to use for attaching this disk, which is either {@code SCSI} + * or {@code NVME}. Persistent disks must always use {@code SCSI}. Scratch SSDs can use either + * {@code NVME} or {@code SCSI}. + */ + public enum InterfaceType { + SCSI, + NVME + } + + AttachedDiskConfiguration(Type type, InterfaceType interfaceType, Boolean boot, + Boolean autoDelete) { + this.type = checkNotNull(type); + this.interfaceType = interfaceType; + this.boot = boot; + this.autoDelete = autoDelete; + } + + /** + * Returns the type of the attached disk. + */ + public Type type() { + return type; + } + + /** + * Returns the interface to use to attach the disk. If not specified, {@link InterfaceType#SCSI} + * is used. + */ + public InterfaceType interfaceType() { + return interfaceType; + } + + /** + * Returns whether to use the attached disk as a boot disk. If {@code true} the virtual machine + * will use the first partition of the disk for its root filesystem. If not specified, the + * disk is not used as a boot disk. + */ + public Boolean boot() { + return boot; + } + + /** + * Returns whether the disk should auto-delete when the instance to which it's attached is + * deleted. If not specified, the disk is not deleted automatically. + */ + public Boolean autoDelete() { + return autoDelete; + } + + MoreObjects.ToStringHelper toStringHelper() { + return MoreObjects.toStringHelper(this) + .add("type", type) + .add("interfaceType", interfaceType) + .add("boot", boot) + .add("autoDelete", autoDelete); + } + + @Override + public String toString() { + return toStringHelper().toString(); + } + + final int baseHashCode() { + return Objects.hash(type, interfaceType); + } + + final boolean baseEquals(AttachedDiskConfiguration diskConfiguration) { + return Objects.equals(toPb(), diskConfiguration.toPb()); + } + + abstract AttachedDiskConfiguration setProjectId(String projectId); + + com.google.api.services.compute.model.AttachedDisk toPb() { + com.google.api.services.compute.model.AttachedDisk attachedDiskPb = + new com.google.api.services.compute.model.AttachedDisk(); + attachedDiskPb.setType(type.name()); + if (interfaceType != null) { + attachedDiskPb.setInterface(interfaceType.name()); + } + attachedDiskPb.setBoot(boot); + attachedDiskPb.setAutoDelete(autoDelete); + return attachedDiskPb; + } + + @SuppressWarnings("unchecked") + static T fromPb( + com.google.api.services.compute.model.AttachedDisk diskPb) { + switch (Type.valueOf(diskPb.getType())) { + case PERSISTENT: + if (diskPb.getSource() == null) { + return (T) CreateDiskConfiguration.fromPb(diskPb); + } else { + return (T) PersistentDiskConfiguration.fromPb(diskPb); + } + case SCRATCH: + return (T) ScratchDiskConfiguration.fromPb(diskPb); + default: + // should be unreachable + throw new IllegalArgumentException("Unrecognized attached disk type"); + } + } + } + + /** + * An attached disk configuration for existing persistent disks. + */ + public static final class PersistentDiskConfiguration extends AttachedDiskConfiguration { + + private static final long serialVersionUID = 6367613188140104726L; + + private final DiskId sourceDisk; + private final Mode mode; + + /** + * Specifies the mode in which to attach the disk. + */ + public enum Mode { + /** + * The instance can both read and write to the disk. + */ + READ_WRITE, + + /** + * The instance is only allowed to read the disk. + */ + READ_ONLY + } + + /** + * A builder for {@code PersistentDiskConfiguration} objects. + */ + public static final class Builder { + + private DiskId sourceDisk; + private Mode mode; + private Boolean boot; + private Boolean autoDelete; + + private Builder() {} + + private Builder(PersistentDiskConfiguration configuration) { + sourceDisk = configuration.sourceDisk; + mode = configuration.mode; + boot = configuration.boot(); + autoDelete = configuration.autoDelete(); + } + + /** + * Sets the identity of the persistent disk to be attached. + */ + public Builder sourceDisk(DiskId sourceDisk) { + this.sourceDisk = checkNotNull(sourceDisk); + return this; + } + + /** + * Sets the mode in which to attach this disk. If not specified, the disk is attached in + * {@link Mode#READ_WRITE} mode. + */ + public Builder mode(Mode mode) { + this.mode = mode; + return this; + } + + /** + * Sets whether to use the attached disk as a boot disk. If {@code true} the virtual machine + * will use the first partition of the disk for its root filesystem. If not specified, the + * disk is not used as a boot disk. + */ + public Builder boot(boolean boot) { + this.boot = boot; + return this; + } + + /** + * Sets whether the disk should auto-delete when the instance to which it's attached is + * deleted. If not specified, the disk is not deleted automatically. + */ + public Builder autoDelete(boolean autoDelete) { + this.autoDelete = autoDelete; + return this; + } + + /** + * Creates a {@code PersistentDiskConfiguration} object. + */ + public PersistentDiskConfiguration build() { + return new PersistentDiskConfiguration(this); + } + } + + PersistentDiskConfiguration(Builder builder) { + super(Type.PERSISTENT, null, builder.boot, builder.autoDelete); + this.sourceDisk = checkNotNull(builder.sourceDisk); + this.mode = builder.mode; + } + + /** + * Returns the identity of the persistent disk to be attached. + */ + public DiskId sourceDisk() { + return sourceDisk; + } + + /** + * Returns the mode in which to attach this disk. If not specified, the disk is attached in + * {@link Mode#READ_WRITE} mode. + */ + public Mode mode() { + return mode; + } + + /** + * Returns a builder for the current configuration. + */ + public Builder toBuilder() { + return new Builder(this); + } + + @Override + MoreObjects.ToStringHelper toStringHelper() { + return super.toStringHelper().add("sourceDisk", sourceDisk).add("mode", mode); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode(), sourceDisk, mode); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(PersistentDiskConfiguration.class) + && baseEquals((PersistentDiskConfiguration) obj); + } + + @Override + PersistentDiskConfiguration setProjectId(String projectId) { + if (sourceDisk.project() != null) { + return this; + } + return toBuilder().sourceDisk(sourceDisk.setProjectId(projectId)).build(); + } + + @Override + com.google.api.services.compute.model.AttachedDisk toPb() { + com.google.api.services.compute.model.AttachedDisk attachedDiskPb = super.toPb(); + attachedDiskPb.setSource(sourceDisk.selfLink()); + attachedDiskPb.setMode(mode != null ? mode.toString() : null); + return attachedDiskPb; + } + + /** + * Returns a builder for a {@code PersistentDiskConfiguration} object given the identity of the + * persistent disk to attach. + */ + public static Builder builder(DiskId sourceDisk) { + return new Builder().sourceDisk(sourceDisk); + } + + /** + * Returns a {@code PersistentDiskConfiguration} object given the identity of the persistent + * disk to attach. + */ + public static PersistentDiskConfiguration of(DiskId sourceDisk) { + return builder(sourceDisk).build(); + } + + @SuppressWarnings("unchecked") + static PersistentDiskConfiguration fromPb( + com.google.api.services.compute.model.AttachedDisk diskPb) { + Builder builder = new Builder(); + builder.sourceDisk(DiskId.fromUrl(diskPb.getSource())); + if (diskPb.getMode() != null) { + builder.mode(Mode.valueOf(diskPb.getMode())); + } + if (diskPb.getBoot() != null) { + builder.boot(diskPb.getBoot()); + } + if (diskPb.getAutoDelete() != null) { + builder.autoDelete(diskPb.getAutoDelete()); + } + return builder.build(); + } + } + + /** + * An attached disk configuration for bootable persistent disks that must be created with the + * instance they are attached to. Attached disks that use this configuration can only be attached + * to an instance upon creation. + */ + public static final class CreateDiskConfiguration extends AttachedDiskConfiguration { + + private static final long serialVersionUID = 961995522284348824L; + + private final String diskName; + private final DiskTypeId diskType; + private final Long diskSizeGb; + private final ImageId sourceImage; + + /** + * A builder for {@code CreateDiskConfiguration} objects. + */ + public static final class Builder { + + private String diskName; + private DiskTypeId diskType; + private Long diskSizeGb; + private ImageId sourceImage; + private Boolean autoDelete; + + private Builder() {} + + private Builder(CreateDiskConfiguration configuration) { + this.diskName = configuration.diskName; + this.diskType = configuration.diskType; + this.diskSizeGb = configuration.diskSizeGb; + this.sourceImage = configuration.sourceImage; + this.autoDelete = configuration.autoDelete(); + } + + /** + * Sets the name to be assigned to the disk. If not specified, the disk is given the + * instance's name. + */ + public Builder diskName(String diskName) { + this.diskName = diskName; + return this; + } + + /** + * Sets the identity of the disk type. If not specified, {@code pd-standard} is used. + */ + public Builder diskType(DiskTypeId diskType) { + this.diskType = diskType; + return this; + } + + /** + * Sets the size of the persistent disk, in GB. If not set the disk will have the size of the + * image. This value can be larger than the image's size. If the provided size is smaller than + * the image's size then instance creation will fail. + */ + public Builder diskSizeGb(Long diskSizeGb) { + this.diskSizeGb = diskSizeGb; + return this; + } + + /** + * Sets the identity of the source image used to create the disk. + */ + public Builder sourceImage(ImageId sourceImage) { + this.sourceImage = checkNotNull(sourceImage); + return this; + } + + /** + * Sets whether the disk should auto-delete when the instance to which it's attached is + * deleted. If not specified, the disk is not deleted automatically. + */ + public Builder autoDelete(Boolean autoDelete) { + this.autoDelete = autoDelete; + return this; + } + + /** + * Creates a {@code CreateDiskConfiguration} object. + */ + public CreateDiskConfiguration build() { + return new CreateDiskConfiguration(this); + } + } + + CreateDiskConfiguration(Builder builder) { + super(Type.PERSISTENT, null, true, builder.autoDelete); + this.diskName = builder.diskName; + this.diskType = builder.diskType; + this.diskSizeGb = builder.diskSizeGb; + this.sourceImage = checkNotNull(builder.sourceImage); + } + + /** + * Returns the name to be assigned to the disk. If not specified, the disk is given the + * instance's name. + */ + public String diskName() { + return diskName; + } + + /** + * Returns the identity of the disk type. If not specified, {@code pd-standard} is used. + */ + public DiskTypeId diskType() { + return diskType; + } + + /** + * Returns the size of the persistent disk, in GB. If not set the disk will have the size of the + * image. This value can be larger than the image's size. If the provided size is smaller than + * the image's size then instance creation will fail. + */ + public Long diskSizeGb() { + return diskSizeGb; + } + + /** + * Returns the identity of the source image used to create the disk. + */ + public ImageId sourceImage() { + return sourceImage; + } + + /** + * Returns a builder for the current configuration. + */ + public Builder toBuilder() { + return new Builder(this); + } + + @Override + MoreObjects.ToStringHelper toStringHelper() { + return super.toStringHelper() + .add("diskName", diskName) + .add("diskType", diskType) + .add("diskSizeGb", diskSizeGb) + .add("sourceImage", sourceImage); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode(), diskName, diskType, diskSizeGb, sourceImage); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(CreateDiskConfiguration.class) + && baseEquals((CreateDiskConfiguration) obj); + } + + @Override + CreateDiskConfiguration setProjectId(String projectId) { + Builder builder = toBuilder(); + if (builder.diskType != null) { + builder.diskType(diskType.setProjectId(projectId)); + } + if (builder.sourceImage != null) { + builder.sourceImage(sourceImage.setProjectId(projectId)); + } + return builder.build(); + } + + @Override + com.google.api.services.compute.model.AttachedDisk toPb() { + AttachedDiskInitializeParams initializeParamsPb = new AttachedDiskInitializeParams(); + initializeParamsPb.setDiskName(diskName); + initializeParamsPb.setDiskSizeGb(diskSizeGb); + initializeParamsPb.setSourceImage(sourceImage.selfLink()); + if (diskType != null) { + initializeParamsPb.setDiskType(diskType.selfLink()); + } + com.google.api.services.compute.model.AttachedDisk attachedDiskPb = super.toPb(); + attachedDiskPb.setInitializeParams(initializeParamsPb); + return attachedDiskPb; + } + + /** + * Returns a builder for a {@code CreateDiskConfiguration} object given the source image that + * will be used to create the disk. + */ + public static Builder builder(ImageId sourceImage) { + return new Builder().sourceImage(sourceImage); + } + + /** + * Returns a {@code CreateDiskConfiguration} object given the source image that will be used to + * create the disk. + */ + public static CreateDiskConfiguration of(ImageId sourceImage) { + return builder(sourceImage).build(); + } + + @SuppressWarnings("unchecked") + static CreateDiskConfiguration fromPb( + com.google.api.services.compute.model.AttachedDisk diskPb) { + AttachedDiskInitializeParams initializeParamsPb = diskPb.getInitializeParams(); + Builder builder = builder(ImageId.fromUrl(initializeParamsPb.getSourceImage())); + if (initializeParamsPb.getDiskType() != null) { + builder.diskType(DiskTypeId.fromUrl(initializeParamsPb.getDiskType())); + } + builder.diskName(initializeParamsPb.getDiskName()); + builder.diskSizeGb(initializeParamsPb.getDiskSizeGb()); + builder.autoDelete(diskPb.getAutoDelete()); + if (initializeParamsPb.getDiskType() != null) { + builder.diskType(DiskTypeId.fromUrl(initializeParamsPb.getDiskType())); + } + return builder.build(); + } + } + + /** + * An attached disk configuration for scratch disks. Attached disks that use this configuration + * can only be attached to an instance upon creation. + */ + public static final class ScratchDiskConfiguration extends AttachedDiskConfiguration { + + private static final long serialVersionUID = -8445453507234691254L; + + private final DiskTypeId diskType; + + /** + * A builder for {@code ScratchDiskConfiguration} objects. + */ + public static final class Builder { + + private DiskTypeId diskType; + private InterfaceType interfaceType; + + private Builder() {} + + private Builder(ScratchDiskConfiguration configuration) { + this.diskType = configuration.diskType; + this.interfaceType = configuration.interfaceType(); + } + + /** + * Sets the identity of the disk type. + */ + public Builder diskType(DiskTypeId diskType) { + this.diskType = diskType; + return this; + } + + /** + * Sets the interface type. If not specified, {@code SCSI} is used. + */ + public Builder interfaceType(InterfaceType interfaceType) { + this.interfaceType = interfaceType; + return this; + } + + /** + * Creates a {@code ScratchDiskConfiguration} object. + */ + public ScratchDiskConfiguration build() { + return new ScratchDiskConfiguration(this); + } + } + + ScratchDiskConfiguration(Builder builder) { + super(Type.SCRATCH, builder.interfaceType, false, true); + this.diskType = builder.diskType; + } + + /** + * Returns the identity of the disk type for the scratch disk to attach. + */ + public DiskTypeId diskType() { + return diskType; + } + + /** + * Returns a builder for the current configuration. + */ + public Builder toBuilder() { + return new Builder(this); + } + + @Override + MoreObjects.ToStringHelper toStringHelper() { + return super.toStringHelper().add("diskType", diskType); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode()); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(ScratchDiskConfiguration.class) + && baseEquals((ScratchDiskConfiguration) obj); + } + + @Override + ScratchDiskConfiguration setProjectId(String projectId) { + if (diskType.project() != null) { + return this; + } + return toBuilder().diskType(diskType.setProjectId(projectId)).build(); + } + + @Override + com.google.api.services.compute.model.AttachedDisk toPb() { + com.google.api.services.compute.model.AttachedDisk attachedDiskPb = super.toPb(); + if (diskType != null) { + AttachedDiskInitializeParams initializeParamsPb = new AttachedDiskInitializeParams(); + initializeParamsPb.setDiskType(diskType.selfLink()); + attachedDiskPb.setInitializeParams(initializeParamsPb); + } + return attachedDiskPb; + } + + /** + * Returns a builder for {@code ScratchDiskConfiguration} objects given the disk type identity. + */ + public static Builder builder(DiskTypeId diskType) { + return new Builder().diskType(diskType); + } + + /** + * Returns a {@code ScratchDiskConfiguration} object given the disk type identity. The disk will + * be attached via the default interface ({@link InterfaceType#SCSI}). + */ + public static ScratchDiskConfiguration of(DiskTypeId diskType) { + return builder(diskType).build(); + } + + @SuppressWarnings("unchecked") + static ScratchDiskConfiguration fromPb( + com.google.api.services.compute.model.AttachedDisk diskPb) { + Builder builder = new Builder(); + if (diskPb.getInterface() != null) { + builder.interfaceType(InterfaceType.valueOf(diskPb.getInterface())); + } + if (diskPb.getInitializeParams() != null + && diskPb.getInitializeParams().getDiskType() != null) { + builder.diskType(DiskTypeId.fromUrl(diskPb.getInitializeParams().getDiskType())); + } + return builder.build(); + } + } + + /** + * A builder for {@code AttachedDisk} objects. + */ + public static final class Builder { + + private String deviceName; + private Integer index; + private AttachedDiskConfiguration configuration; + private List licenses; + + Builder(AttachedDiskConfiguration configuration) { + this.configuration = checkNotNull(configuration); + } + + Builder(AttachedDisk attachedDisk) { + this.deviceName = attachedDisk.deviceName; + this.index = attachedDisk.index; + this.configuration = attachedDisk.configuration; + this.licenses = attachedDisk.licenses; + } + + /** + * Sets the unique device name of your choice that is reflected into the + * {@code /dev/disk/by-id/google-*} tree of a Linux operating system running within the + * instance. This name can be used to reference the device for mounting, resizing, and so on, + * from within the instance. If not specified, the service chooses a default device name to + * apply to this disk, in the form {@code persistent-disks-x}, where x is a number assigned by + * Google Compute Engine. + */ + public Builder deviceName(String deviceName) { + this.deviceName = deviceName; + return this; + } + + /** + * Sets a zero-based index to this disk, where 0 is reserved for the boot disk. For example, + * if you have many disks attached to an instance, each disk would have an unique index number. + * If not specified, the service will choose an appropriate value. + */ + public Builder index(Integer index) { + this.index = index; + return this; + } + + /** + * Sets the attached disk configuration. Use {@link ScratchDiskConfiguration} to attach a + * scratch disk to the instance. Use {@link PersistentDiskConfiguration} to attach a + * persistent disk to the instance. Use {@link CreateDiskConfiguration} to create and attach + * a new persistent disk. + */ + public Builder configuration(AttachedDiskConfiguration configuration) { + this.configuration = checkNotNull(configuration); + return this; + } + + Builder licenses(List licenses) { + this.licenses = licenses; + return this; + } + + /** + * Creates an {@code AttachedDisk} object. + */ + public AttachedDisk build() { + return new AttachedDisk(this); + } + } + + private AttachedDisk(Builder builder) { + this.deviceName = builder.deviceName; + this.index = builder.index; + this.configuration = builder.configuration; + this.licenses = builder.licenses; + } + + /** + * Returns the unique device name of your choice that is reflected into the + * {@code /dev/disk/by-id/google-*} tree of a Linux operating system running within the + * instance. This name can be used to reference the device for mounting, resizing, and so on, + * from within the instance. If not specified, the service chooses a default device name to + * apply to this disk, in the form {@code persistent-disks-x}, where x is a number assigned by + * Google Compute Engine. + */ + public String deviceName() { + return deviceName; + } + + /** + * Returns a zero-based index to this disk, where 0 is reserved for the boot disk. + */ + public Integer index() { + return index; + } + + /** + * Sets the attached disk configuration. Returns {@link ScratchDiskConfiguration} to attach a + * scratch disk to the instance. Returns {@link PersistentDiskConfiguration} to attach a + * persistent disk to the instance. Returns {@link CreateDiskConfiguration} to create and attach + * a new persistent disk. + */ + @SuppressWarnings("unchecked") + public T configuration() { + return (T) configuration; + } + + /** + * Returns a list of publicly accessible licenses. + */ + public List licenses() { + return licenses; + } + + /** + * Returns a builder for the current attached disk. + */ + public Builder toBuilder() { + return new Builder(this); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("deviceName", deviceName) + .add("index", index) + .add("configuration", configuration) + .add("licenses", licenses) + .toString(); + } + + @Override + public final int hashCode() { + return Objects.hash(deviceName, index, configuration, licenses); + } + + @Override + public final boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(AttachedDisk.class) + && Objects.equals(toPb(), ((AttachedDisk) obj).toPb()); + } + + AttachedDisk setProjectId(String projectId) { + return toBuilder().configuration(configuration.setProjectId(projectId)).build(); + } + + com.google.api.services.compute.model.AttachedDisk toPb() { + com.google.api.services.compute.model.AttachedDisk attachedDiskPb = configuration.toPb(); + attachedDiskPb.setDeviceName(deviceName); + attachedDiskPb.setIndex(index); + if (licenses != null) { + attachedDiskPb.setLicenses(Lists.transform(licenses, LicenseId.TO_URL_FUNCTION)); + } + return attachedDiskPb; + } + + /** + * Returns a builder for an {@code AttachedDisk} object given its configuration. + */ + public static Builder builder(AttachedDiskConfiguration configuration) { + return new Builder(configuration).configuration(configuration); + } + + /** + * Returns an {@code AttachedDisk} object given its configuration. + */ + public static AttachedDisk of(AttachedDiskConfiguration configuration) { + return builder(configuration).build(); + } + + /** + * Returns an {@code AttachedDisk} object given its configuration and the device name. + */ + public static AttachedDisk of(String deviceName, AttachedDiskConfiguration configuration) { + return builder(configuration).deviceName(deviceName).build(); + } + + static AttachedDisk fromPb(com.google.api.services.compute.model.AttachedDisk diskPb) { + AttachedDiskConfiguration configuration = AttachedDiskConfiguration.fromPb(diskPb); + Builder builder = builder(configuration); + builder.deviceName(diskPb.getDeviceName()); + builder.index(diskPb.getIndex()); + if (diskPb.getLicenses() != null) { + builder.licenses(Lists.transform(diskPb.getLicenses(), LicenseId.FROM_URL_FUNCTION)); + } + return builder.build(); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/AttachedDiskTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/AttachedDiskTest.java new file mode 100644 index 000000000000..4a9cfbfdc7a9 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/AttachedDiskTest.java @@ -0,0 +1,392 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed 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 com.google.gcloud.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableList; +import com.google.gcloud.compute.AttachedDisk.AttachedDiskConfiguration.InterfaceType; +import com.google.gcloud.compute.AttachedDisk.AttachedDiskConfiguration.Type; +import com.google.gcloud.compute.AttachedDisk.CreateDiskConfiguration; +import com.google.gcloud.compute.AttachedDisk.PersistentDiskConfiguration; +import com.google.gcloud.compute.AttachedDisk.ScratchDiskConfiguration; + +import org.junit.Test; + +import java.util.List; + +public class AttachedDiskTest { + + private static final Boolean AUTO_DELETE = true; + private static final Boolean BOOT = true; + private static final Integer INDEX = 0; + private static final String DEVICE_NAME = "deviceName"; + private static final String DISK_NAME = "diskName"; + private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType"); + private static final Long DISK_SIZE_GB = 42L; + private static final DiskId DISK_ID = DiskId.of("project", "zone", "disk"); + private static final ImageId IMAGE_ID = ImageId.of("project", "image"); + private static final InterfaceType INTERFACE_TYPE = InterfaceType.NVME; + private static final PersistentDiskConfiguration.Mode MODE = + PersistentDiskConfiguration.Mode.READ_ONLY; + private static final PersistentDiskConfiguration PERSISTENT_DISK_CONFIGURATION = + PersistentDiskConfiguration.builder(DISK_ID) + .boot(BOOT) + .autoDelete(AUTO_DELETE) + .mode(MODE) + .build(); + private static final ScratchDiskConfiguration SCRATCH_DISK_CONFIGURATION = + ScratchDiskConfiguration.builder(DISK_TYPE_ID).interfaceType(INTERFACE_TYPE).build(); + private static final CreateDiskConfiguration CREATE_DISK_CONFIGURATION = + CreateDiskConfiguration.builder(IMAGE_ID) + .autoDelete(AUTO_DELETE) + .diskName(DISK_NAME) + .diskType(DISK_TYPE_ID) + .diskSizeGb(DISK_SIZE_GB) + .sourceImage(IMAGE_ID) + .build(); + private static final List LICENSES = ImmutableList.of( + LicenseId.of("project", "license1"), LicenseId.of("project", "license2")); + private static final AttachedDisk PERSISTENT_DISK = + AttachedDisk.builder(PERSISTENT_DISK_CONFIGURATION) + .deviceName(DEVICE_NAME) + .index(INDEX) + .licenses(LICENSES) + .build(); + private static final AttachedDisk SCRATCH_DISK = + AttachedDisk.builder(SCRATCH_DISK_CONFIGURATION) + .deviceName(DEVICE_NAME) + .index(INDEX) + .licenses(LICENSES) + .build(); + private static final AttachedDisk CREATED_DISK = + AttachedDisk.builder(CREATE_DISK_CONFIGURATION) + .deviceName(DEVICE_NAME) + .index(INDEX) + .licenses(LICENSES) + .build(); + + @Test + public void testConfigurationToBuilder() { + comparePersistentDiskConfiguration(PERSISTENT_DISK_CONFIGURATION, + PERSISTENT_DISK_CONFIGURATION.toBuilder().build()); + compareScratchDiskConfiguration(SCRATCH_DISK_CONFIGURATION, + SCRATCH_DISK_CONFIGURATION.toBuilder().build()); + compareCreateDiskConfiguration(CREATE_DISK_CONFIGURATION, + CREATE_DISK_CONFIGURATION.toBuilder().build()); + PersistentDiskConfiguration persistentDiskConfiguration = + PERSISTENT_DISK_CONFIGURATION.toBuilder().autoDelete(false).build(); + assertFalse(persistentDiskConfiguration.autoDelete()); + persistentDiskConfiguration = + persistentDiskConfiguration.toBuilder().autoDelete(AUTO_DELETE).build(); + assertEquals(PERSISTENT_DISK_CONFIGURATION, persistentDiskConfiguration); + ScratchDiskConfiguration scratchDiskConfiguration = + SCRATCH_DISK_CONFIGURATION.toBuilder().interfaceType(InterfaceType.SCSI).build(); + assertEquals(InterfaceType.SCSI, scratchDiskConfiguration.interfaceType()); + scratchDiskConfiguration = + scratchDiskConfiguration.toBuilder().interfaceType(INTERFACE_TYPE).build(); + assertEquals(SCRATCH_DISK_CONFIGURATION, scratchDiskConfiguration); + CreateDiskConfiguration createDiskConfiguration = + CREATE_DISK_CONFIGURATION.toBuilder().autoDelete(false).build(); + assertFalse(createDiskConfiguration.autoDelete()); + createDiskConfiguration = createDiskConfiguration.toBuilder().autoDelete(AUTO_DELETE).build(); + assertEquals(CREATE_DISK_CONFIGURATION, createDiskConfiguration); + } + + @Test + public void testConfigurationToBuilderIncomplete() { + PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DISK_ID); + comparePersistentDiskConfiguration(persistentConfiguration, + AttachedDisk.AttachedDiskConfiguration.fromPb( + persistentConfiguration.toPb())); + ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DISK_TYPE_ID); + compareScratchDiskConfiguration(scratchDiskConfiguration, + AttachedDisk.AttachedDiskConfiguration.fromPb( + scratchDiskConfiguration.toPb())); + CreateDiskConfiguration createDiskConfiguration = CreateDiskConfiguration.of(IMAGE_ID); + compareCreateDiskConfiguration(createDiskConfiguration, + AttachedDisk.AttachedDiskConfiguration.fromPb( + createDiskConfiguration.toPb())); + } + + @Test + public void testToBuilder() { + compareAttachedDisk(PERSISTENT_DISK, PERSISTENT_DISK.toBuilder().build()); + compareAttachedDisk(SCRATCH_DISK, SCRATCH_DISK.toBuilder().build()); + compareAttachedDisk(CREATED_DISK, CREATED_DISK.toBuilder().build()); + AttachedDisk attachedDisk = PERSISTENT_DISK.toBuilder().deviceName("newDeviceName").build(); + assertEquals("newDeviceName", attachedDisk.deviceName()); + attachedDisk = attachedDisk.toBuilder().deviceName(DEVICE_NAME).build(); + compareAttachedDisk(PERSISTENT_DISK, attachedDisk); + } + + @Test + public void testToBuilderIncomplete() { + AttachedDisk attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); + assertEquals(attachedDisk, attachedDisk.toBuilder().build()); + attachedDisk = AttachedDisk.of(SCRATCH_DISK_CONFIGURATION); + assertEquals(attachedDisk, attachedDisk.toBuilder().build()); + attachedDisk = AttachedDisk.of(CREATE_DISK_CONFIGURATION); + assertEquals(attachedDisk, attachedDisk.toBuilder().build()); + } + + @Test + public void testConfigurationBuilder() { + assertTrue(CREATE_DISK_CONFIGURATION.boot()); + assertEquals(AUTO_DELETE, CREATE_DISK_CONFIGURATION.autoDelete()); + assertNull(CREATE_DISK_CONFIGURATION.interfaceType()); + assertEquals(Type.PERSISTENT, CREATE_DISK_CONFIGURATION.type()); + assertEquals(IMAGE_ID, CREATE_DISK_CONFIGURATION.sourceImage()); + assertEquals(DISK_NAME, CREATE_DISK_CONFIGURATION.diskName()); + assertEquals(DISK_TYPE_ID, CREATE_DISK_CONFIGURATION.diskType()); + assertEquals(DISK_SIZE_GB, CREATE_DISK_CONFIGURATION.diskSizeGb()); + assertEquals(IMAGE_ID, CREATE_DISK_CONFIGURATION.sourceImage()); + + assertEquals(BOOT, PERSISTENT_DISK_CONFIGURATION.boot()); + assertEquals(AUTO_DELETE, PERSISTENT_DISK_CONFIGURATION.autoDelete()); + assertNull(PERSISTENT_DISK_CONFIGURATION.interfaceType()); + assertEquals(Type.PERSISTENT, PERSISTENT_DISK_CONFIGURATION.type()); + assertEquals(MODE, PERSISTENT_DISK_CONFIGURATION.mode()); + assertEquals(DISK_ID, PERSISTENT_DISK_CONFIGURATION.sourceDisk()); + + assertFalse(SCRATCH_DISK_CONFIGURATION.boot()); + assertTrue(SCRATCH_DISK_CONFIGURATION.autoDelete()); + assertEquals(INTERFACE_TYPE, SCRATCH_DISK_CONFIGURATION.interfaceType()); + assertEquals(Type.SCRATCH, SCRATCH_DISK_CONFIGURATION.type()); + assertEquals(DISK_TYPE_ID, SCRATCH_DISK_CONFIGURATION.diskType()); + } + + @Test + public void testBuilder() { + assertEquals(PERSISTENT_DISK_CONFIGURATION, PERSISTENT_DISK.configuration()); + assertEquals(DEVICE_NAME, PERSISTENT_DISK.deviceName()); + assertEquals(INDEX, PERSISTENT_DISK.index()); + assertEquals(LICENSES, PERSISTENT_DISK.licenses()); + assertEquals(SCRATCH_DISK_CONFIGURATION, SCRATCH_DISK.configuration()); + assertEquals(DEVICE_NAME, SCRATCH_DISK.deviceName()); + assertEquals(INDEX, SCRATCH_DISK.index()); + assertEquals(LICENSES, SCRATCH_DISK.licenses()); + assertEquals(CREATE_DISK_CONFIGURATION, CREATED_DISK.configuration()); + assertEquals(DEVICE_NAME, CREATED_DISK.deviceName()); + assertEquals(INDEX, CREATED_DISK.index()); + assertEquals(LICENSES, CREATED_DISK.licenses()); + } + + @Test + public void testConfigurationOf() { + PersistentDiskConfiguration persistentConfiguration = PersistentDiskConfiguration.of(DISK_ID); + assertEquals(DISK_ID, persistentConfiguration.sourceDisk()); + assertEquals(Type.PERSISTENT, persistentConfiguration.type()); + assertNull(persistentConfiguration.autoDelete()); + assertNull(persistentConfiguration.boot()); + assertNull(persistentConfiguration.interfaceType()); + ScratchDiskConfiguration scratchDiskConfiguration = ScratchDiskConfiguration.of(DISK_TYPE_ID); + assertEquals(DISK_TYPE_ID, scratchDiskConfiguration.diskType()); + assertNull(scratchDiskConfiguration.interfaceType()); + assertEquals(Type.SCRATCH, scratchDiskConfiguration.type()); + assertTrue(scratchDiskConfiguration.autoDelete()); + assertFalse(scratchDiskConfiguration.boot()); + assertNull(scratchDiskConfiguration.interfaceType()); + CreateDiskConfiguration createDiskConfiguration = CreateDiskConfiguration.of(IMAGE_ID); + assertEquals(IMAGE_ID, createDiskConfiguration.sourceImage()); + assertNull(createDiskConfiguration.diskType()); + assertNull(createDiskConfiguration.diskName()); + assertNull(createDiskConfiguration.diskSizeGb()); + assertNull(createDiskConfiguration.interfaceType()); + assertEquals(Type.PERSISTENT, createDiskConfiguration.type()); + assertNull(createDiskConfiguration.autoDelete()); + assertTrue(createDiskConfiguration.boot()); + assertNull(createDiskConfiguration.interfaceType()); + } + + @Test + public void testOf() { + AttachedDisk attachedDisk = AttachedDisk.of(DEVICE_NAME, PERSISTENT_DISK_CONFIGURATION); + assertEquals(PERSISTENT_DISK_CONFIGURATION, attachedDisk.configuration()); + assertEquals(DEVICE_NAME, attachedDisk.deviceName()); + assertNull(attachedDisk.index()); + assertNull(attachedDisk.licenses()); + attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); + assertEquals(PERSISTENT_DISK_CONFIGURATION, attachedDisk.configuration()); + assertNull(attachedDisk.deviceName()); + assertNull(attachedDisk.index()); + assertNull(attachedDisk.licenses()); + attachedDisk = AttachedDisk.of(DEVICE_NAME, SCRATCH_DISK_CONFIGURATION); + assertEquals(SCRATCH_DISK_CONFIGURATION, attachedDisk.configuration()); + assertEquals(DEVICE_NAME, attachedDisk.deviceName()); + assertNull(attachedDisk.index()); + assertNull(attachedDisk.licenses()); + attachedDisk = AttachedDisk.of(SCRATCH_DISK_CONFIGURATION); + assertEquals(SCRATCH_DISK_CONFIGURATION, attachedDisk.configuration()); + assertNull(attachedDisk.deviceName()); + assertNull(attachedDisk.index()); + assertNull(attachedDisk.licenses()); + attachedDisk = AttachedDisk.of(DEVICE_NAME, CREATE_DISK_CONFIGURATION); + assertEquals(CREATE_DISK_CONFIGURATION, attachedDisk.configuration()); + assertEquals(DEVICE_NAME, attachedDisk.deviceName()); + assertNull(attachedDisk.index()); + assertNull(attachedDisk.licenses()); + attachedDisk = AttachedDisk.of(CREATE_DISK_CONFIGURATION); + assertEquals(CREATE_DISK_CONFIGURATION, attachedDisk.configuration()); + assertNull(attachedDisk.deviceName()); + assertNull(attachedDisk.index()); + assertNull(attachedDisk.licenses()); + } + + @Test + public void testConfigurationToAndFromPb() { + PersistentDiskConfiguration persistentConfiguration = + PersistentDiskConfiguration.of(DISK_ID); + comparePersistentDiskConfiguration(persistentConfiguration, + AttachedDisk.AttachedDiskConfiguration.fromPb( + persistentConfiguration.toPb())); + comparePersistentDiskConfiguration(PERSISTENT_DISK_CONFIGURATION, + AttachedDisk.AttachedDiskConfiguration.fromPb( + PERSISTENT_DISK_CONFIGURATION.toPb())); + ScratchDiskConfiguration scratchDiskConfiguration = + ScratchDiskConfiguration.of(DISK_TYPE_ID); + compareScratchDiskConfiguration(scratchDiskConfiguration, + AttachedDisk.AttachedDiskConfiguration.fromPb( + scratchDiskConfiguration.toPb())); + compareScratchDiskConfiguration(SCRATCH_DISK_CONFIGURATION, + AttachedDisk.AttachedDiskConfiguration.fromPb( + SCRATCH_DISK_CONFIGURATION.toPb())); + CreateDiskConfiguration createDiskConfiguration = + CreateDiskConfiguration.of(IMAGE_ID); + compareCreateDiskConfiguration(createDiskConfiguration, + AttachedDisk.AttachedDiskConfiguration.fromPb( + createDiskConfiguration.toPb())); + compareCreateDiskConfiguration(CREATE_DISK_CONFIGURATION, + AttachedDisk.AttachedDiskConfiguration.fromPb( + CREATE_DISK_CONFIGURATION.toPb())); + } + + @Test + public void testToAndFromPb() { + AttachedDisk attachedDisk = AttachedDisk.fromPb(PERSISTENT_DISK.toPb()); + compareAttachedDisk(PERSISTENT_DISK, attachedDisk); + attachedDisk = AttachedDisk.fromPb(SCRATCH_DISK.toPb()); + compareAttachedDisk(SCRATCH_DISK, attachedDisk); + attachedDisk = AttachedDisk.fromPb(CREATED_DISK.toPb()); + compareAttachedDisk(CREATED_DISK, attachedDisk); + attachedDisk = AttachedDisk.of(DEVICE_NAME, PERSISTENT_DISK_CONFIGURATION); + compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); + attachedDisk = AttachedDisk.of(PERSISTENT_DISK_CONFIGURATION); + compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); + attachedDisk = AttachedDisk.of(DEVICE_NAME, SCRATCH_DISK_CONFIGURATION); + compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); + attachedDisk = AttachedDisk.of(SCRATCH_DISK_CONFIGURATION); + compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); + attachedDisk = AttachedDisk.of(DEVICE_NAME, CREATE_DISK_CONFIGURATION); + compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); + attachedDisk = AttachedDisk.of(CREATE_DISK_CONFIGURATION); + compareAttachedDisk(attachedDisk, AttachedDisk.fromPb(attachedDisk.toPb())); + } + + @Test + public void testConfigurationSetProjectId() { + PersistentDiskConfiguration persistentConfiguration = + PersistentDiskConfiguration.of(DiskId.of("zone", "disk")); + comparePersistentDiskConfiguration( + PersistentDiskConfiguration.of(DiskId.of("project", "zone", "disk")), + persistentConfiguration.setProjectId("project")); + ScratchDiskConfiguration scratchDiskConfiguration = + ScratchDiskConfiguration.of(DiskTypeId.of("zone", "diskType")); + compareScratchDiskConfiguration( + ScratchDiskConfiguration.of(DiskTypeId.of("project", "zone", "diskType")), + scratchDiskConfiguration.setProjectId("project")); + CreateDiskConfiguration createDiskConfiguration = CREATE_DISK_CONFIGURATION.toBuilder() + .diskType(DiskTypeId.of("zone", "diskType")) + .sourceImage(ImageId.of("image")) + .build(); + compareCreateDiskConfiguration(CREATE_DISK_CONFIGURATION, + createDiskConfiguration.setProjectId("project")); + } + + @Test + public void testSetProjectId() { + PersistentDiskConfiguration persistentConfiguration = + PersistentDiskConfiguration.of(DiskId.of("zone", "disk")); + PersistentDiskConfiguration persistentConfigurationWithProject = + PersistentDiskConfiguration.of(DiskId.of("project", "zone", "disk")); + AttachedDisk attachedDisk = AttachedDisk.of(persistentConfiguration); + compareAttachedDisk(AttachedDisk.of(persistentConfigurationWithProject), + attachedDisk.setProjectId("project")); + ScratchDiskConfiguration scratchDiskConfiguration = + ScratchDiskConfiguration.of(DiskTypeId.of("zone", "diskType")); + ScratchDiskConfiguration scratchDiskConfigurationWithProject = + ScratchDiskConfiguration.of(DiskTypeId.of("project", "zone", "diskType")); + compareAttachedDisk(AttachedDisk.of(scratchDiskConfigurationWithProject), + AttachedDisk.of(scratchDiskConfiguration).setProjectId("project")); + CreateDiskConfiguration createDiskConfiguration = + CreateDiskConfiguration.of(ImageId.of("image")); + CreateDiskConfiguration createDiskConfigurationWithProject = + CreateDiskConfiguration.of(ImageId.of("project", "image")); + compareAttachedDisk(AttachedDisk.of(createDiskConfigurationWithProject), + AttachedDisk.of(createDiskConfiguration).setProjectId("project")); + createDiskConfiguration = CREATE_DISK_CONFIGURATION.toBuilder() + .diskType(DiskTypeId.of("zone", "diskType")) + .sourceImage(ImageId.of("image")) + .build(); + compareAttachedDisk(AttachedDisk.of(CREATE_DISK_CONFIGURATION), + AttachedDisk.of(createDiskConfiguration).setProjectId("project")); + } + + public void compareAttachedDiskConfiguration(AttachedDisk.AttachedDiskConfiguration expected, + AttachedDisk.AttachedDiskConfiguration value) { + assertEquals(expected, value); + assertEquals(expected.type(), value.type()); + assertEquals(expected.interfaceType(), value.interfaceType()); + assertEquals(expected.boot(), value.boot()); + assertEquals(expected.autoDelete(), value.autoDelete()); + assertEquals(expected.hashCode(), value.hashCode()); + } + + public void comparePersistentDiskConfiguration(PersistentDiskConfiguration expected, + PersistentDiskConfiguration value) { + compareAttachedDiskConfiguration(expected, value); + assertEquals(expected.mode(), value.mode()); + assertEquals(expected.sourceDisk(), value.sourceDisk()); + } + + public void compareCreateDiskConfiguration(CreateDiskConfiguration expected, + CreateDiskConfiguration value) { + compareAttachedDiskConfiguration(expected, value); + assertEquals(expected.diskName(), value.diskName()); + assertEquals(expected.diskType(), value.diskType()); + assertEquals(expected.diskSizeGb(), value.diskSizeGb()); + assertEquals(expected.sourceImage(), value.sourceImage()); + } + + public void compareScratchDiskConfiguration(ScratchDiskConfiguration expected, + ScratchDiskConfiguration value) { + compareAttachedDiskConfiguration(expected, value); + assertEquals(expected.diskType(), value.diskType()); + } + + public void compareAttachedDisk(AttachedDisk expected, AttachedDisk value) { + assertEquals(expected, value); + assertEquals(expected.deviceName(), value.deviceName()); + assertEquals(expected.index(), value.index()); + assertEquals(expected.configuration(), value.configuration()); + assertEquals(expected.licenses(), value.licenses()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java index bec8844ae111..b9dc43d7f19f 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java @@ -22,6 +22,9 @@ import com.google.common.collect.ImmutableList; import com.google.gcloud.AuthCredentials; import com.google.gcloud.RetryParams; +import com.google.gcloud.compute.AttachedDisk.CreateDiskConfiguration; +import com.google.gcloud.compute.AttachedDisk.PersistentDiskConfiguration; +import com.google.gcloud.compute.AttachedDisk.ScratchDiskConfiguration; import org.junit.Test; @@ -156,6 +159,13 @@ public class SerializationTest { private static final DiskInfo DISK_INFO = DiskInfo.of(DISK_ID, STANDARD_DISK_CONFIGURATION); private static final Disk DISK = new Disk.Builder(COMPUTE, DISK_ID, STANDARD_DISK_CONFIGURATION).build(); + private static final CreateDiskConfiguration CREATE_DISK_CONFIGURATION = + CreateDiskConfiguration.of(IMAGE_ID); + private static final PersistentDiskConfiguration PERSISTENT_DISK_CONFIGURATION = + PersistentDiskConfiguration.of(DISK_ID); + private static final ScratchDiskConfiguration SCRATCH_DISK_CONFIGURATION = + ScratchDiskConfiguration.of(DISK_TYPE_ID); + private static final AttachedDisk ATTACHED_DISK = AttachedDisk.of(CREATE_DISK_CONFIGURATION); private static final Compute.DiskTypeOption DISK_TYPE_OPTION = Compute.DiskTypeOption.fields(); private static final Compute.DiskTypeFilter DISK_TYPE_FILTER = @@ -241,14 +251,15 @@ public void testModelAndRequests() throws Exception { ADDRESS_INFO, ADDRESS, DISK_ID, SNAPSHOT_ID, SNAPSHOT_INFO, SNAPSHOT, IMAGE_ID, DISK_IMAGE_CONFIGURATION, STORAGE_IMAGE_CONFIGURATION, IMAGE_INFO, IMAGE, STANDARD_DISK_CONFIGURATION, IMAGE_DISK_CONFIGURATION, SNAPSHOT_DISK_CONFIGURATION, - DISK_INFO, DISK, DISK_TYPE_OPTION, DISK_TYPE_FILTER, DISK_TYPE_LIST_OPTION, - DISK_TYPE_AGGREGATED_LIST_OPTION, MACHINE_TYPE_OPTION, MACHINE_TYPE_FILTER, - MACHINE_TYPE_LIST_OPTION, MACHINE_TYPE_AGGREGATED_LIST_OPTION, REGION_OPTION, REGION_FILTER, - REGION_LIST_OPTION, ZONE_OPTION, ZONE_FILTER, ZONE_LIST_OPTION, LICENSE_OPTION, - OPERATION_OPTION, OPERATION_FILTER, OPERATION_LIST_OPTION, ADDRESS_OPTION, ADDRESS_FILTER, - ADDRESS_LIST_OPTION, ADDRESS_AGGREGATED_LIST_OPTION, SNAPSHOT_OPTION, SNAPSHOT_FILTER, - SNAPSHOT_LIST_OPTION, IMAGE_OPTION, IMAGE_FILTER, IMAGE_LIST_OPTION, DISK_OPTION, - DISK_FILTER, DISK_LIST_OPTION, DISK_AGGREGATED_LIST_OPTION}; + DISK_INFO, DISK, CREATE_DISK_CONFIGURATION, PERSISTENT_DISK_CONFIGURATION, + SCRATCH_DISK_CONFIGURATION, ATTACHED_DISK, DISK_TYPE_OPTION, DISK_TYPE_FILTER, + DISK_TYPE_LIST_OPTION, DISK_TYPE_AGGREGATED_LIST_OPTION, MACHINE_TYPE_OPTION, + MACHINE_TYPE_FILTER, MACHINE_TYPE_LIST_OPTION, MACHINE_TYPE_AGGREGATED_LIST_OPTION, + REGION_OPTION, REGION_FILTER, REGION_LIST_OPTION, ZONE_OPTION, ZONE_FILTER, + ZONE_LIST_OPTION, LICENSE_OPTION, OPERATION_OPTION, OPERATION_FILTER, OPERATION_LIST_OPTION, + ADDRESS_OPTION, ADDRESS_FILTER, ADDRESS_LIST_OPTION, ADDRESS_AGGREGATED_LIST_OPTION, + SNAPSHOT_OPTION, SNAPSHOT_FILTER, SNAPSHOT_LIST_OPTION, IMAGE_OPTION, IMAGE_FILTER, + IMAGE_LIST_OPTION, DISK_OPTION, DISK_FILTER, DISK_LIST_OPTION, DISK_AGGREGATED_LIST_OPTION}; for (Serializable obj : objects) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj);