From f9b6f6a4762484da32ceee58e5281f1d615a1a12 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 28 Jan 2016 15:55:06 -0800 Subject: [PATCH 01/12] Added DnsService interface. --- .../com/google/gcloud/dns/DnsService.java | 507 ++++++++++++++++++ 1 file changed, 507 insertions(+) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java new file mode 100644 index 000000000000..694b0b288154 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java @@ -0,0 +1,507 @@ +/* + * 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 com.google.common.base.Joiner; +import com.google.common.collect.Sets; +import com.google.gcloud.spi.DnsServiceRpc; + +import java.io.Serializable; +import java.util.Set; + +/** + * An interface for the Google Cloud DNS service. + * + * @see Google Cloud DNS + */ +public interface DnsService extends Service { + + /** + * The fields of a project. + * + *

These values can be used to specify the fields to include in a partial response when calling + * {@code DnsService#getProjectInfo(ProjectOptions...)}. Project ID is always returned, even if + * not specified. + */ + enum ProjectField { + PROJECT_ID("id"), + PROJECT_NUMBER("number"), + QUOTA("quota"); + + private final String selector; + + ProjectField(String selector) { + this.selector = selector; + } + + public String selector() { + return selector; + } + + static String selector(ProjectField... fields) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); + fieldStrings.add(PROJECT_ID.selector()); + for (ProjectField field : fields) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + } + + /** + * The fields of a zone. + * + *

These values can be used to specify the fields to include in a partial response when calling + * {@code DnsService#getZone(BigInteger, ZoneFieldOptions...)} or {@code + * DnsService#getZone(String, ZoneFieldOptions...)}. The ID is always returned, even if not + * specified. + */ + enum ZoneField { + CREATION_TIME("creationTime"), + DESCRIPTION("description"), + DNS_NAME("dnsName"), + ZONE_ID("id"), + NAME("name"), + NAME_SERVER_SET("nameServerSet"), + NAME_SERVERS("nameServers"); + + private final String selector; + + ZoneField(String selector) { + this.selector = selector; + } + + public String selector() { + return selector; + } + + static String selector(ZoneField... fields) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); + fieldStrings.add(ZONE_ID.selector()); + for (ZoneField field : fields) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + } + + /** + * The fields of a DNS record. + * + *

These values can be used to specify the fields to include in a partial response when calling + * {@code DnsService#listDnsRecords(BigInteger, DnsRecordOptions...)} or {@code + * DnsService#listDnsRecords(String, DnsRecordOptions...)}. The name is always returned even if + * not selected. + */ + enum DnsRecordField { + DNS_RECORDS("rrdatas"), + NAME("name"), + TTL("ttl"), + TYPE("type"); + + private final String selector; + + DnsRecordField(String selector) { + this.selector = selector; + } + + public String selector() { + return selector; + } + + static String selector(DnsRecordField... fields) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); + fieldStrings.add(NAME.selector()); + for (DnsRecordField field : fields) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + } + + /** + * The fields of a change request. + * + *

These values can be used to specify the fields to include in a partial response when calling + * {@code DnsService#applyChangeRequest(ChangeRequest, BigInteger, ChangeRequestFieldOptions...)} + * or {@code DnsService#applyChangeRequest(ChangeRequest, String, ChangeRequestFieldOptions...)} + * The ID is always returned even if not selected. + */ + enum ChangeRequestField { + ID("id"), + START_TIME("startTime"), + STATUS("status"), + ADDITIONS("additions"), + DELETIONS("deletions"); + + private final String selector; + + ChangeRequestField(String selector) { + this.selector = selector; + } + + public String selector() { + return selector; + } + + static String selector(ChangeRequestField... fields) { + Set fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1); + fieldStrings.add(ID.selector()); + for (ChangeRequestField field : fields) { + fieldStrings.add(field.selector()); + } + return Joiner.on(',').join(fieldStrings); + } + } + + /** + * The sorting keys for listing change requests. The only currently supported sorting key is the + * change sequence. + */ + enum ChangeRequestSortingKey { + CHANGE_SEQUENCE("changeSequence"); + + private final String selector; + + ChangeRequestSortingKey(String selector) { + this.selector = selector; + } + + public String selector() { + return selector; + } + } + + /** + * The sorting order for listing change requests. + */ + enum ChangeRequestSortingOrder { + DESCENDING, ASCENDING; + + public String selector() { + return this.name().toLowerCase(); + } + } + + /** + * Class that for specifying DNS record options. + */ + class DnsRecordOptions extends AbstractOption implements Serializable { + + private static final long serialVersionUID = 201601261646L; + + DnsRecordOptions(DnsServiceRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify the DNS record's fields to be returned by the RPC call. + * + *

If this option is not provided all record fields are returned. {@code + * DnsRecordField.fields} can be used to specify only the fields of interest. The name of the + * DNS record always returned, even if not specified. {@link DnsRecordField} provides a list of + * fields that can be used. + */ + public static DnsRecordOptions fields(DnsRecordField... fields) { + StringBuilder builder = new StringBuilder(); + builder.append("rrsets(").append(DnsRecordField.selector(fields)).append(")"); + return new DnsRecordOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); + } + + /** + * Returns an option to specify a page token. + * + *

The page token (returned from a previous call to list) indicates from where listing should + * continue. + */ + public static DnsRecordOptions pageToken(String pageToken) { + return new DnsRecordOptions(DnsServiceRpc.Option.PAGE_TOKEN, pageToken); + } + + /** + * The maximum number of DNS records to return per RPC. + * + *

The server can return fewer records than requested. When there are more results than the + * page size, the server will return a page token that can be used to fetch other results. + */ + public static DnsRecordOptions pageSize(int pageSize) { + return new DnsRecordOptions(DnsServiceRpc.Option.PAGE_SIZE, pageSize); + } + + /** + * Restricts the list to return only zones with this fully qualified domain name. + */ + public static DnsRecordOptions dnsName(String dnsName) { + return new DnsRecordOptions(DnsServiceRpc.Option.DNS_NAME, dnsName); + } + } + + /** + * Class for specifying zone field options. + */ + class ZoneFieldOptions extends AbstractOption implements Serializable { + + private static final long serialVersionUID = -7294186261285469986L; + + ZoneFieldOptions(DnsServiceRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify the zones's fields to be returned by the RPC call. + * + *

If this option is not provided all zone fields are returned. {@code + * ZoneFieldOptions.fields} can be used to specify only the fields of interest. Zone ID is + * always returned, even if not specified. {@link ZoneField} provides a list of fields that can + * be used. + */ + public static ZoneFieldOptions fields(ZoneField... fields) { + return new ZoneFieldOptions(DnsServiceRpc.Option.FIELDS, ZoneField.selector(fields)); + } + } + + /** + * Class for specifying zone listing options. + */ + class ZoneListOptions extends AbstractOption implements Serializable { + + private static final long serialVersionUID = -7922038132321229290L; + + ZoneListOptions(DnsServiceRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify the zones's fields to be returned by the RPC call. + * + *

If this option is not provided all zone fields are returned. {@code + * ZoneFieldOptions.fields} can be used to specify only the fields of interest. Zone ID is + * always returned, even if not specified. {@link ZoneField} provides a list of fields that can + * be used. + */ + public static ZoneListOptions fields(ZoneField... fields) { + return new ZoneListOptions(DnsServiceRpc.Option.FIELDS, ZoneField.selector(fields)); + } + + /** + * Returns an option to specify a page token. + * + *

The page token (returned from a previous call to list) indicates from where listing should + * continue. + */ + public static ZoneListOptions pageToken(String pageToken) { + return new ZoneListOptions(DnsServiceRpc.Option.PAGE_TOKEN, pageToken); + } + + /** + * The maximum number of zones to return per RPC. + * + *

The server can return fewer zones than requested. When there are more results than the + * page size, the server will return a page token that can be used to fetch other results. + */ + public static ZoneListOptions pageSize(int pageSize) { + return new ZoneListOptions(DnsServiceRpc.Option.PAGE_SIZE, pageSize); + } + + /** + * Restricts the list to return only zones with this fully qualified domain name. + */ + public static ZoneListOptions dnsName(String dnsName) { + return new ZoneListOptions(DnsServiceRpc.Option.DNS_NAME, dnsName); + } + } + + /** + * Class for specifying project options. + */ + class ProjectOptions extends AbstractOption implements Serializable { + + private static final long serialVersionUID = 6817937338218847748L; + + ProjectOptions(DnsServiceRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify the project's fields to be returned by the RPC call. + * + *

If this option is not provided all project fields are returned. {@code + * ProjectOptions.fields} can be used to specify only the fields of interest. Project ID is + * always returned, even if not specified. {@link ProjectField} provides a list of fields that + * can be used. + */ + public static ProjectOptions fields(ProjectField... fields) { + return new ProjectOptions(DnsServiceRpc.Option.FIELDS, ProjectField.selector(fields)); + } + } + + /** + * Class for specifying change request field options. + */ + class ChangeRequestFieldOptions extends AbstractOption implements Serializable { + + private static final long serialVersionUID = 1067273695061077782L; + + ChangeRequestFieldOptions(DnsServiceRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify which fields of DNS records to be added by the {@link + * ChangeRequest} should be returned by the service. + * + *

If this option is not provided, all record fields are returned. {@code + * ChangeRequestFieldOptions.additionsFields} can be used to specify only the fields of + * interest. The name of the DNS record always returned, even if not specified. {@link + * DnsRecordField} provides a list of fields that can be used. + */ + public static ChangeRequestFieldOptions additionsFields(DnsRecordField... fields) { + StringBuilder builder = new StringBuilder(); + builder.append("additions(").append(DnsRecordField.selector(fields)).append(")"); + return new ChangeRequestFieldOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); + } + + /** + * Returns an option to specify which fields of DNS records to be deleted by the {@link + * ChangeRequest} should be returned by the service. + * + *

If this option is not provided, all record fields are returned. {@code + * ChangeRequestFieldOptions.deletionsFields} can be used to specify only the fields of + * interest. The name of the DNS record always returned, even if not specified. {@link + * DnsRecordField} provides a list of fields that can be used. + */ + public static ChangeRequestFieldOptions deletionsFields(DnsRecordField... fields) { + StringBuilder builder = new StringBuilder(); + builder.append("deletions(").append(DnsRecordField.selector(fields)).append(")"); + return new ChangeRequestFieldOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); + + } + + /** + * Returns an option to specify which fields of {@link ChangeRequest} should be returned by the + * service. + * + *

If this option is not provided all change request fields are returned. {@code + * ChangeRequestFieldOptions.fields} can be used to specify only the fields of interest. The ID + * of the change request is always returned, even if not specified. {@link ChangeRequestField} + * provides a list of fields that can be used. + */ + public static ChangeRequestFieldOptions fields(ChangeRequestField... fields) { + return new ChangeRequestFieldOptions( + DnsServiceRpc.Option.FIELDS, + ChangeRequestField.selector(fields) + ); + } + } + + /** + * Class for specifying change request listing options. + */ + class ChangeRequestListOptions extends AbstractOption implements Serializable { + + private static final long serialVersionUID = -900209143895376089L; + + ChangeRequestListOptions(DnsServiceRpc.Option option, Object value) { + super(option, value); + } + + /** + * Returns an option to specify which fields of DNS records to be added by the {@link + * ChangeRequest} should be returned by the service. + * + *

If this option is not provided, all record fields are returned. {@code + * ChangeRequestFieldOptions.additionsFields} can be used to specify only the fields of + * interest. The name of the DNS record always returned, even if not specified. {@link + * DnsRecordField} provides a list of fields that can be used. + */ + public static ChangeRequestListOptions additionsFields(DnsRecordField... fields) { + StringBuilder builder = new StringBuilder(); + builder.append("changes(additions(").append(DnsRecordField.selector(fields)).append("))"); + return new ChangeRequestListOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); + } + + /** + * Returns an option to specify which fields of DNS records to be deleted by the {@link + * ChangeRequest} should be returned by the service. + * + *

If this option is not provided, all record fields are returned. {@code + * ChangeRequestFieldOptions.deletionsFields} can be used to specify only the fields of + * interest. The name of the DNS record always returned, even if not specified. {@link + * DnsRecordField} provides a list of fields that can be used. + */ + public static ChangeRequestListOptions deletionsFields(DnsRecordField... fields) { + StringBuilder builder = new StringBuilder(); + builder.append("changes(deletions(").append(DnsRecordField.selector(fields)).append("))"); + return new ChangeRequestListOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); + } + + /** + * Returns an option to specify which fields of{@link ChangeRequest} should be returned by the + * service. + * + *

If this option is not provided all change request fields are returned. {@code + * ChangeRequestFieldOptions.fields} can be used to specify only the fields of interest. The ID + * of the change request is always returned, even if not specified. {@link ChangeRequestField} + * provides a list of fields that can be used. + */ + public static ChangeRequestListOptions fields(ChangeRequestField... fields) { + return new ChangeRequestListOptions( + DnsServiceRpc.Option.FIELDS, + ChangeRequestField.selector(fields) + ); + } + + /** + * Returns an option to specify a page token. + * + *

The page token (returned from a previous call to list) indicates from where listing should + * continue. + */ + public static ChangeRequestListOptions pageToken(String pageToken) { + return new ChangeRequestListOptions(DnsServiceRpc.Option.PAGE_TOKEN, pageToken); + } + + /** + * The maximum number of change requests to return per RPC. + * + *

The server can return fewer change requests than requested. When there are more results + * than the page size, the server will return a page token that can be used to fetch other + * results. + */ + public static ChangeRequestListOptions pageSize(int pageSize) { + return new ChangeRequestListOptions(DnsServiceRpc.Option.PAGE_SIZE, pageSize); + } + + /** + * Returns an option for specifying the sorting criterion of change requests. Note the the only + * currently supported criterion is the change sequence. + */ + public static ChangeRequestListOptions sortBy(ChangeRequestSortingKey key) { + return new ChangeRequestListOptions(DnsServiceRpc.Option.SORTING_KEY, key.selector()); + } + + /** + * Returns an option to specify whether the the change requests should be listed in ascending or + * descending order. + */ + public static ChangeRequestListOptions sortOrder(ChangeRequestSortingOrder order) { + return new ChangeRequestListOptions(DnsServiceRpc.Option.SORTING_ORDER, order.selector()); + } + } + + // TODO(mderka) Add methods. Created issue #596. +} From 4fdc8ee53b189a137aa990dca366db369e47cf9c Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 28 Jan 2016 15:57:12 -0800 Subject: [PATCH 02/12] Added AbstractOption. --- .../com/google/gcloud/dns/AbstractOption.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java new file mode 100644 index 000000000000..6b6fbbf0606e --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java @@ -0,0 +1,69 @@ +/* + * 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.gcloud.spi.DnsServiceRpc; + +import java.io.Serializable; +import java.util.Objects; + +/** + * A base class for options. + */ +public abstract class AbstractOption implements Serializable { + + private static final long serialVersionUID = 201601261704L; + private final Object value; + private final DnsServiceRpc.Option rpcOption; + + AbstractOption(DnsServiceRpc.Option rpcOption, Object value) { + this.rpcOption = checkNotNull(rpcOption); + this.value = value; + } + + Object value() { + return value; + } + + DnsServiceRpc.Option rpcOption() { + return rpcOption; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof AbstractOption)) { + return false; + } + AbstractOption other = (AbstractOption) obj; + return Objects.equals(value, other.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("value", value) + .toString(); + } +} From 06becd885dc0d4c45cd779a06be448b5179e77d1 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 28 Jan 2016 15:58:13 -0800 Subject: [PATCH 03/12] Added DnsException. --- .../com/google/gcloud/dns/DnsException.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java new file mode 100644 index 000000000000..ab4a3df0f457 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java @@ -0,0 +1,37 @@ +/* + * 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 com.google.gcloud.BaseServiceException; + +/** + * DNS service exception. + */ +public class DnsException extends BaseServiceException { + + private static final long serialVersionUID = 490302380416260252L; + + public DnsException(int code, String message, boolean retryable) { + super(code, message, retryable); + } + + public DnsException(int code, String message, boolean retryable, Exception cause) { + super(code, message, retryable, cause); + } + + //TODO(mderka) Add translation and retry functionality. Created issue #593. +} From 81cedc4edf78e745817c831c1d7cde7653dfc4f8 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 28 Jan 2016 16:00:19 -0800 Subject: [PATCH 04/12] Added DnsServiceRpc. --- .../com/google/gcloud/spi/DnsServiceRpc.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpc.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpc.java new file mode 100644 index 000000000000..e97ee9a1b001 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpc.java @@ -0,0 +1,56 @@ +/* + * 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.spi; + +import java.util.Map; + +public interface DnsServiceRpc { + + enum Option { + FIELDS("fields"), + PAGE_SIZE("maxSize"), + PAGE_TOKEN("pageToken"), + DNS_NAME("dnsName"), + SORTING_KEY("sortBy"), + SORTING_ORDER("sortOrder"); + + private final String value; + + Option(String value) { + this.value = value; + } + + public String value() { + return value; + } + + @SuppressWarnings("unchecked") + T get(Map options) { + return (T) options.get(this); + } + + String getString(Map options) { + return get(options); + } + + Integer getInt(Map options) { + return get(options); + } + } + + //TODO(mderka) add supported operations. Created issue #594. +} From bf1361c034633137ba395f6e565a1579fb4797fb Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 28 Jan 2016 16:38:21 -0800 Subject: [PATCH 05/12] Added DnsServiceOptions and necessary dependencies. --- .../com/google/gcloud/dns/DnsService.java | 1 + .../google/gcloud/dns/DnsServiceFactory.java | 25 ++++++ .../google/gcloud/dns/DnsServiceOptions.java | 85 +++++++++++++++++++ .../gcloud/spi/DnsServiceRpcFactory.java | 26 ++++++ 4 files changed, 137 insertions(+) create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceFactory.java create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceOptions.java create mode 100644 gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpcFactory.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java index 694b0b288154..9cbd60eccf5a 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java @@ -18,6 +18,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.Sets; +import com.google.gcloud.Service; import com.google.gcloud.spi.DnsServiceRpc; import java.io.Serializable; diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceFactory.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceFactory.java new file mode 100644 index 000000000000..aaa0dfb68e1b --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceFactory.java @@ -0,0 +1,25 @@ +/* + * 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 com.google.gcloud.ServiceFactory; + +/** + * An interface for DnsService factories. + */ +public interface DnsServiceFactory extends ServiceFactory { +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceOptions.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceOptions.java new file mode 100644 index 000000000000..84eb9c33dcaa --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceOptions.java @@ -0,0 +1,85 @@ +/* + * Copyright 2015 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 com.google.gcloud.ServiceOptions; +import com.google.gcloud.spi.DnsServiceRpc; +import com.google.gcloud.spi.DnsServiceRpcFactory; + +import java.util.Set; + +public class DnsServiceOptions + extends ServiceOptions { + + private static final long serialVersionUID = -5311219368450107146L; + + // TODO(mderka) Finish implementation. Created issue #595. + + public static class DefaultDnsServiceFactory implements DnsServiceFactory { + private static final DnsServiceFactory INSTANCE = new DefaultDnsServiceFactory(); + + @Override + public DnsService create(DnsServiceOptions options) { + // TODO(mderka) Implement. Created issue #595. + return null; + } + } + + public static class Builder extends ServiceOptions.Builder { + + private Builder() { + } + + private Builder(DnsServiceOptions options) { + super(options); + } + + @Override + public DnsServiceOptions build() { + return new DnsServiceOptions(this); + } + } + + private DnsServiceOptions(Builder builder) { + super(DnsServiceFactory.class, DnsServiceRpcFactory.class, builder); + } + + @Override + protected DnsServiceFactory defaultServiceFactory() { + return DefaultDnsServiceFactory.INSTANCE; + } + + @Override + protected DnsServiceRpcFactory defaultRpcFactory() { + return null; + } + + @Override + protected Set scopes() { + return null; + } + + @Override + public Builder toBuilder() { + return new Builder(this); + } + + public static Builder builder() { + return new Builder(); + } +} diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpcFactory.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpcFactory.java new file mode 100644 index 000000000000..13ec9fe881c8 --- /dev/null +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpcFactory.java @@ -0,0 +1,26 @@ +/* + * 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.spi; + +import com.google.gcloud.dns.DnsServiceOptions; + +/** + * An interface for DnsServiceRpc factory. Implementation will be loaded via {@link + * java.util.ServiceLoader}. + */ +public interface DnsServiceRpcFactory extends ServiceRpcFactory { +} From 465f5327d81035666a12c1ef37bab0a1bfd55300 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Thu, 28 Jan 2016 18:15:47 -0800 Subject: [PATCH 06/12] Modified DnsException to pass tests. --- .../main/java/com/google/gcloud/dns/DnsException.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java index ab4a3df0f457..d18f6163a881 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsException.java @@ -18,6 +18,8 @@ import com.google.gcloud.BaseServiceException; +import java.io.IOException; + /** * DNS service exception. */ @@ -25,12 +27,8 @@ public class DnsException extends BaseServiceException { private static final long serialVersionUID = 490302380416260252L; - public DnsException(int code, String message, boolean retryable) { - super(code, message, retryable); - } - - public DnsException(int code, String message, boolean retryable, Exception cause) { - super(code, message, retryable, cause); + public DnsException(IOException exception, boolean idempotent) { + super(exception, idempotent); } //TODO(mderka) Add translation and retry functionality. Created issue #593. From a8bee9c1e07337d1e710d378d7a600215e26c1ba Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 29 Jan 2016 16:10:25 -0800 Subject: [PATCH 07/12] Implements comments by @aozarov. --- .../com/google/gcloud/dns/AbstractOption.java | 17 +- .../gcloud/dns/{DnsService.java => Dns.java} | 229 +++++++----------- ...DnsServiceFactory.java => DnsFactory.java} | 4 +- ...DnsServiceOptions.java => DnsOptions.java} | 38 +-- .../spi/{DnsServiceRpc.java => DnsRpc.java} | 3 +- ...viceRpcFactory.java => DnsRpcFactory.java} | 6 +- 6 files changed, 120 insertions(+), 177 deletions(-) rename gcloud-java-dns/src/main/java/com/google/gcloud/dns/{DnsService.java => Dns.java} (51%) rename gcloud-java-dns/src/main/java/com/google/gcloud/dns/{DnsServiceFactory.java => DnsFactory.java} (84%) rename gcloud-java-dns/src/main/java/com/google/gcloud/dns/{DnsServiceOptions.java => DnsOptions.java} (59%) rename gcloud-java-dns/src/main/java/com/google/gcloud/spi/{DnsServiceRpc.java => DnsRpc.java} (96%) rename gcloud-java-dns/src/main/java/com/google/gcloud/spi/{DnsServiceRpcFactory.java => DnsRpcFactory.java} (74%) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java index 6b6fbbf0606e..a148468d14b5 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/AbstractOption.java @@ -19,7 +19,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.MoreObjects; -import com.google.gcloud.spi.DnsServiceRpc; +import com.google.gcloud.spi.DnsRpc; import java.io.Serializable; import java.util.Objects; @@ -27,13 +27,13 @@ /** * A base class for options. */ -public abstract class AbstractOption implements Serializable { +abstract class AbstractOption implements Serializable { - private static final long serialVersionUID = 201601261704L; + private static final long serialVersionUID = -5912727967831484228L; private final Object value; - private final DnsServiceRpc.Option rpcOption; + private final DnsRpc.Option rpcOption; - AbstractOption(DnsServiceRpc.Option rpcOption, Object value) { + AbstractOption(DnsRpc.Option rpcOption, Object value) { this.rpcOption = checkNotNull(rpcOption); this.value = value; } @@ -42,7 +42,7 @@ Object value() { return value; } - DnsServiceRpc.Option rpcOption() { + DnsRpc.Option rpcOption() { return rpcOption; } @@ -52,18 +52,19 @@ public boolean equals(Object obj) { return false; } AbstractOption other = (AbstractOption) obj; - return Objects.equals(value, other.value); + return Objects.equals(value, other.value) && Objects.equals(rpcOption, other.rpcOption); } @Override public int hashCode() { - return Objects.hash(value); + return Objects.hash(value, rpcOption); } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("value", value) + .add("rpcOption", rpcOption) .toString(); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java similarity index 51% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 9cbd60eccf5a..737ec1a38699 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsService.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -19,7 +19,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.Sets; import com.google.gcloud.Service; -import com.google.gcloud.spi.DnsServiceRpc; +import com.google.gcloud.spi.DnsRpc; import java.io.Serializable; import java.util.Set; @@ -29,13 +29,13 @@ * * @see Google Cloud DNS */ -public interface DnsService extends Service { +public interface Dns extends Service { /** * The fields of a project. * *

These values can be used to specify the fields to include in a partial response when calling - * {@code DnsService#getProjectInfo(ProjectOptions...)}. Project ID is always returned, even if + * {@code Dns#getProjectInfo(ProjectGetOption...)}. Project ID is always returned, even if * not specified. */ enum ProjectField { @@ -49,7 +49,7 @@ enum ProjectField { this.selector = selector; } - public String selector() { + String selector() { return selector; } @@ -67,9 +67,8 @@ static String selector(ProjectField... fields) { * The fields of a zone. * *

These values can be used to specify the fields to include in a partial response when calling - * {@code DnsService#getZone(BigInteger, ZoneFieldOptions...)} or {@code - * DnsService#getZone(String, ZoneFieldOptions...)}. The ID is always returned, even if not - * specified. + * {@code Dns#getZone(BigInteger, ZoneFieldOption...)} or {@code Dns#getZone(String, + * ZoneFieldOption...)}. The ID is always returned, even if not specified. */ enum ZoneField { CREATION_TIME("creationTime"), @@ -86,7 +85,7 @@ enum ZoneField { this.selector = selector; } - public String selector() { + String selector() { return selector; } @@ -104,8 +103,8 @@ static String selector(ZoneField... fields) { * The fields of a DNS record. * *

These values can be used to specify the fields to include in a partial response when calling - * {@code DnsService#listDnsRecords(BigInteger, DnsRecordOptions...)} or {@code - * DnsService#listDnsRecords(String, DnsRecordOptions...)}. The name is always returned even if + * {@code Dns#listDnsRecords(BigInteger, DnsRecordListOption...)} or {@code + * Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if * not selected. */ enum DnsRecordField { @@ -120,7 +119,7 @@ enum DnsRecordField { this.selector = selector; } - public String selector() { + String selector() { return selector; } @@ -138,8 +137,8 @@ static String selector(DnsRecordField... fields) { * The fields of a change request. * *

These values can be used to specify the fields to include in a partial response when calling - * {@code DnsService#applyChangeRequest(ChangeRequest, BigInteger, ChangeRequestFieldOptions...)} - * or {@code DnsService#applyChangeRequest(ChangeRequest, String, ChangeRequestFieldOptions...)} + * {@code Dns#applyChangeRequest(ChangeRequest, BigInteger, ChangeRequestOption...)} + * or {@code Dns#applyChangeRequest(ChangeRequest, String, ChangeRequestOption...)} * The ID is always returned even if not selected. */ enum ChangeRequestField { @@ -155,7 +154,7 @@ enum ChangeRequestField { this.selector = selector; } - public String selector() { + String selector() { return selector; } @@ -171,10 +170,10 @@ static String selector(ChangeRequestField... fields) { /** * The sorting keys for listing change requests. The only currently supported sorting key is the - * change sequence. + * when the change request was created. */ enum ChangeRequestSortingKey { - CHANGE_SEQUENCE("changeSequence"); + TIME_CREATED("changeSequence"); private final String selector; @@ -182,15 +181,15 @@ enum ChangeRequestSortingKey { this.selector = selector; } - public String selector() { + String selector() { return selector; } } /** - * The sorting order for listing change requests. + * The sorting order for listing. */ - enum ChangeRequestSortingOrder { + enum SortingOrder { DESCENDING, ASCENDING; public String selector() { @@ -201,11 +200,11 @@ public String selector() { /** * Class that for specifying DNS record options. */ - class DnsRecordOptions extends AbstractOption implements Serializable { + class DnsRecordListOption extends AbstractOption implements Serializable { - private static final long serialVersionUID = 201601261646L; + private static final long serialVersionUID = 1009627025381096098L; - DnsRecordOptions(DnsServiceRpc.Option option, Object value) { + DnsRecordListOption(DnsRpc.Option option, Object value) { super(option, value); } @@ -217,10 +216,10 @@ class DnsRecordOptions extends AbstractOption implements Serializable { * DNS record always returned, even if not specified. {@link DnsRecordField} provides a list of * fields that can be used. */ - public static DnsRecordOptions fields(DnsRecordField... fields) { + public static DnsRecordListOption fields(DnsRecordField... fields) { StringBuilder builder = new StringBuilder(); - builder.append("rrsets(").append(DnsRecordField.selector(fields)).append(")"); - return new DnsRecordOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); + builder.append("rrsets(").append(DnsRecordField.selector(fields)).append(')'); + return new DnsRecordListOption(DnsRpc.Option.FIELDS, builder.toString()); } /** @@ -229,8 +228,8 @@ public static DnsRecordOptions fields(DnsRecordField... fields) { *

The page token (returned from a previous call to list) indicates from where listing should * continue. */ - public static DnsRecordOptions pageToken(String pageToken) { - return new DnsRecordOptions(DnsServiceRpc.Option.PAGE_TOKEN, pageToken); + public static DnsRecordListOption pageToken(String pageToken) { + return new DnsRecordListOption(DnsRpc.Option.PAGE_TOKEN, pageToken); } /** @@ -239,26 +238,34 @@ public static DnsRecordOptions pageToken(String pageToken) { *

The server can return fewer records than requested. When there are more results than the * page size, the server will return a page token that can be used to fetch other results. */ - public static DnsRecordOptions pageSize(int pageSize) { - return new DnsRecordOptions(DnsServiceRpc.Option.PAGE_SIZE, pageSize); + public static DnsRecordListOption pageSize(int pageSize) { + return new DnsRecordListOption(DnsRpc.Option.PAGE_SIZE, pageSize); } /** - * Restricts the list to return only zones with this fully qualified domain name. + * Restricts the list to only DNS records with this fully qualified domain name. */ - public static DnsRecordOptions dnsName(String dnsName) { - return new DnsRecordOptions(DnsServiceRpc.Option.DNS_NAME, dnsName); + public static DnsRecordListOption dnsName(String dnsName) { + return new DnsRecordListOption(DnsRpc.Option.DNS_NAME, dnsName); + } + + /** + * Restricts the list to return only records of this type. If present, {@link + * Dns.DnsRecordListOption#dnsName(String)} must also be present. + */ + public static DnsRecordListOption type(DnsRecord.Type type) { + return new DnsRecordListOption(DnsRpc.Option.DNS_TYPE, type); } } /** * Class for specifying zone field options. */ - class ZoneFieldOptions extends AbstractOption implements Serializable { + class ZoneFieldOption extends AbstractOption implements Serializable { - private static final long serialVersionUID = -7294186261285469986L; + private static final long serialVersionUID = -8065564464895945037L; - ZoneFieldOptions(DnsServiceRpc.Option option, Object value) { + ZoneFieldOption(DnsRpc.Option option, Object value) { super(option, value); } @@ -266,23 +273,23 @@ class ZoneFieldOptions extends AbstractOption implements Serializable { * Returns an option to specify the zones's fields to be returned by the RPC call. * *

If this option is not provided all zone fields are returned. {@code - * ZoneFieldOptions.fields} can be used to specify only the fields of interest. Zone ID is - * always returned, even if not specified. {@link ZoneField} provides a list of fields that can - * be used. + * ZoneFieldOption.fields} can be used to specify only the fields of interest. Zone ID is always + * returned, even if not specified. {@link ZoneField} provides a list of fields that can be + * used. */ - public static ZoneFieldOptions fields(ZoneField... fields) { - return new ZoneFieldOptions(DnsServiceRpc.Option.FIELDS, ZoneField.selector(fields)); + public static ZoneFieldOption fields(ZoneField... fields) { + return new ZoneFieldOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); } } /** * Class for specifying zone listing options. */ - class ZoneListOptions extends AbstractOption implements Serializable { + class ZoneListOption extends AbstractOption implements Serializable { - private static final long serialVersionUID = -7922038132321229290L; + private static final long serialVersionUID = -2830645032124504717L; - ZoneListOptions(DnsServiceRpc.Option option, Object value) { + ZoneListOption(DnsRpc.Option option, Object value) { super(option, value); } @@ -290,12 +297,12 @@ class ZoneListOptions extends AbstractOption implements Serializable { * Returns an option to specify the zones's fields to be returned by the RPC call. * *

If this option is not provided all zone fields are returned. {@code - * ZoneFieldOptions.fields} can be used to specify only the fields of interest. Zone ID is - * always returned, even if not specified. {@link ZoneField} provides a list of fields that can - * be used. + * ZoneFieldOption.fields} can be used to specify only the fields of interest. Zone ID is always + * returned, even if not specified. {@link ZoneField} provides a list of fields that can be + * used. */ - public static ZoneListOptions fields(ZoneField... fields) { - return new ZoneListOptions(DnsServiceRpc.Option.FIELDS, ZoneField.selector(fields)); + public static ZoneListOption fields(ZoneField... fields) { + return new ZoneListOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); } /** @@ -304,8 +311,8 @@ public static ZoneListOptions fields(ZoneField... fields) { *

The page token (returned from a previous call to list) indicates from where listing should * continue. */ - public static ZoneListOptions pageToken(String pageToken) { - return new ZoneListOptions(DnsServiceRpc.Option.PAGE_TOKEN, pageToken); + public static ZoneListOption pageToken(String pageToken) { + return new ZoneListOption(DnsRpc.Option.PAGE_TOKEN, pageToken); } /** @@ -314,26 +321,19 @@ public static ZoneListOptions pageToken(String pageToken) { *

The server can return fewer zones than requested. When there are more results than the * page size, the server will return a page token that can be used to fetch other results. */ - public static ZoneListOptions pageSize(int pageSize) { - return new ZoneListOptions(DnsServiceRpc.Option.PAGE_SIZE, pageSize); - } - - /** - * Restricts the list to return only zones with this fully qualified domain name. - */ - public static ZoneListOptions dnsName(String dnsName) { - return new ZoneListOptions(DnsServiceRpc.Option.DNS_NAME, dnsName); + public static ZoneListOption pageSize(int pageSize) { + return new ZoneListOption(DnsRpc.Option.PAGE_SIZE, pageSize); } } /** * Class for specifying project options. */ - class ProjectOptions extends AbstractOption implements Serializable { + class ProjectGetOption extends AbstractOption implements Serializable { private static final long serialVersionUID = 6817937338218847748L; - ProjectOptions(DnsServiceRpc.Option option, Object value) { + ProjectGetOption(DnsRpc.Option option, Object value) { super(option, value); } @@ -341,69 +341,38 @@ class ProjectOptions extends AbstractOption implements Serializable { * Returns an option to specify the project's fields to be returned by the RPC call. * *

If this option is not provided all project fields are returned. {@code - * ProjectOptions.fields} can be used to specify only the fields of interest. Project ID is + * ProjectGetOption.fields} can be used to specify only the fields of interest. Project ID is * always returned, even if not specified. {@link ProjectField} provides a list of fields that * can be used. */ - public static ProjectOptions fields(ProjectField... fields) { - return new ProjectOptions(DnsServiceRpc.Option.FIELDS, ProjectField.selector(fields)); + public static ProjectGetOption fields(ProjectField... fields) { + return new ProjectGetOption(DnsRpc.Option.FIELDS, ProjectField.selector(fields)); } } /** * Class for specifying change request field options. */ - class ChangeRequestFieldOptions extends AbstractOption implements Serializable { + class ChangeRequestOption extends AbstractOption implements Serializable { private static final long serialVersionUID = 1067273695061077782L; - ChangeRequestFieldOptions(DnsServiceRpc.Option option, Object value) { + ChangeRequestOption(DnsRpc.Option option, Object value) { super(option, value); } - /** - * Returns an option to specify which fields of DNS records to be added by the {@link - * ChangeRequest} should be returned by the service. - * - *

If this option is not provided, all record fields are returned. {@code - * ChangeRequestFieldOptions.additionsFields} can be used to specify only the fields of - * interest. The name of the DNS record always returned, even if not specified. {@link - * DnsRecordField} provides a list of fields that can be used. - */ - public static ChangeRequestFieldOptions additionsFields(DnsRecordField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("additions(").append(DnsRecordField.selector(fields)).append(")"); - return new ChangeRequestFieldOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); - } - - /** - * Returns an option to specify which fields of DNS records to be deleted by the {@link - * ChangeRequest} should be returned by the service. - * - *

If this option is not provided, all record fields are returned. {@code - * ChangeRequestFieldOptions.deletionsFields} can be used to specify only the fields of - * interest. The name of the DNS record always returned, even if not specified. {@link - * DnsRecordField} provides a list of fields that can be used. - */ - public static ChangeRequestFieldOptions deletionsFields(DnsRecordField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("deletions(").append(DnsRecordField.selector(fields)).append(")"); - return new ChangeRequestFieldOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); - - } - /** * Returns an option to specify which fields of {@link ChangeRequest} should be returned by the * service. * *

If this option is not provided all change request fields are returned. {@code - * ChangeRequestFieldOptions.fields} can be used to specify only the fields of interest. The ID + * ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID * of the change request is always returned, even if not specified. {@link ChangeRequestField} * provides a list of fields that can be used. */ - public static ChangeRequestFieldOptions fields(ChangeRequestField... fields) { - return new ChangeRequestFieldOptions( - DnsServiceRpc.Option.FIELDS, + public static ChangeRequestOption fields(ChangeRequestField... fields) { + return new ChangeRequestOption( + DnsRpc.Option.FIELDS, ChangeRequestField.selector(fields) ); } @@ -412,56 +381,26 @@ public static ChangeRequestFieldOptions fields(ChangeRequestField... fields) { /** * Class for specifying change request listing options. */ - class ChangeRequestListOptions extends AbstractOption implements Serializable { + class ChangeRequestListOption extends AbstractOption implements Serializable { private static final long serialVersionUID = -900209143895376089L; - ChangeRequestListOptions(DnsServiceRpc.Option option, Object value) { + ChangeRequestListOption(DnsRpc.Option option, Object value) { super(option, value); } - /** - * Returns an option to specify which fields of DNS records to be added by the {@link - * ChangeRequest} should be returned by the service. - * - *

If this option is not provided, all record fields are returned. {@code - * ChangeRequestFieldOptions.additionsFields} can be used to specify only the fields of - * interest. The name of the DNS record always returned, even if not specified. {@link - * DnsRecordField} provides a list of fields that can be used. - */ - public static ChangeRequestListOptions additionsFields(DnsRecordField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("changes(additions(").append(DnsRecordField.selector(fields)).append("))"); - return new ChangeRequestListOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); - } - - /** - * Returns an option to specify which fields of DNS records to be deleted by the {@link - * ChangeRequest} should be returned by the service. - * - *

If this option is not provided, all record fields are returned. {@code - * ChangeRequestFieldOptions.deletionsFields} can be used to specify only the fields of - * interest. The name of the DNS record always returned, even if not specified. {@link - * DnsRecordField} provides a list of fields that can be used. - */ - public static ChangeRequestListOptions deletionsFields(DnsRecordField... fields) { - StringBuilder builder = new StringBuilder(); - builder.append("changes(deletions(").append(DnsRecordField.selector(fields)).append("))"); - return new ChangeRequestListOptions(DnsServiceRpc.Option.FIELDS, builder.toString()); - } - /** * Returns an option to specify which fields of{@link ChangeRequest} should be returned by the * service. * *

If this option is not provided all change request fields are returned. {@code - * ChangeRequestFieldOptions.fields} can be used to specify only the fields of interest. The ID + * ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID * of the change request is always returned, even if not specified. {@link ChangeRequestField} * provides a list of fields that can be used. */ - public static ChangeRequestListOptions fields(ChangeRequestField... fields) { - return new ChangeRequestListOptions( - DnsServiceRpc.Option.FIELDS, + public static ChangeRequestListOption fields(ChangeRequestField... fields) { + return new ChangeRequestListOption( + DnsRpc.Option.FIELDS, ChangeRequestField.selector(fields) ); } @@ -472,8 +411,8 @@ public static ChangeRequestListOptions fields(ChangeRequestField... fields) { *

The page token (returned from a previous call to list) indicates from where listing should * continue. */ - public static ChangeRequestListOptions pageToken(String pageToken) { - return new ChangeRequestListOptions(DnsServiceRpc.Option.PAGE_TOKEN, pageToken); + public static ChangeRequestListOption pageToken(String pageToken) { + return new ChangeRequestListOption(DnsRpc.Option.PAGE_TOKEN, pageToken); } /** @@ -483,24 +422,24 @@ public static ChangeRequestListOptions pageToken(String pageToken) { * than the page size, the server will return a page token that can be used to fetch other * results. */ - public static ChangeRequestListOptions pageSize(int pageSize) { - return new ChangeRequestListOptions(DnsServiceRpc.Option.PAGE_SIZE, pageSize); + public static ChangeRequestListOption pageSize(int pageSize) { + return new ChangeRequestListOption(DnsRpc.Option.PAGE_SIZE, pageSize); } /** * Returns an option for specifying the sorting criterion of change requests. Note the the only * currently supported criterion is the change sequence. */ - public static ChangeRequestListOptions sortBy(ChangeRequestSortingKey key) { - return new ChangeRequestListOptions(DnsServiceRpc.Option.SORTING_KEY, key.selector()); + public static ChangeRequestListOption sortBy(ChangeRequestSortingKey key) { + return new ChangeRequestListOption(DnsRpc.Option.SORTING_KEY, key.selector()); } /** * Returns an option to specify whether the the change requests should be listed in ascending or * descending order. */ - public static ChangeRequestListOptions sortOrder(ChangeRequestSortingOrder order) { - return new ChangeRequestListOptions(DnsServiceRpc.Option.SORTING_ORDER, order.selector()); + public static ChangeRequestListOption sortOrder(SortingOrder order) { + return new ChangeRequestListOption(DnsRpc.Option.SORTING_ORDER, order.selector()); } } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceFactory.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsFactory.java similarity index 84% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceFactory.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsFactory.java index aaa0dfb68e1b..734652afb24d 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceFactory.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsFactory.java @@ -19,7 +19,7 @@ import com.google.gcloud.ServiceFactory; /** - * An interface for DnsService factories. + * An interface for Dns factories. */ -public interface DnsServiceFactory extends ServiceFactory { +public interface DnsFactory extends ServiceFactory { } diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceOptions.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java similarity index 59% rename from gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceOptions.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java index 84eb9c33dcaa..3663211d3b41 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsServiceOptions.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java @@ -16,62 +16,64 @@ package com.google.gcloud.dns; +import com.google.common.collect.Sets; import com.google.gcloud.ServiceOptions; -import com.google.gcloud.spi.DnsServiceRpc; -import com.google.gcloud.spi.DnsServiceRpcFactory; +import com.google.gcloud.spi.DnsRpc; +import com.google.gcloud.spi.DnsRpcFactory; import java.util.Set; -public class DnsServiceOptions - extends ServiceOptions { +public class DnsOptions + extends ServiceOptions { private static final long serialVersionUID = -5311219368450107146L; // TODO(mderka) Finish implementation. Created issue #595. - public static class DefaultDnsServiceFactory implements DnsServiceFactory { - private static final DnsServiceFactory INSTANCE = new DefaultDnsServiceFactory(); + public static class DefaultDnsFactory implements DnsFactory { + private static final DnsFactory INSTANCE = new DefaultDnsFactory(); @Override - public DnsService create(DnsServiceOptions options) { + public Dns create(DnsOptions options) { // TODO(mderka) Implement. Created issue #595. return null; } } - public static class Builder extends ServiceOptions.Builder { + public static class Builder extends ServiceOptions.Builder { private Builder() { } - private Builder(DnsServiceOptions options) { + private Builder(DnsOptions options) { super(options); } @Override - public DnsServiceOptions build() { - return new DnsServiceOptions(this); + public DnsOptions build() { + return new DnsOptions(this); } } - private DnsServiceOptions(Builder builder) { - super(DnsServiceFactory.class, DnsServiceRpcFactory.class, builder); + private DnsOptions(Builder builder) { + super(DnsFactory.class, DnsRpcFactory.class, builder); } @Override - protected DnsServiceFactory defaultServiceFactory() { - return DefaultDnsServiceFactory.INSTANCE; + protected DnsFactory defaultServiceFactory() { + return DefaultDnsFactory.INSTANCE; } @Override - protected DnsServiceRpcFactory defaultRpcFactory() { + protected DnsRpcFactory defaultRpcFactory() { return null; } @Override protected Set scopes() { - return null; + // TODO(mderka) Verify. + return Sets.newHashSet("ndev.clouddns.readwrite"); } @Override diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java similarity index 96% rename from gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpc.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java index e97ee9a1b001..02afb7309c6a 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java @@ -18,13 +18,14 @@ import java.util.Map; -public interface DnsServiceRpc { +public interface DnsRpc { enum Option { FIELDS("fields"), PAGE_SIZE("maxSize"), PAGE_TOKEN("pageToken"), DNS_NAME("dnsName"), + DNS_TYPE("type"), SORTING_KEY("sortBy"), SORTING_ORDER("sortOrder"); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpcFactory.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpcFactory.java similarity index 74% rename from gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpcFactory.java rename to gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpcFactory.java index 13ec9fe881c8..3d25f09bb1e5 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsServiceRpcFactory.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpcFactory.java @@ -16,11 +16,11 @@ package com.google.gcloud.spi; -import com.google.gcloud.dns.DnsServiceOptions; +import com.google.gcloud.dns.DnsOptions; /** - * An interface for DnsServiceRpc factory. Implementation will be loaded via {@link + * An interface for DnsRpc factory. Implementation will be loaded via {@link * java.util.ServiceLoader}. */ -public interface DnsServiceRpcFactory extends ServiceRpcFactory { +public interface DnsRpcFactory extends ServiceRpcFactory { } From 762483b94f6c95aa3f860803ead53a962f41d336 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 29 Jan 2016 16:11:13 -0800 Subject: [PATCH 08/12] Makes ProjectInfo.Quota serializable. Fixed #599. --- .../src/main/java/com/google/gcloud/dns/ProjectInfo.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java index 4db0497946b1..f7b12b94bae8 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/ProjectInfo.java @@ -44,8 +44,9 @@ public class ProjectInfo implements Serializable { * @see Google Cloud DNS * documentation */ - public static class Quota { + public static class Quota implements Serializable { + private static final long serialVersionUID = 6854685970605363639L; private final int zones; private final int resourceRecordsPerRrset; private final int rrsetAdditionsPerChange; From e17bedb4640e6d5c434670eb03a4ea6b2f9028c9 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 29 Jan 2016 17:50:18 -0800 Subject: [PATCH 09/12] Implemented DnsOptions up to two unavailable classes. --- .../com/google/gcloud/dns/DnsOptions.java | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java index 3663211d3b41..a5c689f4c719 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java @@ -16,7 +16,7 @@ package com.google.gcloud.dns; -import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableSet; import com.google.gcloud.ServiceOptions; import com.google.gcloud.spi.DnsRpc; import com.google.gcloud.spi.DnsRpcFactory; @@ -26,16 +26,28 @@ public class DnsOptions extends ServiceOptions { - private static final long serialVersionUID = -5311219368450107146L; - - // TODO(mderka) Finish implementation. Created issue #595. + private static final long serialVersionUID = -519128051411747771L; + private static final String GC_DNS_RW = "https://www.googleapis.com/auth/ndev.clouddns.readwrite"; + private static final String GC_DNS_R = "https://www.googleapis.com/auth/ndev.clouddns.readonly"; + private static final Set SCOPES = ImmutableSet.of(GC_DNS_RW, GC_DNS_R); public static class DefaultDnsFactory implements DnsFactory { private static final DnsFactory INSTANCE = new DefaultDnsFactory(); @Override public Dns create(DnsOptions options) { - // TODO(mderka) Implement. Created issue #595. + // TODO(mderka) Implement when DnsImpl is available. Created issue #595. + return null; + } + } + + public static class DefaultDnsRpcFactory implements DnsRpcFactory { + + private static final DnsRpcFactory INSTANCE = new DefaultDnsRpcFactory(); + + @Override + public DnsRpc create(DnsOptions options) { + // TODO(mderka) Implement when DefaultDnsRpc is available. Created issue #595. return null; } } @@ -60,11 +72,13 @@ private DnsOptions(Builder builder) { super(DnsFactory.class, DnsRpcFactory.class, builder); } + @SuppressWarnings("unchecked") @Override protected DnsFactory defaultServiceFactory() { return DefaultDnsFactory.INSTANCE; } + @SuppressWarnings("unchecked") @Override protected DnsRpcFactory defaultRpcFactory() { return null; @@ -72,10 +86,10 @@ protected DnsRpcFactory defaultRpcFactory() { @Override protected Set scopes() { - // TODO(mderka) Verify. - return Sets.newHashSet("ndev.clouddns.readwrite"); + return SCOPES; } + @SuppressWarnings("unchecked") @Override public Builder toBuilder() { return new Builder(this); From adf5c1cda7b849d7e1064dba7dae24e1fea5061d Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 1 Feb 2016 09:12:51 -0800 Subject: [PATCH 10/12] Added test for AbstractOption. --- .../main/java/com/google/gcloud/dns/Dns.java | 2 +- .../google/gcloud/dns/AbstractOptionTest.java | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 737ec1a38699..76b11972bb7e 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -193,7 +193,7 @@ enum SortingOrder { DESCENDING, ASCENDING; public String selector() { - return this.name().toLowerCase(); + return name().toLowerCase(); } } diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java new file mode 100644 index 000000000000..3c578c53d0d1 --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java @@ -0,0 +1,70 @@ +/* + * 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 org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.fail; + +import com.google.gcloud.spi.DnsRpc; + +import org.junit.Test; + +public class AbstractOptionTest { + + private static final DnsRpc.Option RPC_OPTION = DnsRpc.Option.DNS_TYPE; + private static final DnsRpc.Option ANOTHER_RPC_OPTION = DnsRpc.Option.DNS_NAME; + private static final String VALUE = "some value"; + private static final String OTHER_VALUE = "another value"; + private static final AbstractOption OPTION = new AbstractOption(RPC_OPTION, VALUE) { + }; + private static final AbstractOption OPTION_EQUALS = new AbstractOption(RPC_OPTION, VALUE) { + }; + private static final AbstractOption OPTION_NOT_EQUALS1 = + new AbstractOption(RPC_OPTION, OTHER_VALUE) { + }; + private static final AbstractOption OPTION_NOT_EQUALS2 = + new AbstractOption(ANOTHER_RPC_OPTION, VALUE) { + }; + + @Test + public void testEquals() { + assertEquals(OPTION, OPTION_EQUALS); + assertNotEquals(OPTION, OPTION_NOT_EQUALS1); + assertNotEquals(OPTION, OPTION_NOT_EQUALS2); + } + + @Test + public void testHashCode() { + assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode()); + } + + @Test + public void testConstructor() { + assertEquals(RPC_OPTION, OPTION.rpcOption()); + assertEquals(VALUE, OPTION.value()); + try { + new AbstractOption(null, VALUE) { + }; + fail("Cannot build with empty option."); + } catch (NullPointerException e) { + // expected + } + new AbstractOption(RPC_OPTION, null) { + }; // null value is ok + } +} From a9cc927e002277face656af9f059175192d31c3d Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 1 Feb 2016 14:20:33 -0800 Subject: [PATCH 11/12] Implemented comments by @aozarov. Added test for option accessors. --- .../main/java/com/google/gcloud/dns/Dns.java | 86 ++++------- .../com/google/gcloud/dns/DnsOptions.java | 3 +- .../java/com/google/gcloud/spi/DnsRpc.java | 1 - .../java/com/google/gcloud/dns/DnsTest.java | 141 ++++++++++++++++++ 4 files changed, 173 insertions(+), 58 deletions(-) create mode 100644 gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 76b11972bb7e..352c7791e18d 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -31,12 +31,14 @@ */ public interface Dns extends Service { + + /** * The fields of a project. * *

These values can be used to specify the fields to include in a partial response when calling - * {@code Dns#getProjectInfo(ProjectGetOption...)}. Project ID is always returned, even if - * not specified. + * {@code Dns#getProjectInfo(ProjectGetOption...)}. Project ID is always returned, even if not + * specified. */ enum ProjectField { PROJECT_ID("id"), @@ -67,8 +69,8 @@ static String selector(ProjectField... fields) { * The fields of a zone. * *

These values can be used to specify the fields to include in a partial response when calling - * {@code Dns#getZone(BigInteger, ZoneFieldOption...)} or {@code Dns#getZone(String, - * ZoneFieldOption...)}. The ID is always returned, even if not specified. + * {@code Dns#getZone(BigInteger, ZoneOption...)} or {@code Dns#getZone(String, ZoneOption...)}. + * The ID is always returned, even if not specified. */ enum ZoneField { CREATION_TIME("creationTime"), @@ -104,8 +106,8 @@ static String selector(ZoneField... fields) { * *

These values can be used to specify the fields to include in a partial response when calling * {@code Dns#listDnsRecords(BigInteger, DnsRecordListOption...)} or {@code - * Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if - * not selected. + * Dns#listDnsRecords(String, DnsRecordListOption...)}. The name is always returned even if not + * selected. */ enum DnsRecordField { DNS_RECORDS("rrdatas"), @@ -137,9 +139,9 @@ static String selector(DnsRecordField... fields) { * The fields of a change request. * *

These values can be used to specify the fields to include in a partial response when calling - * {@code Dns#applyChangeRequest(ChangeRequest, BigInteger, ChangeRequestOption...)} - * or {@code Dns#applyChangeRequest(ChangeRequest, String, ChangeRequestOption...)} - * The ID is always returned even if not selected. + * {@code Dns#applyChangeRequest(ChangeRequest, BigInteger, ChangeRequestOption...)} or {@code + * Dns#applyChangeRequest(ChangeRequest, String, ChangeRequestOption...)} The ID is always + * returned even if not selected. */ enum ChangeRequestField { ID("id"), @@ -168,24 +170,6 @@ static String selector(ChangeRequestField... fields) { } } - /** - * The sorting keys for listing change requests. The only currently supported sorting key is the - * when the change request was created. - */ - enum ChangeRequestSortingKey { - TIME_CREATED("changeSequence"); - - private final String selector; - - ChangeRequestSortingKey(String selector) { - this.selector = selector; - } - - String selector() { - return selector; - } - } - /** * The sorting order for listing. */ @@ -261,24 +245,23 @@ public static DnsRecordListOption type(DnsRecord.Type type) { /** * Class for specifying zone field options. */ - class ZoneFieldOption extends AbstractOption implements Serializable { + class ZoneOption extends AbstractOption implements Serializable { private static final long serialVersionUID = -8065564464895945037L; - ZoneFieldOption(DnsRpc.Option option, Object value) { + ZoneOption(DnsRpc.Option option, Object value) { super(option, value); } /** * Returns an option to specify the zones's fields to be returned by the RPC call. * - *

If this option is not provided all zone fields are returned. {@code - * ZoneFieldOption.fields} can be used to specify only the fields of interest. Zone ID is always - * returned, even if not specified. {@link ZoneField} provides a list of fields that can be - * used. + *

If this option is not provided all zone fields are returned. {@code ZoneOption.fields} can + * be used to specify only the fields of interest. Zone ID is always returned, even if not + * specified. {@link ZoneField} provides a list of fields that can be used. */ - public static ZoneFieldOption fields(ZoneField... fields) { - return new ZoneFieldOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); + public static ZoneOption fields(ZoneField... fields) { + return new ZoneOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); } } @@ -296,10 +279,9 @@ class ZoneListOption extends AbstractOption implements Serializable { /** * Returns an option to specify the zones's fields to be returned by the RPC call. * - *

If this option is not provided all zone fields are returned. {@code - * ZoneFieldOption.fields} can be used to specify only the fields of interest. Zone ID is always - * returned, even if not specified. {@link ZoneField} provides a list of fields that can be - * used. + *

If this option is not provided all zone fields are returned. {@code ZoneOption.fields} can + * be used to specify only the fields of interest. Zone ID is always returned, even if not + * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneListOption fields(ZoneField... fields) { return new ZoneListOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); @@ -366,9 +348,9 @@ class ChangeRequestOption extends AbstractOption implements Serializable { * service. * *

If this option is not provided all change request fields are returned. {@code - * ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID - * of the change request is always returned, even if not specified. {@link ChangeRequestField} - * provides a list of fields that can be used. + * ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID of the + * change request is always returned, even if not specified. {@link ChangeRequestField} provides + * a list of fields that can be used. */ public static ChangeRequestOption fields(ChangeRequestField... fields) { return new ChangeRequestOption( @@ -394,9 +376,9 @@ class ChangeRequestListOption extends AbstractOption implements Serializable { * service. * *

If this option is not provided all change request fields are returned. {@code - * ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID - * of the change request is always returned, even if not specified. {@link ChangeRequestField} - * provides a list of fields that can be used. + * ChangeRequestOption.fields} can be used to specify only the fields of interest. The ID of the + * change request is always returned, even if not specified. {@link ChangeRequestField} provides + * a list of fields that can be used. */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { return new ChangeRequestListOption( @@ -427,16 +409,10 @@ public static ChangeRequestListOption pageSize(int pageSize) { } /** - * Returns an option for specifying the sorting criterion of change requests. Note the the only - * currently supported criterion is the change sequence. - */ - public static ChangeRequestListOption sortBy(ChangeRequestSortingKey key) { - return new ChangeRequestListOption(DnsRpc.Option.SORTING_KEY, key.selector()); - } - - /** - * Returns an option to specify whether the the change requests should be listed in ascending or - * descending order. + * Returns an option to specify whether the the change requests should be listed in ascending + * (most-recent last) or descending (most-recent first) order with respect to when the change + * request was accepted by the server. If this option is not provided, the listing order is + * undefined. */ public static ChangeRequestListOption sortOrder(SortingOrder order) { return new ChangeRequestListOption(DnsRpc.Option.SORTING_ORDER, order.selector()); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java index a5c689f4c719..1845467c2537 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/DnsOptions.java @@ -28,8 +28,7 @@ public class DnsOptions private static final long serialVersionUID = -519128051411747771L; private static final String GC_DNS_RW = "https://www.googleapis.com/auth/ndev.clouddns.readwrite"; - private static final String GC_DNS_R = "https://www.googleapis.com/auth/ndev.clouddns.readonly"; - private static final Set SCOPES = ImmutableSet.of(GC_DNS_RW, GC_DNS_R); + private static final Set SCOPES = ImmutableSet.of(GC_DNS_RW); public static class DefaultDnsFactory implements DnsFactory { private static final DnsFactory INSTANCE = new DefaultDnsFactory(); diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java index 02afb7309c6a..f6a0f330a327 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/spi/DnsRpc.java @@ -26,7 +26,6 @@ enum Option { PAGE_TOKEN("pageToken"), DNS_NAME("dnsName"), DNS_TYPE("type"), - SORTING_KEY("sortBy"), SORTING_ORDER("sortOrder"); private final String value; diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java new file mode 100644 index 000000000000..2e98dbd46de4 --- /dev/null +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/DnsTest.java @@ -0,0 +1,141 @@ +/* + * 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 org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.gcloud.spi.DnsRpc; + +import org.junit.Test; + +public class DnsTest { + + private static final Integer PAGE_SIZE = 20; + private static final String PAGE_TOKEN = "page token"; + + @Test + public void testDnsRecordListOption() { + // dns name + String dnsName = "some name"; + Dns.DnsRecordListOption dnsRecordListOption = Dns.DnsRecordListOption.dnsName(dnsName); + assertEquals(dnsName, dnsRecordListOption.value()); + assertEquals(DnsRpc.Option.DNS_NAME, dnsRecordListOption.rpcOption()); + // page token + dnsRecordListOption = Dns.DnsRecordListOption.pageToken(PAGE_TOKEN); + assertEquals(PAGE_TOKEN, dnsRecordListOption.value()); + assertEquals(DnsRpc.Option.PAGE_TOKEN, dnsRecordListOption.rpcOption()); + // page size + dnsRecordListOption = Dns.DnsRecordListOption.pageSize(PAGE_SIZE); + assertEquals(PAGE_SIZE, dnsRecordListOption.value()); + assertEquals(DnsRpc.Option.PAGE_SIZE, dnsRecordListOption.rpcOption()); + // record type + DnsRecord.Type recordType = DnsRecord.Type.AAAA; + dnsRecordListOption = Dns.DnsRecordListOption.type(recordType); + assertEquals(recordType, dnsRecordListOption.value()); + assertEquals(DnsRpc.Option.DNS_TYPE, dnsRecordListOption.rpcOption()); + // fields + dnsRecordListOption = Dns.DnsRecordListOption.fields(Dns.DnsRecordField.NAME, + Dns.DnsRecordField.TTL); + assertEquals(DnsRpc.Option.FIELDS, dnsRecordListOption.rpcOption()); + assertTrue(dnsRecordListOption.value() instanceof String); + assertTrue(((String) dnsRecordListOption.value()).contains( + Dns.DnsRecordField.NAME.selector())); + assertTrue(((String) dnsRecordListOption.value()).contains( + Dns.DnsRecordField.TTL.selector())); + assertTrue(((String) dnsRecordListOption.value()).contains( + Dns.DnsRecordField.NAME.selector())); + } + + @Test + public void testZoneOption() { + Dns.ZoneOption fields = Dns.ZoneOption.fields(Dns.ZoneField.CREATION_TIME, + Dns.ZoneField.DESCRIPTION); + assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption()); + assertTrue(fields.value() instanceof String); + assertTrue(((String) fields.value()).contains(Dns.ZoneField.CREATION_TIME.selector())); + assertTrue(((String) fields.value()).contains(Dns.ZoneField.DESCRIPTION.selector())); + } + + @Test + public void testZoneList() { + // fields + Dns.ZoneListOption fields = Dns.ZoneListOption.fields(Dns.ZoneField.CREATION_TIME, + Dns.ZoneField.DESCRIPTION); + assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption()); + assertTrue(fields.value() instanceof String); + assertTrue(((String) fields.value()).contains(Dns.ZoneField.CREATION_TIME.selector())); + assertTrue(((String) fields.value()).contains(Dns.ZoneField.DESCRIPTION.selector())); + assertTrue(((String) fields.value()).contains(Dns.ZoneField.ZONE_ID.selector())); + // page token + Dns.ZoneListOption option = Dns.ZoneListOption.pageToken(PAGE_TOKEN); + assertEquals(PAGE_TOKEN, option.value()); + assertEquals(DnsRpc.Option.PAGE_TOKEN, option.rpcOption()); + // page size + option = Dns.ZoneListOption.pageSize(PAGE_SIZE); + assertEquals(PAGE_SIZE, option.value()); + assertEquals(DnsRpc.Option.PAGE_SIZE, option.rpcOption()); + } + + @Test + public void testProjectGetOption() { + // fields + Dns.ProjectGetOption fields = Dns.ProjectGetOption.fields(Dns.ProjectField.QUOTA); + assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption()); + assertTrue(fields.value() instanceof String); + assertTrue(((String) fields.value()).contains(Dns.ProjectField.QUOTA.selector())); + assertTrue(((String) fields.value()).contains(Dns.ProjectField.PROJECT_ID.selector())); + } + + @Test + public void testChangeRequestOption() { + // fields + Dns.ChangeRequestOption fields = Dns.ChangeRequestOption.fields( + Dns.ChangeRequestField.START_TIME, Dns.ChangeRequestField.STATUS); + assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption()); + assertTrue(fields.value() instanceof String); + assertTrue(((String) fields.value()).contains( + Dns.ChangeRequestField.START_TIME.selector())); + assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.ID.selector())); + } + + @Test + public void testChangeRequestListOption() { + // fields + Dns.ChangeRequestListOption fields = Dns.ChangeRequestListOption.fields( + Dns.ChangeRequestField.START_TIME, Dns.ChangeRequestField.STATUS); + assertEquals(DnsRpc.Option.FIELDS, fields.rpcOption()); + assertTrue(fields.value() instanceof String); + assertTrue(((String) fields.value()).contains( + Dns.ChangeRequestField.START_TIME.selector())); + assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.STATUS.selector())); + assertTrue(((String) fields.value()).contains(Dns.ChangeRequestField.ID.selector())); + // page token + Dns.ChangeRequestListOption option = Dns.ChangeRequestListOption.pageToken(PAGE_TOKEN); + assertEquals(PAGE_TOKEN, option.value()); + assertEquals(DnsRpc.Option.PAGE_TOKEN, option.rpcOption()); + // page size + option = Dns.ChangeRequestListOption.pageSize(PAGE_SIZE); + assertEquals(PAGE_SIZE, option.value()); + assertEquals(DnsRpc.Option.PAGE_SIZE, option.rpcOption()); + // sort order + option = Dns.ChangeRequestListOption.sortOrder(Dns.SortingOrder.ASCENDING); + assertEquals(DnsRpc.Option.SORTING_ORDER, option.rpcOption()); + assertEquals(Dns.SortingOrder.ASCENDING.selector(), option.value()); + } +} From 6ecde35c355ca8e38e726044e9a0012f5a3935ab Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Mon, 1 Feb 2016 16:07:24 -0800 Subject: [PATCH 12/12] Comments by @aozarov second round. --- .../main/java/com/google/gcloud/dns/Dns.java | 15 ++++++----- .../google/gcloud/dns/AbstractOptionTest.java | 26 +++++++------------ 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java index 352c7791e18d..28e79104cf58 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/Dns.java @@ -24,6 +24,8 @@ import java.io.Serializable; import java.util.Set; +import static com.google.gcloud.dns.Dns.ZoneField.selector; + /** * An interface for the Google Cloud DNS service. * @@ -31,8 +33,6 @@ */ public interface Dns extends Service { - - /** * The fields of a project. * @@ -284,7 +284,9 @@ class ZoneListOption extends AbstractOption implements Serializable { * specified. {@link ZoneField} provides a list of fields that can be used. */ public static ZoneListOption fields(ZoneField... fields) { - return new ZoneListOption(DnsRpc.Option.FIELDS, ZoneField.selector(fields)); + StringBuilder builder = new StringBuilder(); + builder.append("managedZones(").append(selector(fields)).append(')'); + return new ZoneListOption(DnsRpc.Option.FIELDS, builder.toString()); } /** @@ -381,10 +383,9 @@ class ChangeRequestListOption extends AbstractOption implements Serializable { * a list of fields that can be used. */ public static ChangeRequestListOption fields(ChangeRequestField... fields) { - return new ChangeRequestListOption( - DnsRpc.Option.FIELDS, - ChangeRequestField.selector(fields) - ); + StringBuilder builder = new StringBuilder(); + builder.append("changes(").append(ChangeRequestField.selector(fields)).append(')'); + return new ChangeRequestListOption(DnsRpc.Option.FIELDS, builder.toString()); } /** diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java index 3c578c53d0d1..e3f2896bd10b 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/AbstractOptionTest.java @@ -16,30 +16,26 @@ package com.google.gcloud.dns; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.fail; - import com.google.gcloud.spi.DnsRpc; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.fail; + public class AbstractOptionTest { private static final DnsRpc.Option RPC_OPTION = DnsRpc.Option.DNS_TYPE; private static final DnsRpc.Option ANOTHER_RPC_OPTION = DnsRpc.Option.DNS_NAME; private static final String VALUE = "some value"; private static final String OTHER_VALUE = "another value"; - private static final AbstractOption OPTION = new AbstractOption(RPC_OPTION, VALUE) { - }; - private static final AbstractOption OPTION_EQUALS = new AbstractOption(RPC_OPTION, VALUE) { - }; + private static final AbstractOption OPTION = new AbstractOption(RPC_OPTION, VALUE) {}; + private static final AbstractOption OPTION_EQUALS = new AbstractOption(RPC_OPTION, VALUE) {}; private static final AbstractOption OPTION_NOT_EQUALS1 = - new AbstractOption(RPC_OPTION, OTHER_VALUE) { - }; + new AbstractOption(RPC_OPTION, OTHER_VALUE) {}; private static final AbstractOption OPTION_NOT_EQUALS2 = - new AbstractOption(ANOTHER_RPC_OPTION, VALUE) { - }; + new AbstractOption(ANOTHER_RPC_OPTION, VALUE) {}; @Test public void testEquals() { @@ -58,13 +54,11 @@ public void testConstructor() { assertEquals(RPC_OPTION, OPTION.rpcOption()); assertEquals(VALUE, OPTION.value()); try { - new AbstractOption(null, VALUE) { - }; + new AbstractOption(null, VALUE) {}; fail("Cannot build with empty option."); } catch (NullPointerException e) { // expected } - new AbstractOption(RPC_OPTION, null) { - }; // null value is ok + new AbstractOption(RPC_OPTION, null) {}; // null value is ok } }