Skip to content

Commit 25e3559

Browse files
committed
Merge pull request #755 from mziccard/compute
Add support for Compute's addresses
2 parents a4f6db9 + 5d172b6 commit 25e3559

41 files changed

Lines changed: 4466 additions & 419 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 java.io.IOException;
22+
import java.io.ObjectInputStream;
23+
import java.util.Objects;
24+
25+
/**
26+
* A Google Compute Engine address. With Compute Engine you can create static external IP addresses
27+
* that are assigned to your project and persist until you explicitly release them. A region address
28+
* can be assigned to a Compute Engine instance or to a regional forwarding rule. Compute Engine
29+
* also allows you to create global addresses that are used for global forwarding rules. Both global
30+
* addresses and global forwarding rules can only be used for HTTP load balancing. {@code Address}
31+
* adds a layer of service-related functionality over {@link AddressInfo}. Objects of this class are
32+
* immutable. To get an {@code Address} object with the most recent information use {@link #reload}.
33+
*
34+
* @see <a href="https://cloud.google.com/compute/docs/instances-and-network#reservedaddress">
35+
* Static external IP addresses</a>
36+
* @see <a href="https://cloud.google.com/compute/docs/load-balancing/http/">HTTP Load Balancing</a>
37+
*/
38+
public class Address extends AddressInfo {
39+
40+
private static final long serialVersionUID = 3457542817554062712L;
41+
42+
private final ComputeOptions options;
43+
private transient Compute compute;
44+
45+
/**
46+
* A builder for {@code Address} objects.
47+
*/
48+
public static class Builder extends AddressInfo.Builder {
49+
50+
private final Compute compute;
51+
private final AddressInfo.BuilderImpl infoBuilder;
52+
53+
Builder(Compute compute, AddressId addressId) {
54+
this.compute = compute;
55+
this.infoBuilder = new AddressInfo.BuilderImpl();
56+
this.infoBuilder.addressId(addressId);
57+
}
58+
59+
Builder(Address address) {
60+
this.compute = address.compute;
61+
this.infoBuilder = new AddressInfo.BuilderImpl(address);
62+
}
63+
64+
@Override
65+
public Builder address(String address) {
66+
infoBuilder.address(address);
67+
return this;
68+
}
69+
70+
@Override
71+
Builder creationTimestamp(Long creationTimestamp) {
72+
infoBuilder.creationTimestamp(creationTimestamp);
73+
return this;
74+
}
75+
76+
@Override
77+
public Builder description(String description) {
78+
infoBuilder.description(description);
79+
return this;
80+
}
81+
82+
@Override
83+
Builder id(String id) {
84+
infoBuilder.id(id);
85+
return this;
86+
}
87+
88+
@Override
89+
public Builder addressId(AddressId addressId) {
90+
infoBuilder.addressId(addressId);
91+
return this;
92+
}
93+
94+
@Override
95+
Builder status(Status status) {
96+
infoBuilder.status(status);
97+
return this;
98+
}
99+
100+
@Override
101+
Builder usage(Usage usage) {
102+
infoBuilder.usage(usage);
103+
return this;
104+
}
105+
106+
@Override
107+
public Address build() {
108+
return new Address(compute, infoBuilder);
109+
}
110+
}
111+
112+
Address(Compute compute, AddressInfo.BuilderImpl infoBuilder) {
113+
super(infoBuilder);
114+
this.compute = checkNotNull(compute);
115+
this.options = compute.options();
116+
}
117+
118+
/**
119+
* Checks if this address exists.
120+
*
121+
* @return {@code true} if this address exists, {@code false} otherwise
122+
* @throws ComputeException upon failure
123+
*/
124+
public boolean exists() {
125+
return reload(Compute.AddressOption.fields()) != null;
126+
}
127+
128+
/**
129+
* Fetches the current address' latest information. Returns {@code null} if the address does not
130+
* exist.
131+
*
132+
* @param options address options
133+
* @return an {@code Address} object with latest information or {@code null} if not found
134+
* @throws ComputeException upon failure
135+
*/
136+
public Address reload(Compute.AddressOption... options) {
137+
return compute.get(addressId(), options);
138+
}
139+
140+
/**
141+
* Deletes this address.
142+
*
143+
* @return an {@code Operation} object if delete request was successfully sent, {@code null} if
144+
* the address was not found
145+
* @throws ComputeException upon failure
146+
*/
147+
public Operation delete(Compute.OperationOption... options) {
148+
return compute.delete(addressId(), options);
149+
}
150+
151+
/**
152+
* Returns the address's {@code Compute} object used to issue requests.
153+
*/
154+
public Compute compute() {
155+
return compute;
156+
}
157+
158+
@Override
159+
public Builder toBuilder() {
160+
return new Builder(this);
161+
}
162+
163+
@Override
164+
public final boolean equals(Object obj) {
165+
return obj instanceof Address
166+
&& Objects.equals(toPb(), ((Address) obj).toPb())
167+
&& Objects.equals(options, ((Address) obj).options);
168+
}
169+
170+
@Override
171+
public final int hashCode() {
172+
return Objects.hash(super.hashCode(), options);
173+
}
174+
175+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
176+
in.defaultReadObject();
177+
this.compute = options.service();
178+
}
179+
180+
static Address fromPb(Compute compute, com.google.api.services.compute.model.Address addressPb) {
181+
return new Address(compute, new AddressInfo.BuilderImpl(addressPb));
182+
}
183+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
23+
import java.util.Objects;
24+
25+
/**
26+
* Base class for Google Compute Engine address identities.
27+
*/
28+
public abstract class AddressId extends ResourceId {
29+
30+
private static final long serialVersionUID = 147328216049936438L;
31+
32+
private final String address;
33+
34+
/**
35+
* Possible types for a Google Compute Engine address identity.
36+
*/
37+
enum Type {
38+
/**
39+
* Global static external IP addresses can be assigned to global forwarding rules.
40+
*/
41+
GLOBAL,
42+
43+
/**
44+
* Region static external IP addresses can be assigned to instances and region forwarding rules.
45+
*/
46+
REGION
47+
}
48+
49+
AddressId(String project, String address) {
50+
super(project);
51+
this.address = checkNotNull(address);
52+
}
53+
54+
/**
55+
* Returns the type of this address identity.
56+
*/
57+
public abstract Type type();
58+
59+
/**
60+
* Returns the name of the address resource. The name must be 1-63 characters long and comply with
61+
* RFC1035. Specifically, the name must match the regular expression
62+
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter,
63+
* and all following characters must be a dash, lowercase letter, or digit, except the last
64+
* character, which cannot be a dash.
65+
*
66+
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a>
67+
*/
68+
public String address() {
69+
return address;
70+
}
71+
72+
@Override
73+
MoreObjects.ToStringHelper toStringHelper() {
74+
return super.toStringHelper().add("address", address);
75+
}
76+
77+
@Override
78+
final int baseHashCode() {
79+
return Objects.hash(super.baseHashCode(), address);
80+
}
81+
82+
@Override
83+
final boolean baseEquals(ResourceId resourceId) {
84+
return resourceId instanceof AddressId
85+
&& super.baseEquals(resourceId)
86+
&& Objects.equals(address, ((AddressId) resourceId).address);
87+
}
88+
89+
@Override
90+
abstract AddressId setProjectId(String projectId);
91+
}

0 commit comments

Comments
 (0)