|
| 1 | +/* |
| 2 | + * Copyright 2016 Google LLC |
| 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.cloud.compute.deprecated; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.cloud.compute.deprecated.Compute.AddressOption; |
| 22 | +import com.google.cloud.compute.deprecated.Compute.OperationOption; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.ObjectInputStream; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * A Google Compute Engine address. With Compute Engine you can create static external IP addresses |
| 30 | + * that are assigned to your project and persist until you explicitly release them. A region address |
| 31 | + * can be assigned to a Compute Engine instance or to a regional forwarding rule. Compute Engine |
| 32 | + * also allows you to create global addresses that are used for global forwarding rules. Both global |
| 33 | + * addresses and global forwarding rules can only be used for HTTP load balancing. {@code Address} |
| 34 | + * adds a layer of service-related functionality over {@link AddressInfo}. Objects of this class are |
| 35 | + * immutable. To get an {@code Address} object with the most recent information use {@link #reload}. |
| 36 | + * |
| 37 | + * @see <a href="https://cloud.google.com/compute/docs/instances-and-network#reservedaddress"> |
| 38 | + * Static external IP addresses</a> |
| 39 | + * @see <a href="https://cloud.google.com/compute/docs/load-balancing/http/">HTTP Load Balancing</a> |
| 40 | + */ |
| 41 | +public class Address extends AddressInfo { |
| 42 | + |
| 43 | + private static final long serialVersionUID = 3457542817554062712L; |
| 44 | + |
| 45 | + private final ComputeOptions options; |
| 46 | + private transient Compute compute; |
| 47 | + |
| 48 | + /** |
| 49 | + * A builder for {@code Address} objects. |
| 50 | + */ |
| 51 | + public static class Builder extends AddressInfo.Builder { |
| 52 | + |
| 53 | + private final Compute compute; |
| 54 | + private final AddressInfo.BuilderImpl infoBuilder; |
| 55 | + |
| 56 | + Builder(Compute compute, AddressId addressId) { |
| 57 | + this.compute = compute; |
| 58 | + this.infoBuilder = new AddressInfo.BuilderImpl(); |
| 59 | + this.infoBuilder.setAddressId(addressId); |
| 60 | + } |
| 61 | + |
| 62 | + Builder(Address address) { |
| 63 | + this.compute = address.compute; |
| 64 | + this.infoBuilder = new AddressInfo.BuilderImpl(address); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Builder setAddress(String address) { |
| 69 | + infoBuilder.setAddress(address); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + Builder setCreationTimestamp(Long creationTimestamp) { |
| 75 | + infoBuilder.setCreationTimestamp(creationTimestamp); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Builder setDescription(String description) { |
| 81 | + infoBuilder.setDescription(description); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + Builder setGeneratedId(String generatedId) { |
| 87 | + infoBuilder.setGeneratedId(generatedId); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Builder setAddressId(AddressId addressId) { |
| 93 | + infoBuilder.setAddressId(addressId); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + Builder setStatus(Status status) { |
| 99 | + infoBuilder.setStatus(status); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + Builder setUsage(Usage usage) { |
| 105 | + infoBuilder.setUsage(usage); |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Address build() { |
| 111 | + return new Address(compute, infoBuilder); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + Address(Compute compute, AddressInfo.BuilderImpl infoBuilder) { |
| 116 | + super(infoBuilder); |
| 117 | + this.compute = checkNotNull(compute); |
| 118 | + this.options = compute.getOptions(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Checks if this address exists. |
| 123 | + * |
| 124 | + * @return {@code true} if this address exists, {@code false} otherwise |
| 125 | + * @throws ComputeException upon failure |
| 126 | + */ |
| 127 | + public boolean exists() { |
| 128 | + return reload(AddressOption.fields()) != null; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Fetches the current address' latest information. Returns {@code null} if the address does not |
| 133 | + * exist. |
| 134 | + * |
| 135 | + * @param options address options |
| 136 | + * @return an {@code Address} object with latest information or {@code null} if not found |
| 137 | + * @throws ComputeException upon failure |
| 138 | + */ |
| 139 | + public Address reload(AddressOption... options) { |
| 140 | + return compute.getAddress(getAddressId(), options); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Deletes this address. |
| 145 | + * |
| 146 | + * @return an {@code Operation} object if delete request was successfully sent, {@code null} if |
| 147 | + * the address was not found |
| 148 | + * @throws ComputeException upon failure |
| 149 | + */ |
| 150 | + public Operation delete(OperationOption... options) { |
| 151 | + return compute.deleteAddress(getAddressId(), options); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Returns the address's {@code Compute} object used to issue requests. |
| 156 | + */ |
| 157 | + public Compute getCompute() { |
| 158 | + return compute; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public Builder toBuilder() { |
| 163 | + return new Builder(this); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public final boolean equals(Object obj) { |
| 168 | + if (obj == this) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + if (obj == null || !obj.getClass().equals(Address.class)) { |
| 172 | + return false; |
| 173 | + } |
| 174 | + Address other = (Address) obj; |
| 175 | + return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public final int hashCode() { |
| 180 | + return Objects.hash(super.hashCode(), options); |
| 181 | + } |
| 182 | + |
| 183 | + private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { |
| 184 | + input.defaultReadObject(); |
| 185 | + this.compute = options.getService(); |
| 186 | + } |
| 187 | + |
| 188 | + static Address fromPb(Compute compute, com.google.api.services.compute.model.Address addressPb) { |
| 189 | + return new Address(compute, new AddressInfo.BuilderImpl(addressPb)); |
| 190 | + } |
| 191 | +} |
0 commit comments