-
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 support for Compute's addresses #755
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c45336b
Add support for Compute's addresses
mziccard aee1ac3
Provide better javadoc to address related classes
mziccard a59366b
Refactor identity classes
mziccard e5194e5
Refactor Address and AddressInfo classes
mziccard 26092cd
Remove unused imports
mziccard 645417e
Refactor Compute service class
mziccard 2f7a07f
Better javadoc for RemoteComputeHelper.baseResourceName
mziccard 3e9c62e
Make AddressInfo.builder return Builder instead of BuilderImpl
mziccard e541400
Use TO_PB_FUNCTION when possible in ComputeImplTest
mziccard 61d87e3
Remove repeated casts in identities'equals method
mziccard 5d172b6
Replace 'an unique' with 'the unique' everywhere
mziccard 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
183 changes: 183 additions & 0 deletions
183
gcloud-java-compute/src/main/java/com/google/gcloud/compute/Address.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,183 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A Google Compute Engine address. With Compute Engine you can create static external IP addresses | ||
* that are assigned to your project and persist until you explicitly release them. A region address | ||
* can be assigned to a Compute Engine instance or to a regional forwarding rule. Compute Engine | ||
* also allows you to create global addresses that are used for global forwarding rules. Both global | ||
* addresses and global forwarding rules can only be used for HTTP load balancing. {@code Address} | ||
* adds a layer of service-related functionality over {@link AddressInfo}. Objects of this class are | ||
* immutable. To get an {@code Address} object with the most recent information use {@link #reload}. | ||
* | ||
* @see <a href="https://cloud.google.com/compute/docs/instances-and-network#reservedaddress"> | ||
* Static external IP addresses</a> | ||
* @see <a href="https://cloud.google.com/compute/docs/load-balancing/http/">HTTP Load Balancing</a> | ||
*/ | ||
public class Address extends AddressInfo { | ||
|
||
private static final long serialVersionUID = 3457542817554062712L; | ||
|
||
private final ComputeOptions options; | ||
private transient Compute compute; | ||
|
||
/** | ||
* A builder for {@code Address} objects. | ||
*/ | ||
public static class Builder extends AddressInfo.Builder { | ||
|
||
private final Compute compute; | ||
private final AddressInfo.BuilderImpl infoBuilder; | ||
|
||
Builder(Compute compute, AddressId addressId) { | ||
this.compute = compute; | ||
this.infoBuilder = new AddressInfo.BuilderImpl(); | ||
this.infoBuilder.addressId(addressId); | ||
} | ||
|
||
Builder(Address address) { | ||
this.compute = address.compute; | ||
this.infoBuilder = new AddressInfo.BuilderImpl(address); | ||
} | ||
|
||
@Override | ||
public Builder address(String address) { | ||
infoBuilder.address(address); | ||
return this; | ||
} | ||
|
||
@Override | ||
Builder creationTimestamp(Long creationTimestamp) { | ||
infoBuilder.creationTimestamp(creationTimestamp); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder description(String description) { | ||
infoBuilder.description(description); | ||
return this; | ||
} | ||
|
||
@Override | ||
Builder id(String id) { | ||
infoBuilder.id(id); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder addressId(AddressId addressId) { | ||
infoBuilder.addressId(addressId); | ||
return this; | ||
} | ||
|
||
@Override | ||
Builder status(Status status) { | ||
infoBuilder.status(status); | ||
return this; | ||
} | ||
|
||
@Override | ||
Builder usage(Usage usage) { | ||
infoBuilder.usage(usage); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Address build() { | ||
return new Address(compute, infoBuilder); | ||
} | ||
} | ||
|
||
Address(Compute compute, AddressInfo.BuilderImpl infoBuilder) { | ||
super(infoBuilder); | ||
this.compute = checkNotNull(compute); | ||
this.options = compute.options(); | ||
} | ||
|
||
/** | ||
* Checks if this address exists. | ||
* | ||
* @return {@code true} if this address exists, {@code false} otherwise | ||
* @throws ComputeException upon failure | ||
*/ | ||
public boolean exists() { | ||
return reload(Compute.AddressOption.fields()) != null; | ||
} | ||
|
||
/** | ||
* Fetches the current address' latest information. Returns {@code null} if the address does not | ||
* exist. | ||
* | ||
* @param options address options | ||
* @return an {@code Address} object with latest information or {@code null} if not found | ||
* @throws ComputeException upon failure | ||
*/ | ||
public Address reload(Compute.AddressOption... options) { | ||
return compute.get(addressId(), options); | ||
} | ||
|
||
/** | ||
* Deletes this address. | ||
* | ||
* @return an {@code Operation} object if delete request was successfully sent, {@code null} if | ||
* the address was not found | ||
* @throws ComputeException upon failure | ||
*/ | ||
public Operation delete(Compute.OperationOption... options) { | ||
return compute.delete(addressId(), options); | ||
} | ||
|
||
/** | ||
* Returns the address's {@code Compute} object used to issue requests. | ||
*/ | ||
public Compute compute() { | ||
return compute; | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return new Builder(this); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object obj) { | ||
return obj instanceof Address | ||
&& Objects.equals(toPb(), ((Address) obj).toPb()) | ||
&& Objects.equals(options, ((Address) obj).options); | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(super.hashCode(), options); | ||
} | ||
|
||
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { | ||
in.defaultReadObject(); | ||
this.compute = options.service(); | ||
} | ||
|
||
static Address fromPb(Compute compute, com.google.api.services.compute.model.Address addressPb) { | ||
return new Address(compute, new AddressInfo.BuilderImpl(addressPb)); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
gcloud-java-compute/src/main/java/com/google/gcloud/compute/AddressId.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,91 @@ | ||
/* | ||
* 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 java.util.Objects; | ||
|
||
/** | ||
* Base class for Google Compute Engine address identities. | ||
*/ | ||
public abstract class AddressId extends ResourceId { | ||
|
||
private static final long serialVersionUID = 147328216049936438L; | ||
|
||
private final String address; | ||
|
||
/** | ||
* Possible types for a Google Compute Engine address identity. | ||
*/ | ||
enum Type { | ||
/** | ||
* Global static external IP addresses can be assigned to global forwarding rules. | ||
*/ | ||
GLOBAL, | ||
|
||
/** | ||
* Region static external IP addresses can be assigned to instances and region forwarding rules. | ||
*/ | ||
REGION | ||
} | ||
|
||
AddressId(String project, String address) { | ||
super(project); | ||
this.address = checkNotNull(address); | ||
} | ||
|
||
/** | ||
* Returns the type of this address identity. | ||
*/ | ||
public abstract Type type(); | ||
|
||
/** | ||
* Returns the name of the address resource. The name must be 1-63 characters long and comply with | ||
* RFC1035. Specifically, the name must match the regular expression | ||
* {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, | ||
* and all following characters must be a dash, lowercase letter, or digit, except the last | ||
* character, which cannot be a dash. | ||
* | ||
* @see <a href="https://www.ietf.org/rfc/rfc1035.txt">RFC1035</a> | ||
*/ | ||
public String address() { | ||
return address; | ||
} | ||
|
||
@Override | ||
MoreObjects.ToStringHelper toStringHelper() { | ||
return super.toStringHelper().add("address", address); | ||
} | ||
|
||
@Override | ||
final int baseHashCode() { | ||
return Objects.hash(super.baseHashCode(), address); | ||
} | ||
|
||
@Override | ||
final boolean baseEquals(ResourceId resourceId) { | ||
return resourceId instanceof AddressId | ||
&& super.baseEquals(resourceId) | ||
&& Objects.equals(address, ((AddressId) resourceId).address); | ||
} | ||
|
||
@Override | ||
abstract AddressId setProjectId(String projectId); | ||
} |
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.