Skip to content

Commit f7b7430

Browse files
committed
Move InstanceInfo inner classes outside
1 parent d0c3e68 commit f7b7430

12 files changed

Lines changed: 968 additions & 756 deletions

File tree

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

Lines changed: 0 additions & 608 deletions
Large diffs are not rendered by default.
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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.Function;
22+
import com.google.common.base.MoreObjects;
23+
import com.google.common.collect.ImmutableMap;
24+
import com.google.common.collect.Lists;
25+
import com.google.common.collect.Maps;
26+
27+
import java.io.Serializable;
28+
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Objects;
31+
32+
/**
33+
* Metadata for Google Compute Engine Instance as ket/value pairs. This includes custom metadata
34+
* and predefined keys.
35+
*
36+
* @see <a href="https://cloud.google.com/compute/docs/metadata">Metadata</a>
37+
*/
38+
public final class Metadata implements Serializable {
39+
40+
static final Function<com.google.api.services.compute.model.Metadata, Metadata>
41+
FROM_PB_FUNCTION =
42+
new Function<com.google.api.services.compute.model.Metadata, Metadata>() {
43+
@Override
44+
public Metadata apply(com.google.api.services.compute.model.Metadata pb) {
45+
return Metadata.fromPb(pb);
46+
}
47+
};
48+
static final Function<Metadata, com.google.api.services.compute.model.Metadata> TO_PB_FUNCTION =
49+
new Function<Metadata, com.google.api.services.compute.model.Metadata>() {
50+
@Override
51+
public com.google.api.services.compute.model.Metadata apply(Metadata metadata) {
52+
return metadata.toPb();
53+
}
54+
};
55+
56+
private static final long serialVersionUID = -945038809838910107L;
57+
58+
private final Map<String, String> values;
59+
private final String fingerprint;
60+
61+
/**
62+
* A builder for {@code Metadata} objects.
63+
*/
64+
public static final class Builder {
65+
66+
private Map<String, String> values;
67+
private String fingerprint;
68+
69+
Builder() {
70+
values = Maps.newHashMap();
71+
}
72+
73+
Builder(Metadata metadata) {
74+
this.values = metadata.values != null ? Maps.newHashMap(metadata.values)
75+
: Maps.<String, String>newHashMap();
76+
this.fingerprint = metadata.fingerprint;
77+
}
78+
79+
/**
80+
* Sets the metadata for the instance as key/value pairs. The total size of all keys and
81+
* values must be less than 512 KB. Keys must conform to the following regexp:
82+
* {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of
83+
* a URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with
84+
* any other metadata keys for the project. Values must be less than or equal to 32768 bytes.
85+
*/
86+
public Builder values(Map<String, String> values) {
87+
this.values = Maps.newHashMap(checkNotNull(values));
88+
return this;
89+
}
90+
91+
/**
92+
* Adds a key/value pair to the instance metadata. The total size of all keys and values must
93+
* be less than 512 KB. Keys must conform to the following regexp: {@code [a-zA-Z0-9-_]+}, and
94+
* be less than 128 bytes in length. This is reflected as part of a URL in the metadata
95+
* server. Additionally, to avoid ambiguity, keys must not conflict with any other metadata
96+
* keys for the project. Values must be less than or equal to 32768 bytes.
97+
*/
98+
public Builder add(String key, String value) {
99+
this.values.put(key, value);
100+
return this;
101+
}
102+
103+
/**
104+
* Sets the fingerprint for the metadata. This value can be used to update instance's
105+
* metadata.
106+
*/
107+
public Builder fingerprint(String fingerprint) {
108+
this.fingerprint = fingerprint;
109+
return this;
110+
}
111+
112+
/**
113+
* Creates a {@code Metadata} object.
114+
*/
115+
public Metadata build() {
116+
return new Metadata(this);
117+
}
118+
}
119+
120+
private Metadata(Builder builder) {
121+
this.values = ImmutableMap.copyOf(builder.values);
122+
this.fingerprint = builder.fingerprint;
123+
}
124+
125+
/**
126+
* Returns instance's metadata as key/value pairs.
127+
*/
128+
public Map<String, String> values() {
129+
return values;
130+
}
131+
132+
/**
133+
* Returns the fingerprint for the metadata. This value can be used to update instance's
134+
* metadata.
135+
*/
136+
public String fingerprint() {
137+
return fingerprint;
138+
}
139+
140+
/**
141+
* Returns a builder for the current instance metadata.
142+
*/
143+
public Builder toBuilder() {
144+
return new Builder(this);
145+
}
146+
147+
@Override
148+
public String toString() {
149+
return MoreObjects.toStringHelper(this)
150+
.add("values", values)
151+
.add("fingerprint", fingerprint)
152+
.toString();
153+
}
154+
155+
@Override
156+
public int hashCode() {
157+
return Objects.hash(values, fingerprint);
158+
}
159+
160+
@Override
161+
public boolean equals(Object obj) {
162+
return obj == this
163+
|| obj instanceof Metadata
164+
&& Objects.equals(toPb(), ((Metadata) obj).toPb());
165+
}
166+
167+
com.google.api.services.compute.model.Metadata toPb() {
168+
com.google.api.services.compute.model.Metadata metadataPb =
169+
new com.google.api.services.compute.model.Metadata();
170+
metadataPb.setFingerprint(fingerprint);
171+
List<com.google.api.services.compute.model.Metadata.Items> itemsPb =
172+
Lists.newArrayListWithCapacity(values.size());
173+
for (Map.Entry<String, String> entry : values.entrySet()) {
174+
itemsPb.add(new com.google.api.services.compute.model.Metadata.Items()
175+
.setKey(entry.getKey()).setValue(entry.getValue()));
176+
}
177+
metadataPb.setItems(itemsPb);
178+
metadataPb.setFingerprint(fingerprint);
179+
return metadataPb;
180+
}
181+
182+
/**
183+
* Returns a builder for a {@code Metadata} object.
184+
*/
185+
public static Builder builder() {
186+
return new Builder();
187+
}
188+
189+
/**
190+
* Returns a {@code Metadata} object given the the metadata as a map. The total size of all keys
191+
* and values must be less than 512 KB. Keys must conform to the following regexp:
192+
* {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of a
193+
* URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with any
194+
* other metadata keys for the project. Values must be less than or equal to 32768 bytes.
195+
*/
196+
public static Metadata of(Map<String, String> values) {
197+
return builder().values(values).build();
198+
}
199+
200+
static Metadata fromPb(com.google.api.services.compute.model.Metadata metadataPb) {
201+
Builder builder = builder();
202+
if (metadataPb.getItems() != null) {
203+
Map<String, String> metadataValues =
204+
Maps.newHashMapWithExpectedSize(metadataPb.getItems().size());
205+
for (com.google.api.services.compute.model.Metadata.Items item : metadataPb.getItems()) {
206+
metadataValues.put(item.getKey(), item.getValue());
207+
}
208+
builder.values(metadataValues);
209+
}
210+
return builder.fingerprint(metadataPb.getFingerprint()).build();
211+
}
212+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ static NetworkInterface fromPb(
493493
com.google.api.services.compute.model.NetworkInterface interfacePb) {
494494
Builder builder = builder(NetworkId.fromUrl(interfacePb.getNetwork()))
495495
.name(interfacePb.getName());
496-
if (interfacePb.getSubnetwork() != null){
496+
if (interfacePb.getSubnetwork() != null) {
497497
builder.subnetwork(SubnetworkId.fromUrl(interfacePb.getSubnetwork()));
498498
}
499499
builder.networkIp(interfacePb.getNetworkIP());
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 com.google.common.base.MoreObjects;
20+
21+
import java.io.Serializable;
22+
import java.util.Objects;
23+
24+
/**
25+
* A Google Compute Engine instance scheduling options. When there are system events that might
26+
* cause your instances to be disrupted, Google Compute Engine automatically manages the
27+
* scheduling decisions for your instances. Use {@code SchedulingOptions.preemptible()} to create
28+
* a preemptible instance. Use {@code SchedulingOptions.standard()} to configure scheduling
29+
* options for a standard instance.
30+
*
31+
* @see <a href="https://cloud.google.com/compute/docs/instances/setting-instance-scheduling-options">
32+
* Setting Instance Scheduling Options</a>
33+
*/
34+
public final class SchedulingOptions implements Serializable {
35+
36+
private static final long serialVersionUID = 4199610694227857331L;
37+
38+
private final boolean automaticRestart;
39+
private final Maintenance maintenance;
40+
private final boolean isPreemptible;
41+
42+
/**
43+
* Defines the maintenance behavior for this instance.
44+
*/
45+
public enum Maintenance {
46+
/**
47+
* The default behavior for standard instances.
48+
*/
49+
MIGRATE,
50+
51+
/**
52+
* The default and only possible behavior for preemptible instances.
53+
*/
54+
TERMINATE
55+
}
56+
57+
private SchedulingOptions(Boolean automaticRestart, Maintenance maintenance,
58+
Boolean isPreemptible) {
59+
this.automaticRestart = automaticRestart;
60+
this.maintenance = maintenance;
61+
this.isPreemptible = isPreemptible;
62+
}
63+
64+
/**
65+
* Returns whether the instance should be automatically restarted if it is terminated by Compute
66+
* Engine (not terminated by a user).
67+
*/
68+
public Boolean automaticRestart() {
69+
return automaticRestart;
70+
}
71+
72+
/**
73+
* Returns the maintenance behavior for the instance.
74+
*/
75+
public Maintenance maintenance() {
76+
return maintenance;
77+
}
78+
79+
/**
80+
* Returns {@code true} if the instance is preemptible, {@code false} otherwhise.
81+
*
82+
* @see <a href="https://cloud.google.com/compute/docs/instances/preemptible">Preemptible
83+
* Instance</a>
84+
*/
85+
public boolean isPreemptible() {
86+
return isPreemptible;
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return MoreObjects.toStringHelper(this)
92+
.add("automaticRestart", automaticRestart)
93+
.add("maintenance", maintenance)
94+
.add("isPreemptible", isPreemptible)
95+
.toString();
96+
}
97+
98+
@Override
99+
public int hashCode() {
100+
return Objects.hash(automaticRestart, maintenance, isPreemptible);
101+
}
102+
103+
@Override
104+
public boolean equals(Object obj) {
105+
return obj == this
106+
|| obj instanceof SchedulingOptions
107+
&& Objects.equals(toPb(), ((SchedulingOptions) obj).toPb());
108+
}
109+
110+
com.google.api.services.compute.model.Scheduling toPb() {
111+
com.google.api.services.compute.model.Scheduling schedulingPb =
112+
new com.google.api.services.compute.model.Scheduling();
113+
schedulingPb.setAutomaticRestart(automaticRestart);
114+
schedulingPb.setPreemptible(isPreemptible);
115+
if (maintenance != null) {
116+
schedulingPb.setOnHostMaintenance(maintenance.name());
117+
}
118+
return schedulingPb;
119+
}
120+
121+
/**
122+
* Returns a {@code SchedulingOptions} object for a preemptible instance.
123+
*
124+
* @see <a href="https://cloud.google.com/compute/docs/instances/preemptible">Preemptible
125+
* Instance</a>
126+
*/
127+
public static SchedulingOptions preemptible() {
128+
return new SchedulingOptions(false, Maintenance.TERMINATE, true);
129+
}
130+
131+
/**
132+
* Returns a {@code SchedulingOptions} object for a standard instance.
133+
*
134+
* @param automaticRestart specifies whether the instance should be automatically restarted if
135+
* it is terminated by Compute Engine (not terminated by a user)
136+
* @param maintenance defines the maintenance behavior for the instance
137+
*/
138+
public static SchedulingOptions standard(boolean automaticRestart, Maintenance maintenance) {
139+
return new SchedulingOptions(automaticRestart, maintenance, false);
140+
}
141+
142+
static SchedulingOptions fromPb(com.google.api.services.compute.model.Scheduling schedPb) {
143+
Maintenance maintenance = null;
144+
if (schedPb.getOnHostMaintenance() != null) {
145+
maintenance = Maintenance.valueOf(schedPb.getOnHostMaintenance());
146+
}
147+
return new SchedulingOptions(schedPb.getAutomaticRestart(), maintenance,
148+
schedPb.getPreemptible());
149+
}
150+
}

0 commit comments

Comments
 (0)