forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoneInfo.java
More file actions
317 lines (276 loc) · 9.04 KB
/
ZoneInfo.java
File metadata and controls
317 lines (276 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* 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.dns;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;
/**
* A {@code Zone} represents a DNS zone hosted by the Google Cloud DNS service. A zone is a subtree
* of the DNS namespace under one administrative responsibility. See <a
* href="https://cloud.google.com/dns/api/v1/managedZones">Google Cloud DNS documentation</a> for
* more information.
*/
public class ZoneInfo implements Serializable {
private static final long serialVersionUID = 201601191647L;
private final String name;
private final String id;
private final Long creationTimeMillis;
private final String dnsName;
private final String description;
private final String nameServerSet;
private final List<String> nameServers;
/**
* Builder for {@code ZoneInfo}.
*/
public abstract static class Builder {
/**
* Sets a mandatory user-provided name for the zone. It must be unique within the project.
*/
public abstract Builder name(String name);
/**
* Sets an id for the zone which is assigned to the zone by the server.
*/
abstract Builder id(String id);
/**
* Sets the time when this zone was created.
*/
abstract Builder creationTimeMillis(long creationTimeMillis);
/**
* Sets a mandatory DNS name of this zone, for instance "example.com.".
*/
public abstract Builder dnsName(String dnsName);
/**
* Sets a mandatory description for this zone. The value is a string of at most 1024 characters
* which has no effect on the zone's function.
*/
public abstract Builder description(String description);
/**
* Optionally specifies the NameServerSet for this zone. A NameServerSet is a set of DNS name
* servers that all host the same zones. Most users will not need to specify this value.
*/
abstract Builder nameServerSet(String nameServerSet);
// this should not be included in tooling as per the service owners
/**
* Sets a list of servers that hold the information about the zone. This information is provided
* by Google Cloud DNS and is read only.
*/
abstract Builder nameServers(List<String> nameServers);
/**
* Builds the instance of {@code ZoneInfo} based on the information set by this builder.
*/
public abstract ZoneInfo build();
}
static class BuilderImpl extends Builder {
private String name;
private String id;
private Long creationTimeMillis;
private String dnsName;
private String description;
private String nameServerSet;
private List<String> nameServers;
private BuilderImpl(String name) {
this.name = checkNotNull(name);
}
/**
* Creates a builder from an existing ZoneInfo object.
*/
BuilderImpl(ZoneInfo info) {
this.name = info.name;
this.id = info.id;
this.creationTimeMillis = info.creationTimeMillis;
this.dnsName = info.dnsName;
this.description = info.description;
this.nameServerSet = info.nameServerSet;
if (info.nameServers != null) {
this.nameServers = ImmutableList.copyOf(info.nameServers);
}
}
@Override
public Builder name(String name) {
this.name = checkNotNull(name);
return this;
}
@Override
Builder id(String id) {
this.id = id;
return this;
}
@Override
Builder creationTimeMillis(long creationTimeMillis) {
this.creationTimeMillis = creationTimeMillis;
return this;
}
@Override
public Builder dnsName(String dnsName) {
this.dnsName = checkNotNull(dnsName);
return this;
}
@Override
public Builder description(String description) {
this.description = checkNotNull(description);
return this;
}
@Override
Builder nameServerSet(String nameServerSet) {
this.nameServerSet = checkNotNull(nameServerSet);
return this;
}
@Override
Builder nameServers(List<String> nameServers) {
checkNotNull(nameServers);
this.nameServers = Lists.newLinkedList(nameServers);
return this;
}
@Override
public ZoneInfo build() {
return new ZoneInfo(this);
}
}
ZoneInfo(BuilderImpl builder) {
this.name = builder.name;
this.id = builder.id;
this.creationTimeMillis = builder.creationTimeMillis;
this.dnsName = builder.dnsName;
this.description = builder.description;
this.nameServerSet = builder.nameServerSet;
this.nameServers = builder.nameServers == null
? null : ImmutableList.copyOf(builder.nameServers);
}
/**
* Returns a ZoneInfo object with assigned {@code name}, {@code dnsName} and {@code description}.
*/
public static ZoneInfo of(String name, String dnsName, String description) {
return new BuilderImpl(name).dnsName(dnsName).description(description).build();
}
/**
* Returns the user-defined name of the zone.
*/
public String name() {
return name;
}
/**
* Returns the read-only zone id assigned by the server.
*/
public String id() {
return id;
}
/**
* Returns the time when this zone was created on the server.
*/
public Long creationTimeMillis() {
return creationTimeMillis;
}
/**
* Returns the DNS name of this zone, for instance "example.com.".
*/
public String dnsName() {
return dnsName;
}
/**
* Returns the description of this zone.
*/
public String description() {
return description;
}
/**
* Returns the optionally specified set of DNS name servers that all host this zone. This value is
* set only for specific use cases and is left empty for vast majority of users.
*/
public String nameServerSet() {
return nameServerSet;
}
/**
* The nameservers that the zone should be delegated to. This is defined by the Google DNS cloud.
*/
public List<String> nameServers() {
return nameServers == null ? ImmutableList.<String>of() : nameServers;
}
/**
* Returns a builder for {@code ZoneInfo} prepopulated with the metadata of this zone.
*/
public Builder toBuilder() {
return new BuilderImpl(this);
}
com.google.api.services.dns.model.ManagedZone toPb() {
com.google.api.services.dns.model.ManagedZone pb =
new com.google.api.services.dns.model.ManagedZone();
pb.setDescription(this.description());
pb.setDnsName(this.dnsName());
if (this.id() != null) {
pb.setId(new BigInteger(this.id()));
}
pb.setName(this.name());
pb.setNameServers(this.nameServers); // do use real attribute value which may be null
pb.setNameServerSet(this.nameServerSet());
if (this.creationTimeMillis() != null) {
pb.setCreationTime(ISODateTimeFormat.dateTime()
.withZoneUTC()
.print(this.creationTimeMillis()));
}
return pb;
}
static ZoneInfo fromPb(com.google.api.services.dns.model.ManagedZone pb) {
Builder builder = new BuilderImpl(pb.getName());
if (pb.getDescription() != null) {
builder.description(pb.getDescription());
}
if (pb.getDnsName() != null) {
builder.dnsName(pb.getDnsName());
}
if (pb.getId() != null) {
builder.id(pb.getId().toString());
}
if (pb.getNameServers() != null) {
builder.nameServers(pb.getNameServers());
}
if (pb.getNameServerSet() != null) {
builder.nameServerSet(pb.getNameServerSet());
}
if (pb.getCreationTime() != null) {
builder.creationTimeMillis(DateTime.parse(pb.getCreationTime()).getMillis());
}
return builder.build();
}
@Override
public boolean equals(Object obj) {
return obj != null && obj.getClass().equals(ZoneInfo.class)
&& Objects.equals(toPb(), ((ZoneInfo) obj).toPb());
}
@Override
public int hashCode() {
return Objects.hash(name, id, creationTimeMillis, dnsName,
description, nameServerSet, nameServers);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name())
.add("id", id())
.add("description", description())
.add("dnsName", dnsName())
.add("nameServerSet", nameServerSet())
.add("nameServers", nameServers())
.add("creationTimeMillis", creationTimeMillis())
.toString();
}
}