|
| 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.dns; |
| 18 | + |
| 19 | +import static com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.common.base.MoreObjects; |
| 22 | + |
| 23 | +import java.io.Serializable; |
| 24 | +import java.math.BigInteger; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +/** |
| 28 | + * The class provides the Google Cloud DNS information associated with this project. A project is a |
| 29 | + * top level container for resources including {@code ManagedZone}s. Projects can be created only in |
| 30 | + * the APIs console. |
| 31 | + * |
| 32 | + * @see <a href="https://cloud.google.com/dns/api/v1/projects">Google Cloud DNS documentation</a> |
| 33 | + */ |
| 34 | +public class ProjectInfo implements Serializable { |
| 35 | + |
| 36 | + private static final long serialVersionUID = 8696578863323485036L; |
| 37 | + private final String id; |
| 38 | + private final BigInteger number; |
| 39 | + private final Quota quota; |
| 40 | + |
| 41 | + /** |
| 42 | + * This class represents quotas assigned to the {@code ProjectInfo}. |
| 43 | + * |
| 44 | + * @see <a href="https://cloud.google.com/dns/api/v1/projects#quota">Google Cloud DNS |
| 45 | + * documentation</a> |
| 46 | + */ |
| 47 | + public static class Quota { |
| 48 | + |
| 49 | + private final int zones; |
| 50 | + private final int resourceRecordsPerRrset; |
| 51 | + private final int rrsetAdditionsPerChange; |
| 52 | + private final int rrsetDeletionsPerChange; |
| 53 | + private final int rrsetsPerManagedZone; |
| 54 | + private final int totalRrdataSizePerChange; |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates an instance of {@code Quota}. |
| 58 | + * |
| 59 | + * <p>This is the only way of creating an instance of {@code Quota}. As the service does not |
| 60 | + * allow for specifying options, quota is an "all-or-nothing object" and we do not need a |
| 61 | + * builder. |
| 62 | + */ |
| 63 | + Quota(int zones, |
| 64 | + int resourceRecordsPerRrset, |
| 65 | + int rrsetAdditionsPerChange, |
| 66 | + int rrsetDeletionsPerChange, |
| 67 | + int rrsetsPerManagedZone, |
| 68 | + int totalRrdataSizePerChange) { |
| 69 | + this.zones = zones; |
| 70 | + this.resourceRecordsPerRrset = resourceRecordsPerRrset; |
| 71 | + this.rrsetAdditionsPerChange = rrsetAdditionsPerChange; |
| 72 | + this.rrsetDeletionsPerChange = rrsetDeletionsPerChange; |
| 73 | + this.rrsetsPerManagedZone = rrsetsPerManagedZone; |
| 74 | + this.totalRrdataSizePerChange = totalRrdataSizePerChange; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the maximum allowed number of managed zones in the project. |
| 79 | + */ |
| 80 | + public int zones() { |
| 81 | + return zones; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns the maximum allowed number of records per {@link DnsRecord}. |
| 86 | + */ |
| 87 | + public int resourceRecordsPerRrset() { |
| 88 | + return resourceRecordsPerRrset; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns the maximum allowed number of {@link DnsRecord}s to add per {@link ChangeRequest}. |
| 93 | + */ |
| 94 | + public int rrsetAdditionsPerChange() { |
| 95 | + return rrsetAdditionsPerChange; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns the maximum allowed number of {@link DnsRecord}s to delete per {@link |
| 100 | + * ChangeRequest}. |
| 101 | + */ |
| 102 | + public int rrsetDeletionsPerChange() { |
| 103 | + return rrsetDeletionsPerChange; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the maximum allowed number of {@link DnsRecord}s per {@link ManagedZoneInfo} in the |
| 108 | + * project. |
| 109 | + */ |
| 110 | + public int rrsetsPerManagedZone() { |
| 111 | + return rrsetsPerManagedZone; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns the maximum allowed size for total records in one ChangesRequest in bytes. |
| 116 | + */ |
| 117 | + public int totalRrdataSizePerChange() { |
| 118 | + return totalRrdataSizePerChange; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean equals(Object other) { |
| 123 | + return (other instanceof Quota) && this.toPb().equals(((Quota) other).toPb()); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + return Objects.hash(zones, resourceRecordsPerRrset, rrsetAdditionsPerChange, |
| 129 | + rrsetDeletionsPerChange, rrsetsPerManagedZone, totalRrdataSizePerChange); |
| 130 | + } |
| 131 | + |
| 132 | + com.google.api.services.dns.model.Quota toPb() { |
| 133 | + com.google.api.services.dns.model.Quota pb = new com.google.api.services.dns.model.Quota(); |
| 134 | + pb.setManagedZones(zones); |
| 135 | + pb.setResourceRecordsPerRrset(resourceRecordsPerRrset); |
| 136 | + pb.setRrsetAdditionsPerChange(rrsetAdditionsPerChange); |
| 137 | + pb.setRrsetDeletionsPerChange(rrsetDeletionsPerChange); |
| 138 | + pb.setRrsetsPerManagedZone(rrsetsPerManagedZone); |
| 139 | + pb.setTotalRrdataSizePerChange(totalRrdataSizePerChange); |
| 140 | + return pb; |
| 141 | + } |
| 142 | + |
| 143 | + static Quota fromPb(com.google.api.services.dns.model.Quota pb) { |
| 144 | + Quota quota = new Quota(pb.getManagedZones(), |
| 145 | + pb.getResourceRecordsPerRrset(), |
| 146 | + pb.getRrsetAdditionsPerChange(), |
| 147 | + pb.getRrsetDeletionsPerChange(), |
| 148 | + pb.getRrsetsPerManagedZone(), |
| 149 | + pb.getTotalRrdataSizePerChange() |
| 150 | + ); |
| 151 | + return quota; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String toString() { |
| 156 | + return MoreObjects.toStringHelper(this) |
| 157 | + .add("zones", zones) |
| 158 | + .add("resourceRecordsPerRrset", resourceRecordsPerRrset) |
| 159 | + .add("rrsetAdditionsPerChange", rrsetAdditionsPerChange) |
| 160 | + .add("rrsetDeletionsPerChange", rrsetDeletionsPerChange) |
| 161 | + .add("rrsetsPerManagedZone", rrsetsPerManagedZone) |
| 162 | + .add("totalRrdataSizePerChange", totalRrdataSizePerChange) |
| 163 | + .toString(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * A builder for {@code ProjectInfo}. |
| 169 | + */ |
| 170 | + static class Builder { |
| 171 | + private String id; |
| 172 | + private BigInteger number; |
| 173 | + private Quota quota; |
| 174 | + |
| 175 | + private Builder() { |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Sets an id of the project. |
| 180 | + */ |
| 181 | + Builder id(String id) { |
| 182 | + this.id = checkNotNull(id); |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Sets a number of the project. |
| 188 | + */ |
| 189 | + Builder number(BigInteger number) { |
| 190 | + this.number = checkNotNull(number); |
| 191 | + return this; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Sets quotas assigned to the project. |
| 196 | + */ |
| 197 | + Builder quota(Quota quota) { |
| 198 | + this.quota = checkNotNull(quota); |
| 199 | + return this; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Builds an instance of the {@code ProjectInfo}. |
| 204 | + */ |
| 205 | + ProjectInfo build() { |
| 206 | + return new ProjectInfo(this); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private ProjectInfo(Builder builder) { |
| 211 | + this.id = builder.id; |
| 212 | + this.number = builder.number; |
| 213 | + this.quota = builder.quota; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Returns a builder for {@code ProjectInfo}. |
| 218 | + */ |
| 219 | + static Builder builder() { |
| 220 | + return new Builder(); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Returns the {@code Quota} object which contains quotas assigned to this project. |
| 225 | + */ |
| 226 | + public Quota quota() { |
| 227 | + return quota; |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Returns project number. For internal use only. |
| 232 | + */ |
| 233 | + BigInteger number() { |
| 234 | + return number; |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Returns project id. For internal use only. |
| 239 | + */ |
| 240 | + String id() { |
| 241 | + return id; |
| 242 | + } |
| 243 | + |
| 244 | + com.google.api.services.dns.model.Project toPb() { |
| 245 | + com.google.api.services.dns.model.Project pb = new com.google.api.services.dns.model.Project(); |
| 246 | + pb.setId(id); |
| 247 | + pb.setNumber(number); |
| 248 | + if (this.quota != null) { |
| 249 | + pb.setQuota(quota.toPb()); |
| 250 | + } |
| 251 | + return pb; |
| 252 | + } |
| 253 | + |
| 254 | + static ProjectInfo fromPb(com.google.api.services.dns.model.Project pb) { |
| 255 | + Builder builder = builder(); |
| 256 | + if (pb.getId() != null) { |
| 257 | + builder.id(pb.getId()); |
| 258 | + } |
| 259 | + if (pb.getNumber() != null) { |
| 260 | + builder.number(pb.getNumber()); |
| 261 | + } |
| 262 | + if (pb.getQuota() != null) { |
| 263 | + builder.quota(Quota.fromPb(pb.getQuota())); |
| 264 | + } |
| 265 | + return builder.build(); |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public boolean equals(Object other) { |
| 270 | + return (other instanceof ProjectInfo) && toPb().equals(((ProjectInfo) other).toPb()); |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public int hashCode() { |
| 275 | + return Objects.hash(id, number, quota); |
| 276 | + } |
| 277 | + |
| 278 | + @Override |
| 279 | + public String toString() { |
| 280 | + return MoreObjects.toStringHelper(this) |
| 281 | + .add("id", id) |
| 282 | + .add("number", number) |
| 283 | + .add("quota", quota) |
| 284 | + .toString(); |
| 285 | + } |
| 286 | +} |
0 commit comments