|
| 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 static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.common.base.MoreObjects; |
| 22 | + |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.regex.Matcher; |
| 25 | +import java.util.regex.Pattern; |
| 26 | + |
| 27 | +/** |
| 28 | + * Identity for a Google Compute Engine disk. |
| 29 | + */ |
| 30 | +public final class DiskId extends ResourceId { |
| 31 | + |
| 32 | + private static final String REGEX = ResourceId.REGEX + "zones/([^/]+)/disks/([^/]+)"; |
| 33 | + private static final Pattern PATTERN = Pattern.compile(REGEX); |
| 34 | + private static final long serialVersionUID = -8761290740495870787L; |
| 35 | + |
| 36 | + private final String zone; |
| 37 | + private final String disk; |
| 38 | + |
| 39 | + private DiskId(String project, String zone, String disk) { |
| 40 | + super(project); |
| 41 | + this.zone = checkNotNull(zone); |
| 42 | + this.disk = checkNotNull(disk); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the name of the zone this disk belongs to. |
| 47 | + */ |
| 48 | + public String zone() { |
| 49 | + return zone; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the identity of the zone this disk belongs to. |
| 54 | + */ |
| 55 | + public ZoneId zoneId() { |
| 56 | + return ZoneId.of(project(), zone); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the name of the disk. The name must be 1-63 characters long and comply with RFC1035. |
| 61 | + * Specifically, the name must match the regular expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} |
| 62 | + * which means the first character must be a lowercase letter, and all following characters must |
| 63 | + * be a dash, lowercase letter, or digit, except the last character, which cannot be a dash. |
| 64 | + * |
| 65 | + * @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> |
| 66 | + */ |
| 67 | + public String disk() { |
| 68 | + return disk; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String selfLink() { |
| 73 | + return super.selfLink() + "/zones/" + zone + "/disks/" + disk; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + MoreObjects.ToStringHelper toStringHelper() { |
| 78 | + return super.toStringHelper().add("zone", zone).add("disk", disk); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int hashCode() { |
| 83 | + return Objects.hash(super.baseHashCode(), zone, disk); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean equals(Object obj) { |
| 88 | + if (obj == this) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + if (!(obj instanceof DiskId)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + DiskId other = (DiskId) obj; |
| 95 | + return baseEquals(other) |
| 96 | + && Objects.equals(zone, other.zone) |
| 97 | + && Objects.equals(disk, other.disk); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + DiskId setProjectId(String projectId) { |
| 102 | + if (project() != null) { |
| 103 | + return this; |
| 104 | + } |
| 105 | + return DiskId.of(projectId, zone, disk); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns a disk identity given the zone identity and the disk name. The disk name must be 1-63 |
| 110 | + * characters long and comply with RFC1035. Specifically, the name must match the regular |
| 111 | + * expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a |
| 112 | + * lowercase letter, and all following characters must be a dash, lowercase letter, or digit, |
| 113 | + * except the last character, which cannot be a dash. |
| 114 | + * |
| 115 | + * @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> |
| 116 | + */ |
| 117 | + public static DiskId of(ZoneId zoneId, String disk) { |
| 118 | + return new DiskId(zoneId.project(), zoneId.zone(), disk); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns a disk identity given the zone and disk names. The disk name must be 1-63 characters |
| 123 | + * long and comply with RFC1035. Specifically, the name must match the regular expression |
| 124 | + * {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, |
| 125 | + * and all following characters must be a dash, lowercase letter, or digit, except the last |
| 126 | + * character, which cannot be a dash. |
| 127 | + * |
| 128 | + * @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> |
| 129 | + */ |
| 130 | + public static DiskId of(String zone, String disk) { |
| 131 | + return new DiskId(null, zone, disk); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns a disk identity given project, zone and disks names. The disk name must be 1-63 |
| 136 | + * characters long and comply with RFC1035. Specifically, the name must match the regular |
| 137 | + * expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a |
| 138 | + * lowercase letter, and all following characters must be a dash, lowercase letter, or digit, |
| 139 | + * except the last character, which cannot be a dash. |
| 140 | + * |
| 141 | + * @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> |
| 142 | + */ |
| 143 | + public static DiskId of(String project, String zone, String disk) { |
| 144 | + return new DiskId(project, zone, disk); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns {@code true} if the provided string matches the expected format of a disk URL. Returns |
| 149 | + * {@code false} otherwise. |
| 150 | + */ |
| 151 | + static boolean matchesUrl(String url) { |
| 152 | + return PATTERN.matcher(url).matches(); |
| 153 | + } |
| 154 | + |
| 155 | + static DiskId fromUrl(String url) { |
| 156 | + Matcher matcher = PATTERN.matcher(url); |
| 157 | + if (!matcher.matches()) { |
| 158 | + throw new IllegalArgumentException(url + " is not a valid disk URL"); |
| 159 | + } |
| 160 | + return DiskId.of(matcher.group(1), matcher.group(2), matcher.group(3)); |
| 161 | + } |
| 162 | +} |
0 commit comments