Skip to content

Commit d19f7c6

Browse files
committed
Add SubnetworkInfo, NetworkId, SubnetworkId and test classes (#895)
1 parent 3fe785b commit d19f7c6

File tree

7 files changed

+948
-5
lines changed

7 files changed

+948
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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.ToStringHelper;
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 network.
29+
*/
30+
public final class NetworkId extends ResourceId {
31+
32+
private static final String REGEX = ResourceId.REGEX + "global/networks/([^/]+)";
33+
private static final Pattern PATTERN = Pattern.compile(REGEX);
34+
private static final long serialVersionUID = 2386765228138819506L;
35+
36+
private final String network;
37+
38+
NetworkId(String project, String network) {
39+
super(project);
40+
this.network = checkNotNull(network);
41+
}
42+
43+
private NetworkId(NetworkId networkId) {
44+
super(networkId.project());
45+
this.network = checkNotNull(networkId.network());
46+
}
47+
48+
/**
49+
* Returns the name of the network. The network name must be 1-63 characters long and comply with
50+
* RFC1035. Specifically, the name must match the regular expression
51+
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
52+
* and all following characters must be a dash, lowercase letter, or digit, except the last
53+
* character, which cannot be a dash.
54+
*
55+
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
56+
*/
57+
public String network() {
58+
return network;
59+
}
60+
61+
@Override
62+
public String selfLink() {
63+
return super.selfLink() + "/global/networks/" + network;
64+
}
65+
66+
@Override
67+
ToStringHelper toStringHelper() {
68+
return super.toStringHelper().add("network", network);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hash(baseHashCode(), network);
74+
}
75+
76+
@Override
77+
public boolean equals(Object obj) {
78+
if (obj == this) {
79+
return true;
80+
}
81+
if (!(obj instanceof NetworkId)) {
82+
return false;
83+
}
84+
NetworkId other = (NetworkId) obj;
85+
return baseEquals(other) && Objects.equals(network, other.network);
86+
}
87+
88+
@Override
89+
NetworkId setProjectId(String projectId) {
90+
if (project() != null) {
91+
return this;
92+
}
93+
return NetworkId.of(projectId, network);
94+
}
95+
96+
/**
97+
* Returns a new network identity given project and network names. The network name must be 1-63
98+
* characters long and comply with RFC1035. Specifically, the name must match the regular
99+
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a
100+
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit,
101+
* except the last character, which cannot be a dash.
102+
*
103+
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
104+
*/
105+
public static NetworkId of(String project, String network) {
106+
return new NetworkId(project, network);
107+
}
108+
109+
/**
110+
* Returns a new network identity given network name. The network name must be 1-63 characters
111+
* long and comply with RFC1035. Specifically, the name must match the regular expression
112+
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
113+
* and all following characters must be a dash, lowercase letter, or digit, except the last
114+
* character, which cannot be a dash.
115+
*
116+
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
117+
*/
118+
public static NetworkId of(String network) {
119+
return NetworkId.of(null, network);
120+
}
121+
122+
/**
123+
* Returns {@code true} if the provided string matches the expected format of a network URL.
124+
* Returns {@code false} otherwise.
125+
*/
126+
static boolean matchesUrl(String url) {
127+
return PATTERN.matcher(url).matches();
128+
}
129+
130+
static NetworkId fromUrl(String url) {
131+
Matcher matcher = PATTERN.matcher(url);
132+
if (!matcher.matches()) {
133+
throw new IllegalArgumentException(url + " is not a valid network URL");
134+
}
135+
return NetworkId.of(matcher.group(1), matcher.group(2));
136+
}
137+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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

Comments
 (0)