Skip to content

Commit

Permalink
Merge pull request #755 from mziccard/compute
Browse files Browse the repository at this point in the history
Add support for Compute's addresses
  • Loading branch information
mziccard committed Mar 22, 2016
2 parents a4f6db9 + 5d172b6 commit 25e3559
Show file tree
Hide file tree
Showing 41 changed files with 4,466 additions and 419 deletions.
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));
}
}
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);
}
Loading

0 comments on commit 25e3559

Please sign in to comment.