Skip to content

Commit 31f7783

Browse files
committed
---
yaml --- r: 3283 b: refs/heads/master c: feec3f1 h: refs/heads/master i: 3281: 361da19 3279: 88f0001
1 parent 1af02ae commit 31f7783

14 files changed

Lines changed: 141 additions & 3 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 785c3fe3a43c2cb4af9a08279b7cf047fa9a71ba
2+
refs/heads/master: feec3f1b7b6e33838a8a0c1e76c82520553af863
33
refs/heads/travis: e21ee7b88a5edc3f3d8c71f90c3fc32abf7e8dd6
44
refs/heads/gh-pages: 4e0561bb4504bf647db669a14417b2b2c87ba45d
55
refs/heads/pubsub-alpha: 1a0e970f265af871e02274085b9662b3fe29058b

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(DiskTypeId diskTypeId) {
4141
}
4242
};
4343

44+
static final String REGEX = ZoneResourceId.REGEX + "diskTypes/[^/]+";
4445
private static final long serialVersionUID = 7337881474103686219L;
4546

4647
private final String diskType;
@@ -112,7 +113,18 @@ public static DiskTypeId of(String project, String zone, String diskType) {
112113
return of(ZoneId.of(project, zone), diskType);
113114
}
114115

116+
/**
117+
* Returns {@code true} if the provided string matches the expected format of a disk type URL.
118+
* Returns {@code false} otherwise.
119+
*/
120+
static boolean matchesUrl(String url) {
121+
return url.matches(REGEX);
122+
}
123+
115124
static DiskTypeId fromUrl(String url) {
125+
if (!matchesUrl(url)) {
126+
throw new IllegalArgumentException(url + " is not a valid disk type URL");
127+
}
116128
int projectsIndex = url.indexOf("/projects/");
117129
int zonesIndex = url.indexOf("/zones/");
118130
int diskTypesIndex = url.indexOf("/diskTypes/");

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(LicenseId licenseId) {
4141
}
4242
};
4343

44+
static final String REGEX = ResourceId.REGEX + "global/licenses/[^/]+";
4445
private static final long serialVersionUID = -2239484554024469651L;
4546

4647
private final String license;
@@ -105,7 +106,18 @@ public static LicenseId of(String project, String license) {
105106
return new LicenseId(project, license);
106107
}
107108

109+
/**
110+
* Returns {@code true} if the provided string matches the expected format of a license URL.
111+
* Returns {@code false} otherwise.
112+
*/
113+
static boolean matchesUrl(String url) {
114+
return url.matches(REGEX);
115+
}
116+
108117
static LicenseId fromUrl(String url) {
118+
if (!matchesUrl(url)) {
119+
throw new IllegalArgumentException(url + " is not a valid license URL");
120+
}
109121
int projectsIndex = url.indexOf("/projects/");
110122
int licensesIndex = url.indexOf("/global/licenses/");
111123
String project = url.substring(projectsIndex + 10, licensesIndex);

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public String apply(MachineTypeId machineTypeId) {
4343
}
4444
};
4545

46+
static final String REGEX = ZoneResourceId.REGEX + "machineTypes/[^/]+";
4647
private static final long serialVersionUID = -5819598544478859608L;
4748

4849
private final String machineType;
@@ -101,7 +102,18 @@ public static MachineTypeId of(String project, String zone, String machineType)
101102
return new MachineTypeId(project, zone, machineType);
102103
}
103104

105+
/**
106+
* Returns {@code true} if the provided string matches the expected format of a machine type URL.
107+
* Returns {@code false} otherwise.
108+
*/
109+
static boolean matchesUrl(String url) {
110+
return url.matches(REGEX);
111+
}
112+
104113
static MachineTypeId fromUrl(String url) {
114+
if (!matchesUrl(url)) {
115+
throw new IllegalArgumentException(url + " is not a valid machine type URL");
116+
}
105117
int projectsIndex = url.indexOf("/projects/");
106118
int zonesIndex = url.indexOf("/zones/");
107119
int machineTypesIndex = url.indexOf("/machineTypes/");

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(RegionId regionId) {
4141
}
4242
};
4343

44+
static final String REGEX = ResourceId.REGEX + "regions/[^/]+";
4445
private static final long serialVersionUID = 5569092266957249294L;
4546

4647
private final String region;
@@ -105,9 +106,17 @@ public static RegionId of(String region) {
105106
}
106107

107108
/**
108-
* Returns a new region identity given a region URL.
109+
* Returns {@code true} if the provided string matches the expected format of a region URL.
110+
* Returns {@code false} otherwise.
109111
*/
112+
static boolean matchesUrl(String url) {
113+
return url.matches(REGEX);
114+
}
115+
110116
static RegionId fromUrl(String url) {
117+
if (!matchesUrl(url)) {
118+
throw new IllegalArgumentException(url + " is not a valid region URL");
119+
}
111120
int projectsIndex = url.indexOf("/projects/");
112121
int regionsIndex = url.indexOf("/regions/");
113122
String project = url.substring(projectsIndex + 10, regionsIndex);

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
public abstract class RegionResourceId extends ResourceId {
2929

30+
static final String REGEX = ResourceId.REGEX + "regions/[^/]+/";
3031
private static final long serialVersionUID = 5569092266957249294L;
3132

3233
private final String region;

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
public abstract class ResourceId implements Serializable {
2828

29+
static final String REGEX =
30+
"(https?://(www|content).googleapis.com/compute/v1/)?projects/[^/]+/";
2931
private static final long serialVersionUID = -8028734746870421573L;
3032

3133
private String project;

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String apply(ZoneId zoneId) {
4141
}
4242
};
4343

44+
static final String REGEX = ResourceId.REGEX + "zones/[^/]+";
4445
private static final long serialVersionUID = -7635391994812946733L;
4546

4647
private final String zone;
@@ -100,9 +101,17 @@ public static ZoneId of(String zone) {
100101
}
101102

102103
/**
103-
* Returns a new zone identity given a zone URL.
104+
* Returns {@code true} if the provided string matches the expected format of a zone URL.
105+
* Returns {@code false} otherwise.
104106
*/
107+
static boolean matchesUrl(String url) {
108+
return url.matches(REGEX);
109+
}
110+
105111
static ZoneId fromUrl(String url) {
112+
if (!matchesUrl(url)) {
113+
throw new IllegalArgumentException(url + " is not a valid zone URL");
114+
}
106115
int projectsIndex = url.indexOf("/projects/");
107116
int zonesIndex = url.indexOf("/zones/");
108117
String project = url.substring(projectsIndex + 10, zonesIndex);

trunk/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
public abstract class ZoneResourceId extends ResourceId {
2929

30+
static final String REGEX = ResourceId.REGEX + "zones/[^/]+/";
3031
private static final long serialVersionUID = -6249546895344926888L;
3132

3233
private final String zone;

trunk/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
package com.google.gcloud.compute;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
2224

25+
import org.junit.Rule;
2326
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
2428

2529
public class DiskTypeIdTest {
2630

@@ -30,6 +34,9 @@ public class DiskTypeIdTest {
3034
private static final String URL =
3135
"https://www.googleapis.com/compute/v1/projects/project/zones/zone/diskTypes/diskType";
3236

37+
@Rule
38+
public ExpectedException thrown = ExpectedException.none();
39+
3340
@Test
3441
public void testOf() {
3542
DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE);
@@ -48,6 +55,9 @@ public void testToAndFromUrl() {
4855
DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE);
4956
assertSame(diskTypeId, diskTypeId.setProjectId(PROJECT));
5057
compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.toUrl()));
58+
thrown.expect(IllegalArgumentException.class);
59+
thrown.expectMessage("notMatchingUrl is not a valid disk type URL");
60+
diskTypeId = DiskTypeId.fromUrl("notMatchingUrl");
5161
}
5262

5363
@Test
@@ -57,6 +67,12 @@ public void testSetProjectId() {
5767
compareDiskTypeId(diskTypeId, DiskTypeId.of(ZONE, DISK_TYPE).setProjectId(PROJECT));
5868
}
5969

70+
@Test
71+
public void testMatchesUrl() {
72+
assertTrue(DiskTypeId.matchesUrl(DiskTypeId.of(PROJECT, ZONE, DISK_TYPE).toUrl()));
73+
assertFalse(DiskTypeId.matchesUrl("notMatchingUrl"));
74+
}
75+
6076
private void compareDiskTypeId(DiskTypeId expected, DiskTypeId value) {
6177
assertEquals(expected, value);
6278
assertEquals(expected.project(), expected.project());

0 commit comments

Comments
 (0)