-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SnapshotInfo, related classes and tests #773
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gcloud.compute; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Identity for a Google Compute Engine disk. | ||
*/ | ||
public final class DiskId extends ResourceId { | ||
|
||
private static final String REGEX = ResourceId.REGEX + "zones/([^/]+)/disks/([^/]+)"; | ||
private static final Pattern PATTERN = Pattern.compile(REGEX); | ||
private static final long serialVersionUID = -8761290740495870787L; | ||
|
||
private final String zone; | ||
private final String disk; | ||
|
||
private DiskId(String project, String zone, String disk) { | ||
super(project); | ||
this.zone = checkNotNull(zone); | ||
this.disk = checkNotNull(disk); | ||
} | ||
|
||
/** | ||
* Returns the name of the zone this disk belongs to. | ||
*/ | ||
public String zone() { | ||
return zone; | ||
} | ||
|
||
/** | ||
* Returns the identity of the zone this disk belongs to. | ||
*/ | ||
public ZoneId zoneId() { | ||
return ZoneId.of(project(), zone); | ||
} | ||
|
||
/** | ||
* Returns the name of the disk. The name must be 1-63 characters long and comply with RFC1035. | ||
* Specifically, the name must match the regular expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} | ||
* which means the first character must be a lowercase letter, and all following characters must | ||
* be a dash, lowercase letter, or digit, except the last character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public String disk() { | ||
return disk; | ||
} | ||
|
||
@Override | ||
public String selfLink() { | ||
return super.selfLink() + "/zones/" + zone + "/disks/" + disk; | ||
} | ||
|
||
@Override | ||
MoreObjects.ToStringHelper toStringHelper() { | ||
return super.toStringHelper().add("zone", zone).add("disk", disk); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.baseHashCode(), zone, disk); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!(obj instanceof DiskId)) { | ||
return false; | ||
} | ||
DiskId other = (DiskId) obj; | ||
return baseEquals(other) | ||
&& Objects.equals(zone, other.zone) | ||
&& Objects.equals(disk, other.disk); | ||
} | ||
|
||
@Override | ||
DiskId setProjectId(String projectId) { | ||
if (project() != null) { | ||
return this; | ||
} | ||
return DiskId.of(projectId, zone, disk); | ||
} | ||
|
||
/** | ||
* Returns a disk identity given the zone identity and the disk name. The address name must be | ||
* 1-63 characters long and comply with RFC1035. Specifically, the name must match the regular | ||
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a | ||
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit, | ||
* except the last character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public static DiskId of(ZoneId zoneId, String disk) { | ||
return new DiskId(zoneId.project(), zoneId.zone(), disk); | ||
} | ||
|
||
/** | ||
* Returns a disk identity given the zone and disk names. The address name must be 1-63 characters | ||
* long and comply with RFC1035. Specifically, the name must match the regular expression | ||
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, | ||
* and all following characters must be a dash, lowercase letter, or digit, except the last | ||
* character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public static DiskId of(String zone, String disk) { | ||
return new DiskId(null, zone, disk); | ||
} | ||
|
||
/** | ||
* Returns a disk identity given project, zone and disks names. The address name must be 1-63 | ||
* characters long and comply with RFC1035. Specifically, the name must match the regular | ||
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a | ||
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit, | ||
* except the last character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public static DiskId of(String project, String zone, String disk) { | ||
return new DiskId(project, zone, disk); | ||
} | ||
|
||
/** | ||
* Returns {@code true} if the provided string matches the expected format of a disk URL. Returns | ||
* {@code false} otherwise. | ||
*/ | ||
static boolean matchesUrl(String url) { | ||
return PATTERN.matcher(url).matches(); | ||
} | ||
|
||
static DiskId fromUrl(String url) { | ||
Matcher matcher = PATTERN.matcher(url); | ||
if (!matcher.matches()) { | ||
throw new IllegalArgumentException(url + " is not a valid disk URL"); | ||
} | ||
return DiskId.of(matcher.group(1), matcher.group(2), matcher.group(3)); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
gcloud-java-compute/src/main/java/com/google/gcloud/compute/SnapshotId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gcloud.compute; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Identity for a Google Compute Engine snapshot. | ||
*/ | ||
public final class SnapshotId extends ResourceId { | ||
|
||
private static final String REGEX = ResourceId.REGEX + "global/snapshots/([^/]+)"; | ||
private static final Pattern PATTERN = Pattern.compile(REGEX); | ||
private static final long serialVersionUID = -1699492866663041082L; | ||
|
||
private final String snapshot; | ||
|
||
private SnapshotId(String project, String snapshot) { | ||
super(project); | ||
this.snapshot = checkNotNull(snapshot); | ||
} | ||
|
||
/** | ||
* Returns the name of the snapshot. The name must be 1-63 characters long and comply with | ||
* RFC1035. Specifically, the name must match the regular expression | ||
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, | ||
* and all following characters must be a dash, lowercase letter, or digit, except the last | ||
* character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public String snapshot() { | ||
return snapshot; | ||
} | ||
|
||
@Override | ||
public String selfLink() { | ||
return super.selfLink() + "/global/snapshots/" + snapshot; | ||
} | ||
|
||
@Override | ||
MoreObjects.ToStringHelper toStringHelper() { | ||
return super.toStringHelper().add("snapshot", snapshot); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(baseHashCode(), snapshot); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!(obj instanceof SnapshotId)) { | ||
return false; | ||
} | ||
SnapshotId other = (SnapshotId) obj; | ||
return baseEquals(other) && Objects.equals(snapshot, other.snapshot); | ||
} | ||
|
||
@Override | ||
SnapshotId setProjectId(String projectId) { | ||
if (project() != null) { | ||
return this; | ||
} | ||
return SnapshotId.of(projectId, snapshot); | ||
} | ||
|
||
/** | ||
* Returns a snapshot identity given the snapshot name. The snapshot name must be 1-63 characters | ||
* long and comply with RFC1035. Specifically, the name must match the regular expression | ||
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, | ||
* and all following characters must be a dash, lowercase letter, or digit, except the last | ||
* character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public static SnapshotId of(String snapshot) { | ||
return new SnapshotId(null, snapshot); | ||
} | ||
|
||
/** | ||
* Returns a snapshot identity given project and snapshot names. The snapshot name must be 1-63 | ||
* characters long and comply with RFC1035. Specifically, the name must match the regular | ||
* expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a | ||
* lowercase letter, and all following characters must be a dash, lowercase letter, or digit, | ||
* except the last character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public static SnapshotId of(String project, String snapshot) { | ||
return new SnapshotId(project, snapshot); | ||
} | ||
|
||
/** | ||
* Returns {@code true} if the provided string matches the expected format of a snapshot URL. | ||
* Returns {@code false} otherwise. | ||
*/ | ||
static boolean matchesUrl(String url) { | ||
return url.matches(REGEX); | ||
} | ||
|
||
static SnapshotId fromUrl(String url) { | ||
Matcher matcher = PATTERN.matcher(url); | ||
if (!matcher.matches()) { | ||
throw new IllegalArgumentException(url + " is not a valid snapshot URL"); | ||
} | ||
return SnapshotId.of(matcher.group(1), matcher.group(2)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.