Skip to content

Commit b1274b5

Browse files
committed
Merge pull request #773 from mziccard/compute
Add SnapshotInfo, related classes and tests
2 parents 25e3559 + f968d38 commit b1274b5

File tree

7 files changed

+1087
-5
lines changed

7 files changed

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

0 commit comments

Comments
 (0)