Skip to content

Commit 2338a0c

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 2517 b: refs/heads/update-datastore c: 6074605 h: refs/heads/master i: 2515: e9414ea
1 parent 8d80552 commit 2338a0c

5 files changed

Lines changed: 56 additions & 59 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/gh-pages: 4e0561bb4504bf647db669a14417b2b2c87ba45d
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3
66
refs/heads/pubsub-alpha: 1a0e970f265af871e02274085b9662b3fe29058b
77
refs/heads/resource-manager: ebf4adc5ee835cd2086c4ac5b4e78d01a5a005a7
8-
refs/heads/update-datastore: 9394d2819d2c92dc3421d986846c101d5e65f7ac
8+
refs/heads/update-datastore: 607460518d477a235ebcd8afd155b5c35c220fd2
99
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444
1010
refs/tags/v0.0.10: 207ebd2a3472fddee69fe1298eb90429e3306efd
1111
refs/tags/v0.0.11: ffbfba48a6426ff63c08ff2117e58681f251fbf2

branches/update-datastore/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
6969
1. Project ID supplied when building the service options
7070
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
7171
3. App Engine project ID
72-
4. Compute Engine project ID
73-
5. Google Cloud SDK project ID
72+
4. Google Cloud SDK project ID
73+
5. Compute Engine project ID
7474

7575
Authentication
7676
--------------

branches/update-datastore/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,6 @@ protected String defaultProject() {
364364
}
365365

366366
protected static String googleCloudProjectId() {
367-
try {
368-
URL url = new URL("http://metadata/computeMetadata/v1/project/project-id");
369-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
370-
connection.setRequestProperty("X-Google-Metadata-Request", "True");
371-
InputStream input = connection.getInputStream();
372-
if (connection.getResponseCode() == 200) {
373-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
374-
return reader.readLine();
375-
}
376-
}
377-
} catch (IOException ignore) {
378-
// ignore
379-
}
380367
File configDir;
381368
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
382369
configDir = new File(System.getenv("CLOUDSDK_CONFIG"));
@@ -385,38 +372,52 @@ protected static String googleCloudProjectId() {
385372
} else {
386373
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
387374
}
388-
FileReader fileReader;
375+
FileReader fileReader = null;
389376
try {
390377
fileReader = new FileReader(new File(configDir, "configurations/config_default"));
391378
} catch (FileNotFoundException newConfigFileNotFoundEx) {
392379
try {
393380
fileReader = new FileReader(new File(configDir, "properties"));
394381
} catch (FileNotFoundException oldConfigFileNotFoundEx) {
395-
// return null if we can't find config file
396-
return null;
382+
// ignore
397383
}
398384
}
399-
try (BufferedReader reader = new BufferedReader(fileReader)) {
400-
String line;
401-
String section = null;
402-
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
403-
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
404-
while ((line = reader.readLine()) != null) {
405-
if (line.isEmpty() || line.startsWith(";")) {
406-
continue;
407-
}
408-
line = line.trim();
409-
Matcher matcher = sectionPattern.matcher(line);
410-
if (matcher.matches()) {
411-
section = matcher.group(1);
412-
} else if (section == null || section.equals("core")) {
413-
matcher = projectPattern.matcher(line);
385+
if (fileReader != null) {
386+
try (BufferedReader reader = new BufferedReader(fileReader)) {
387+
String line;
388+
String section = null;
389+
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
390+
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
391+
while ((line = reader.readLine()) != null) {
392+
if (line.isEmpty() || line.startsWith(";")) {
393+
continue;
394+
}
395+
line = line.trim();
396+
Matcher matcher = sectionPattern.matcher(line);
414397
if (matcher.matches()) {
415-
return matcher.group(1);
398+
section = matcher.group(1);
399+
} else if (section == null || section.equals("core")) {
400+
matcher = projectPattern.matcher(line);
401+
if (matcher.matches()) {
402+
return matcher.group(1);
403+
}
416404
}
417405
}
406+
} catch (IOException ex) {
407+
// ignore
418408
}
419-
} catch (IOException ex) {
409+
}
410+
try {
411+
URL url = new URL("http://metadata/computeMetadata/v1/project/project-id");
412+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
413+
connection.setRequestProperty("X-Google-Metadata-Request", "True");
414+
InputStream input = connection.getInputStream();
415+
if (connection.getResponseCode() == 200) {
416+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
417+
return reader.readLine();
418+
}
419+
}
420+
} catch (IOException ignore) {
420421
// ignore
421422
}
422423
// return null if can't determine

branches/update-datastore/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,19 @@ public class SerializationTest {
133133

134134
@Test
135135
public void testServiceOptions() throws Exception {
136-
DatastoreOptions options =
137-
DatastoreOptions.builder()
138-
.normalizeDataset(false)
139-
.projectId("ds1")
140-
.build();
136+
DatastoreOptions options = DatastoreOptions.builder()
137+
.normalizeDataset(false)
138+
.projectId("ds1")
139+
.build();
141140
DatastoreOptions serializedCopy = serializeAndDeserialize(options);
142141
assertEquals(options, serializedCopy);
143142

144-
options =
145-
options.toBuilder()
146-
.namespace("ns1")
147-
.retryParams(RetryParams.getDefaultInstance())
148-
.authCredentials(AuthCredentials.noCredentials())
149-
.force(true)
150-
.build();
143+
options = options.toBuilder()
144+
.namespace("ns1")
145+
.retryParams(RetryParams.getDefaultInstance())
146+
.authCredentials(AuthCredentials.noCredentials())
147+
.force(true)
148+
.build();
151149
serializedCopy = serializeAndDeserialize(options);
152150
assertEquals(options, serializedCopy);
153151
}

branches/update-datastore/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,18 @@ public class SerializationTest {
7373

7474
@Test
7575
public void testServiceOptions() throws Exception {
76-
StorageOptions options =
77-
StorageOptions.builder()
78-
.projectId("p1")
79-
.build();
76+
StorageOptions options = StorageOptions.builder()
77+
.projectId("p1")
78+
.build();
8079
StorageOptions serializedCopy = serializeAndDeserialize(options);
8180
assertEquals(options, serializedCopy);
8281

83-
options =
84-
options.toBuilder()
85-
.projectId("p2")
86-
.retryParams(RetryParams.getDefaultInstance())
87-
.authCredentials(AuthCredentials.noCredentials())
88-
.pathDelimiter(":")
89-
.build();
82+
options = options.toBuilder()
83+
.projectId("p2")
84+
.retryParams(RetryParams.getDefaultInstance())
85+
.authCredentials(AuthCredentials.noCredentials())
86+
.pathDelimiter(":")
87+
.build();
9088
serializedCopy = serializeAndDeserialize(options);
9189
assertEquals(options, serializedCopy);
9290
}

0 commit comments

Comments
 (0)