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
+ }
0 commit comments