1616
1717package com .google .gcloud .dns ;
1818
19+ import static com .google .common .base .Preconditions .checkArgument ;
20+
21+ import com .google .api .client .googleapis .batch .BatchRequest ;
22+ import com .google .api .client .googleapis .batch .json .JsonBatchCallback ;
23+ import com .google .api .client .googleapis .json .GoogleJsonError ;
24+ import com .google .api .client .http .HttpHeaders ;
25+ import com .google .api .services .dns .model .ManagedZone ;
26+ import com .google .api .services .dns .model .ManagedZonesListResponse ;
27+ import com .google .common .collect .ImmutableList ;
28+ import com .google .common .collect .ImmutableMap ;
29+ import com .google .common .collect .Iterables ;
30+ import com .google .common .collect .Maps ;
1931import com .google .gcloud .Page ;
32+ import com .google .gcloud .PageImpl ;
33+ import com .google .gcloud .dns .spi .DnsRpc ;
2034
21- import java .util . LinkedList ;
35+ import java .io . IOException ;
2236import java .util .List ;
37+ import java .util .Map ;
2338
2439/**
2540 * A batch of operations to be submitted to Google Cloud DNS using a single HTTP request.
2641 */
2742public class DnsBatch {
2843
29- private List <Request > requests = new LinkedList <>();
30- private Dns dns ;
44+ private final BatchRequest batch ;
45+ private transient DnsRpc dnsRpc ;
46+ private final DnsOptions options ;
3147
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- }
48+ DnsBatch (DnsOptions options ) {
49+ this .options = options ;
50+ this .dnsRpc = options .rpc ();
51+ this .batch = dnsRpc .createBatch ();
15652 }
15753
15854 /**
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
55+ * Adds a request representing the list zones operation to this batch. The
18656 * request will not have initialized any fields except for the operation type and options (if
18757 * provided). The {@code options} can be used to restrict the fields returned or provide page size
18858 * limits in the same way as for {@link Dns#listZones(Dns.ZoneListOption...)}.
18959 */
19060 public DnsBatchResult <Page <Zone >> listZones (Dns .ZoneListOption ... options ) {
19161 DnsBatchResult <Page <Zone >> result = new DnsBatchResult <>();
192- Request request = Request .builder (Operation .LIST_ZONES , result , options ).build ();
193- requests .add (request );
62+ final Map <DnsRpc .Option , ?> optionMap = optionMap (options );
63+ JsonBatchCallback callback = listZonesCallback (this .options , result , optionMap );
64+ dnsRpc .prepareListZones (this .batch , callback , optionMap );
19465 return result ;
19566 }
19667
197- // todo(mderka) add the rest of the operations
198-
19968 /**
20069 * Submits this batch for processing using a single HTTP request.
20170 */
20271 public void submit () {
203- dns .submitBatch (this );
72+ dnsRpc .submitBatch (batch );
73+ }
74+
75+ // todo(mderka) make methods to prepare other callbacks
76+ private JsonBatchCallback listZonesCallback (final DnsOptions serviceOptions ,
77+ final DnsBatchResult result , final Map <DnsRpc .Option , ?> optionMap ) {
78+ JsonBatchCallback callback = new JsonBatchCallback <ManagedZonesListResponse >() {
79+ @ Override
80+ public void onSuccess (ManagedZonesListResponse response , HttpHeaders httpHeaders )
81+ throws IOException {
82+ List <ManagedZone > zones = response .getManagedZones ();
83+ Page <Zone > zonePage = new PageImpl <>(
84+ new DnsImpl .ZonePageFetcher (options , response .getNextPageToken (), optionMap ),
85+ response .getNextPageToken (), zones == null ? ImmutableList .<Zone >of ()
86+ : Iterables .transform (zones , DnsImpl .pbToZoneFunction (serviceOptions )));
87+ result .success (zonePage );
88+ }
89+
90+ @ Override
91+ public void onFailure (GoogleJsonError googleJsonError , HttpHeaders httpHeaders )
92+ throws IOException {
93+ result .error (new DnsException (googleJsonError ));
94+ }
95+ };
96+ return callback ;
97+ }
98+
99+ private Map <DnsRpc .Option , ?> optionMap (AbstractOption ... options ) {
100+ Map <DnsRpc .Option , Object > temp = Maps .newEnumMap (DnsRpc .Option .class );
101+ for (AbstractOption option : options ) {
102+ Object prev = temp .put (option .rpcOption (), option .value ());
103+ checkArgument (prev == null , "Duplicate option %s" , option );
104+ }
105+ return ImmutableMap .copyOf (temp );
204106 }
205- }
107+ }
0 commit comments