Skip to content

Commit

Permalink
Modify project id lookup order and revert style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Nov 9, 2015
1 parent 9394d28 commit 6074605
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
1. Project ID supplied when building the service options
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
3. App Engine project ID
4. Compute Engine project ID
5. Google Cloud SDK project ID
4. Google Cloud SDK project ID
5. Compute Engine project ID

Authentication
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,6 @@ protected String defaultProject() {
}

protected static String googleCloudProjectId() {
try {
URL url = new URL("http://metadata/computeMetadata/v1/project/project-id");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-Google-Metadata-Request", "True");
InputStream input = connection.getInputStream();
if (connection.getResponseCode() == 200) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
return reader.readLine();
}
}
} catch (IOException ignore) {
// ignore
}
File configDir;
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
configDir = new File(System.getenv("CLOUDSDK_CONFIG"));
Expand All @@ -385,38 +372,52 @@ protected static String googleCloudProjectId() {
} else {
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
}
FileReader fileReader;
FileReader fileReader = null;
try {
fileReader = new FileReader(new File(configDir, "configurations/config_default"));
} catch (FileNotFoundException newConfigFileNotFoundEx) {
try {
fileReader = new FileReader(new File(configDir, "properties"));
} catch (FileNotFoundException oldConfigFileNotFoundEx) {
// return null if we can't find config file
return null;
// ignore
}
}
try (BufferedReader reader = new BufferedReader(fileReader)) {
String line;
String section = null;
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
while ((line = reader.readLine()) != null) {
if (line.isEmpty() || line.startsWith(";")) {
continue;
}
line = line.trim();
Matcher matcher = sectionPattern.matcher(line);
if (matcher.matches()) {
section = matcher.group(1);
} else if (section == null || section.equals("core")) {
matcher = projectPattern.matcher(line);
if (fileReader != null) {
try (BufferedReader reader = new BufferedReader(fileReader)) {
String line;
String section = null;
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
while ((line = reader.readLine()) != null) {
if (line.isEmpty() || line.startsWith(";")) {
continue;
}
line = line.trim();
Matcher matcher = sectionPattern.matcher(line);
if (matcher.matches()) {
return matcher.group(1);
section = matcher.group(1);
} else if (section == null || section.equals("core")) {
matcher = projectPattern.matcher(line);
if (matcher.matches()) {
return matcher.group(1);
}
}
}
} catch (IOException ex) {
// ignore
}
} catch (IOException ex) {
}
try {
URL url = new URL("http://metadata/computeMetadata/v1/project/project-id");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-Google-Metadata-Request", "True");
InputStream input = connection.getInputStream();
if (connection.getResponseCode() == 200) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
return reader.readLine();
}
}
} catch (IOException ignore) {
// ignore
}
// return null if can't determine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,19 @@ public class SerializationTest {

@Test
public void testServiceOptions() throws Exception {
DatastoreOptions options =
DatastoreOptions.builder()
.normalizeDataset(false)
.projectId("ds1")
.build();
DatastoreOptions options = DatastoreOptions.builder()
.normalizeDataset(false)
.projectId("ds1")
.build();
DatastoreOptions serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);

options =
options.toBuilder()
.namespace("ns1")
.retryParams(RetryParams.getDefaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.force(true)
.build();
options = options.toBuilder()
.namespace("ns1")
.retryParams(RetryParams.getDefaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.force(true)
.build();
serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,18 @@ public class SerializationTest {

@Test
public void testServiceOptions() throws Exception {
StorageOptions options =
StorageOptions.builder()
.projectId("p1")
.build();
StorageOptions options = StorageOptions.builder()
.projectId("p1")
.build();
StorageOptions serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);

options =
options.toBuilder()
.projectId("p2")
.retryParams(RetryParams.getDefaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.pathDelimiter(":")
.build();
options = options.toBuilder()
.projectId("p2")
.retryParams(RetryParams.getDefaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.pathDelimiter(":")
.build();
serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);
}
Expand Down

0 comments on commit 6074605

Please sign in to comment.