Skip to content

Commit 95318d8

Browse files
committed
Added third concept of DNS batch.
1 parent 2f90e7e commit 95318d8

File tree

9 files changed

+534
-17
lines changed

9 files changed

+534
-17
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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;
18+
19+
/**
20+
* The class that holds a single result of a batch call.
21+
*/
22+
public interface BatchResult<T, E extends Throwable> {
23+
24+
/**
25+
* Returns {@code true} if the batch has been submitted and the result is available, and {@code
26+
* false} otherwise
27+
*/
28+
boolean submitted();
29+
30+
/**
31+
* Returns result of this call.
32+
*
33+
* @throws IllegalArgumentException if the batch has not been submitted yet
34+
* @throws E if an error occured when processing this request
35+
*/
36+
T get() throws E;
37+
}

gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,12 @@ ChangeRequest getChangeRequest(String zoneName, String changeRequestId,
533533
* @see <a href="https://cloud.google.com/dns/api/v1/changes/list">Cloud DNS Chages: list</a>
534534
*/
535535
Page<ChangeRequest> listChangeRequests(String zoneName, ChangeRequestListOption... options);
536+
537+
/**
538+
* Initiates a new empty batch ready to be populated with service calls, which will use this
539+
* {@code Dns} instance when submitted for processing to Google Cloud DNS.
540+
*/
541+
DnsBatch batch();
542+
543+
void submitBatch(DnsBatch batch);
536544
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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 com.google.gcloud.Page;
20+
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
24+
/**
25+
* A batch of operations to be submitted to Google Cloud DNS using a single HTTP request.
26+
*/
27+
public class DnsBatch {
28+
29+
private List<Request> requests = new LinkedList<>();
30+
private Dns dns;
31+
32+
/**
33+
* An operation to be submitted to Google Cloud DNS within this batch. Only an subset of the class
34+
* attributes appropriate for the represented operation is initialized. Refer to the class method
35+
* and attribute documentation for the specific fields.
36+
*/
37+
public static class Request {
38+
39+
private final String zoneName;
40+
private final String changeId;
41+
private final ChangeRequest changeRequest;
42+
private final ZoneInfo zoneInfo;
43+
private final Operation operation;
44+
private final AbstractOption[] options;
45+
private final DnsBatchResult result;
46+
47+
private Request(RequestBuilder builder) {
48+
this.zoneName = builder.zoneName;
49+
this.changeId = builder.changeId;
50+
this.changeRequest = builder.changeRequest;
51+
this.zoneInfo = builder.zoneInfo;
52+
this.operation = builder.operation;
53+
this.options = builder.options;
54+
this.result = builder.result;
55+
}
56+
57+
private static RequestBuilder builder(Operation operation, DnsBatchResult result,
58+
AbstractOption... options) {
59+
return new RequestBuilder(operation, result, options);
60+
}
61+
62+
/**
63+
* Returns the name of the zone to which the operation is applied. This field is initialized for
64+
* zone create, get and delete operation, and listing DNS records and changes within a zone.
65+
* Returns {@code null} in other cases.
66+
*/
67+
public String zoneName() {
68+
return zoneName;
69+
}
70+
71+
/**
72+
* Returns the id of the change request which is being retrieved. Getting a change request is
73+
* the only operation when this attribute is initialized. The method returns {@code null} in the
74+
* remaining cases.
75+
*/
76+
public String changeId() {
77+
return changeId;
78+
}
79+
80+
/**
81+
* Returns the change request which is being created. Creating a change request is the only
82+
* operation when this attribute is initialized. The method returns {@code null} in the
83+
* remaining cases.
84+
*/
85+
public ChangeRequest changeRequest() {
86+
return changeRequest;
87+
}
88+
89+
/**
90+
* Returns the zone which is being created. Creating a zone is the only operation when this
91+
* attribute is initialized. The method returns {@code null} in the remaining cases.
92+
*/
93+
public ZoneInfo zoneInfo() {
94+
return zoneInfo;
95+
}
96+
97+
/**
98+
* Returns the type of the operation represented by this {@link DnsBatch.Request}. This field is
99+
* always initialized.
100+
*/
101+
public Operation operation() {
102+
return operation;
103+
}
104+
105+
/**
106+
* Returns options provided to the operation. Returns an empty array if no options were
107+
* provided.
108+
*/
109+
public AbstractOption[] options() {
110+
return options == null ? new AbstractOption[0] : options;
111+
}
112+
113+
DnsBatchResult result() {
114+
return result;
115+
}
116+
}
117+
118+
static class RequestBuilder {
119+
private final AbstractOption[] options;
120+
private String zoneName;
121+
private String changeId;
122+
private ChangeRequest changeRequest;
123+
private ZoneInfo zoneInfo;
124+
private final Operation operation;
125+
private final DnsBatchResult result;
126+
127+
RequestBuilder(Operation operation, DnsBatchResult result, AbstractOption... options) {
128+
this.operation = operation;
129+
this.options = options;
130+
this.result = result;
131+
}
132+
133+
RequestBuilder zoneName(String zoneName) {
134+
this.zoneName = zoneName;
135+
return this;
136+
}
137+
138+
RequestBuilder changeId(String changeId) {
139+
this.changeId = changeId;
140+
return this;
141+
}
142+
143+
RequestBuilder changeRequest(ChangeRequest changeRequest) {
144+
this.changeRequest = changeRequest;
145+
return this;
146+
}
147+
148+
RequestBuilder zoneInfo(ZoneInfo zoneInfo) {
149+
this.zoneInfo = zoneInfo;
150+
return this;
151+
}
152+
153+
Request build() {
154+
return new Request(this);
155+
}
156+
}
157+
158+
/**
159+
* Represents the type of the batch operation.
160+
*/
161+
public enum Operation {
162+
CREATE_ZONE,
163+
DELETE_ZONE,
164+
GET_ZONE,
165+
LIST_ZONES,
166+
APPLY_CHANGE_REQUEST,
167+
GET_CHANGE_REQUEST,
168+
LIST_CHANGES_REQUESTS,
169+
LIST_DNS_RECORDS
170+
}
171+
172+
DnsBatch(Dns dns) {
173+
this.dns = dns;
174+
}
175+
176+
public Dns service() {
177+
return dns;
178+
}
179+
180+
List<Request> requests() {
181+
return requests;
182+
}
183+
184+
/**
185+
* Adds a {@code DnsBatch.Request} representing the list zones operation to this batch. The
186+
* request will not have initialized any fields except for the operation type and options (if
187+
* provided). The {@code options} can be used to restrict the fields returned or provide page size
188+
* limits in the same way as for {@link Dns#listZones(Dns.ZoneListOption...)}.
189+
*/
190+
public DnsBatchResult<Page<Zone>> listZones(Dns.ZoneListOption... options) {
191+
DnsBatchResult<Page<Zone>> result = new DnsBatchResult<>();
192+
Request request = Request.builder(Operation.LIST_ZONES, result, options).build();
193+
requests.add(request);
194+
return result;
195+
}
196+
197+
// todo(mderka) add the rest of the operations
198+
199+
/**
200+
* Submits this batch for processing using a single HTTP request.
201+
*/
202+
public void submit() {
203+
dns.submitBatch(this);
204+
}
205+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.google.gcloud.dns;
2+
3+
import com.google.gcloud.BatchResult;
4+
5+
/**
6+
* This class holds a single result of a batch call to the Cloud DNS.
7+
*/
8+
public class DnsBatchResult<T> implements BatchResult<T, DnsException> {
9+
10+
private T result;
11+
private boolean submitted = false;
12+
private DnsException error;
13+
14+
DnsBatchResult() {
15+
}
16+
17+
@Override
18+
public boolean submitted() {
19+
return submitted;
20+
}
21+
22+
@Override
23+
public T get() throws DnsException {
24+
if(!submitted()) {
25+
throw new IllegalStateException("Batch has not been submitted yet");
26+
}
27+
if(error != null) {
28+
throw error;
29+
}
30+
return result;
31+
}
32+
33+
void result(T result) {
34+
this.result = result;
35+
}
36+
37+
void error(DnsException error) {
38+
this.error = error;
39+
}
40+
41+
void submit() {
42+
this.submitted = true;
43+
}
44+
}

gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gcloud.dns;
1818

19+
import com.google.api.client.googleapis.json.GoogleJsonError;
1920
import com.google.common.collect.ImmutableSet;
2021
import com.google.gcloud.BaseServiceException;
2122
import com.google.gcloud.RetryHelper.RetryHelperException;
@@ -39,6 +40,10 @@ public class DnsException extends BaseServiceException {
3940
new Error(null, "rateLimitExceeded"));
4041
private static final long serialVersionUID = 490302380416260252L;
4142

43+
public DnsException(GoogleJsonError error) {
44+
super(error, true);
45+
}
46+
4247
public DnsException(IOException exception) {
4348
super(exception, true);
4449
}

0 commit comments

Comments
 (0)