• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.dns;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import com.google.api.services.dns.model.Project;
22 import com.google.common.base.MoreObjects;
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 Zone}s. Projects can be created only in the
30  * 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 implements Serializable {
48 
49     private static final long serialVersionUID = 6854685970605363639L;
50     private final int zones;
51     private final int resourceRecordsPerRrset;
52     private final int rrsetAdditionsPerChange;
53     private final int rrsetDeletionsPerChange;
54     private final int rrsetsPerZone;
55     private final int totalRrdataSizePerChange;
56 
57     /**
58      * Creates an instance of {@code Quota}.
59      *
60      * <p>This is the only way of creating an instance of {@code Quota}. As the service does not
61      * allow for specifying options, quota is an "all-or-nothing object" and we do not need a
62      * builder.
63      */
Quota( int zones, int resourceRecordsPerRrset, int rrsetAdditionsPerChange, int rrsetDeletionsPerChange, int rrsetsPerZone, int totalRrdataSizePerChange)64     Quota(
65         int zones,
66         int resourceRecordsPerRrset,
67         int rrsetAdditionsPerChange,
68         int rrsetDeletionsPerChange,
69         int rrsetsPerZone,
70         int totalRrdataSizePerChange) {
71       this.zones = zones;
72       this.resourceRecordsPerRrset = resourceRecordsPerRrset;
73       this.rrsetAdditionsPerChange = rrsetAdditionsPerChange;
74       this.rrsetDeletionsPerChange = rrsetDeletionsPerChange;
75       this.rrsetsPerZone = rrsetsPerZone;
76       this.totalRrdataSizePerChange = totalRrdataSizePerChange;
77     }
78 
79     /** Returns the maximum allowed number of zones in the project. */
getZones()80     public int getZones() {
81       return zones;
82     }
83 
84     /** Returns the maximum allowed number of records per {@link RecordSet}. */
getResourceRecordsPerRrset()85     public int getResourceRecordsPerRrset() {
86       return resourceRecordsPerRrset;
87     }
88 
89     /**
90      * Returns the maximum allowed number of {@link RecordSet}s to add per {@link ChangeRequest}.
91      */
getRrsetAdditionsPerChange()92     public int getRrsetAdditionsPerChange() {
93       return rrsetAdditionsPerChange;
94     }
95 
96     /**
97      * Returns the maximum allowed number of {@link RecordSet}s to delete per {@link ChangeRequest}.
98      */
getRrsetDeletionsPerChange()99     public int getRrsetDeletionsPerChange() {
100       return rrsetDeletionsPerChange;
101     }
102 
103     /**
104      * Returns the maximum allowed number of {@link RecordSet}s per {@link ZoneInfo} in the project.
105      */
getRrsetsPerZone()106     public int getRrsetsPerZone() {
107       return rrsetsPerZone;
108     }
109 
110     /** Returns the maximum allowed size for total records in one ChangesRequest in bytes. */
getTotalRrdataSizePerChange()111     public int getTotalRrdataSizePerChange() {
112       return totalRrdataSizePerChange;
113     }
114 
115     @Override
equals(Object other)116     public boolean equals(Object other) {
117       return (other instanceof Quota) && this.toPb().equals(((Quota) other).toPb());
118     }
119 
120     @Override
hashCode()121     public int hashCode() {
122       return Objects.hash(
123           zones,
124           resourceRecordsPerRrset,
125           rrsetAdditionsPerChange,
126           rrsetDeletionsPerChange,
127           rrsetsPerZone,
128           totalRrdataSizePerChange);
129     }
130 
toPb()131     com.google.api.services.dns.model.Quota toPb() {
132       com.google.api.services.dns.model.Quota pb = new com.google.api.services.dns.model.Quota();
133       pb.setManagedZones(zones);
134       pb.setResourceRecordsPerRrset(resourceRecordsPerRrset);
135       pb.setRrsetAdditionsPerChange(rrsetAdditionsPerChange);
136       pb.setRrsetDeletionsPerChange(rrsetDeletionsPerChange);
137       pb.setRrsetsPerManagedZone(rrsetsPerZone);
138       pb.setTotalRrdataSizePerChange(totalRrdataSizePerChange);
139       return pb;
140     }
141 
fromPb(com.google.api.services.dns.model.Quota pb)142     static Quota fromPb(com.google.api.services.dns.model.Quota pb) {
143       return new Quota(
144           pb.getManagedZones(),
145           pb.getResourceRecordsPerRrset(),
146           pb.getRrsetAdditionsPerChange(),
147           pb.getRrsetDeletionsPerChange(),
148           pb.getRrsetsPerManagedZone(),
149           pb.getTotalRrdataSizePerChange());
150     }
151 
152     @Override
toString()153     public String toString() {
154       return MoreObjects.toStringHelper(this)
155           .add("zones", zones)
156           .add("resourceRecordsPerRrset", resourceRecordsPerRrset)
157           .add("rrsetAdditionsPerChange", rrsetAdditionsPerChange)
158           .add("rrsetDeletionsPerChange", rrsetDeletionsPerChange)
159           .add("rrsetsPerZone", rrsetsPerZone)
160           .add("totalRrdataSizePerChange", totalRrdataSizePerChange)
161           .toString();
162     }
163   }
164 
165   /** A builder for {@code ProjectInfo}. */
166   static class Builder {
167     private String id;
168     private BigInteger number;
169     private Quota quota;
170 
Builder()171     private Builder() {}
172 
173     /** Sets an id of the project. */
setId(String id)174     Builder setId(String id) {
175       this.id = checkNotNull(id);
176       return this;
177     }
178 
179     /** Sets a number of the project. */
setNumber(BigInteger number)180     Builder setNumber(BigInteger number) {
181       this.number = checkNotNull(number);
182       return this;
183     }
184 
185     /** Sets quotas assigned to the project. */
setQuota(Quota quota)186     Builder setQuota(Quota quota) {
187       this.quota = checkNotNull(quota);
188       return this;
189     }
190 
191     /** Builds an instance of the {@code ProjectInfo}. */
build()192     ProjectInfo build() {
193       return new ProjectInfo(this);
194     }
195   }
196 
ProjectInfo(Builder builder)197   private ProjectInfo(Builder builder) {
198     this.id = builder.id;
199     this.number = builder.number;
200     this.quota = builder.quota;
201   }
202 
203   /** Returns a builder for {@code ProjectInfo}. */
newBuilder()204   static Builder newBuilder() {
205     return new Builder();
206   }
207 
208   /** Returns the {@code Quota} object which contains quotas assigned to this project. */
getQuota()209   public Quota getQuota() {
210     return quota;
211   }
212 
213   /** Returns project number. For internal use only. */
getNumber()214   BigInteger getNumber() {
215     return number;
216   }
217 
218   /** Returns project id. For internal use only. */
getId()219   String getId() {
220     return id;
221   }
222 
toPb()223   Project toPb() {
224     Project pb = new Project();
225     pb.setId(id);
226     pb.setNumber(number);
227     if (this.quota != null) {
228       pb.setQuota(quota.toPb());
229     }
230     return pb;
231   }
232 
fromPb(Project pb)233   static ProjectInfo fromPb(Project pb) {
234     Builder builder = newBuilder();
235     if (pb.getId() != null) {
236       builder.setId(pb.getId());
237     }
238     if (pb.getNumber() != null) {
239       builder.setNumber(pb.getNumber());
240     }
241     if (pb.getQuota() != null) {
242       builder.setQuota(Quota.fromPb(pb.getQuota()));
243     }
244     return builder.build();
245   }
246 
247   @Override
equals(Object obj)248   public final boolean equals(Object obj) {
249     return obj == this
250         || obj != null
251             && obj.getClass().equals(ProjectInfo.class)
252             && toPb().equals(((ProjectInfo) obj).toPb());
253   }
254 
255   @Override
hashCode()256   public final int hashCode() {
257     return Objects.hash(id, number, quota);
258   }
259 
260   @Override
toString()261   public String toString() {
262     return MoreObjects.toStringHelper(this)
263         .add("id", id)
264         .add("number", number)
265         .add("quota", quota)
266         .toString();
267   }
268 }
269