Skip to content

Commit 4e16b54

Browse files
committed
Merge pull request #696 from mziccard/compute
Add service methods for immutable Compute resources and tests
2 parents 2be0171 + 29f35f7 commit 4e16b54

File tree

9 files changed

+3056
-0
lines changed

9 files changed

+3056
-0
lines changed

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java

Lines changed: 865 additions & 0 deletions
Large diffs are not rendered by default.

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java

Lines changed: 445 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.compute;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.base.MoreObjects;
22+
import com.google.gcloud.spi.ComputeRpc;
23+
24+
import java.io.Serializable;
25+
import java.util.Objects;
26+
27+
/**
28+
* Base class for Compute operation option.
29+
*/
30+
class Option implements Serializable {
31+
32+
private static final long serialVersionUID = 4116849309806774350L;
33+
34+
private final ComputeRpc.Option rpcOption;
35+
private final Object value;
36+
37+
Option(ComputeRpc.Option rpcOption, Object value) {
38+
this.rpcOption = checkNotNull(rpcOption);
39+
this.value = value;
40+
}
41+
42+
ComputeRpc.Option rpcOption() {
43+
return rpcOption;
44+
}
45+
46+
Object value() {
47+
return value;
48+
}
49+
50+
@Override
51+
public boolean equals(Object obj) {
52+
if (!(obj instanceof Option)) {
53+
return false;
54+
}
55+
Option other = (Option) obj;
56+
return Objects.equals(rpcOption, other.rpcOption)
57+
&& Objects.equals(value, other.value);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash(rpcOption, value);
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return MoreObjects.toStringHelper(this)
68+
.add("name", rpcOption.value())
69+
.add("value", value)
70+
.toString();
71+
}
72+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.compute.testing;
18+
19+
import com.google.gcloud.AuthCredentials;
20+
import com.google.gcloud.RetryParams;
21+
import com.google.gcloud.compute.ComputeOptions;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
28+
/**
29+
* Utility to create a remote Compute configuration for testing. Compute options can be obtained
30+
* via the {@link #options()} method. Returned options have custom
31+
* {@link ComputeOptions#retryParams()}: {@link RetryParams#retryMaxAttempts()} is {@code 10},
32+
* {@link RetryParams#retryMinAttempts()} is {@code 6}, {@link RetryParams#maxRetryDelayMillis()} is
33+
* {@code 30000}, {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and
34+
* {@link RetryParams#initialRetryDelayMillis()} is {@code 250}.
35+
* {@link ComputeOptions#connectTimeout()} and {@link ComputeOptions#readTimeout()} are both set to
36+
* {@code 60000}.
37+
*/
38+
public class RemoteComputeHelper {
39+
40+
private static final Logger log = Logger.getLogger(RemoteComputeHelper.class.getName());
41+
private final ComputeOptions options;
42+
43+
private RemoteComputeHelper(ComputeOptions options) {
44+
this.options = options;
45+
}
46+
47+
/**
48+
* Returns a {@link ComputeOptions} object to be used for testing.
49+
*/
50+
public ComputeOptions options() {
51+
return options;
52+
}
53+
54+
/**
55+
* Creates a {@code RemoteComputeHelper} object for the given project id and JSON key input
56+
* stream.
57+
*
58+
* @param projectId id of the project to be used for running the tests
59+
* @param keyStream input stream for a JSON key
60+
* @return A {@code RemoteComputeHelper} object for the provided options
61+
* @throws ComputeHelperException if {@code keyStream} is not a valid JSON key stream
62+
*/
63+
public static RemoteComputeHelper create(String projectId, InputStream keyStream)
64+
throws ComputeHelperException {
65+
try {
66+
ComputeOptions computeOptions = ComputeOptions.builder()
67+
.authCredentials(AuthCredentials.createForJson(keyStream))
68+
.projectId(projectId)
69+
.retryParams(retryParams())
70+
.connectTimeout(60000)
71+
.readTimeout(60000)
72+
.build();
73+
return new RemoteComputeHelper(computeOptions);
74+
} catch (IOException ex) {
75+
if (log.isLoggable(Level.WARNING)) {
76+
log.log(Level.WARNING, ex.getMessage());
77+
}
78+
throw ComputeHelperException.translate(ex);
79+
}
80+
}
81+
82+
/**
83+
* Creates a {@code RemoteComputeHelper} object using default project id and authentication
84+
* credentials.
85+
*/
86+
public static RemoteComputeHelper create() {
87+
ComputeOptions computeOptions = ComputeOptions.builder()
88+
.retryParams(retryParams())
89+
.connectTimeout(60000)
90+
.readTimeout(60000)
91+
.build();
92+
return new RemoteComputeHelper(computeOptions);
93+
}
94+
95+
private static RetryParams retryParams() {
96+
return RetryParams.builder()
97+
.retryMaxAttempts(10)
98+
.retryMinAttempts(6)
99+
.maxRetryDelayMillis(30000)
100+
.totalRetryPeriodMillis(120000)
101+
.initialRetryDelayMillis(250)
102+
.build();
103+
}
104+
105+
public static class ComputeHelperException extends RuntimeException {
106+
107+
private static final long serialVersionUID = -5747977015007639912L;
108+
109+
public ComputeHelperException(String message, Throwable cause) {
110+
super(message, cause);
111+
}
112+
113+
public static ComputeHelperException translate(Exception ex) {
114+
return new ComputeHelperException(ex.getMessage(), ex);
115+
}
116+
}
117+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* A testing helper for Google Compute Engine.
19+
*
20+
* <p>A simple usage example:
21+
*
22+
* <p>Before the test:
23+
* <pre> {@code
24+
* RemoteComputeHelper computeHelper = RemoteComputeHelper.create();
25+
* Compute compute = computeHelper.options().service();
26+
* } </pre>
27+
*
28+
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-compute">
29+
* gcloud-java tools for testing</a>
30+
*/
31+
package com.google.gcloud.compute.testing;

gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,149 @@
1616

1717
package com.google.gcloud.spi;
1818

19+
import com.google.api.services.compute.model.DiskType;
20+
import com.google.api.services.compute.model.License;
21+
import com.google.api.services.compute.model.MachineType;
22+
import com.google.api.services.compute.model.Region;
23+
import com.google.api.services.compute.model.Zone;
24+
import com.google.gcloud.compute.ComputeException;
25+
26+
import java.util.Map;
27+
1928
public interface ComputeRpc {
29+
30+
// These options are part of the Google Compute Engine query parameters
31+
enum Option {
32+
FIELDS("fields"),
33+
MAX_RESULTS("maxResults"),
34+
PAGE_TOKEN("pageToken"),
35+
FILTER("filter");
36+
37+
private final String value;
38+
39+
Option(String value) {
40+
this.value = value;
41+
}
42+
43+
public String value() {
44+
return value;
45+
}
46+
47+
@SuppressWarnings("unchecked")
48+
<T> T get(Map<Option, ?> options) {
49+
return (T) options.get(this);
50+
}
51+
52+
String getString(Map<Option, ?> options) {
53+
return get(options);
54+
}
55+
56+
Long getLong(Map<Option, ?> options) {
57+
return get(options);
58+
}
59+
60+
Boolean getBoolean(Map<Option, ?> options) {
61+
return get(options);
62+
}
63+
}
64+
65+
class Tuple<X, Y> {
66+
67+
private final X x;
68+
private final Y y;
69+
70+
private Tuple(X x, Y y) {
71+
this.x = x;
72+
this.y = y;
73+
}
74+
75+
public static <X, Y> Tuple<X, Y> of(X x, Y y) {
76+
return new Tuple<>(x, y);
77+
}
78+
79+
public X x() {
80+
return x;
81+
}
82+
83+
public Y y() {
84+
return y;
85+
}
86+
}
87+
88+
/**
89+
* Returns the requested disk type or {@code null} if not found.
90+
*
91+
* @throws ComputeException upon failure
92+
*/
93+
DiskType getDiskType(String zone, String diskType, Map<Option, ?> options);
94+
95+
/**
96+
* Lists the disk types in the provided zone.
97+
*
98+
* @throws ComputeException upon failure
99+
*/
100+
Tuple<String, Iterable<DiskType>> listDiskTypes(String zone, Map<Option, ?> options);
101+
102+
/**
103+
* Lists all disk types.
104+
*
105+
* @throws ComputeException upon failure
106+
*/
107+
Tuple<String, Iterable<DiskType>> listDiskTypes(Map<Option, ?> options);
108+
109+
/**
110+
* Returns the requested machine type or {@code null} if not found.
111+
*
112+
* @throws ComputeException upon failure
113+
*/
114+
MachineType getMachineType(String zone, String diskType, Map<Option, ?> options);
115+
116+
/**
117+
* Lists the machine types in the provided zone.
118+
*
119+
* @throws ComputeException upon failure
120+
*/
121+
Tuple<String, Iterable<MachineType>> listMachineTypes(String zone, Map<Option, ?> options);
122+
123+
/**
124+
* Lists all machine types.
125+
*
126+
* @throws ComputeException upon failure
127+
*/
128+
Tuple<String, Iterable<MachineType>> listMachineTypes(Map<Option, ?> options);
129+
130+
/**
131+
* Returns the requested region or {@code null} if not found.
132+
*
133+
* @throws ComputeException upon failure
134+
*/
135+
Region getRegion(String region, Map<Option, ?> options);
136+
137+
/**
138+
* Lists the regions.
139+
*
140+
* @throws ComputeException upon failure
141+
*/
142+
Tuple<String, Iterable<Region>> listRegions(Map<Option, ?> options);
143+
144+
/**
145+
* Returns the requested zone or {@code null} if not found.
146+
*
147+
* @throws ComputeException upon failure
148+
*/
149+
Zone getZone(String zone, Map<Option, ?> options);
150+
151+
/**
152+
* Lists the zones.
153+
*
154+
* @throws ComputeException upon failure
155+
*/
156+
Tuple<String, Iterable<Zone>> listZones(Map<Option, ?> options);
157+
158+
/**
159+
* Returns the requested license or {@code null} if not found.
160+
*
161+
* @throws ComputeException upon failure
162+
*/
163+
License getLicense(String project, String license, Map<Option, ?> options);
20164
}

0 commit comments

Comments
 (0)