Skip to content

Commit 23a852f

Browse files
committed
Add NetworkInfo, configuration classes and tests
1 parent eb36f92 commit 23a852f

File tree

7 files changed

+884
-0
lines changed

7 files changed

+884
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.api.services.compute.model.Network;
20+
import com.google.common.base.MoreObjects;
21+
import com.google.common.base.MoreObjects.ToStringHelper;
22+
23+
import java.io.Serializable;
24+
import java.util.Objects;
25+
26+
/**
27+
* Base class for Google Compute Engine network configuration. Use
28+
* {@link StandardNetworkConfiguration} to create a standard network with associated address range.
29+
* Use {@link SubnetNetworkConfiguration} to create a network that supports subnetworks, up to one
30+
* per region, each with its own address range.
31+
*
32+
* @see <a href="https://cloud.google.com/compute/docs/networking">Using Networks and Firewalls</a>
33+
*/
34+
public abstract class NetworkConfiguration implements Serializable {
35+
36+
private static final long serialVersionUID = 6599798536784576564L;
37+
38+
private final Type type;
39+
40+
/**
41+
* Type of a Google Compute Engine disk configuration.
42+
*/
43+
public enum Type {
44+
/**
45+
* A Google Compute Engine network with no subnetworks.
46+
*/
47+
STANDARD,
48+
49+
/**
50+
* A Google Compute Engine network that supports the creation of subnetworks (either automatic
51+
* or manual).
52+
*/
53+
SUBNET
54+
}
55+
56+
NetworkConfiguration(Type type) {
57+
this.type = type;
58+
}
59+
60+
/**
61+
* Returns the network's type. This method returns {@link Type#STANDARD} for a standard networks
62+
* with no subnetworks. This method returns {@link Type#SUBNET} for a network that supports the
63+
* creation of subnetworks (either automatic or manual).
64+
*/
65+
public Type type() {
66+
return type;
67+
}
68+
69+
ToStringHelper toStringHelper() {
70+
return MoreObjects.toStringHelper(this).add("type", type);
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return toStringHelper().toString();
76+
}
77+
78+
final int baseHashCode() {
79+
return Objects.hash(type);
80+
}
81+
82+
final boolean baseEquals(NetworkConfiguration networkConfiguration) {
83+
return networkConfiguration != null
84+
&& getClass().equals(networkConfiguration.getClass())
85+
&& Objects.equals(toPb(), networkConfiguration.toPb());
86+
}
87+
88+
abstract Network toPb();
89+
90+
@SuppressWarnings("unchecked")
91+
static <T extends NetworkConfiguration> T fromPb(Network networkPb) {
92+
if (networkPb.getIPv4Range() != null) {
93+
return (T) StandardNetworkConfiguration.fromPb(networkPb);
94+
} else {
95+
return (T) SubnetNetworkConfiguration.fromPb(networkPb);
96+
}
97+
}
98+
}
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
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.api.services.compute.model.Network;
22+
import com.google.common.base.Function;
23+
import com.google.common.base.MoreObjects;
24+
25+
import org.joda.time.format.DateTimeFormatter;
26+
import org.joda.time.format.ISODateTimeFormat;
27+
28+
import java.io.Serializable;
29+
import java.math.BigInteger;
30+
import java.util.Objects;
31+
32+
/**
33+
* A Google Compute Engine Network. Every virtual machine instance is created as a member of a
34+
* network. Networks connect instances to each other and to the Internet. You can segment your
35+
* networks, use firewall rules to restrict access to instances, and create static routes to forward
36+
* traffic to specific destinations.
37+
*
38+
* @see <a href="https://cloud.google.com/compute/docs/networking">Using Networks and Firewalls</a>
39+
*/
40+
public class NetworkInfo implements Serializable {
41+
42+
static final Function<Network, NetworkInfo> FROM_PB_FUNCTION =
43+
new Function<Network, NetworkInfo>() {
44+
@Override
45+
public NetworkInfo apply(Network pb) {
46+
return NetworkInfo.fromPb(pb);
47+
}
48+
};
49+
static final Function<NetworkInfo, Network> TO_PB_FUNCTION =
50+
new Function<NetworkInfo, Network>() {
51+
@Override
52+
public Network apply(NetworkInfo network) {
53+
return network.toPb();
54+
}
55+
};
56+
57+
private static final long serialVersionUID = 4336912581538114026L;
58+
private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime();
59+
60+
private final String id;
61+
private final NetworkId networkId;
62+
private final Long creationTimestamp;
63+
private final String description;
64+
private final NetworkConfiguration configuration;
65+
66+
/**
67+
* A builder for {@code NetworkInfo} objects.
68+
*/
69+
public abstract static class Builder {
70+
71+
abstract Builder id(String id);
72+
73+
abstract Builder creationTimestamp(Long creationTimestamp);
74+
75+
/**
76+
* Sets the identity of the network.
77+
*/
78+
public abstract Builder networkId(NetworkId networkId);
79+
80+
/**
81+
* Sets an optional textual description of the network.
82+
*/
83+
public abstract Builder description(String description);
84+
85+
/**
86+
* Sets the network configuration. Use {@link StandardNetworkConfiguration} to create a standard
87+
* network with associated IPv4 range. Use {@link SubnetNetworkConfiguration} to create a
88+
* network that could be divided into subnetworks, up to one per region, each with its own
89+
* address range.
90+
*/
91+
public abstract Builder configuration(NetworkConfiguration configuration);
92+
93+
/**
94+
* Creates a {@code NetworkInfo} object.
95+
*/
96+
public abstract NetworkInfo build();
97+
}
98+
99+
static final class BuilderImpl extends Builder {
100+
101+
private String id;
102+
private NetworkId networkId;
103+
private Long creationTimestamp;
104+
private String description;
105+
private NetworkConfiguration configuration;
106+
107+
BuilderImpl(NetworkId networkId, NetworkConfiguration configuration) {
108+
this.networkId = checkNotNull(networkId);
109+
this.configuration = checkNotNull(configuration);
110+
}
111+
112+
BuilderImpl(NetworkInfo networkInfo) {
113+
this.id = networkInfo.id;
114+
this.creationTimestamp = networkInfo.creationTimestamp;
115+
this.networkId = networkInfo.networkId;
116+
this.description = networkInfo.description;
117+
this.configuration = networkInfo.configuration;
118+
}
119+
120+
BuilderImpl(Network networkPb) {
121+
if (networkPb.getId() != null) {
122+
this.id = networkPb.getId().toString();
123+
}
124+
if (networkPb.getCreationTimestamp() != null) {
125+
this.creationTimestamp = TIMESTAMP_FORMATTER.parseMillis(networkPb.getCreationTimestamp());
126+
}
127+
this.networkId = NetworkId.fromUrl(networkPb.getSelfLink());
128+
this.description = networkPb.getDescription();
129+
this.configuration = NetworkConfiguration.fromPb(networkPb);
130+
}
131+
132+
@Override
133+
BuilderImpl id(String id) {
134+
this.id = id;
135+
return this;
136+
}
137+
138+
@Override
139+
BuilderImpl creationTimestamp(Long creationTimestamp) {
140+
this.creationTimestamp = creationTimestamp;
141+
return this;
142+
}
143+
144+
@Override
145+
public BuilderImpl networkId(NetworkId networkId) {
146+
this.networkId = checkNotNull(networkId);
147+
return this;
148+
}
149+
150+
@Override
151+
public BuilderImpl description(String description) {
152+
this.description = description;
153+
return this;
154+
}
155+
156+
@Override
157+
public BuilderImpl configuration(NetworkConfiguration configuration) {
158+
this.configuration = checkNotNull(configuration);
159+
return this;
160+
}
161+
162+
@Override
163+
public NetworkInfo build() {
164+
return new NetworkInfo(this);
165+
}
166+
}
167+
168+
NetworkInfo(BuilderImpl builder) {
169+
this.id = builder.id;
170+
this.creationTimestamp = builder.creationTimestamp;
171+
this.networkId = builder.networkId;
172+
this.description = builder.description;
173+
this.configuration = builder.configuration;
174+
}
175+
176+
/**
177+
* Returns the unique identifier for the subnetwork; defined by the service.
178+
*/
179+
public String id() {
180+
return id;
181+
}
182+
183+
/**
184+
* Returns the creation timestamp in milliseconds since epoch.
185+
*/
186+
public Long creationTimestamp() {
187+
return creationTimestamp;
188+
}
189+
190+
/**
191+
* Returns the network identity.
192+
*/
193+
public NetworkId networkId() {
194+
return networkId;
195+
}
196+
197+
/**
198+
* Returns a textual description of the network.
199+
*/
200+
public String description() {
201+
return description;
202+
}
203+
204+
/**
205+
* Returns the network configuration. Returns a {@link StandardNetworkConfiguration} for standard
206+
* networks with associated IPv4 range. Returns {@link SubnetNetworkConfiguration} for networks
207+
* that could be divided into subnetworks, up to one per region, each with its own address range.
208+
*/
209+
@SuppressWarnings("unchecked")
210+
public <T extends NetworkConfiguration> T configuration() {
211+
return (T) configuration;
212+
}
213+
214+
/**
215+
* Returns a builder for the current network.
216+
*/
217+
public Builder toBuilder() {
218+
return new BuilderImpl(this);
219+
}
220+
221+
@Override
222+
public String toString() {
223+
return MoreObjects.toStringHelper(this)
224+
.add("id", id)
225+
.add("creationTimestamp", creationTimestamp)
226+
.add("networkId", networkId)
227+
.add("description", description)
228+
.add("configuration", configuration)
229+
.toString();
230+
}
231+
232+
@Override
233+
public int hashCode() {
234+
return Objects.hash(id, networkId, creationTimestamp, description, configuration);
235+
}
236+
237+
@Override
238+
public boolean equals(Object obj) {
239+
return obj == this
240+
|| obj != null
241+
&& obj.getClass().equals(NetworkInfo.class)
242+
&& Objects.equals(toPb(), ((NetworkInfo) obj).toPb());
243+
}
244+
245+
NetworkInfo setProjectId(String projectId) {
246+
return toBuilder()
247+
.networkId(networkId.setProjectId(projectId))
248+
.build();
249+
}
250+
251+
Network toPb() {
252+
Network networkPb = configuration.toPb();
253+
if (id != null) {
254+
networkPb.setId(new BigInteger(id));
255+
}
256+
if (creationTimestamp != null) {
257+
networkPb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp));
258+
}
259+
networkPb.setName(networkId.network());
260+
networkPb.setDescription(description);
261+
networkPb.setSelfLink(networkId.selfLink());
262+
return networkPb;
263+
}
264+
265+
/**
266+
* Returns a builder for a {@code NetworkInfo} object given the network identity and its
267+
* configuration. Use {@link StandardNetworkConfiguration} to create a standard network with
268+
* associated address range. Use {@link SubnetNetworkConfiguration} to create a network that
269+
* supports subnetworks, up to one per region, each with its own address range.
270+
*/
271+
public static Builder builder(NetworkId networkId, NetworkConfiguration configuration) {
272+
return new BuilderImpl(networkId, configuration);
273+
}
274+
275+
/**
276+
* Returns a {@code NetworkInfo} object given the network identity. Use
277+
* {@link StandardNetworkConfiguration} to create a standard network with associated address
278+
* range. Use {@link SubnetNetworkConfiguration} to create a network that supports subnetworks, up
279+
* to one per region, each with its own address range.
280+
*/
281+
public static NetworkInfo of(NetworkId networkId, NetworkConfiguration configuration) {
282+
return builder(networkId, configuration).build();
283+
}
284+
285+
static NetworkInfo fromPb(Network networkPb) {
286+
return new BuilderImpl(networkPb).build();
287+
}
288+
}

0 commit comments

Comments
 (0)