-
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 service methods for immutable Compute resources and tests #696
Merged
Merged
Changes from 2 commits
Commits
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
550 changes: 550 additions & 0 deletions
550
gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java
Large diffs are not rendered by default.
Oops, something went wrong.
454 changes: 454 additions & 0 deletions
454
gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java
Large diffs are not rendered by default.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
gcloud-java-compute/src/main/java/com/google/gcloud/compute/Option.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,72 @@ | ||
/* | ||
* 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 com.google.gcloud.spi.ComputeRpc; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Base class for Compute operation option. | ||
*/ | ||
class Option implements Serializable { | ||
|
||
private static final long serialVersionUID = 4116849309806774350L; | ||
|
||
private final ComputeRpc.Option rpcOption; | ||
private final Object value; | ||
|
||
Option(ComputeRpc.Option rpcOption, Object value) { | ||
this.rpcOption = checkNotNull(rpcOption); | ||
this.value = value; | ||
} | ||
|
||
ComputeRpc.Option rpcOption() { | ||
return rpcOption; | ||
} | ||
|
||
Object value() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Option)) { | ||
return false; | ||
} | ||
Option other = (Option) obj; | ||
return Objects.equals(rpcOption, other.rpcOption) | ||
&& Objects.equals(value, other.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(rpcOption, value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("name", rpcOption.value()) | ||
.add("value", value) | ||
.toString(); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/RemoteComputeHelper.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,117 @@ | ||
/* | ||
* 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.testing; | ||
|
||
import com.google.gcloud.AuthCredentials; | ||
import com.google.gcloud.RetryParams; | ||
import com.google.gcloud.compute.ComputeOptions; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Utility to create a remote Compute configuration for testing. Compute options can be obtained | ||
* via the {@link #options()} method. Returned options have custom | ||
* {@link ComputeOptions#retryParams()}: {@link RetryParams#retryMaxAttempts()} is {@code 10}, | ||
* {@link RetryParams#retryMinAttempts()} is {@code 6}, {@link RetryParams#maxRetryDelayMillis()} is | ||
* {@code 30000}, {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and | ||
* {@link RetryParams#initialRetryDelayMillis()} is {@code 250}. | ||
* {@link ComputeOptions#connectTimeout()} and {@link ComputeOptions#readTimeout()} are both set to | ||
* {@code 60000}. | ||
*/ | ||
public class RemoteComputeHelper { | ||
|
||
private static final Logger log = Logger.getLogger(RemoteComputeHelper.class.getName()); | ||
private final ComputeOptions options; | ||
|
||
private RemoteComputeHelper(ComputeOptions options) { | ||
this.options = options; | ||
} | ||
|
||
/** | ||
* Returns a {@link ComputeOptions} object to be used for testing. | ||
*/ | ||
public ComputeOptions options() { | ||
return options; | ||
} | ||
|
||
/** | ||
* Creates a {@code RemoteComputeHelper} object for the given project id and JSON key input | ||
* stream. | ||
* | ||
* @param projectId id of the project to be used for running the tests | ||
* @param keyStream input stream for a JSON key | ||
* @return A {@code RemoteComputeHelper} object for the provided options | ||
* @throws ComputeHelperException if {@code keyStream} is not a valid JSON key stream | ||
*/ | ||
public static RemoteComputeHelper create(String projectId, InputStream keyStream) | ||
throws ComputeHelperException { | ||
try { | ||
ComputeOptions computeOptions = ComputeOptions.builder() | ||
.authCredentials(AuthCredentials.createForJson(keyStream)) | ||
.projectId(projectId) | ||
.retryParams(retryParams()) | ||
.connectTimeout(60000) | ||
.readTimeout(60000) | ||
.build(); | ||
return new RemoteComputeHelper(computeOptions); | ||
} catch (IOException ex) { | ||
if (log.isLoggable(Level.WARNING)) { | ||
log.log(Level.WARNING, ex.getMessage()); | ||
} | ||
throw ComputeHelperException.translate(ex); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a {@code RemoteComputeHelper} object using default project id and authentication | ||
* credentials. | ||
*/ | ||
public static RemoteComputeHelper create() { | ||
ComputeOptions computeOptions = ComputeOptions.builder() | ||
.retryParams(retryParams()) | ||
.connectTimeout(60000) | ||
.readTimeout(60000) | ||
.build(); | ||
return new RemoteComputeHelper(computeOptions); | ||
} | ||
|
||
private static RetryParams retryParams() { | ||
return RetryParams.builder() | ||
.retryMaxAttempts(10) | ||
.retryMinAttempts(6) | ||
.maxRetryDelayMillis(30000) | ||
.totalRetryPeriodMillis(120000) | ||
.initialRetryDelayMillis(250) | ||
.build(); | ||
} | ||
|
||
public static class ComputeHelperException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = -5747977015007639912L; | ||
|
||
public ComputeHelperException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public static ComputeHelperException translate(Exception ex) { | ||
return new ComputeHelperException(ex.getMessage(), ex); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/package-info.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,31 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* A testing helper for Google Compute Engine. | ||
* | ||
* <p>A simple usage example: | ||
* | ||
* <p>Before the test: | ||
* <pre> {@code | ||
* RemoteComputeHelper computeHelper = RemoteComputeHelper.create(); | ||
* Compute compute = computeHelper.options().service(); | ||
* } </pre> | ||
* | ||
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-compute"> | ||
* gcloud-java tools for testing</a> | ||
*/ | ||
package com.google.gcloud.compute.testing; |
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
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.