|
| 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.LinkedHashMap; |
| 22 | +import java.util.Map; |
| 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 Map<Request, Callback> requests = new LinkedHashMap<>(); |
| 30 | + private Dns dns; |
| 31 | + |
| 32 | + /** |
| 33 | + * An interface for the callback which will be invoked when the operation has been executed. The |
| 34 | + * parameter {@code <T>} represents the type of the result of the operation and thus depends on |
| 35 | + * the {@link DnsBatch.Request} that this call back belongs to and it should be as follows: |
| 36 | + * |
| 37 | + * <ul> |
| 38 | + * <li>{@link Zone} for creating and getting a zone</li> |
| 39 | + * <li>{@link Boolean} for deleting a zone</li> |
| 40 | + * <li>{@link ChangeRequest} for creating and getting a change request</li> |
| 41 | + * <li>{@link ProjectInfo} for getting a project</li> |
| 42 | + * <li>{@code Page<Zone>} for listing zones</li> |
| 43 | + * <li>{@code Page<DnsRecord>} for listing {@link DnsRecord}s inside a zone</li> |
| 44 | + * <li>{@code Page<ChangeRequest>} for listing {@link ChangeRequest}s for a zone</li> |
| 45 | + * </ul> |
| 46 | + */ |
| 47 | + public interface Callback<T> { |
| 48 | + /** |
| 49 | + * A method which will be called if the {@link DnsBatch.Request} succeeds. See the {@link |
| 50 | + * Callback} documentation for details on type {@code T}. |
| 51 | + * |
| 52 | + * @param output the result of the operation |
| 53 | + * @param request the request which succeeded |
| 54 | + */ |
| 55 | + void success(T output, DnsBatch.Request request); |
| 56 | + |
| 57 | + /** |
| 58 | + * A method which will be called if the {@link DnsBatch.Request} fails. |
| 59 | + * |
| 60 | + * @param ex the error |
| 61 | + * @param request the request which failed |
| 62 | + */ |
| 63 | + void error(DnsException ex, DnsBatch.Request request); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * An operation to be submitted to Google Cloud DNS within this batch. Only an subset of the class |
| 68 | + * attributes appropriate for the represented operation is initialized. Refer to the class method |
| 69 | + * and attribute documentation for the specific fields. |
| 70 | + */ |
| 71 | + public static class Request { |
| 72 | + |
| 73 | + private final String zoneName; |
| 74 | + private final String changeId; |
| 75 | + private final ChangeRequest changeRequest; |
| 76 | + private final ZoneInfo zoneInfo; |
| 77 | + private final Operation operation; |
| 78 | + private final AbstractOption[] options; |
| 79 | + |
| 80 | + private Request(RequestBuilder builder) { |
| 81 | + this.zoneName = builder.zoneName; |
| 82 | + this.changeId = builder.changeId; |
| 83 | + this.changeRequest = builder.changeRequest; |
| 84 | + this.zoneInfo = builder.zoneInfo; |
| 85 | + this.operation = builder.operation; |
| 86 | + this.options = builder.options; |
| 87 | + } |
| 88 | + |
| 89 | + private static RequestBuilder builder(Operation operation, AbstractOption... options) { |
| 90 | + return new RequestBuilder(operation, options); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the name of the zone to which the operation is applied. This field is initialized for |
| 95 | + * zone create, get and delete operation, and listing DNS records and changes within a zone. |
| 96 | + * Returns {@code null} in other cases. |
| 97 | + */ |
| 98 | + public String zoneName() { |
| 99 | + return zoneName; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the id of the change request which is being retrieved. Getting a change request is |
| 104 | + * the only operation when this attribute is initialized. The method returns {@code null} in the |
| 105 | + * remaining cases. |
| 106 | + */ |
| 107 | + public String changeId() { |
| 108 | + return changeId; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the change request which is being created. Creating a change request is the only |
| 113 | + * operation when this attribute is initialized. The method returns {@code null} in the |
| 114 | + * remaining cases. |
| 115 | + */ |
| 116 | + public ChangeRequest changeRequest() { |
| 117 | + return changeRequest; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the zone which is being created. Creating a zone is the only operation when this |
| 122 | + * attribute is initialized. The method returns {@code null} in the remaining cases. |
| 123 | + */ |
| 124 | + public ZoneInfo zoneInfo() { |
| 125 | + return zoneInfo; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns the type of the operation represented by this {@link DnsBatch.Request}. This field is |
| 130 | + * always initialized. |
| 131 | + */ |
| 132 | + public Operation operation() { |
| 133 | + return operation; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns options provided to the operation. Returns an empty array if no options were |
| 138 | + * provided. |
| 139 | + */ |
| 140 | + public AbstractOption[] options() { |
| 141 | + return options == null ? new AbstractOption[0] : options; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + static class RequestBuilder { |
| 146 | + private final AbstractOption[] options; |
| 147 | + private String zoneName; |
| 148 | + private String changeId; |
| 149 | + private ChangeRequest changeRequest; |
| 150 | + private ZoneInfo zoneInfo; |
| 151 | + private final Operation operation; |
| 152 | + |
| 153 | + RequestBuilder(Operation operation, AbstractOption... options) { |
| 154 | + this.operation = operation; |
| 155 | + this.options = options; |
| 156 | + } |
| 157 | + |
| 158 | + RequestBuilder zoneName(String zoneName) { |
| 159 | + this.zoneName = zoneName; |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + RequestBuilder changeId(String changeId) { |
| 164 | + this.changeId = changeId; |
| 165 | + return this; |
| 166 | + } |
| 167 | + |
| 168 | + RequestBuilder changeRequest(ChangeRequest changeRequest) { |
| 169 | + this.changeRequest = changeRequest; |
| 170 | + return this; |
| 171 | + } |
| 172 | + |
| 173 | + RequestBuilder zoneInfo(ZoneInfo zoneInfo) { |
| 174 | + this.zoneInfo = zoneInfo; |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + Request build() { |
| 179 | + return new Request(this); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Represents the type of the batch operation. |
| 185 | + */ |
| 186 | + public enum Operation { |
| 187 | + CREATE_ZONE, |
| 188 | + DELETE_ZONE, |
| 189 | + GET_ZONE, |
| 190 | + LIST_ZONES, |
| 191 | + APPLY_CHANGE_REQUEST, |
| 192 | + GET_CHANGE_REQUEST, |
| 193 | + LIST_CHANGES_REQUESTS, |
| 194 | + LIST_DNS_RECORDS |
| 195 | + } |
| 196 | + |
| 197 | + DnsBatch(Dns dns) { |
| 198 | + this.dns = dns; |
| 199 | + } |
| 200 | + |
| 201 | + public Dns service() { |
| 202 | + return dns; |
| 203 | + } |
| 204 | + |
| 205 | + Map<Request, Callback> requests() { |
| 206 | + return requests; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Adds a {@code DnsBatch.Request} represeting the list zones operation to this batch. The request |
| 211 | + * will not have initialized any fields except for the operation type and options (if provided). |
| 212 | + * The {@code callback} will receive a page of {@link Zone}s upon success of the request. The |
| 213 | + * {@code options} can be used to restrict the fields returned or provide page size limits in the |
| 214 | + * same way as for {@link Dns#listZones(Dns.ZoneListOption...)}. |
| 215 | + */ |
| 216 | + public DnsBatch listZones(Callback<Page<Zone>> callback, Dns.ZoneListOption... options) { |
| 217 | + Request request = Request.builder(Operation.LIST_ZONES, options).build(); |
| 218 | + requests.put(request, callback); |
| 219 | + return this; |
| 220 | + } |
| 221 | + |
| 222 | + // todo(mderka) add the rest of the operations |
| 223 | + |
| 224 | + /** |
| 225 | + * Submits this batch for processing using a single HTTP request. This will invoke all callbacks |
| 226 | + * for the invidual {@link DnsBatch.Request}s contained in this batch. |
| 227 | + */ |
| 228 | + public void submit() { |
| 229 | + dns.submitBatch(this); |
| 230 | + } |
| 231 | +} |
0 commit comments