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