|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.gcloud.compute; |
| 18 | + |
| 19 | +import com.google.api.services.compute.model.Disk; |
| 20 | +import com.google.common.base.MoreObjects; |
| 21 | +import com.google.common.base.MoreObjects.ToStringHelper; |
| 22 | + |
| 23 | +import java.io.Serializable; |
| 24 | +import java.util.Objects; |
| 25 | + |
| 26 | +/** |
| 27 | + * Base class for Google Compute Engine disk configurations. A disk can be used as primary storage |
| 28 | + * for your virtual machine instances. Use {@link StandardDiskConfiguration} to create a standard |
| 29 | + * disk given a disk type and size. Use {@link ImageDiskConfiguration} to create a disk from a |
| 30 | + * Compute Engine disk image. Use {@link SnapshotDiskConfiguration} to create a disk from a Compute |
| 31 | + * Engine disk snapshot. |
| 32 | + * |
| 33 | + * @see <a href="https://cloud.google.com/compute/docs/disks/">Block Storage</a> |
| 34 | + */ |
| 35 | +public abstract class DiskConfiguration implements Serializable { |
| 36 | + |
| 37 | + private static final long serialVersionUID = -1783061701255428417L; |
| 38 | + |
| 39 | + private final Type type; |
| 40 | + private final Long sizeGb; |
| 41 | + private final DiskTypeId diskType; |
| 42 | + |
| 43 | + /** |
| 44 | + * Type of a Google Compute Engine disk configuration. |
| 45 | + */ |
| 46 | + public enum Type { |
| 47 | + /** |
| 48 | + * A Google Compute Engine standard disk configuration. |
| 49 | + */ |
| 50 | + STANDARD, |
| 51 | + |
| 52 | + /** |
| 53 | + * A Google Compute Engine disk configuration that creates a disk from an image. |
| 54 | + */ |
| 55 | + IMAGE, |
| 56 | + |
| 57 | + /** |
| 58 | + * A Google Compute Engine disk configuration that creates a disk from a snapshot. |
| 59 | + */ |
| 60 | + SNAPSHOT |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Base builder for disk configurations. |
| 65 | + * |
| 66 | + * @param <T> the disk configuration type |
| 67 | + * @param <B> the disk configuration builder |
| 68 | + */ |
| 69 | + public abstract static class Builder<T extends DiskConfiguration, B extends Builder<T, B>> { |
| 70 | + |
| 71 | + private Type type; |
| 72 | + private Long sizeGb; |
| 73 | + private DiskTypeId diskType; |
| 74 | + |
| 75 | + Builder(Type type) { |
| 76 | + this.type = type; |
| 77 | + } |
| 78 | + |
| 79 | + Builder(DiskConfiguration diskConfiguration) { |
| 80 | + this.type = diskConfiguration.type; |
| 81 | + this.sizeGb = diskConfiguration.sizeGb; |
| 82 | + this.diskType = diskConfiguration.diskType; |
| 83 | + } |
| 84 | + |
| 85 | + Builder(Type type, Disk diskPb) { |
| 86 | + this.type = type; |
| 87 | + this.sizeGb = diskPb.getSizeGb(); |
| 88 | + if (diskPb.getType() != null) { |
| 89 | + this.diskType = DiskTypeId.fromUrl(diskPb.getType()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + protected B self() { |
| 95 | + return (B) this; |
| 96 | + } |
| 97 | + |
| 98 | + B type(Type type) { |
| 99 | + this.type = type; |
| 100 | + return self(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Sets the size of the persistent disk, in GB. |
| 105 | + */ |
| 106 | + public B sizeGb(Long sizeGb) { |
| 107 | + this.sizeGb = sizeGb; |
| 108 | + return self(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets the identity of the disk type. If not set {@code pd-standard} will be used. |
| 113 | + */ |
| 114 | + public B diskType(DiskTypeId diskType) { |
| 115 | + this.diskType = diskType; |
| 116 | + return self(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Creates an object. |
| 121 | + */ |
| 122 | + public abstract T build(); |
| 123 | + } |
| 124 | + |
| 125 | + DiskConfiguration(Builder builder) { |
| 126 | + this.type = builder.type; |
| 127 | + this.sizeGb = builder.sizeGb; |
| 128 | + this.diskType = builder.diskType; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns the disk configuration's type. This method returns {@link Type#STANDARD} for a standard |
| 133 | + * configuration that creates a disk given its type and size. This method returns |
| 134 | + * {@link Type#SNAPSHOT} for a configuration that creates a disk from a Google Compute Engine |
| 135 | + * snapshot. This method returns {@link Type#IMAGE} for a configuration that creates a disk |
| 136 | + * from a Google Compute Engine image. |
| 137 | + */ |
| 138 | + public Type type() { |
| 139 | + return type; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Returns the size of the persistent disk, in GB. |
| 144 | + */ |
| 145 | + public Long sizeGb() { |
| 146 | + return sizeGb; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns the identity of the disk type. |
| 151 | + */ |
| 152 | + public DiskTypeId diskType() { |
| 153 | + return diskType; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Returns a builder for the object. |
| 158 | + */ |
| 159 | + public abstract Builder toBuilder(); |
| 160 | + |
| 161 | + ToStringHelper toStringHelper() { |
| 162 | + return MoreObjects.toStringHelper(this) |
| 163 | + .add("type", type) |
| 164 | + .add("sizeGb", sizeGb) |
| 165 | + .add("diskType", diskType); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public String toString() { |
| 170 | + return toStringHelper().toString(); |
| 171 | + } |
| 172 | + |
| 173 | + final int baseHashCode() { |
| 174 | + return Objects.hash(type, sizeGb, diskType); |
| 175 | + } |
| 176 | + |
| 177 | + final boolean baseEquals(DiskConfiguration diskConfiguration) { |
| 178 | + return diskConfiguration != null |
| 179 | + && getClass().equals(diskConfiguration.getClass()) |
| 180 | + && Objects.equals(toPb(), diskConfiguration.toPb()); |
| 181 | + } |
| 182 | + |
| 183 | + abstract DiskConfiguration setProjectId(String projectId); |
| 184 | + |
| 185 | + Disk toPb() { |
| 186 | + Disk diskPb = new Disk(); |
| 187 | + diskPb.setSizeGb(sizeGb); |
| 188 | + if (diskType != null) { |
| 189 | + diskPb.setType(diskType.selfLink()); |
| 190 | + } |
| 191 | + return diskPb; |
| 192 | + } |
| 193 | + |
| 194 | + @SuppressWarnings("unchecked") |
| 195 | + static <T extends DiskConfiguration> T fromPb(Disk diskPb) { |
| 196 | + if (diskPb.getSourceImage() != null) { |
| 197 | + return (T) ImageDiskConfiguration.fromPb(diskPb); |
| 198 | + } else if (diskPb.getSourceSnapshot() != null) { |
| 199 | + return (T) SnapshotDiskConfiguration.fromPb(diskPb); |
| 200 | + } |
| 201 | + return (T) StandardDiskConfiguration.fromPb(diskPb); |
| 202 | + } |
| 203 | +} |
0 commit comments