From ba5467f0da8bd3743dfd5f1222d3fb56cab46e89 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 22 Feb 2016 15:42:45 +0100 Subject: [PATCH 01/10] Add gcloud-java-compute module --- gcloud-java-compute/README.md | 100 +++++++++++++++ gcloud-java-compute/pom.xml | 49 ++++++++ .../com/google/gcloud/compute/Compute.java | 27 +++++ .../gcloud/compute/ComputeException.java | 59 +++++++++ .../google/gcloud/compute/ComputeFactory.java | 25 ++++ .../google/gcloud/compute/ComputeImpl.java | 30 +++++ .../google/gcloud/compute/ComputeOptions.java | 114 ++++++++++++++++++ .../com/google/gcloud/spi/ComputeRpc.java | 20 +++ .../google/gcloud/spi/ComputeRpcFactory.java | 26 ++++ .../google/gcloud/spi/DefaultComputeRpc.java | 46 +++++++ gcloud-java/pom.xml | 5 + pom.xml | 1 + 12 files changed, 502 insertions(+) create mode 100644 gcloud-java-compute/README.md create mode 100644 gcloud-java-compute/pom.xml create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeException.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeFactory.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeOptions.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpcFactory.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java diff --git a/gcloud-java-compute/README.md b/gcloud-java-compute/README.md new file mode 100644 index 000000000000..aef53c781c20 --- /dev/null +++ b/gcloud-java-compute/README.md @@ -0,0 +1,100 @@ +Google Cloud Java Client for Compute (Alpha) +==================================== + +Java idiomatic client for [Google Cloud Compute] (https://cloud.google.com/compute). + +[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java) +[![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master) + +[![Codacy Badge](https://api.codacy.com/project/badge/grade/9da006ad7c3a4fe1abd142e77c003917)](https://www.codacy.com/app/mziccard/gcloud-java) +[![Dependency Status](https://www.versioneye.com/user/projects/56bd8ee72a29ed002d2b0969/badge.svg?style=flat)](https://www.versioneye.com/user/projects/56bd8ee72a29ed002d2b0969) + +- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/) + + +> Note: This client is a work-in-progress, and may occasionally +> make backwards-incompatible changes. + +Quickstart +---------- +If you are using Maven, add this to your pom.xml file + +If you are using Gradle, add this to your dependencies + +If you are using SBT, add this to your dependencies + + +Example Application +------------------- + + +Authentication +-------------- + +See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README. + +About Google Cloud BigQuery +-------------------------- + +[Google Cloud Compute][cloud-compute] delivers virtual machines running in Google's innovative data +centers and worldwide fiber network. Compute Engine's tooling and workflow support enable scaling +from single instances to global, load-balanced cloud computing. Compute Engine's VMs boot quickly, +come with persistent disk storage, deliver consistent performance and are available in many +configurations. + +Be sure to activate the Google Cloud Compute API on the Developer's Console to use Compute from +your project. + +See the ``gcloud-java`` API [compute documentation][compute-api] to learn how to interact +with Google Cloud Compute using this Client Library. + +Getting Started +--------------- + + +Troubleshooting +--------------- + +To get help, follow the `gcloud-java` links in the `gcloud-*`[shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting). + +Java Versions +------------- + +Java 7 or above is required for using this client. + +Testing +------- + +This library has tools to help make tests for code using Cloud BigQuery. + +See [TESTING] to read more about testing. + +Versioning +---------- + +This library follows [Semantic Versioning] (http://semver.org/). + +It is currently in major version zero (``0.y.z``), which means that anything +may change at any time and the public API should not be considered +stable. + +Contributing +------------ + +Contributions to this library are always welcome and highly encouraged. + +See [CONTRIBUTING] for more information on how to get started. + +License +------- + +Apache 2.0 - See [LICENSE] for more information. + + +[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md +[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE +[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-compute +[cloud-platform]: https://cloud.google.com/ + +[cloud-compute]: https://cloud.google.com/compute/ +[compute-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/compute/package-summary.html diff --git a/gcloud-java-compute/pom.xml b/gcloud-java-compute/pom.xml new file mode 100644 index 000000000000..70e4f127c119 --- /dev/null +++ b/gcloud-java-compute/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + gcloud-java-compute + jar + GCloud Java compute + + Java idiomatic client for Google Cloud Compute Engine. + + + com.google.gcloud + gcloud-java-pom + 0.1.5-SNAPSHOT + + + gcloud-java-compute + + + + ${project.groupId} + gcloud-java-core + ${project.version} + + + com.google.apis + google-api-services-compute + v1-rev93-1.21.0 + compile + + + com.google.guava + guava-jdk5 + + + + + junit + junit + 4.12 + test + + + org.easymock + easymock + 3.4 + test + + + diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java new file mode 100644 index 000000000000..4576f62f84b9 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java @@ -0,0 +1,27 @@ +/* + * 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.compute; + +import com.google.gcloud.Service; + +/** + * An interface for Google Cloud Compute Engine. + * + * @see Google Cloud Compute Engine + */ +public interface Compute extends Service { +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeException.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeException.java new file mode 100644 index 000000000000..94a409305338 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeException.java @@ -0,0 +1,59 @@ +/* + * 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.compute; + +import com.google.common.collect.ImmutableSet; +import com.google.gcloud.BaseServiceException; +import com.google.gcloud.RetryHelper.RetryHelperException; +import com.google.gcloud.RetryHelper.RetryInterruptedException; + +import java.io.IOException; +import java.util.Set; + +/** + * Compute Engine service exception. + */ +public class ComputeException extends BaseServiceException { + + private static final Set RETRYABLE_ERRORS = ImmutableSet.of(new Error(500, null)); + private static final long serialVersionUID = -8039359778707845810L; + + public ComputeException(int code, String message) { + super(code, message, null, true); + } + + public ComputeException(IOException exception) { + super(exception, true); + } + + @Override + protected Set retryableErrors() { + return RETRYABLE_ERRORS; + } + + /** + * Translate RetryHelperException to the ComputeException that caused the error. This method will + * always throw an exception. + * + * @throws ComputeException when {@code ex} was caused by a {@code ComputeException} + * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} + */ + static BaseServiceException translateAndThrow(RetryHelperException ex) { + BaseServiceException.translateAndPropagateIfPossible(ex); + throw new ComputeException(UNKNOWN_CODE, ex.getMessage()); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeFactory.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeFactory.java new file mode 100644 index 000000000000..100631eef91d --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeFactory.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.compute; + +import com.google.gcloud.ServiceFactory; + +/** + * An interface for Compute factories. + */ +public interface ComputeFactory extends ServiceFactory { +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java new file mode 100644 index 000000000000..8a3a82af8827 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java @@ -0,0 +1,30 @@ +/* + * 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.compute; + +import com.google.gcloud.BaseService; +import com.google.gcloud.spi.ComputeRpc; + +final class ComputeImpl extends BaseService implements Compute { + + private final ComputeRpc computeRpc; + + ComputeImpl(ComputeOptions options) { + super(options); + computeRpc = options.rpc(); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeOptions.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeOptions.java new file mode 100644 index 000000000000..9fab138b26b4 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeOptions.java @@ -0,0 +1,114 @@ +/* + * 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.compute; + +import com.google.common.collect.ImmutableSet; +import com.google.gcloud.ServiceOptions; +import com.google.gcloud.spi.ComputeRpc; +import com.google.gcloud.spi.ComputeRpcFactory; +import com.google.gcloud.spi.DefaultComputeRpc; + +import java.util.Set; + +public class ComputeOptions extends ServiceOptions { + + private static final String COMPUTE_SCOPE = "https://www.googleapis.com/auth/compute"; + private static final Set SCOPES = ImmutableSet.of(COMPUTE_SCOPE); + private static final long serialVersionUID = 6509557711917342058L; + + public static class DefaultComputeFactory implements ComputeFactory { + + private static final ComputeFactory INSTANCE = new DefaultComputeFactory(); + + @Override + public Compute create(ComputeOptions options) { + return new ComputeImpl(options); + } + } + + public static class DefaultComputeRpcFactory implements ComputeRpcFactory { + + private static final ComputeRpcFactory INSTANCE = new DefaultComputeRpcFactory(); + + @Override + public ComputeRpc create(ComputeOptions options) { + return new DefaultComputeRpc(options); + } + } + + public static class Builder extends + ServiceOptions.Builder { + + private Builder() { + } + + private Builder(ComputeOptions options) { + super(options); + } + + @Override + public ComputeOptions build() { + return new ComputeOptions(this); + } + } + + private ComputeOptions(Builder builder) { + super(ComputeFactory.class, ComputeRpcFactory.class, builder); + } + + @Override + protected ComputeFactory defaultServiceFactory() { + return DefaultComputeFactory.INSTANCE; + } + + @Override + protected ComputeRpcFactory defaultRpcFactory() { + return DefaultComputeRpcFactory.INSTANCE; + } + + @Override + protected Set scopes() { + return SCOPES; + } + + @Override + public Builder toBuilder() { + return new Builder(this); + } + + @Override + public int hashCode() { + return baseHashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ComputeOptions)) { + return false; + } + ComputeOptions other = (ComputeOptions) obj; + return baseEquals(other); + } + + public static ComputeOptions defaultInstance() { + return builder().build(); + } + + public static Builder builder() { + return new Builder(); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java new file mode 100644 index 000000000000..b7a59a9413c4 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpc.java @@ -0,0 +1,20 @@ +/* + * 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; + +public interface ComputeRpc { +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpcFactory.java b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpcFactory.java new file mode 100644 index 000000000000..5defc86199ef --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/ComputeRpcFactory.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.compute.ComputeOptions; + +/** + * An interface for Compute RPC factory. + * Implementation will be loaded via {@link java.util.ServiceLoader}. + */ +public interface ComputeRpcFactory extends ServiceRpcFactory { +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java new file mode 100644 index 000000000000..6667588dd0ef --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/spi/DefaultComputeRpc.java @@ -0,0 +1,46 @@ +/* + * 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.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.jackson.JacksonFactory; +import com.google.api.services.compute.Compute; +import com.google.gcloud.compute.ComputeException; +import com.google.gcloud.compute.ComputeOptions; + +import java.io.IOException; + +public class DefaultComputeRpc implements ComputeRpc { + + private final ComputeOptions options; + private final Compute compute; + + public DefaultComputeRpc(ComputeOptions options) { + HttpTransport transport = options.httpTransportFactory().create(); + HttpRequestInitializer initializer = options.httpRequestInitializer(); + this.options = options; + compute = new Compute.Builder(transport, new JacksonFactory(), initializer) + .setRootUrl(options.host()) + .setApplicationName(options.applicationName()) + .build(); + } + + private static ComputeException translate(IOException exception) { + return new ComputeException(exception); + } +} diff --git a/gcloud-java/pom.xml b/gcloud-java/pom.xml index adfa716fe27b..e76d349483bd 100644 --- a/gcloud-java/pom.xml +++ b/gcloud-java/pom.xml @@ -18,6 +18,11 @@ gcloud-java-bigquery ${project.version} + + ${project.groupId} + gcloud-java-compute + ${project.version} + ${project.groupId} gcloud-java-core diff --git a/pom.xml b/pom.xml index 4bc8f37c35de..5f37fd2b7d6a 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,7 @@ gcloud-java gcloud-java-bigquery + gcloud-java-compute gcloud-java-contrib gcloud-java-core gcloud-java-datastore From d3deaf90ef22161f1803592501988c1388642a3a Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 22 Feb 2016 16:04:51 +0100 Subject: [PATCH 02/10] Add Compute Engine immutable resources --- .../com/google/gcloud/compute/DiskType.java | 227 +++++++++++ .../com/google/gcloud/compute/DiskTypeId.java | 110 +++++ .../com/google/gcloud/compute/License.java | 98 +++++ .../com/google/gcloud/compute/LicenseId.java | 115 ++++++ .../google/gcloud/compute/MachineType.java | 298 ++++++++++++++ .../google/gcloud/compute/MachineTypeId.java | 97 +++++ .../com/google/gcloud/compute/Region.java | 319 +++++++++++++++ .../com/google/gcloud/compute/RegionId.java | 103 +++++ .../gcloud/compute/RegionResourceId.java | 79 ++++ .../com/google/gcloud/compute/ResourceId.java | 74 ++++ .../java/com/google/gcloud/compute/Zone.java | 380 ++++++++++++++++++ .../com/google/gcloud/compute/ZoneId.java | 112 ++++++ .../google/gcloud/compute/ZoneResourceId.java | 79 ++++ .../google/gcloud/compute/DiskTypeIdTest.java | 65 +++ .../google/gcloud/compute/DiskTypeTest.java | 73 ++++ .../google/gcloud/compute/LicenseIdTest.java | 61 +++ .../google/gcloud/compute/LicenseTest.java | 52 +++ .../gcloud/compute/MachineTypeIdTest.java | 65 +++ .../gcloud/compute/MachineTypeTest.java | 90 +++++ .../google/gcloud/compute/RegionIdTest.java | 61 +++ .../com/google/gcloud/compute/RegionTest.java | 85 ++++ .../gcloud/compute/SerializationTest.java | 157 ++++++++ .../com/google/gcloud/compute/ZoneIdTest.java | 61 +++ .../com/google/gcloud/compute/ZoneTest.java | 86 ++++ 24 files changed, 2947 insertions(+) create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java new file mode 100644 index 000000000000..2cc6cb9bb865 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java @@ -0,0 +1,227 @@ +/* + * 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.compute; + +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.math.BigInteger; +import java.util.Objects; + +/** + * Google Compute Engine Disk type. A disk type represents the type of disk to use, such as a + * {@code pd-ssd} or {@code pd-standard}. + */ +public final class DiskType implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public DiskType apply(com.google.api.services.compute.model.DiskType pb) { + return DiskType.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.DiskType apply(DiskType diskType) { + return diskType.toPb(); + } + }; + + private static final long serialVersionUID = -944042261695072026L; + + private final Long id; + private final DiskTypeId diskTypeId; + private final String creationTimestamp; + private final String description; + private final String validDiskSize; + private final String selfLink; + private final Long defaultDiskSizeGb; + + static final class Builder { + + private Long id; + private DiskTypeId diskTypeId; + private String creationTimestamp; + private String description; + private String validDiskSize; + private String selfLink; + private Long defaultDiskSizeGb; + + private Builder() {} + + Builder id(Long id) { + this.id = id; + return this; + } + + Builder creationTimestamp(String creationTimestamp) { + this.creationTimestamp = creationTimestamp; + return this; + } + + Builder diskTypeId(DiskTypeId diskTypeId) { + this.diskTypeId = diskTypeId; + return this; + } + + Builder description(String description) { + this.description = description; + return this; + } + + Builder validDiskSize(String validDiskSize) { + this.validDiskSize = validDiskSize; + return this; + } + + Builder selfLink(String selfLink) { + this.selfLink = selfLink; + return this; + } + + Builder defaultDiskSizeGb(Long defaultDiskSizeGb) { + this.defaultDiskSizeGb = defaultDiskSizeGb; + return this; + } + + /** + * Creates a {@code DiskType} object. + */ + DiskType build() { + return new DiskType(this); + } + } + + private DiskType(Builder builder) { + this.id = builder.id; + this.creationTimestamp = builder.creationTimestamp; + this.diskTypeId = builder.diskTypeId; + this.description = builder.description; + this.validDiskSize = builder.validDiskSize; + this.selfLink = builder.selfLink; + this.defaultDiskSizeGb = builder.defaultDiskSizeGb; + } + + /** + * Returns the creation timestamp in RFC3339 text format. + * + * @see RFC3339 + */ + public String creationTimestamp() { + return creationTimestamp; + } + + /** + * Returns the disk type's identity. + */ + public DiskTypeId diskTypeId() { + return diskTypeId; + } + + /** + * Returns an unique identifier for the disk type; defined by the service. + */ + public Long id() { + return id; + } + + /** + * Returns a textual description of the disk type. + */ + public String description() { + return description; + } + + /** + * Returns an optional textual description of the valid disk size, such as "10GB-10TB". + */ + public String validDiskSize() { + return validDiskSize; + } + + /** + * Returns a service-defined URL for the disk type. + */ + public String selfLink() { + return selfLink; + } + + /** + * Returns the service-defined default disk size in GB. + */ + public Long defaultDiskSizeGb() { + return defaultDiskSizeGb; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("id", id) + .add("creationTimestamp", creationTimestamp) + .add("description", description) + .add("validDiskSize", validDiskSize) + .add("selfLink", selfLink) + .add("defaultDiskSizeGb", defaultDiskSizeGb) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof DiskType && Objects.equals(toPb(), ((DiskType) obj).toPb()); + } + + com.google.api.services.compute.model.DiskType toPb() { + com.google.api.services.compute.model.DiskType diskTypePb = + new com.google.api.services.compute.model.DiskType(); + if (id != null) { + diskTypePb.setId(BigInteger.valueOf(id)); + } + diskTypePb.setCreationTimestamp(creationTimestamp); + diskTypePb.setDescription(description); + diskTypePb.setValidDiskSize(validDiskSize); + diskTypePb.setSelfLink(selfLink); + diskTypePb.setDefaultDiskSizeGb(defaultDiskSizeGb); + diskTypePb.setZone(diskTypeId.zoneId().toUrl()); + return diskTypePb; + } + + static Builder builder() { + return new Builder(); + } + + static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb) { + Builder builder = builder(); + if (diskTypePb.getId() != null ) { + builder.id(diskTypePb.getId().longValue()); + } + builder.creationTimestamp(diskTypePb.getCreationTimestamp()); + builder.diskTypeId(DiskTypeId.fromUrl(diskTypePb.getSelfLink())); + builder.description(diskTypePb.getDescription()); + builder.validDiskSize(diskTypePb.getValidDiskSize()); + builder.selfLink(diskTypePb.getSelfLink()); + builder.defaultDiskSizeGb(diskTypePb.getDefaultDiskSizeGb()); + return builder.build(); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java new file mode 100644 index 000000000000..527f7da124b4 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java @@ -0,0 +1,110 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.MoreObjects; + +import java.util.Objects; + +/** + * Identity for a Google Compute Engine disk type. + */ +public final class DiskTypeId extends ZoneResourceId { + + private static final long serialVersionUID = 7337881474103686219L; + + private final String diskType; + + DiskTypeId(String project, String zone, String diskType) { + super(project, zone); + this.diskType = checkNotNull(diskType); + } + + /** + * Returns the name of the disk type resource. The name must be 1-63 characters long, and comply + * with RFC1035. Specifically, the name must be 1-63 characters long and match the regular + * expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a + * lowercase letter, and all following characters must be a dash, lowercase letter, or digit, + * except the last character, which cannot be a dash. + * + * @see RFC1035 + */ + public String diskType() { + return diskType; + } + + @Override + public String toUrl() { + return super.toUrl() + "/diskTypes/" + diskType; + } + + @Override + MoreObjects.ToStringHelper toStringHelper() { + return super.toStringHelper().add("diskType", diskType); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), diskType); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof DiskTypeId && baseEquals((DiskTypeId) obj); + } + + @Override + DiskTypeId setProjectId(String projectId) { + if (project() != null) { + return this; + } + return DiskTypeId.of(projectId, zone(), diskType); + } + + /** + * Returns a disk type identity given the zone identity and the disk type name. + */ + public static DiskTypeId of(ZoneId zoneId, String diskType) { + return new DiskTypeId(zoneId.project(), zoneId.zone(), diskType); + } + + /** + * Returns a disk type identity given the zone disk and disk type. + */ + public static DiskTypeId of(String zone, String diskType) { + return of(ZoneId.of(null, zone), diskType); + } + + /** + * Returns a disk type identity given project disk, zone disk and disk type. + */ + public static DiskTypeId of(String project, String zone, String diskType) { + return of(ZoneId.of(project, zone), diskType); + } + + static DiskTypeId fromUrl(String url) { + int projectsIndex = url.indexOf("/projects/"); + int zonesIndex = url.indexOf("/zones/"); + int diskTypesIndex = url.indexOf("/diskTypes/"); + String project = url.substring(projectsIndex + 10, zonesIndex); + String zone = url.substring(zonesIndex + 7, diskTypesIndex); + String diskType = url.substring(diskTypesIndex + 11, url.length()); + return DiskTypeId.of(project, zone, diskType); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java new file mode 100644 index 000000000000..17e4f0d22be0 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java @@ -0,0 +1,98 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.util.Objects; + +/** + * A Google Compute Engine License. A License represents a software license. Licenses are used to + * track software usage in images, persistent disks, snapshots, and virtual machine instances. + */ +public final class License implements Serializable { + + private static final long serialVersionUID = 6907923910319640363L; + + private final LicenseId licenseId; + private final Boolean chargesUseFee; + private final String selfLink; + + License(LicenseId licenseId, Boolean chargesUseFee, String selfLink) { + this.licenseId = checkNotNull(licenseId); + this.chargesUseFee = chargesUseFee; + this.selfLink = selfLink; + } + + /** + * Returns the identity of the license. + */ + public LicenseId licenseId() { + return licenseId; + } + + /** + * Returns {@code true} if the customer will be charged a license fee for running software that + * contains this license on an instance. + */ + public Boolean chargesUseFee() { + return chargesUseFee; + } + + /** + * Returns a service-defined URL for the license. + */ + public String selfLink() { + return selfLink; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("licenseId", licenseId) + .add("chargesUseFee", chargesUseFee) + .add("selfLink", selfLink) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(licenseId, chargesUseFee, selfLink); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof License && Objects.equals(toPb(), ((License) obj).toPb()); + } + + com.google.api.services.compute.model.License toPb() { + com.google.api.services.compute.model.License licensePb = + new com.google.api.services.compute.model.License(); + licensePb.setName(licenseId.license()); + licensePb.setChargesUseFee(chargesUseFee); + licensePb.setSelfLink(selfLink); + return licensePb; + } + + static License fromPb(com.google.api.services.compute.model.License licensePb) { + return new License(LicenseId.fromUrl(licensePb.getSelfLink()), licensePb.getChargesUseFee(), + licensePb.getSelfLink()); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java new file mode 100644 index 000000000000..f32695e03c03 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java @@ -0,0 +1,115 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; + +import java.util.Objects; + +/** + * Identity for a Google Compute Engine license. + */ +public final class LicenseId extends ResourceId { + + static final Function FROM_URL_FUNCTION = new Function() { + @Override + public LicenseId apply(String pb) { + return LicenseId.fromUrl(pb); + } + }; + static final Function TO_URL_FUNCTION = new Function() { + @Override + public String apply(LicenseId licenseId) { + return licenseId.toUrl(); + } + }; + + private static final long serialVersionUID = -2239484554024469651L; + + private final String license; + + LicenseId(String project, String license) { + super(project); + this.license = checkNotNull(license); + } + + /** + * Returns the name of the license. The name must be 1-63 characters long, and comply with + * RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression + * {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, + * and all following characters must be a dash, lowercase letter, or digit, except the last + * character, which cannot be a dash. + * + * @see RFC1035 + */ + public String license() { + return license; + } + + @Override + public String toUrl() { + return super.toUrl() + "/global/licenses/" + license; + } + + @Override + MoreObjects.ToStringHelper toStringHelper() { + return super.toStringHelper().add("license", license); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode(), license); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof LicenseId && baseEquals((LicenseId) obj); + } + + @Override + LicenseId setProjectId(String projectId) { + if (project() != null) { + return this; + } + return LicenseId.of(projectId, license); + } + + /** + * Returns a license identity given the license name. + */ + public static LicenseId of(String license) { + return new LicenseId(null, license); + } + + /** + * Returns a license identity given project and license names. + */ + public static LicenseId of(String project, String license) { + return new LicenseId(project, license); + } + + static LicenseId fromUrl(String url) { + int projectsIndex = url.indexOf("/projects/"); + int licensesIndex = url.indexOf("/global/licenses/"); + String project = url.substring(projectsIndex + 10, licensesIndex); + String license = url.substring(licensesIndex + 17, url.length()); + return LicenseId.of(project, license); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java new file mode 100644 index 000000000000..bf1d6ce49f52 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java @@ -0,0 +1,298 @@ +/* + * 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.compute; + +import com.google.api.services.compute.model.MachineType.ScratchDisks; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import com.google.common.collect.Lists; + +import java.io.Serializable; +import java.math.BigInteger; +import java.util.List; +import java.util.Objects; + +public class MachineType implements Serializable { + + static final Function + FROM_PB_FUNCTION = + new Function() { + @Override + public MachineType apply(com.google.api.services.compute.model.MachineType pb) { + return MachineType.fromPb(pb); + } + }; + static final Function + TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.MachineType apply(MachineType type) { + return type.toPb(); + } + }; + + private static final long serialVersionUID = -4210962597502860450L; + + private final MachineTypeId machineTypeId; + private final Long id; + private final String creationTimestamp; + private final String description; + private final String selfLink; + private final Integer guestCpus; + private final Integer memoryMb; + private final List scratchDisks; + private final Integer maximumPersistentDisks; + private final Long maximumPersistentDisksSizeGb; + + public static final class Builder { + + private MachineTypeId machineTypeId; + private Long id; + private String creationTimestamp; + private String description; + private String selfLink; + private Integer guestCpus; + private Integer memoryMb; + private List scratchDisks; + private Integer maximumPersistentDisks; + private Long maximumPersistentDisksSizeGb; + + Builder() {} + + + Builder machineTypeId(MachineTypeId machineTypeId) { + this.machineTypeId = machineTypeId; + return this; + } + + Builder id(Long id) { + this.id = id; + return this; + } + + Builder creationTimestamp(String creationTimestamp) { + this.creationTimestamp = creationTimestamp; + return this; + } + + Builder description(String description) { + this.description = description; + return this; + } + + Builder selfLink(String selfLink) { + this.selfLink = selfLink; + return this; + } + + Builder guestCpus(Integer guestCpus) { + this.guestCpus = guestCpus; + return this; + } + + Builder memoryMb(Integer memoryMb) { + this.memoryMb = memoryMb; + return this; + } + + Builder scratchDisks(List scratchDisks) { + this.scratchDisks = scratchDisks; + return this; + } + + Builder maximumPersistentDisks(Integer maximumPersistentDisks) { + this.maximumPersistentDisks = maximumPersistentDisks; + return this; + } + + Builder maximumPersistentDisksSizeGb(Long maximumPersistentDisksSizeGb) { + this.maximumPersistentDisksSizeGb = maximumPersistentDisksSizeGb; + return this; + } + + MachineType build() { + return new MachineType(this); + } + } + + MachineType(Builder builder) { + this.machineTypeId = builder.machineTypeId; + this.id = builder.id; + this.creationTimestamp = builder.creationTimestamp; + this.description = builder.description; + this.selfLink = builder.selfLink; + this.guestCpus = builder.guestCpus; + this.memoryMb = builder.memoryMb; + this.scratchDisks = builder.scratchDisks; + this.maximumPersistentDisks = builder.maximumPersistentDisks; + this.maximumPersistentDisksSizeGb = builder.maximumPersistentDisksSizeGb; + } + + /** + * Returns the machine type's identity. + */ + public MachineTypeId machineTypeId() { + return machineTypeId; + } + + /** + * Returns an unique identifier for the machin type; defined by the service. + */ + public Long id() { + return id; + } + + /** + * Returns the creation timestamp in RFC3339 text format. + * + * @see RFC3339 + */ + public String creationTimestamp() { + return creationTimestamp; + } + + /** + * Returns an optional textual description of the machine type. + */ + public String description() { + return description; + } + + /** + * Returns a service-defined URL for the machine type. + */ + public String selfLink() { + return selfLink; + } + + /** + * Returns the number of virtual CPUs that are available to the instance. + */ + public Integer guestCpus() { + return guestCpus; + } + + /** + * Returns the amount of physical memory available to the instance, defined in MB. + */ + public Integer memoryMb() { + return memoryMb; + } + + /** + * Returns the size of all extended scratch disks assigned to the instance, defined in GB. + */ + public List scratchDisks() { + return scratchDisks; + } + + /** + * Returns the maximum number of persistent disks allowed by this instance type. + */ + public Integer maximumPersistentDisks() { + return maximumPersistentDisks; + } + + /** + * Returns the maximum total persistent disks size allowed, defined in GB. + */ + public Long maximumPersistentDisksSizeGb() { + return maximumPersistentDisksSizeGb; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("machineTypeId", machineTypeId) + .add("id", id) + .add("creationTimestamp", creationTimestamp) + .add("description", description) + .add("selfLink", selfLink) + .add("guestCpus", guestCpus) + .add("memoryMb", memoryMb) + .add("scratchDisks", scratchDisks) + .add("maximumPersistentDisks", maximumPersistentDisks) + .add("maximumPersistentDisksSizeGb", maximumPersistentDisksSizeGb) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(machineTypeId); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof MachineType && Objects.equals(toPb(), ((MachineType) obj).toPb()); + } + + com.google.api.services.compute.model.MachineType toPb() { + com.google.api.services.compute.model.MachineType machineTypePb = + new com.google.api.services.compute.model.MachineType(); + if (id != null) { + machineTypePb.setId(BigInteger.valueOf(id)); + } + machineTypePb.setCreationTimestamp(creationTimestamp); + machineTypePb.setName(machineTypeId.machineType()); + machineTypePb.setDescription(description); + machineTypePb.setSelfLink(selfLink); + machineTypePb.setGuestCpus(guestCpus); + machineTypePb.setMemoryMb(memoryMb); + if (scratchDisks != null) { + machineTypePb.setScratchDisks(Lists.transform(scratchDisks, + new Function() { + @Override + public ScratchDisks apply(Integer diskSize) { + return new ScratchDisks().setDiskGb(diskSize); + } + })); + } + machineTypePb.setMaximumPersistentDisks(maximumPersistentDisks); + machineTypePb.setMaximumPersistentDisksSizeGb(maximumPersistentDisksSizeGb); + machineTypePb.setZone(machineTypeId.zoneId().zone()); + return machineTypePb; + } + + static Builder builder() { + return new Builder(); + } + + static MachineType fromPb(com.google.api.services.compute.model.MachineType machineTypePb) { + Builder builder = builder(); + builder.machineTypeId(MachineTypeId.fromUrl(machineTypePb.getSelfLink())); + if (machineTypePb.getId() != null) { + builder.id(machineTypePb.getId().longValue()); + } + builder.creationTimestamp(machineTypePb.getCreationTimestamp()); + builder.description(machineTypePb.getDescription()); + builder.selfLink(machineTypePb.getSelfLink()); + builder.guestCpus(machineTypePb.getGuestCpus()); + builder.memoryMb(machineTypePb.getMemoryMb()); + if (machineTypePb.getScratchDisks() != null) { + builder.scratchDisks( + Lists.transform(machineTypePb.getScratchDisks(), new Function() { + @Override + public Integer apply(ScratchDisks scratchDiskPb) { + return scratchDiskPb.getDiskGb(); + } + })); + } + builder.maximumPersistentDisks(machineTypePb.getMaximumPersistentDisks()); + builder.maximumPersistentDisksSizeGb(machineTypePb.getMaximumPersistentDisksSizeGb()); + return builder.build(); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java new file mode 100644 index 000000000000..4a215dd207d3 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java @@ -0,0 +1,97 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.MoreObjects; + +import java.util.Objects; + +/** + * Identity for a Google Compute Engine machine type. + */ +public final class MachineTypeId extends ZoneResourceId { + + private static final long serialVersionUID = -5819598544478859608L; + + private final String machineType; + + MachineTypeId(String project, String zone, String machineType) { + super(project, zone); + this.machineType = checkNotNull(machineType); + } + + /** + * Returns the name of the machine type. + */ + public String machineType() { + return machineType; + } + + @Override + public String toUrl() { + return super.toUrl() + "/machineTypes/" + machineType; + } + + @Override + MoreObjects.ToStringHelper toStringHelper() { + return super.toStringHelper().add("machineType", machineType); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), machineType); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof MachineTypeId && baseEquals((MachineTypeId) obj); + } + + @Override + MachineTypeId setProjectId(String projectId) { + if (project() != null) { + return this; + } + return MachineTypeId.of(projectId, zone(), machineType); + } + + /** + * Returns a machine type identity given the zone and type names. + */ + public static MachineTypeId of(String zone, String machineType) { + return new MachineTypeId(null, zone, machineType); + } + + /** + * Returns a machine type identity given project, zone and type names. + */ + public static MachineTypeId of(String project, String zone, String machineType) { + return new MachineTypeId(project, zone, machineType); + } + + static MachineTypeId fromUrl(String url) { + int projectsIndex = url.indexOf("/projects/"); + int zonesIndex = url.indexOf("/zones/"); + int machineTypesIndex = url.indexOf("/machineTypes/"); + String project = url.substring(projectsIndex + 10, zonesIndex); + String zone = url.substring(zonesIndex + 7, machineTypesIndex); + String machineType = url.substring(machineTypesIndex + 14, url.length()); + return MachineTypeId.of(project, zone, machineType); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java new file mode 100644 index 000000000000..ded705bc2a8d --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java @@ -0,0 +1,319 @@ +/* + * 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.compute; + +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import com.google.common.collect.Lists; + +import java.io.Serializable; +import java.math.BigInteger; +import java.util.List; +import java.util.Objects; + +/** + * A Google Compute Engine region. + */ +public final class Region implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public Region apply(com.google.api.services.compute.model.Region pb) { + return Region.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.Region apply(Region region) { + return region.toPb(); + } + }; + + private static final long serialVersionUID = -3578710133393645135L; + + /** + * Status of the region. + */ + public enum Status { + UP, + DOWN + } + + /** + * A quota assigned to this region. + */ + public static class Quota implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public Quota apply(com.google.api.services.compute.model.Quota pb) { + return Quota.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.Quota apply(Quota quota) { + return quota.toPb(); + } + }; + private static final long serialVersionUID = -4357118665133226338L; + + private final String metric; + private final double limit; + private final double usage; + + /** + * Returns a region quota object. + */ + Quota(String metric, double limit, double usage) { + this.metric = metric; + this.limit = limit; + this.usage = usage; + } + + public String metric() { + return metric; + } + + public double limit() { + return limit; + } + + public double usage() { + return usage; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("metric", metric) + .add("limit", limit) + .add("usage", usage) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(metric, limit, usage); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Quota)) { + return false; + } + Quota other = (Quota) obj; + return Objects.equals(metric, other.metric) + && Objects.equals(limit, other.limit) + && Objects.equals(usage, other.usage); + } + + com.google.api.services.compute.model.Quota toPb() { + return new com.google.api.services.compute.model.Quota() + .setMetric(metric) + .setLimit(limit) + .setUsage(usage); + } + + static Quota fromPb(com.google.api.services.compute.model.Quota quotaPb) { + return new Quota(quotaPb.getMetric(), quotaPb.getLimit(), quotaPb.getUsage()); + } + } + + private final RegionId regionId; + private final Long id; + private final String creationTimestamp; + private final String description; + private final String selfLink; + private final Status status; + private final List zones; + private final List quotas; + + public static final class Builder { + + private RegionId regionId; + private Long id; + private String creationTimestamp; + private String description; + private String selfLink; + private Status status; + private List zones; + private List quotas; + + Builder() {} + + Builder regionId(RegionId regionId) { + this.regionId = regionId; + return this; + } + + Builder id(Long id) { + this.id = id; + return this; + } + + Builder creationTimestamp(String creationTimestamp) { + this.creationTimestamp = creationTimestamp; + return this; + } + + Builder description(String description) { + this.description = description; + return this; + } + + Builder selfLink(String selfLink) { + this.selfLink = selfLink; + return this; + } + + Builder status(Status status) { + this.status = status; + return this; + } + + Builder zones(List zones) { + this.zones = zones; + return this; + } + + Builder quotas(List quotas) { + this.quotas = quotas; + return this; + } + + Region build() { + return new Region(this); + } + } + + Region(Builder builder) { + this.regionId = builder.regionId; + this.id = builder.id; + this.creationTimestamp = builder.creationTimestamp; + this.description = builder.description; + this.selfLink = builder.selfLink; + this.status = builder.status; + this.zones = builder.zones; + this.quotas = builder.quotas; + } + + public RegionId regionId() { + return regionId; + } + + public Long id() { + return id; + } + + public String creationTimestamp() { + return creationTimestamp; + } + + public String description() { + return description; + } + + public String selfLink() { + return selfLink; + } + + public Status status() { + return status; + } + + public List zones() { + return zones; + } + + public List quotas() { + return quotas; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("regionId", regionId) + .add("id", id) + .add("creationTimestamp", creationTimestamp) + .add("description", description) + .add("selfLink", selfLink) + .add("status", status) + .add("zones", zones) + .add("quotas", quotas) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(regionId); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof Region && Objects.equals(toPb(), ((Region) obj).toPb()); + } + + com.google.api.services.compute.model.Region toPb() { + com.google.api.services.compute.model.Region regionPb = + new com.google.api.services.compute.model.Region(); + if (id != null) { + regionPb.setId(BigInteger.valueOf(id)); + } + regionPb.setCreationTimestamp(creationTimestamp); + regionPb.setName(regionId.region()); + regionPb.setDescription(description); + regionPb.setSelfLink(selfLink); + regionPb.setStatus(status.name()); + if (zones != null) { + regionPb.setZones(Lists.transform(zones, ZoneId.TO_URL_FUNCTION)); + } + if (quotas != null) { + regionPb.setQuotas(Lists.transform(quotas, Quota.TO_PB_FUNCTION)); + } + return regionPb; + } + + static Builder builder() { + return new Builder(); + } + + static Region fromPb(com.google.api.services.compute.model.Region regionPb) { + Builder builder = builder(); + builder.regionId(RegionId.fromUrl(regionPb.getSelfLink())); + if (regionPb.getId() != null) { + builder.id(regionPb.getId().longValue()); + } + builder.creationTimestamp(regionPb.getCreationTimestamp()); + builder.description(regionPb.getDescription()); + builder.selfLink(regionPb.getSelfLink()); + if (regionPb.getStatus() != null) { + builder.status(Status.valueOf(regionPb.getStatus())); + } + if (regionPb.getZones() != null) { + builder.zones(Lists.transform(regionPb.getZones(), ZoneId.FROM_URL_FUNCTION)); + } + if (regionPb.getQuotas() != null) { + builder.quotas(Lists.transform(regionPb.getQuotas(), Quota.FROM_PB_FUNCTION)); + } + return builder.build(); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java new file mode 100644 index 000000000000..60b603778863 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java @@ -0,0 +1,103 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.MoreObjects.ToStringHelper; + +import java.util.Objects; + +/** + * A Google Compute Engine region identity. + */ +public final class RegionId extends ResourceId { + + private static final long serialVersionUID = 5569092266957249294L; + + private final String region; + + RegionId(String project, String region) { + super(project); + this.region = checkNotNull(region); + } + + RegionId(RegionId regionId) { + super(regionId.project()); + this.region = checkNotNull(regionId.region()); + } + + /** + * Returns the name of the region. + */ + public final String region() { + return region; + } + + @Override + public String toUrl() { + return super.toUrl() + "/regions/" + region; + } + + @Override + ToStringHelper toStringHelper() { + return super.toStringHelper().add("region", region); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode(), region); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof RegionId && baseEquals((RegionId) obj); + } + + @Override + RegionId setProjectId(String projectId) { + if (project() != null) { + return this; + } + return RegionId.of(projectId, region); + } + + /** + * Returns a new region identity given project and region names. + */ + public static RegionId of(String project, String region) { + return new RegionId(project, region); + } + + /** + * Returns a new region identity given region name. + */ + public static RegionId of(String region) { + return RegionId.of(null, region); + } + + /** + * Returns a new region identity given a region URL. + */ + static RegionId fromUrl(String url) { + int projectsIndex = url.indexOf("/projects/"); + int regionsIndex = url.indexOf("/regions/"); + String project = url.substring(projectsIndex + 10, regionsIndex); + String region = url.substring(regionsIndex + 9, url.length()); + return RegionId.of(project, region); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java new file mode 100644 index 000000000000..618ae0a9a4e1 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java @@ -0,0 +1,79 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.MoreObjects.ToStringHelper; + +import java.util.Objects; + +/** + * A base class for the identity of Google Compute Engine resources that live in a region. + */ +public abstract class RegionResourceId extends ResourceId { + + private static final long serialVersionUID = 5569092266957249294L; + + private final String region; + + RegionResourceId(String project, String region) { + super(project); + this.region = checkNotNull(region); + } + + RegionResourceId(RegionId regionId) { + super(regionId.project()); + this.region = checkNotNull(regionId.region()); + } + + /** + * Returns the name of the region this resource belongs to. + */ + public final String region() { + return region; + } + + /** + * Returns the identity of the region this resource belongs to. + */ + public final RegionId regionId() { + return RegionId.of(project(), region); + } + + @Override + public String toUrl() { + return super.toUrl() + "/regions/" + region; + } + + @Override + ToStringHelper toStringHelper() { + return super.toStringHelper().add("region", region); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode(), region); + } + + @Override + public boolean equals(Object obj) { + return obj != null + && obj.getClass().equals(RegionResourceId.class) + && baseEquals((RegionResourceId) obj); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java new file mode 100644 index 000000000000..dc69d6ae53b4 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java @@ -0,0 +1,74 @@ +/* + * 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.compute; + +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Base class for Google Compute Engine resource identities. + */ +public abstract class ResourceId implements Serializable { + + private static final long serialVersionUID = -8028734746870421573L; + + private String project; + + ResourceId(String project) { + this.project = project; + } + + /** + * Base URL for a resource URL. + */ + private static final String BASE_URL = "https://www.googleapis.com/compute/v1/projects/"; + + /** + * Returns a fully qualified URL to the entity. + */ + public String toUrl() { + return BASE_URL + project; + } + + /** + * Returns the name of the project. + */ + public final String project() { + return project; + } + + MoreObjects.ToStringHelper toStringHelper() { + return MoreObjects.toStringHelper(this).add("project", project); + } + + @Override + public String toString() { + return toStringHelper().toString(); + } + + final int baseHashCode() { + return Objects.hash(project); + } + + final boolean baseEquals(ResourceId resourceId) { + return Objects.equals(toUrl(), resourceId.toUrl()); + } + + abstract ResourceId setProjectId(String projectId); +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java new file mode 100644 index 000000000000..b98e863011bb --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java @@ -0,0 +1,380 @@ +/* + * 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.compute; + +import com.google.api.services.compute.model.Zone.MaintenanceWindows; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import com.google.common.collect.Lists; + +import java.io.Serializable; +import java.math.BigInteger; +import java.util.List; +import java.util.Objects; + +/** + * A Google Compute Engine zone. + */ +public final class Zone implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public Zone apply(com.google.api.services.compute.model.Zone pb) { + return Zone.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.Zone apply(Zone region) { + return region.toPb(); + } + }; + private static final long serialVersionUID = 6113636504417213010L; + + /** + * Status of the region. + */ + public enum Status { + UP, + DOWN + } + + /** + * A scheduled maintenance windows for this zone. When a zone is in a maintenance window, all + * resources which reside in the zone will be unavailable. + * + * @see Maintenance + * Windows + */ + public static class MaintenanceWindow implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public MaintenanceWindow apply(MaintenanceWindows pb) { + return MaintenanceWindow.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public MaintenanceWindows apply(MaintenanceWindow maintenanceWindow) { + return maintenanceWindow.toPb(); + } + }; + + private static final long serialVersionUID = 2270641266683329963L; + + private final String name; + private final String description; + private final String beginTime; + private final String endTime; + + /** + * Returns a zone maintenance window object. + */ + MaintenanceWindow(String name, String description, String beginTime, String endTime) { + this.name = name; + this.description = description; + this.beginTime = beginTime; + this.endTime = endTime; + } + + /** + * Returns the disk of the maintanance window. + */ + public String name() { + return name; + } + + /** + * Returns a textual description of the maintenance window. + */ + public String description() { + return description; + } + + /** + * Returns the starting time of the maintenance window in RFC3339 text format. + * + * @see RFC3339 + */ + public String beginTime() { + return beginTime; + } + + /** + * Returns the ending time of the maintenance window in RFC3339 text format. + * + * @see RFC3339 + */ + public String endTime() { + return endTime; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("disk", name) + .add("description", description) + .add("beginTime", beginTime) + .add("endTime", endTime) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(name, description, beginTime, endTime); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof MaintenanceWindow)) { + return false; + } + MaintenanceWindow other = (MaintenanceWindow) obj; + return Objects.equals(name, other.name) + && Objects.equals(description, other.description) + && Objects.equals(beginTime, other.beginTime) + && Objects.equals(endTime, other.endTime); + } + + MaintenanceWindows toPb() { + return new MaintenanceWindows() + .setName(name) + .setDescription(description) + .setBeginTime(beginTime) + .setEndTime(endTime); + } + + static MaintenanceWindow fromPb(MaintenanceWindows windowPb) { + return new MaintenanceWindow(windowPb.getName(), windowPb.getDescription(), + windowPb.getBeginTime(), windowPb.getEndTime()); + } + } + + private final ZoneId zoneId; + private final Long id; + private final String creationTimestamp; + private final String description; + private final String selfLink; + private final Status status; + private final List maintenanceWindows; + private final RegionId region; + + public static final class Builder { + + private ZoneId zoneId; + private Long id; + private String creationTimestamp; + private String description; + private String selfLink; + private Status status; + private List maintenanceWindows; + private RegionId region; + + Builder() {} + + Builder zoneId(ZoneId zoneId) { + this.zoneId = zoneId; + return this; + } + + Builder id(Long id) { + this.id = id; + return this; + } + + Builder creationTimestamp(String creationTimestamp) { + this.creationTimestamp = creationTimestamp; + return this; + } + + Builder description(String description) { + this.description = description; + return this; + } + + Builder selfLink(String selfLink) { + this.selfLink = selfLink; + return this; + } + + Builder status(Status status) { + this.status = status; + return this; + } + + Builder maintenanceWindows(List maintenanceWindows) { + this.maintenanceWindows = maintenanceWindows; + return this; + } + + Builder region(RegionId region) { + this.region = region; + return this; + } + + Zone build() { + return new Zone(this); + } + } + + Zone(Builder builder) { + this.zoneId = builder.zoneId; + this.id = builder.id; + this.creationTimestamp = builder.creationTimestamp; + this.description = builder.description; + this.selfLink = builder.selfLink; + this.status = builder.status; + this.maintenanceWindows = builder.maintenanceWindows; + this.region = builder.region; + } + + /** + * Returns the zone's identity. + */ + public ZoneId zoneId() { + return zoneId; + } + + /** + * Returns the creation timestamp in RFC3339 text format. + * + * @see RFC3339 + */ + public String creationTimestamp() { + return creationTimestamp; + } + + /** + * Returns an optional textual description of the zone. + */ + public String description() { + return description; + } + + /** + * Returns an unique identifier for the zone; defined by the service. + */ + public Long id() { + return id; + } + + /** + * Returns a service-defined URL for the zone. + */ + public String selfLink() { + return selfLink; + } + + /** + * Returns the status of the zone. + */ + public Status status() { + return status; + } + + /** + * Returns the scheduled maintenance windows for this zone, if any. When the zone is in a + * maintenance window, all resources which reside in the zone will be unavailable. + * + * @see Maintenance + * Windows + */ + public List maintenanceWindows() { + return maintenanceWindows; + } + + /** + * Returns the identity of the region that hosts the zone. + */ + public RegionId region() { + return region; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("zoneId", zoneId) + .add("id", id) + .add("creationTimestamp", creationTimestamp) + .add("description", description) + .add("selfLink", selfLink) + .add("status", status) + .add("maintenanceWindows", maintenanceWindows) + .add("region", region) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(zoneId); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof Zone && Objects.equals(toPb(), ((Zone) obj).toPb()); + } + + com.google.api.services.compute.model.Zone toPb() { + com.google.api.services.compute.model.Zone zonePb = + new com.google.api.services.compute.model.Zone(); + zonePb.setId(BigInteger.valueOf(id)); + zonePb.setCreationTimestamp(creationTimestamp); + zonePb.setName(zoneId.zone()); + zonePb.setDescription(description); + zonePb.setSelfLink(selfLink); + zonePb.setStatus(status.name()); + if (maintenanceWindows != null) { + zonePb.setMaintenanceWindows( + Lists.transform(maintenanceWindows, MaintenanceWindow.TO_PB_FUNCTION)); + } + if (region != null) { + zonePb.setRegion(region.toUrl()); + } + return zonePb; + } + + static Builder builder() { + return new Builder(); + } + + static Zone fromPb(com.google.api.services.compute.model.Zone zonePb) { + Builder builder = builder(); + builder.zoneId(ZoneId.fromUrl(zonePb.getSelfLink())); + if (zonePb.getId() != null) { + builder.id(zonePb.getId().longValue()); + } + builder.creationTimestamp(zonePb.getCreationTimestamp()); + builder.description(zonePb.getDescription()); + builder.selfLink(zonePb.getSelfLink()); + if (zonePb.getStatus() != null) { + builder.status(Status.valueOf(zonePb.getStatus())); + } + if (zonePb.getMaintenanceWindows() != null) { + builder.maintenanceWindows( + Lists.transform(zonePb.getMaintenanceWindows(), MaintenanceWindow.FROM_PB_FUNCTION)); + } + if (zonePb.getRegion() != null) { + builder.region(RegionId.fromUrl(zonePb.getRegion())); + } + return builder.build(); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java new file mode 100644 index 000000000000..3637ae302483 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java @@ -0,0 +1,112 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Function; +import com.google.common.base.MoreObjects.ToStringHelper; + +import java.util.Objects; + +/** + * A Google Compute Engine zone identity. + */ +public final class ZoneId extends ResourceId { + + static final Function FROM_URL_FUNCTION = new Function() { + @Override + public ZoneId apply(String pb) { + return ZoneId.fromUrl(pb); + } + }; + static final Function TO_URL_FUNCTION = new Function() { + @Override + public String apply(ZoneId zoneId) { + return zoneId.toUrl(); + } + }; + + private static final long serialVersionUID = -7635391994812946733L; + + private final String zone; + + ZoneId(String project, String zone) { + super(project); + this.zone = checkNotNull(zone); + } + + /** + * Returns the name of the zone. + */ + public final String zone() { + return zone; + } + + @Override + public String toUrl() { + return super.toUrl() + "/zones/" + zone; + } + + @Override + ToStringHelper toStringHelper() { + return super.toStringHelper().add("zone", zone); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode(), zone); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof ZoneId && baseEquals((ZoneId) obj); + } + + @Override + ZoneId setProjectId(String projectId) { + if (project() != null) { + return this; + } + return ZoneId.of(projectId, zone); + } + + /** + * Returns a new zone identity given project and zone names. + */ + public static ZoneId of(String project, String zone) { + return new ZoneId(project, zone); + } + + /** + * Returns a new zone identity given zone name. + */ + public static ZoneId of(String zone) { + return ZoneId.of(null, zone); + } + + /** + * Returns a new zone identity given a zone URL. + */ + static ZoneId fromUrl(String url) { + int projectsIndex = url.indexOf("/projects/"); + int zonesIndex = url.indexOf("/zones/"); + String project = url.substring(projectsIndex + 10, zonesIndex); + String zone = url.substring(zonesIndex + 7, url.length()); + return ZoneId.of(project, zone); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java new file mode 100644 index 000000000000..961ca7cd499b --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java @@ -0,0 +1,79 @@ +/* + * 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.compute; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.MoreObjects.ToStringHelper; + +import java.util.Objects; + +/** + * A base class for the identity of Google Compute Engine resources that live in a zone. + */ +public abstract class ZoneResourceId extends ResourceId { + + private static final long serialVersionUID = -6249546895344926888L; + + private final String zone; + + ZoneResourceId(String project, String zone) { + super(project); + this.zone = checkNotNull(zone); + } + + ZoneResourceId(ZoneId zoneId) { + super(zoneId.project()); + this.zone = checkNotNull(zoneId.zone()); + } + + /** + * Returns the name of the zone this resource belongs to. + */ + public final String zone() { + return zone; + } + + /** + * Returns the identity of the zone this resource belongs to. + */ + public final ZoneId zoneId() { + return ZoneId.of(project(), zone); + } + + @Override + public String toUrl() { + return super.toUrl() + "/zones/" + zone; + } + + @Override + ToStringHelper toStringHelper() { + return super.toStringHelper().add("zone", zone); + } + + @Override + public int hashCode() { + return Objects.hash(baseHashCode(), zone); + } + + @Override + public boolean equals(Object obj) { + return obj != null + && obj.getClass().equals(ZoneResourceId.class) + && baseEquals((RegionResourceId) obj); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java new file mode 100644 index 000000000000..7bfb28362119 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java @@ -0,0 +1,65 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class DiskTypeIdTest { + + private static final String PROJECT = "project"; + private static final String ZONE = "zone"; + private static final String DISK_TYPE = "diskType"; + private static final String URL = + "https://www.googleapis.com/compute/v1/projects/project/zones/zone/diskTypes/diskType"; + + @Test + public void testOf() { + DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); + assertEquals(PROJECT, diskTypeId.project()); + assertEquals(ZONE, diskTypeId.zone()); + assertEquals(DISK_TYPE, diskTypeId.diskType()); + assertEquals(URL, diskTypeId.toUrl()); + diskTypeId = DiskTypeId.of(ZONE, DISK_TYPE); + assertNull(diskTypeId.project()); + assertEquals(ZONE, diskTypeId.zone()); + assertEquals(DISK_TYPE, diskTypeId.diskType()); + } + + @Test + public void testToAndFromUrl() { + DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); + compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.toUrl())); + } + + @Test + public void testSetProjectId() { + DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); + compareDiskTypeId(diskTypeId, DiskTypeId.of(ZONE, DISK_TYPE).setProjectId(PROJECT)); + } + + private void compareDiskTypeId(DiskTypeId expected, DiskTypeId value) { + assertEquals(expected, value); + assertEquals(expected.project(), expected.project()); + assertEquals(expected.zone(), expected.zone()); + assertEquals(expected.diskType(), expected.diskType()); + assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.hashCode(), expected.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java new file mode 100644 index 000000000000..239c37c49f67 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java @@ -0,0 +1,73 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class DiskTypeTest { + + private static final Long ID = 42L; + private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String DESCRIPTION = "description"; + private static final String VALID_DISK_SIZE = "10GB-10TB"; + private static final Long DEFAULT_DISK_SIZE_GB = 10L; + private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType"); + private static final DiskType DISK_TYPE = DiskType.builder() + .id(ID) + .diskTypeId(DISK_TYPE_ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .validDiskSize(VALID_DISK_SIZE) + .selfLink(DISK_TYPE_ID.toUrl()) + .defaultDiskSizeGb(DEFAULT_DISK_SIZE_GB) + .build(); + + @Test + public void testBuilder() { + assertEquals(ID, DISK_TYPE.id()); + assertEquals(DISK_TYPE_ID, DISK_TYPE.diskTypeId()); + assertEquals(CREATION_TIMESTAMP, DISK_TYPE.creationTimestamp()); + assertEquals(DESCRIPTION, DISK_TYPE.description()); + assertEquals(VALID_DISK_SIZE, DISK_TYPE.validDiskSize()); + assertEquals(DISK_TYPE_ID.toUrl(), DISK_TYPE.selfLink()); + assertEquals(DEFAULT_DISK_SIZE_GB, DISK_TYPE.defaultDiskSizeGb()); + } + + @Test + public void testToPbAndFromPb() { + compareDiskTypes(DISK_TYPE, DiskType.fromPb(DISK_TYPE.toPb())); + DiskType diskType = DiskType.builder() + .id(ID) + .diskTypeId(DISK_TYPE_ID) + .selfLink(DISK_TYPE_ID.toUrl()) + .build(); + compareDiskTypes(diskType, DiskType.fromPb(diskType.toPb())); + } + + private void compareDiskTypes(DiskType expected, DiskType value) { + assertEquals(expected.id(), value.id()); + assertEquals(expected.diskTypeId(), value.diskTypeId()); + assertEquals(expected.creationTimestamp(), value.creationTimestamp()); + assertEquals(expected.description(), value.description()); + assertEquals(expected.validDiskSize(), value.validDiskSize()); + assertEquals(expected.selfLink(), value.selfLink()); + assertEquals(expected.defaultDiskSizeGb(), value.defaultDiskSizeGb()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java new file mode 100644 index 000000000000..bf473b8218b6 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java @@ -0,0 +1,61 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class LicenseIdTest { + + private static final String PROJECT = "project"; + private static final String LICENSE = "license"; + private static final String URL = + "https://www.googleapis.com/compute/v1/projects/project/global/licenses/license"; + + @Test + public void testOf() { + LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); + assertEquals(PROJECT, licenseId.project()); + assertEquals(LICENSE, licenseId.license()); + assertEquals(URL, licenseId.toUrl()); + licenseId = LicenseId.of(LICENSE); + assertNull(licenseId.project()); + assertEquals(LICENSE, licenseId.license()); + } + + @Test + public void testToAndFromUrl() { + LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); + compareLicenseId(licenseId, LicenseId.fromUrl(licenseId.toUrl())); + } + + @Test + public void testSetProjectId() { + LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); + compareLicenseId(licenseId, LicenseId.of(LICENSE).setProjectId(PROJECT)); + } + + private void compareLicenseId(LicenseId expected, LicenseId value) { + assertEquals(expected, value); + assertEquals(expected.project(), expected.project()); + assertEquals(expected.license(), expected.license()); + assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.hashCode(), expected.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java new file mode 100644 index 000000000000..450979d8ecc8 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java @@ -0,0 +1,52 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class LicenseTest { + + private static final LicenseId LICENSE_ID = LicenseId.of("project", "license"); + private static final Boolean CHARGES_USE_FEE = true; + private static final String SELF_LINK = LICENSE_ID.toUrl(); + private static final License LICENSE = new License(LICENSE_ID, CHARGES_USE_FEE, SELF_LINK); + + @Test + public void testBuilder() { + assertEquals(LICENSE_ID, LICENSE.licenseId()); + assertEquals(CHARGES_USE_FEE, LICENSE.chargesUseFee()); + assertEquals(LICENSE_ID.toUrl(), LICENSE.selfLink()); + } + + @Test + public void testToAndFromPb() { + License license = License.fromPb(LICENSE.toPb()); + compareLicenses(LICENSE, license); + assertEquals(LICENSE_ID.project(), license.licenseId().project()); + assertEquals(LICENSE_ID.license(), license.licenseId().license()); + } + + private void compareLicenses(License expected, License value) { + assertEquals(expected, value); + assertEquals(expected.licenseId(), value.licenseId()); + assertEquals(expected.chargesUseFee(), value.chargesUseFee()); + assertEquals(expected.selfLink(), value.selfLink()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java new file mode 100644 index 000000000000..cb1353487947 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java @@ -0,0 +1,65 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class MachineTypeIdTest { + + private static final String PROJECT = "project"; + private static final String ZONE = "zone"; + private static final String TYPE = "type"; + private static final String URL = + "https://www.googleapis.com/compute/v1/projects/project/zones/zone/machineTypes/type"; + + @Test + public void testOf() { + MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE); + assertEquals(PROJECT, machineTypeId.project()); + assertEquals(ZONE, machineTypeId.zone()); + assertEquals(TYPE, machineTypeId.machineType()); + assertEquals(URL, machineTypeId.toUrl()); + machineTypeId = MachineTypeId.of(ZONE, TYPE); + assertNull(machineTypeId.project()); + assertEquals(ZONE, machineTypeId.zone()); + assertEquals(TYPE, machineTypeId.machineType()); + } + + @Test + public void testToAndFromUrl() { + MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE); + compareMachineTypeId(machineTypeId, MachineTypeId.fromUrl(machineTypeId.toUrl())); + } + + @Test + public void testSetProjectId() { + MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE); + compareMachineTypeId(machineTypeId, MachineTypeId.of(ZONE, TYPE).setProjectId(PROJECT)); + } + + private void compareMachineTypeId(MachineTypeId expected, MachineTypeId value) { + assertEquals(expected, value); + assertEquals(expected.project(), expected.project()); + assertEquals(expected.zone(), expected.zone()); + assertEquals(expected.machineType(), expected.machineType()); + assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.hashCode(), expected.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java new file mode 100644 index 000000000000..ff89f2e1a77b --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java @@ -0,0 +1,90 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +import java.util.List; + +public class MachineTypeTest { + + private static final Long ID = 42L; + private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String DESCRIPTION = "description"; + private static final MachineTypeId MACHINE_TYPE_ID = MachineTypeId.of("project", "zone", "type"); + private static final Integer GUEST_CPUS = 1; + private static final Integer MEMORY_MB = 2; + private static final List SCRATCH_DISKS = ImmutableList.of(3); + private static final Integer MAXIMUM_PERSISTENT_DISKS = 4; + private static final Long MAXIMUM_PERSISTENT_DISKS_SIZE_GB = 5L; + private static final MachineType MACHINE_TYPE = MachineType.builder() + .id(ID) + .machineTypeId(MACHINE_TYPE_ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .selfLink(MACHINE_TYPE_ID.toUrl()) + .guestCpus(GUEST_CPUS) + .memoryMb(MEMORY_MB) + .scratchDisks(SCRATCH_DISKS) + .maximumPersistentDisks(MAXIMUM_PERSISTENT_DISKS) + .maximumPersistentDisksSizeGb(MAXIMUM_PERSISTENT_DISKS_SIZE_GB) + .build(); + + @Test + public void testBuilder() { + assertEquals(ID, MACHINE_TYPE.id()); + assertEquals(MACHINE_TYPE_ID, MACHINE_TYPE.machineTypeId()); + assertEquals(CREATION_TIMESTAMP, MACHINE_TYPE.creationTimestamp()); + assertEquals(DESCRIPTION, MACHINE_TYPE.description()); + assertEquals(MACHINE_TYPE_ID.toUrl(), MACHINE_TYPE.selfLink()); + assertEquals(GUEST_CPUS, MACHINE_TYPE.guestCpus()); + assertEquals(MEMORY_MB, MACHINE_TYPE.memoryMb()); + assertEquals(SCRATCH_DISKS, MACHINE_TYPE.scratchDisks()); + assertEquals(MAXIMUM_PERSISTENT_DISKS, MACHINE_TYPE.maximumPersistentDisks()); + assertEquals(MAXIMUM_PERSISTENT_DISKS_SIZE_GB, MACHINE_TYPE.maximumPersistentDisksSizeGb()); + } + + @Test + public void testToPbAndFromPb() { + compareMachineTypes(MACHINE_TYPE, MachineType.fromPb(MACHINE_TYPE.toPb())); + MachineType machineType = MachineType.builder() + .id(ID) + .machineTypeId(MACHINE_TYPE_ID) + .selfLink(MACHINE_TYPE_ID.toUrl()) + .build(); + compareMachineTypes(machineType, MachineType.fromPb(machineType.toPb())); + } + + private void compareMachineTypes(MachineType expected, MachineType value) { + assertEquals(expected.id(), value.id()); + assertEquals(expected.machineTypeId(), value.machineTypeId()); + assertEquals(expected.id(), value.id()); + assertEquals(expected.creationTimestamp(), value.creationTimestamp()); + assertEquals(expected.description(), value.description()); + assertEquals(expected.selfLink(), value.selfLink()); + assertEquals(expected.guestCpus(), value.guestCpus()); + assertEquals(expected.memoryMb(), value.memoryMb()); + assertEquals(expected.scratchDisks(), value.scratchDisks()); + assertEquals(expected.maximumPersistentDisks(), value.maximumPersistentDisks()); + assertEquals(expected.maximumPersistentDisksSizeGb(), value.maximumPersistentDisksSizeGb()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java new file mode 100644 index 000000000000..7796c9dde66a --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java @@ -0,0 +1,61 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class RegionIdTest { + + private static final String PROJECT = "project"; + private static final String REGION = "region"; + private static final String URL = + "https://www.googleapis.com/compute/v1/projects/project/regions/region"; + + @Test + public void testOf() { + RegionId regionId = RegionId.of(PROJECT, REGION); + assertEquals(PROJECT, regionId.project()); + assertEquals(REGION, regionId.region()); + assertEquals(URL, regionId.toUrl()); + regionId = RegionId.of(REGION); + assertNull(regionId.project()); + assertEquals(REGION, regionId.region()); + } + + @Test + public void testToAndFromUrl() { + RegionId regionId = RegionId.of(PROJECT, REGION); + compareRegionId(regionId, RegionId.fromUrl(regionId.toUrl())); + } + + @Test + public void testSetProjectId() { + RegionId regionId = RegionId.of(PROJECT, REGION); + compareRegionId(regionId, RegionId.of(REGION).setProjectId(PROJECT)); + } + + private void compareRegionId(RegionId expected, RegionId value) { + assertEquals(expected, value); + assertEquals(expected.project(), expected.project()); + assertEquals(expected.region(), expected.region()); + assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.hashCode(), expected.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java new file mode 100644 index 000000000000..45b93730d84f --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java @@ -0,0 +1,85 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +import java.util.List; + +public class RegionTest { + + private static final RegionId REGION_ID = RegionId.of("project", "region"); + private static final Long ID = 42L; + private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String DESCRIPTION = "description"; + private static final Region.Status STATUS = Region.Status.DOWN; + private static final ZoneId ZONE_ID1 = ZoneId.of("project", "zone1"); + private static final ZoneId ZONE_ID2 = ZoneId.of("project", "zone2"); + private static final List ZONES = ImmutableList.of(ZONE_ID1, ZONE_ID2); + private static final Region.Quota QUOTA1 = + new Region.Quota("METRIC1", 2, 1); + private static final Region.Quota QUOTA2 = + new Region.Quota("METRIC2", 4, 3); + private static final List QUOTAS = ImmutableList.of(QUOTA1, QUOTA2); + private static final Region REGION = Region.builder() + .regionId(REGION_ID) + .id(ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .selfLink(REGION_ID.toUrl()) + .status(STATUS) + .zones(ZONES) + .quotas(QUOTAS) + .build(); + + @Test + public void testBuilder() { + assertEquals(REGION_ID, REGION.regionId()); + assertEquals(ID, REGION.id()); + assertEquals(CREATION_TIMESTAMP, REGION.creationTimestamp()); + assertEquals(DESCRIPTION, REGION.description()); + assertEquals(REGION_ID.toUrl(), REGION.selfLink()); + assertEquals(STATUS, REGION.status()); + assertEquals(ZONES, REGION.zones()); + assertEquals(QUOTAS, REGION.quotas()); + } + + @Test + public void testToAndFromPb() { + Region region = Region.fromPb(REGION.toPb()); + compareRegions(REGION, region); + assertEquals(REGION_ID.project(), region.regionId().project()); + assertEquals(REGION_ID.region(), region.regionId().region()); + } + + private void compareRegions(Region expected, Region value) { + assertEquals(expected, value); + assertEquals(expected.regionId(), value.regionId()); + assertEquals(expected.id(), value.id()); + assertEquals(expected.creationTimestamp(), value.creationTimestamp()); + assertEquals(expected.description(), value.description()); + assertEquals(expected.selfLink(), value.selfLink()); + assertEquals(expected.status(), value.status()); + assertEquals(expected.zones(), value.zones()); + assertEquals(expected.quotas(), value.quotas()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java new file mode 100644 index 000000000000..2b35be0f8bbb --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java @@ -0,0 +1,157 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; + +import com.google.common.collect.ImmutableList; +import com.google.gcloud.AuthCredentials; +import com.google.gcloud.RetryParams; +import com.google.gcloud.compute.Zone.MaintenanceWindow; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.List; + +import org.junit.Test; + +public class SerializationTest { + + private static final Long ID = 42L; + private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String DESCRIPTION = "description"; + private static final String VALID_DISK_SIZE = "10GB-10TB"; + private static final Long DEFAULT_DISK_SIZE_GB = 10L; + private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType"); + private static final DiskType DISK_TYPE = DiskType.builder() + .id(ID) + .diskTypeId(DISK_TYPE_ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .validDiskSize(VALID_DISK_SIZE) + .selfLink(DISK_TYPE_ID.toUrl()) + .defaultDiskSizeGb(DEFAULT_DISK_SIZE_GB) + .build(); + private static final MachineTypeId MACHINE_TYPE_ID = MachineTypeId.of("project", "zone", "type"); + private static final Integer GUEST_CPUS = 1; + private static final Integer MEMORY_MB = 2; + private static final List SCRATCH_DISKS = ImmutableList.of(3); + private static final Integer MAXIMUM_PERSISTENT_DISKS = 4; + private static final Long MAXIMUM_PERSISTENT_DISKS_SIZE_GB = 5L; + private static final MachineType MACHINE_TYPE = MachineType.builder() + .id(ID) + .machineTypeId(MACHINE_TYPE_ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .selfLink(MACHINE_TYPE_ID.toUrl()) + .guestCpus(GUEST_CPUS) + .memoryMb(MEMORY_MB) + .scratchDisks(SCRATCH_DISKS) + .maximumPersistentDisks(MAXIMUM_PERSISTENT_DISKS) + .maximumPersistentDisksSizeGb(MAXIMUM_PERSISTENT_DISKS_SIZE_GB) + .build(); + private static final RegionId REGION_ID = RegionId.of("project", "region"); + private static final Region.Status REGION_STATUS = Region.Status.DOWN; + private static final ZoneId ZONE_ID1 = ZoneId.of("project", "zone1"); + private static final ZoneId ZONE_ID2 = ZoneId.of("project", "zone2"); + private static final List ZONES = ImmutableList.of(ZONE_ID1, ZONE_ID2); + private static final Region.Quota QUOTA1 = + new Region.Quota("METRIC1", 2, 1); + private static final Region.Quota QUOTA2 = + new Region.Quota("METRIC2", 4, 3); + private static final List QUOTAS = ImmutableList.of(QUOTA1, QUOTA2); + private static final Region REGION = Region.builder() + .regionId(REGION_ID) + .id(ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .selfLink(REGION_ID.toUrl()) + .status(REGION_STATUS) + .zones(ZONES) + .quotas(QUOTAS) + .build(); + private static final ZoneId ZONE_ID = ZoneId.of("project", "zone"); + private static final Zone.Status ZONE_STATUS = Zone.Status.DOWN; + private static final MaintenanceWindow WINDOW1 = new MaintenanceWindow("NAME1", "DESCRIPTION1", + "2016-01-20T04:39:00.210-08:00", "2016-01-21T04:39:00.210-08:00"); + private static final MaintenanceWindow WINDOW2 = new MaintenanceWindow("NAME2", "DESCRIPTION2", + "2016-01-21T04:39:00.210-08:00", "2016-01-22T04:39:00.210-08:00"); + private static final List WINDOWS = ImmutableList.of(WINDOW1, WINDOW2); + private static final Zone ZONE = Zone.builder() + .zoneId(ZONE_ID) + .id(ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .selfLink(ZONE_ID.toUrl()) + .status(ZONE_STATUS) + .maintenanceWindows(WINDOWS) + .region(REGION_ID) + .build(); + private static final LicenseId LICENSE_ID = LicenseId.of("project", "license"); + private static final Boolean CHARGES_USE_FEE = true; + private static final String SELF_LINK = LICENSE_ID.toUrl(); + private static final License LICENSE = new License(LICENSE_ID, CHARGES_USE_FEE, SELF_LINK); + + @Test + public void testServiceOptions() throws Exception { + ComputeOptions options = ComputeOptions.builder() + .projectId("p1") + .authCredentials(AuthCredentials.createForAppEngine()) + .build(); + ComputeOptions serializedCopy = serializeAndDeserialize(options); + assertEquals(options, serializedCopy); + + options = options.toBuilder() + .projectId("p2") + .retryParams(RetryParams.defaultInstance()) + .authCredentials(null) + .build(); + serializedCopy = serializeAndDeserialize(options); + assertEquals(options, serializedCopy); + } + + @Test + public void testModelAndRequests() throws Exception { + Serializable[] objects = {DISK_TYPE_ID, DISK_TYPE, MACHINE_TYPE_ID, MACHINE_TYPE, REGION_ID, + REGION, ZONE_ID, ZONE, LICENSE_ID, LICENSE}; + for (Serializable obj : objects) { + Object copy = serializeAndDeserialize(obj); + assertEquals(obj, obj); + assertEquals(obj, copy); + assertNotSame(obj, copy); + assertEquals(copy, copy); + } + } + + @SuppressWarnings("unchecked") + private T serializeAndDeserialize(T obj) + throws IOException, ClassNotFoundException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { + output.writeObject(obj); + } + try (ObjectInputStream input = + new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { + return (T) input.readObject(); + } + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java new file mode 100644 index 000000000000..5722036db1b4 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java @@ -0,0 +1,61 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class ZoneIdTest { + + private static final String PROJECT = "project"; + private static final String ZONE = "zone"; + private static final String URL = + "https://www.googleapis.com/compute/v1/projects/project/zones/zone"; + + @Test + public void testOf() { + ZoneId zoneId = ZoneId.of(PROJECT, ZONE); + assertEquals(PROJECT, zoneId.project()); + assertEquals(ZONE, zoneId.zone()); + assertEquals(URL, zoneId.toUrl()); + zoneId = ZoneId.of(ZONE); + assertNull(zoneId.project()); + assertEquals(ZONE, zoneId.zone()); + } + + @Test + public void testToAndFromUrl() { + ZoneId zoneId = ZoneId.of(PROJECT, ZONE); + compareZoneId(zoneId, ZoneId.fromUrl(zoneId.toUrl())); + } + + @Test + public void testSetProjectId() { + ZoneId zoneId = ZoneId.of(PROJECT, ZONE); + compareZoneId(zoneId, ZoneId.of(ZONE).setProjectId(PROJECT)); + } + + private void compareZoneId(ZoneId expected, ZoneId value) { + assertEquals(expected, value); + assertEquals(expected.project(), expected.project()); + assertEquals(expected.zone(), expected.zone()); + assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.hashCode(), expected.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java new file mode 100644 index 000000000000..02ab2574cd70 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java @@ -0,0 +1,86 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.ImmutableList; +import com.google.gcloud.compute.Zone.MaintenanceWindow; + +import org.junit.Test; + +import java.util.List; + +public class ZoneTest { + + private static final ZoneId ZONE_ID = ZoneId.of("project", "zone"); + private static final RegionId REGION_ID = RegionId.of("project", "region"); + private static final Long ID = 42L; + private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String DESCRIPTION = "description"; + private static final Zone.Status STATUS = Zone.Status.DOWN; + private static final MaintenanceWindow WINDOW1 = new MaintenanceWindow("NAME1", "DESCRIPTION1", + "2016-01-20T04:39:00.210-08:00", "2016-01-21T04:39:00.210-08:00"); + private static final MaintenanceWindow WINDOW2 = new MaintenanceWindow("NAME2", "DESCRIPTION2", + "2016-01-21T04:39:00.210-08:00", "2016-01-22T04:39:00.210-08:00"); + private static final List WINDOWS = ImmutableList.of(WINDOW1, WINDOW2); + private static final Zone ZONE = Zone.builder() + .zoneId(ZONE_ID) + .id(ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .selfLink(ZONE_ID.toUrl()) + .status(STATUS) + .maintenanceWindows(WINDOWS) + .region(REGION_ID) + .build(); + + @Test + public void testBuilder() { + assertEquals(REGION_ID, ZONE.region()); + assertEquals(ID, ZONE.id()); + assertEquals(CREATION_TIMESTAMP, ZONE.creationTimestamp()); + assertEquals(DESCRIPTION, ZONE.description()); + assertEquals(ZONE_ID.toUrl(), ZONE.selfLink()); + assertEquals(STATUS, ZONE.status()); + assertEquals(WINDOWS, ZONE.maintenanceWindows()); + assertEquals(REGION_ID, ZONE.region()); + } + + @Test + public void testToAndFromPb() { + com.google.api.services.compute.model.Zone zonePb = ZONE.toPb(); + assertEquals(REGION_ID.toUrl(), zonePb.getRegion()); + Zone zone = Zone.fromPb(zonePb); + compareZones(ZONE, zone); + assertEquals(ZONE_ID.project(), zone.zoneId().project()); + assertEquals(ZONE_ID.zone(), zone.zoneId().zone()); + } + + private void compareZones(Zone expected, Zone value) { + assertEquals(expected, value); + assertEquals(expected.zoneId(), value.zoneId()); + assertEquals(expected.id(), value.id()); + assertEquals(expected.creationTimestamp(), value.creationTimestamp()); + assertEquals(expected.description(), value.description()); + assertEquals(expected.selfLink(), value.selfLink()); + assertEquals(expected.status(), value.status()); + assertEquals(expected.maintenanceWindows(), value.maintenanceWindows()); + assertEquals(expected.region(), value.region()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} From 6d1f2b97b73fce98caaf1994f8c0ac82196de519 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 22 Feb 2016 23:01:35 +0100 Subject: [PATCH 03/10] Add DeprecationStatus to disk and machine type, other fixes --- gcloud-java-compute/README.md | 4 +- gcloud-java-compute/pom.xml | 2 +- .../gcloud/compute/DeprecationStatus.java | 162 ++++++++++++++++++ .../com/google/gcloud/compute/DiskType.java | 36 +++- .../com/google/gcloud/compute/DiskTypeId.java | 16 +- .../com/google/gcloud/compute/License.java | 4 +- .../com/google/gcloud/compute/LicenseId.java | 2 +- .../google/gcloud/compute/MachineType.java | 42 ++++- .../google/gcloud/compute/MachineTypeId.java | 18 +- .../com/google/gcloud/compute/Region.java | 15 +- .../com/google/gcloud/compute/RegionId.java | 4 +- .../java/com/google/gcloud/compute/Zone.java | 21 +-- .../com/google/gcloud/compute/ZoneId.java | 2 +- .../gcloud/compute/DeprecationStatusTest.java | 78 +++++++++ .../google/gcloud/compute/DiskTypeIdTest.java | 3 + .../google/gcloud/compute/DiskTypeTest.java | 26 ++- .../google/gcloud/compute/LicenseIdTest.java | 2 + .../gcloud/compute/MachineTypeIdTest.java | 2 + .../gcloud/compute/MachineTypeTest.java | 11 +- .../google/gcloud/compute/RegionIdTest.java | 2 + .../gcloud/compute/SerializationTest.java | 12 +- .../com/google/gcloud/compute/ZoneIdTest.java | 2 + 22 files changed, 415 insertions(+), 51 deletions(-) create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java diff --git a/gcloud-java-compute/README.md b/gcloud-java-compute/README.md index aef53c781c20..653a586d76e6 100644 --- a/gcloud-java-compute/README.md +++ b/gcloud-java-compute/README.md @@ -33,7 +33,7 @@ Authentication See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README. -About Google Cloud BigQuery +About Google Cloud Compute -------------------------- [Google Cloud Compute][cloud-compute] delivers virtual machines running in Google's innovative data @@ -65,7 +65,7 @@ Java 7 or above is required for using this client. Testing ------- -This library has tools to help make tests for code using Cloud BigQuery. +This library has tools to help make tests for code using Cloud Compute. See [TESTING] to read more about testing. diff --git a/gcloud-java-compute/pom.xml b/gcloud-java-compute/pom.xml index 70e4f127c119..419c7c4780a2 100644 --- a/gcloud-java-compute/pom.xml +++ b/gcloud-java-compute/pom.xml @@ -24,7 +24,7 @@ com.google.apis google-api-services-compute - v1-rev93-1.21.0 + v1-rev97-1.21.0 compile diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java new file mode 100644 index 000000000000..7b16107c9210 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java @@ -0,0 +1,162 @@ +/* + * 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.compute; + +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.util.Objects; + +/** + * The deprecation status associated to a Google Compute Engine resource. + */ +public final class DeprecationStatus implements Serializable { + + private static final long serialVersionUID = -2695077634793679794L; + + private final String deleted; + private final String deprecated; + private final String obsolete; + private final T replacement; + private final Status status; + + /** + * The deprecation status of a Google Compute Engine resource. + */ + public enum Status { + /** + * Operations that create Google Compute Engine entity using a deprecated resource will return + * successfully but with a warning indicating the deprecation and suggesting a replacement. + */ + DEPRECATED, + /** + * Operations that create Google Compute Engine entity using an obsolete resource will be + * rejected and result in an error. + */ + OBSOLETE, + /** + * Operations that create Google Compute Engine entity using a deleted resource will be + * rejected and result in an error. + */ + DELETED + } + + DeprecationStatus(String deleted, String deprecated, String obsolete, T replacement, + Status status) { + this.deleted = deleted; + this.deprecated = deprecated; + this.obsolete = obsolete; + this.replacement = replacement; + this.status = status; + } + + /** + * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource + * will be changed to {@link Status#DELETED}. + * + * @see RFC3339 + */ + public String deleted() { + return deleted; + } + + /** + * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource + * will be changed to {@link Status#DEPRECATED}. + * + * @see RFC3339 + */ + public String deprecated() { + return deprecated; + } + + /** + * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource + * will be changed to {@link Status#OBSOLETE}. + * + * @see RFC3339 + */ + public String obsolete() { + return obsolete; + } + + /** + * Returns the identity of the suggested replacement for a deprecated resource. The suggested + * replacement resource must be the same kind of resource as the deprecated resource. + */ + public T replacement() { + return replacement; + } + + /** + * Returns the deprecation state of this resource. + */ + public Status status() { + return status; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("deleted", deleted) + .add("deprecated", deprecated) + .add("obsolete", obsolete) + .add("replacement", replacement) + .add("status", status) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(deleted, deprecated, obsolete, replacement, status); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof DeprecationStatus + && Objects.equals(toPb(), ((DeprecationStatus) obj).toPb()); + } + + com.google.api.services.compute.model.DeprecationStatus toPb() { + com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb = + new com.google.api.services.compute.model.DeprecationStatus(); + deprecationStatusPb.setDeleted(deleted); + deprecationStatusPb.setDeprecated(deprecated); + deprecationStatusPb.setObsolete(obsolete); + if (replacement != null) { + deprecationStatusPb.setReplacement(replacement.toUrl()); + } + if (status() != null) { + deprecationStatusPb.setState(status.name()); + } + return deprecationStatusPb; + } + + static DeprecationStatus fromPb( + com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb, + Function fromUrl) { + return new DeprecationStatus( + deprecationStatusPb.getDeleted(), + deprecationStatusPb.getDeprecated(), + deprecationStatusPb.getObsolete(), + deprecationStatusPb.getReplacement() != null + ? fromUrl.apply(deprecationStatusPb.getReplacement()) : null, + deprecationStatusPb.getState() != null + ? Status.valueOf(deprecationStatusPb.getState()) : null); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java index 2cc6cb9bb865..48701cc2a3be 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java @@ -24,8 +24,10 @@ import java.util.Objects; /** - * Google Compute Engine Disk type. A disk type represents the type of disk to use, such as a + * A Google Compute Engine disk type. A disk type represents the type of disk to use, such as * {@code pd-ssd} or {@code pd-standard}. + * + * @see Disk Types */ public final class DiskType implements Serializable { @@ -53,6 +55,7 @@ public com.google.api.services.compute.model.DiskType apply(DiskType diskType) { private final String validDiskSize; private final String selfLink; private final Long defaultDiskSizeGb; + private final DeprecationStatus deprecationStatus; static final class Builder { @@ -63,6 +66,7 @@ static final class Builder { private String validDiskSize; private String selfLink; private Long defaultDiskSizeGb; + private DeprecationStatus deprecationStatus; private Builder() {} @@ -101,9 +105,11 @@ Builder defaultDiskSizeGb(Long defaultDiskSizeGb) { return this; } - /** - * Creates a {@code DiskType} object. - */ + Builder deprecationStatus(DeprecationStatus deprecationStatus) { + this.deprecationStatus = deprecationStatus; + return this; + } + DiskType build() { return new DiskType(this); } @@ -117,6 +123,7 @@ private DiskType(Builder builder) { this.validDiskSize = builder.validDiskSize; this.selfLink = builder.selfLink; this.defaultDiskSizeGb = builder.defaultDiskSizeGb; + this.deprecationStatus = builder.deprecationStatus; } /** @@ -170,6 +177,15 @@ public Long defaultDiskSizeGb() { return defaultDiskSizeGb; } + /** + * Returns the deprecation status of the disk type. If {@link DeprecationStatus#status()} is + * either {@link DeprecationStatus.Status#DELETED} or {@link DeprecationStatus.Status#OBSOLETE} + * the disk type should not be used. Returns {@code null} if the disk type is not deprecated. + */ + public DeprecationStatus deprecationStatus() { + return deprecationStatus; + } + @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -179,12 +195,13 @@ public String toString() { .add("validDiskSize", validDiskSize) .add("selfLink", selfLink) .add("defaultDiskSizeGb", defaultDiskSizeGb) + .add("deprecationStatus", deprecationStatus) .toString(); } @Override public int hashCode() { - return Objects.hash(id); + return Objects.hash(diskTypeId); } @Override @@ -204,6 +221,9 @@ com.google.api.services.compute.model.DiskType toPb() { diskTypePb.setSelfLink(selfLink); diskTypePb.setDefaultDiskSizeGb(defaultDiskSizeGb); diskTypePb.setZone(diskTypeId.zoneId().toUrl()); + if (deprecationStatus != null) { + diskTypePb.setDeprecated(deprecationStatus.toPb()); + } return diskTypePb; } @@ -213,7 +233,7 @@ static Builder builder() { static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb) { Builder builder = builder(); - if (diskTypePb.getId() != null ) { + if (diskTypePb.getId() != null) { builder.id(diskTypePb.getId().longValue()); } builder.creationTimestamp(diskTypePb.getCreationTimestamp()); @@ -222,6 +242,10 @@ static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb builder.validDiskSize(diskTypePb.getValidDiskSize()); builder.selfLink(diskTypePb.getSelfLink()); builder.defaultDiskSizeGb(diskTypePb.getDefaultDiskSizeGb()); + if (diskTypePb.getDeprecated() != null) { + builder.deprecationStatus( + DeprecationStatus.fromPb(diskTypePb.getDeprecated(), DiskTypeId.FROM_URL_FUNCTION)); + } return builder.build(); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java index 527f7da124b4..dfaa58b6f481 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Function; import com.google.common.base.MoreObjects; import java.util.Objects; @@ -27,11 +28,24 @@ */ public final class DiskTypeId extends ZoneResourceId { + static final Function FROM_URL_FUNCTION = new Function() { + @Override + public DiskTypeId apply(String pb) { + return DiskTypeId.fromUrl(pb); + } + }; + static final Function TO_URL_FUNCTION = new Function() { + @Override + public String apply(DiskTypeId diskTypeId) { + return diskTypeId.toUrl(); + } + }; + private static final long serialVersionUID = 7337881474103686219L; private final String diskType; - DiskTypeId(String project, String zone, String diskType) { + private DiskTypeId(String project, String zone, String diskType) { super(project, zone); this.diskType = checkNotNull(diskType); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java index 17e4f0d22be0..5116761a0b2f 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java @@ -26,6 +26,8 @@ /** * A Google Compute Engine License. A License represents a software license. Licenses are used to * track software usage in images, persistent disks, snapshots, and virtual machine instances. + * + * @see Licenses */ public final class License implements Serializable { @@ -74,7 +76,7 @@ public String toString() { @Override public int hashCode() { - return Objects.hash(licenseId, chargesUseFee, selfLink); + return Objects.hash(licenseId); } @Override diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java index f32695e03c03..2fd2b5eb3058 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java @@ -45,7 +45,7 @@ public String apply(LicenseId licenseId) { private final String license; - LicenseId(String project, String license) { + private LicenseId(String project, String license) { super(project); this.license = checkNotNull(license); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java index bf1d6ce49f52..29581852f346 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java @@ -26,7 +26,14 @@ import java.util.List; import java.util.Objects; -public class MachineType implements Serializable { +/** + * A Google Compute Engine machine type. A machine type determine the virtualized hardware + * specifications of your virtual machine instances, such as the amount of memory or number of + * virtual CPUs. + * + * @see Machine Types + */ +public final class MachineType implements Serializable { static final Function FROM_PB_FUNCTION = @@ -57,8 +64,9 @@ public com.google.api.services.compute.model.MachineType apply(MachineType type) private final List scratchDisks; private final Integer maximumPersistentDisks; private final Long maximumPersistentDisksSizeGb; + private final DeprecationStatus deprecationStatus; - public static final class Builder { + static final class Builder { private MachineTypeId machineTypeId; private Long id; @@ -70,9 +78,9 @@ public static final class Builder { private List scratchDisks; private Integer maximumPersistentDisks; private Long maximumPersistentDisksSizeGb; + private DeprecationStatus deprecationStatus; - Builder() {} - + private Builder() {} Builder machineTypeId(MachineTypeId machineTypeId) { this.machineTypeId = machineTypeId; @@ -124,12 +132,17 @@ Builder maximumPersistentDisksSizeGb(Long maximumPersistentDisksSizeGb) { return this; } + Builder deprecationStatus(DeprecationStatus deprecationStatus) { + this.deprecationStatus = deprecationStatus; + return this; + } + MachineType build() { return new MachineType(this); } } - MachineType(Builder builder) { + private MachineType(Builder builder) { this.machineTypeId = builder.machineTypeId; this.id = builder.id; this.creationTimestamp = builder.creationTimestamp; @@ -140,6 +153,7 @@ MachineType build() { this.scratchDisks = builder.scratchDisks; this.maximumPersistentDisks = builder.maximumPersistentDisks; this.maximumPersistentDisksSizeGb = builder.maximumPersistentDisksSizeGb; + this.deprecationStatus = builder.deprecationStatus; } /** @@ -214,6 +228,16 @@ public Long maximumPersistentDisksSizeGb() { return maximumPersistentDisksSizeGb; } + /** + * Returns the deprecation status of the machine type. If {@link DeprecationStatus#status()} is + * either {@link DeprecationStatus.Status#DELETED} or {@link DeprecationStatus.Status#OBSOLETE} + * the machine type should not be used. Returns {@code null} if the machine type is not + * deprecated. + */ + public DeprecationStatus deprecationStatus() { + return deprecationStatus; + } + @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -227,6 +251,7 @@ public String toString() { .add("scratchDisks", scratchDisks) .add("maximumPersistentDisks", maximumPersistentDisks) .add("maximumPersistentDisksSizeGb", maximumPersistentDisksSizeGb) + .add("deprecationStatus", deprecationStatus) .toString(); } @@ -264,6 +289,9 @@ public ScratchDisks apply(Integer diskSize) { machineTypePb.setMaximumPersistentDisks(maximumPersistentDisks); machineTypePb.setMaximumPersistentDisksSizeGb(maximumPersistentDisksSizeGb); machineTypePb.setZone(machineTypeId.zoneId().zone()); + if (deprecationStatus != null) { + machineTypePb.setDeprecated(deprecationStatus.toPb()); + } return machineTypePb; } @@ -293,6 +321,10 @@ public Integer apply(ScratchDisks scratchDiskPb) { } builder.maximumPersistentDisks(machineTypePb.getMaximumPersistentDisks()); builder.maximumPersistentDisksSizeGb(machineTypePb.getMaximumPersistentDisksSizeGb()); + if (machineTypePb.getDeprecated() != null) { + builder.deprecationStatus( + DeprecationStatus.fromPb(machineTypePb.getDeprecated(), MachineTypeId.FROM_URL_FUNCTION)); + } return builder.build(); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java index 4a215dd207d3..04073863b814 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Function; import com.google.common.base.MoreObjects; import java.util.Objects; @@ -27,11 +28,26 @@ */ public final class MachineTypeId extends ZoneResourceId { + static final Function FROM_URL_FUNCTION = + new Function() { + @Override + public MachineTypeId apply(String pb) { + return MachineTypeId.fromUrl(pb); + } + }; + static final Function TO_URL_FUNCTION = + new Function() { + @Override + public String apply(MachineTypeId machineTypeId) { + return machineTypeId.toUrl(); + } + }; + private static final long serialVersionUID = -5819598544478859608L; private final String machineType; - MachineTypeId(String project, String zone, String machineType) { + private MachineTypeId(String project, String zone, String machineType) { super(project, zone); this.machineType = checkNotNull(machineType); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java index ded705bc2a8d..2195d79244ef 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java @@ -18,6 +18,7 @@ import com.google.common.base.Function; import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.io.Serializable; @@ -27,6 +28,8 @@ /** * A Google Compute Engine region. + * + * @see Region and Zones */ public final class Region implements Serializable { @@ -58,7 +61,7 @@ public enum Status { /** * A quota assigned to this region. */ - public static class Quota implements Serializable { + public static final class Quota implements Serializable { static final Function FROM_PB_FUNCTION = new Function() { @@ -147,7 +150,7 @@ static Quota fromPb(com.google.api.services.compute.model.Quota quotaPb) { private final List zones; private final List quotas; - public static final class Builder { + static final class Builder { private RegionId regionId; private Long id; @@ -158,7 +161,7 @@ public static final class Builder { private List zones; private List quotas; - Builder() {} + private Builder() {} Builder regionId(RegionId regionId) { this.regionId = regionId; @@ -191,12 +194,12 @@ Builder status(Status status) { } Builder zones(List zones) { - this.zones = zones; + this.zones = ImmutableList.copyOf(zones); return this; } Builder quotas(List quotas) { - this.quotas = quotas; + this.quotas = ImmutableList.copyOf(quotas); return this; } @@ -205,7 +208,7 @@ Region build() { } } - Region(Builder builder) { + private Region(Builder builder) { this.regionId = builder.regionId; this.id = builder.id; this.creationTimestamp = builder.creationTimestamp; diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java index 60b603778863..09c81941ee43 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java @@ -31,12 +31,12 @@ public final class RegionId extends ResourceId { private final String region; - RegionId(String project, String region) { + private RegionId(String project, String region) { super(project); this.region = checkNotNull(region); } - RegionId(RegionId regionId) { + private RegionId(RegionId regionId) { super(regionId.project()); this.region = checkNotNull(regionId.region()); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java index b98e863011bb..87bf085cf047 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java @@ -28,6 +28,8 @@ /** * A Google Compute Engine zone. + * + * @see Region and Zones */ public final class Zone implements Serializable { @@ -45,6 +47,7 @@ public com.google.api.services.compute.model.Zone apply(Zone region) { return region.toPb(); } }; + private static final long serialVersionUID = 6113636504417213010L; /** @@ -62,7 +65,7 @@ public enum Status { * @see Maintenance * Windows */ - public static class MaintenanceWindow implements Serializable { + public static final class MaintenanceWindow implements Serializable { static final Function FROM_PB_FUNCTION = new Function() { @@ -145,14 +148,8 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (!(obj instanceof MaintenanceWindow)) { - return false; - } - MaintenanceWindow other = (MaintenanceWindow) obj; - return Objects.equals(name, other.name) - && Objects.equals(description, other.description) - && Objects.equals(beginTime, other.beginTime) - && Objects.equals(endTime, other.endTime); + return obj instanceof MaintenanceWindow + && Objects.equals(toPb(), ((MaintenanceWindow) obj).toPb()); } MaintenanceWindows toPb() { @@ -178,7 +175,7 @@ static MaintenanceWindow fromPb(MaintenanceWindows windowPb) { private final List maintenanceWindows; private final RegionId region; - public static final class Builder { + static final class Builder { private ZoneId zoneId; private Long id; @@ -189,7 +186,7 @@ public static final class Builder { private List maintenanceWindows; private RegionId region; - Builder() {} + private Builder() {} Builder zoneId(ZoneId zoneId) { this.zoneId = zoneId; @@ -236,7 +233,7 @@ Zone build() { } } - Zone(Builder builder) { + private Zone(Builder builder) { this.zoneId = builder.zoneId; this.id = builder.id; this.creationTimestamp = builder.creationTimestamp; diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java index 3637ae302483..14e38c22cad7 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java @@ -45,7 +45,7 @@ public String apply(ZoneId zoneId) { private final String zone; - ZoneId(String project, String zone) { + private ZoneId(String project, String zone) { super(project); this.zone = checkNotNull(zone); } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java new file mode 100644 index 000000000000..32bda09a5bdd --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java @@ -0,0 +1,78 @@ +/* + * 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.compute; + +import static org.junit.Assert.assertEquals; + +import com.google.gcloud.compute.DeprecationStatus.Status; + +import org.junit.Test; + +public class DeprecationStatusTest { + + private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; + private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; + private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone","diskType"); + private static final MachineTypeId MACHINE_TYPE_ID = + MachineTypeId.of("project", "zone","machineType"); + private static final Status STATUS = Status.DELETED; + private static final DeprecationStatus DISK_TYPE_STATUS = + new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, DISK_TYPE_ID, STATUS); + private static final DeprecationStatus MACHINE_TYPE_STATUS = + new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, MACHINE_TYPE_ID, STATUS); + + @Test + public void testConstructor() { + assertEquals(DELETED, DISK_TYPE_STATUS.deleted()); + assertEquals(DEPRECATED, DISK_TYPE_STATUS.deprecated()); + assertEquals(OBSOLETE, DISK_TYPE_STATUS.obsolete()); + assertEquals(DISK_TYPE_ID, DISK_TYPE_STATUS.replacement()); + assertEquals(STATUS, DISK_TYPE_STATUS.status()); + assertEquals(DELETED, MACHINE_TYPE_STATUS.deleted()); + assertEquals(DEPRECATED, MACHINE_TYPE_STATUS.deprecated()); + assertEquals(OBSOLETE, MACHINE_TYPE_STATUS.obsolete()); + assertEquals(MACHINE_TYPE_ID, MACHINE_TYPE_STATUS.replacement()); + assertEquals(STATUS, MACHINE_TYPE_STATUS.status()); + } + + @Test + public void testToAndFromPb() { + DeprecationStatus diskStatus = + DeprecationStatus.fromPb(DISK_TYPE_STATUS.toPb(), DiskTypeId.FROM_URL_FUNCTION); + compareDeprecationStatus(DISK_TYPE_STATUS, diskStatus); + DeprecationStatus machineStatus = + DeprecationStatus.fromPb(MACHINE_TYPE_STATUS.toPb(), MachineTypeId.FROM_URL_FUNCTION); + compareDeprecationStatus(MACHINE_TYPE_STATUS, machineStatus); + diskStatus = new DeprecationStatus<>(null, DEPRECATED, null, DISK_TYPE_ID, STATUS); + assertEquals(diskStatus, + DeprecationStatus.fromPb(diskStatus.toPb(), DiskTypeId.FROM_URL_FUNCTION)); + machineStatus = new DeprecationStatus<>(null, DEPRECATED, null, MACHINE_TYPE_ID, STATUS); + assertEquals(machineStatus, + DeprecationStatus.fromPb(machineStatus.toPb(), MachineTypeId.FROM_URL_FUNCTION)); + } + + private void compareDeprecationStatus(DeprecationStatus expected, DeprecationStatus value) { + assertEquals(expected, value); + assertEquals(expected.deleted(), value.deleted()); + assertEquals(expected.deprecated(), value.deprecated()); + assertEquals(expected.obsolete(), value.obsolete()); + assertEquals(expected.replacement(), value.replacement()); + assertEquals(expected.status(), value.status()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java index 7bfb28362119..0555f7c1d570 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import org.junit.Test; @@ -45,12 +46,14 @@ public void testOf() { @Test public void testToAndFromUrl() { DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); + assertSame(diskTypeId, diskTypeId.setProjectId(PROJECT)); compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.toUrl())); } @Test public void testSetProjectId() { DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); + assertSame(diskTypeId, diskTypeId.setProjectId(PROJECT)); compareDiskTypeId(diskTypeId, DiskTypeId.of(ZONE, DISK_TYPE).setProjectId(PROJECT)); } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java index 239c37c49f67..5d486c78cd5c 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java @@ -28,15 +28,22 @@ public class DiskTypeTest { private static final String VALID_DISK_SIZE = "10GB-10TB"; private static final Long DEFAULT_DISK_SIZE_GB = 10L; private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType"); + private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; + private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; + private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final DeprecationStatus.Status STATUS = DeprecationStatus.Status.DELETED; + private static final DeprecationStatus DEPRECATION_STATUS = + new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, DISK_TYPE_ID, STATUS); private static final DiskType DISK_TYPE = DiskType.builder() - .id(ID) - .diskTypeId(DISK_TYPE_ID) - .creationTimestamp(CREATION_TIMESTAMP) - .description(DESCRIPTION) - .validDiskSize(VALID_DISK_SIZE) - .selfLink(DISK_TYPE_ID.toUrl()) - .defaultDiskSizeGb(DEFAULT_DISK_SIZE_GB) - .build(); + .id(ID) + .diskTypeId(DISK_TYPE_ID) + .creationTimestamp(CREATION_TIMESTAMP) + .description(DESCRIPTION) + .validDiskSize(VALID_DISK_SIZE) + .selfLink(DISK_TYPE_ID.toUrl()) + .defaultDiskSizeGb(DEFAULT_DISK_SIZE_GB) + .deprecationStatus(DEPRECATION_STATUS) + .build(); @Test public void testBuilder() { @@ -47,6 +54,7 @@ public void testBuilder() { assertEquals(VALID_DISK_SIZE, DISK_TYPE.validDiskSize()); assertEquals(DISK_TYPE_ID.toUrl(), DISK_TYPE.selfLink()); assertEquals(DEFAULT_DISK_SIZE_GB, DISK_TYPE.defaultDiskSizeGb()); + assertEquals(DEPRECATION_STATUS, DISK_TYPE.deprecationStatus()); } @Test @@ -61,6 +69,7 @@ public void testToPbAndFromPb() { } private void compareDiskTypes(DiskType expected, DiskType value) { + assertEquals(expected, value); assertEquals(expected.id(), value.id()); assertEquals(expected.diskTypeId(), value.diskTypeId()); assertEquals(expected.creationTimestamp(), value.creationTimestamp()); @@ -68,6 +77,7 @@ private void compareDiskTypes(DiskType expected, DiskType value) { assertEquals(expected.validDiskSize(), value.validDiskSize()); assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.defaultDiskSizeGb(), value.defaultDiskSizeGb()); + assertEquals(expected.deprecationStatus(), value.deprecationStatus()); assertEquals(expected.hashCode(), value.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java index bf473b8218b6..eb5c40849956 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import org.junit.Test; @@ -48,6 +49,7 @@ public void testToAndFromUrl() { @Test public void testSetProjectId() { LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); + assertSame(licenseId, licenseId.setProjectId(PROJECT)); compareLicenseId(licenseId, LicenseId.of(LICENSE).setProjectId(PROJECT)); } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java index cb1353487947..34241fb9e3d1 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import org.junit.Test; @@ -51,6 +52,7 @@ public void testToAndFromUrl() { @Test public void testSetProjectId() { MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE); + assertSame(machineTypeId, machineTypeId.setProjectId(PROJECT)); compareMachineTypeId(machineTypeId, MachineTypeId.of(ZONE, TYPE).setProjectId(PROJECT)); } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java index ff89f2e1a77b..285f4939c5fd 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java @@ -35,6 +35,12 @@ public class MachineTypeTest { private static final List SCRATCH_DISKS = ImmutableList.of(3); private static final Integer MAXIMUM_PERSISTENT_DISKS = 4; private static final Long MAXIMUM_PERSISTENT_DISKS_SIZE_GB = 5L; + private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; + private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; + private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final DeprecationStatus.Status STATUS = DeprecationStatus.Status.DELETED; + private static final DeprecationStatus DEPRECATION_STATUS = + new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, MACHINE_TYPE_ID, STATUS); private static final MachineType MACHINE_TYPE = MachineType.builder() .id(ID) .machineTypeId(MACHINE_TYPE_ID) @@ -46,6 +52,7 @@ public class MachineTypeTest { .scratchDisks(SCRATCH_DISKS) .maximumPersistentDisks(MAXIMUM_PERSISTENT_DISKS) .maximumPersistentDisksSizeGb(MAXIMUM_PERSISTENT_DISKS_SIZE_GB) + .deprecationStatus(DEPRECATION_STATUS) .build(); @Test @@ -60,6 +67,7 @@ public void testBuilder() { assertEquals(SCRATCH_DISKS, MACHINE_TYPE.scratchDisks()); assertEquals(MAXIMUM_PERSISTENT_DISKS, MACHINE_TYPE.maximumPersistentDisks()); assertEquals(MAXIMUM_PERSISTENT_DISKS_SIZE_GB, MACHINE_TYPE.maximumPersistentDisksSizeGb()); + assertEquals(DEPRECATION_STATUS, MACHINE_TYPE.deprecationStatus()); } @Test @@ -74,7 +82,7 @@ public void testToPbAndFromPb() { } private void compareMachineTypes(MachineType expected, MachineType value) { - assertEquals(expected.id(), value.id()); + assertEquals(expected, value); assertEquals(expected.machineTypeId(), value.machineTypeId()); assertEquals(expected.id(), value.id()); assertEquals(expected.creationTimestamp(), value.creationTimestamp()); @@ -85,6 +93,7 @@ private void compareMachineTypes(MachineType expected, MachineType value) { assertEquals(expected.scratchDisks(), value.scratchDisks()); assertEquals(expected.maximumPersistentDisks(), value.maximumPersistentDisks()); assertEquals(expected.maximumPersistentDisksSizeGb(), value.maximumPersistentDisksSizeGb()); + assertEquals(expected.deprecationStatus(), value.deprecationStatus()); assertEquals(expected.hashCode(), value.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java index 7796c9dde66a..74d08aa27ab6 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import org.junit.Test; @@ -48,6 +49,7 @@ public void testToAndFromUrl() { @Test public void testSetProjectId() { RegionId regionId = RegionId.of(PROJECT, REGION); + assertSame(regionId, regionId.setProjectId(PROJECT)); compareRegionId(regionId, RegionId.of(REGION).setProjectId(PROJECT)); } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java index 2b35be0f8bbb..4ef16bbc0181 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java @@ -24,6 +24,8 @@ import com.google.gcloud.RetryParams; import com.google.gcloud.compute.Zone.MaintenanceWindow; +import org.junit.Test; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -32,8 +34,6 @@ import java.io.Serializable; import java.util.List; -import org.junit.Test; - public class SerializationTest { private static final Long ID = 42L; @@ -106,6 +106,12 @@ public class SerializationTest { .maintenanceWindows(WINDOWS) .region(REGION_ID) .build(); + private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; + private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; + private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final DeprecationStatus DEPRECATION_STATUS = + new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, MACHINE_TYPE_ID, + DeprecationStatus.Status.DELETED); private static final LicenseId LICENSE_ID = LicenseId.of("project", "license"); private static final Boolean CHARGES_USE_FEE = true; private static final String SELF_LINK = LICENSE_ID.toUrl(); @@ -132,7 +138,7 @@ public void testServiceOptions() throws Exception { @Test public void testModelAndRequests() throws Exception { Serializable[] objects = {DISK_TYPE_ID, DISK_TYPE, MACHINE_TYPE_ID, MACHINE_TYPE, REGION_ID, - REGION, ZONE_ID, ZONE, LICENSE_ID, LICENSE}; + REGION, ZONE_ID, ZONE, LICENSE_ID, LICENSE, DEPRECATION_STATUS}; for (Serializable obj : objects) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java index 5722036db1b4..5c3ccdac86d0 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import org.junit.Test; @@ -48,6 +49,7 @@ public void testToAndFromUrl() { @Test public void testSetProjectId() { ZoneId zoneId = ZoneId.of(PROJECT, ZONE); + assertSame(zoneId, zoneId.setProjectId(PROJECT)); compareZoneId(zoneId, ZoneId.of(ZONE).setProjectId(PROJECT)); } From deef55c7067fbef56c3f9b894285b81cb4c35de5 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 23 Feb 2016 11:19:43 +0100 Subject: [PATCH 04/10] Migrate IDs from Long to BigInteger --- .../com/google/gcloud/compute/DiskType.java | 16 ++++------ .../google/gcloud/compute/MachineType.java | 16 ++++------ .../com/google/gcloud/compute/Region.java | 32 ++++++++----------- .../java/com/google/gcloud/compute/Zone.java | 30 ++++++++--------- .../google/gcloud/compute/DiskTypeTest.java | 4 ++- .../gcloud/compute/MachineTypeTest.java | 3 +- .../com/google/gcloud/compute/RegionTest.java | 3 +- .../gcloud/compute/SerializationTest.java | 3 +- .../com/google/gcloud/compute/ZoneTest.java | 3 +- 9 files changed, 51 insertions(+), 59 deletions(-) diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java index 48701cc2a3be..a9d12fc20813 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java @@ -48,7 +48,7 @@ public com.google.api.services.compute.model.DiskType apply(DiskType diskType) { private static final long serialVersionUID = -944042261695072026L; - private final Long id; + private final BigInteger id; private final DiskTypeId diskTypeId; private final String creationTimestamp; private final String description; @@ -59,7 +59,7 @@ public com.google.api.services.compute.model.DiskType apply(DiskType diskType) { static final class Builder { - private Long id; + private BigInteger id; private DiskTypeId diskTypeId; private String creationTimestamp; private String description; @@ -70,7 +70,7 @@ static final class Builder { private Builder() {} - Builder id(Long id) { + Builder id(BigInteger id) { this.id = id; return this; } @@ -145,7 +145,7 @@ public DiskTypeId diskTypeId() { /** * Returns an unique identifier for the disk type; defined by the service. */ - public Long id() { + public BigInteger id() { return id; } @@ -212,9 +212,7 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.DiskType toPb() { com.google.api.services.compute.model.DiskType diskTypePb = new com.google.api.services.compute.model.DiskType(); - if (id != null) { - diskTypePb.setId(BigInteger.valueOf(id)); - } + diskTypePb.setId(id); diskTypePb.setCreationTimestamp(creationTimestamp); diskTypePb.setDescription(description); diskTypePb.setValidDiskSize(validDiskSize); @@ -233,9 +231,7 @@ static Builder builder() { static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb) { Builder builder = builder(); - if (diskTypePb.getId() != null) { - builder.id(diskTypePb.getId().longValue()); - } + builder.id(diskTypePb.getId()); builder.creationTimestamp(diskTypePb.getCreationTimestamp()); builder.diskTypeId(DiskTypeId.fromUrl(diskTypePb.getSelfLink())); builder.description(diskTypePb.getDescription()); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java index 29581852f346..c6bb7780aba9 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java @@ -55,7 +55,7 @@ public com.google.api.services.compute.model.MachineType apply(MachineType type) private static final long serialVersionUID = -4210962597502860450L; private final MachineTypeId machineTypeId; - private final Long id; + private final BigInteger id; private final String creationTimestamp; private final String description; private final String selfLink; @@ -69,7 +69,7 @@ public com.google.api.services.compute.model.MachineType apply(MachineType type) static final class Builder { private MachineTypeId machineTypeId; - private Long id; + private BigInteger id; private String creationTimestamp; private String description; private String selfLink; @@ -87,7 +87,7 @@ Builder machineTypeId(MachineTypeId machineTypeId) { return this; } - Builder id(Long id) { + Builder id(BigInteger id) { this.id = id; return this; } @@ -166,7 +166,7 @@ public MachineTypeId machineTypeId() { /** * Returns an unique identifier for the machin type; defined by the service. */ - public Long id() { + public BigInteger id() { return id; } @@ -268,9 +268,7 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.MachineType toPb() { com.google.api.services.compute.model.MachineType machineTypePb = new com.google.api.services.compute.model.MachineType(); - if (id != null) { - machineTypePb.setId(BigInteger.valueOf(id)); - } + machineTypePb.setId(id); machineTypePb.setCreationTimestamp(creationTimestamp); machineTypePb.setName(machineTypeId.machineType()); machineTypePb.setDescription(description); @@ -302,9 +300,7 @@ static Builder builder() { static MachineType fromPb(com.google.api.services.compute.model.MachineType machineTypePb) { Builder builder = builder(); builder.machineTypeId(MachineTypeId.fromUrl(machineTypePb.getSelfLink())); - if (machineTypePb.getId() != null) { - builder.id(machineTypePb.getId().longValue()); - } + builder.id(machineTypePb.getId()); builder.creationTimestamp(machineTypePb.getCreationTimestamp()); builder.description(machineTypePb.getDescription()); builder.selfLink(machineTypePb.getSelfLink()); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java index 2195d79244ef..b6e490482396 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java @@ -50,6 +50,15 @@ public com.google.api.services.compute.model.Region apply(Region region) { private static final long serialVersionUID = -3578710133393645135L; + private final RegionId regionId; + private final BigInteger id; + private final String creationTimestamp; + private final String description; + private final String selfLink; + private final Status status; + private final List zones; + private final List quotas; + /** * Status of the region. */ @@ -141,19 +150,10 @@ static Quota fromPb(com.google.api.services.compute.model.Quota quotaPb) { } } - private final RegionId regionId; - private final Long id; - private final String creationTimestamp; - private final String description; - private final String selfLink; - private final Status status; - private final List zones; - private final List quotas; - static final class Builder { private RegionId regionId; - private Long id; + private BigInteger id; private String creationTimestamp; private String description; private String selfLink; @@ -168,7 +168,7 @@ Builder regionId(RegionId regionId) { return this; } - Builder id(Long id) { + Builder id(BigInteger id) { this.id = id; return this; } @@ -223,7 +223,7 @@ public RegionId regionId() { return regionId; } - public Long id() { + public BigInteger id() { return id; } @@ -278,9 +278,7 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.Region toPb() { com.google.api.services.compute.model.Region regionPb = new com.google.api.services.compute.model.Region(); - if (id != null) { - regionPb.setId(BigInteger.valueOf(id)); - } + regionPb.setId(id); regionPb.setCreationTimestamp(creationTimestamp); regionPb.setName(regionId.region()); regionPb.setDescription(description); @@ -302,9 +300,7 @@ static Builder builder() { static Region fromPb(com.google.api.services.compute.model.Region regionPb) { Builder builder = builder(); builder.regionId(RegionId.fromUrl(regionPb.getSelfLink())); - if (regionPb.getId() != null) { - builder.id(regionPb.getId().longValue()); - } + builder.id(regionPb.getId()); builder.creationTimestamp(regionPb.getCreationTimestamp()); builder.description(regionPb.getDescription()); builder.selfLink(regionPb.getSelfLink()); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java index 87bf085cf047..bb494477bf53 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java @@ -50,6 +50,15 @@ public com.google.api.services.compute.model.Zone apply(Zone region) { private static final long serialVersionUID = 6113636504417213010L; + private final ZoneId zoneId; + private final BigInteger id; + private final String creationTimestamp; + private final String description; + private final String selfLink; + private final Status status; + private final List maintenanceWindows; + private final RegionId region; + /** * Status of the region. */ @@ -166,19 +175,10 @@ static MaintenanceWindow fromPb(MaintenanceWindows windowPb) { } } - private final ZoneId zoneId; - private final Long id; - private final String creationTimestamp; - private final String description; - private final String selfLink; - private final Status status; - private final List maintenanceWindows; - private final RegionId region; - static final class Builder { private ZoneId zoneId; - private Long id; + private BigInteger id; private String creationTimestamp; private String description; private String selfLink; @@ -193,7 +193,7 @@ Builder zoneId(ZoneId zoneId) { return this; } - Builder id(Long id) { + Builder id(BigInteger id) { this.id = id; return this; } @@ -270,7 +270,7 @@ public String description() { /** * Returns an unique identifier for the zone; defined by the service. */ - public Long id() { + public BigInteger id() { return id; } @@ -333,7 +333,7 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.Zone toPb() { com.google.api.services.compute.model.Zone zonePb = new com.google.api.services.compute.model.Zone(); - zonePb.setId(BigInteger.valueOf(id)); + zonePb.setId(id); zonePb.setCreationTimestamp(creationTimestamp); zonePb.setName(zoneId.zone()); zonePb.setDescription(description); @@ -356,9 +356,7 @@ static Builder builder() { static Zone fromPb(com.google.api.services.compute.model.Zone zonePb) { Builder builder = builder(); builder.zoneId(ZoneId.fromUrl(zonePb.getSelfLink())); - if (zonePb.getId() != null) { - builder.id(zonePb.getId().longValue()); - } + builder.id(zonePb.getId()); builder.creationTimestamp(zonePb.getCreationTimestamp()); builder.description(zonePb.getDescription()); builder.selfLink(zonePb.getSelfLink()); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java index 5d486c78cd5c..02dc92d85e93 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java @@ -20,9 +20,11 @@ import org.junit.Test; +import java.math.BigInteger; + public class DiskTypeTest { - private static final Long ID = 42L; + private static final BigInteger ID = BigInteger.valueOf(42L); private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; private static final String DESCRIPTION = "description"; private static final String VALID_DISK_SIZE = "10GB-10TB"; diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java index 285f4939c5fd..8c035a15a3c6 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java @@ -22,11 +22,12 @@ import org.junit.Test; +import java.math.BigInteger; import java.util.List; public class MachineTypeTest { - private static final Long ID = 42L; + private static final BigInteger ID = BigInteger.valueOf(42L); private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; private static final String DESCRIPTION = "description"; private static final MachineTypeId MACHINE_TYPE_ID = MachineTypeId.of("project", "zone", "type"); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java index 45b93730d84f..fafd80884c7d 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java @@ -22,12 +22,13 @@ import org.junit.Test; +import java.math.BigInteger; import java.util.List; public class RegionTest { private static final RegionId REGION_ID = RegionId.of("project", "region"); - private static final Long ID = 42L; + private static final BigInteger ID = BigInteger.valueOf(42L); private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; private static final String DESCRIPTION = "description"; private static final Region.Status STATUS = Region.Status.DOWN; diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java index 4ef16bbc0181..04f52d9aea17 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java @@ -32,11 +32,12 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.math.BigInteger; import java.util.List; public class SerializationTest { - private static final Long ID = 42L; + private static final BigInteger ID = BigInteger.valueOf(42L); private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; private static final String DESCRIPTION = "description"; private static final String VALID_DISK_SIZE = "10GB-10TB"; diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java index 02ab2574cd70..f0020197d699 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java @@ -23,13 +23,14 @@ import org.junit.Test; +import java.math.BigInteger; import java.util.List; public class ZoneTest { private static final ZoneId ZONE_ID = ZoneId.of("project", "zone"); private static final RegionId REGION_ID = RegionId.of("project", "region"); - private static final Long ID = 42L; + private static final BigInteger ID = BigInteger.valueOf(42L); private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; private static final String DESCRIPTION = "description"; private static final Zone.Status STATUS = Zone.Status.DOWN; From ea5f070df848002315fb368e228664bc731c8cdc Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 24 Feb 2016 11:43:14 +0100 Subject: [PATCH 05/10] Add deprecation status to zone and region --- .../gcloud/compute/DeprecationStatus.java | 8 +-- .../com/google/gcloud/compute/Region.java | 51 +++++++++++++++++++ .../com/google/gcloud/compute/RegionId.java | 14 +++++ .../java/com/google/gcloud/compute/Zone.java | 25 +++++++++ .../gcloud/compute/DeprecationStatusTest.java | 4 +- .../com/google/gcloud/compute/RegionTest.java | 8 +++ .../com/google/gcloud/compute/ZoneTest.java | 8 +++ 7 files changed, 113 insertions(+), 5 deletions(-) diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java index 7b16107c9210..1e8c5f98c7e6 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java @@ -40,17 +40,19 @@ public final class DeprecationStatus implements Serializab */ public enum Status { /** - * Operations that create Google Compute Engine entity using a deprecated resource will return + * Operations that create a Google Compute Engine entity using a deprecated resource will return * successfully but with a warning indicating the deprecation and suggesting a replacement. */ DEPRECATED, + /** - * Operations that create Google Compute Engine entity using an obsolete resource will be + * Operations that create a Google Compute Engine entity using an obsolete resource will be * rejected and result in an error. */ OBSOLETE, + /** - * Operations that create Google Compute Engine entity using a deleted resource will be + * Operations that create a Google Compute Engine entity using a deleted resource will be * rejected and result in an error. */ DELETED diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java index b6e490482396..abc8233ac04e 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java @@ -58,6 +58,7 @@ public com.google.api.services.compute.model.Region apply(Region region) { private final Status status; private final List zones; private final List quotas; + private final DeprecationStatus deprecationStatus; /** * Status of the region. @@ -160,6 +161,7 @@ static final class Builder { private Status status; private List zones; private List quotas; + private DeprecationStatus deprecationStatus; private Builder() {} @@ -203,6 +205,11 @@ Builder quotas(List quotas) { return this; } + Builder deprecationStatus(DeprecationStatus deprecationStatus) { + this.deprecationStatus = deprecationStatus; + return this; + } + Region build() { return new Region(this); } @@ -217,40 +224,76 @@ private Region(Builder builder) { this.status = builder.status; this.zones = builder.zones; this.quotas = builder.quotas; + this.deprecationStatus = builder.deprecationStatus; } + /** + * Returns the region's identity. + */ public RegionId regionId() { return regionId; } + /** + * Returns an unique identifier for the region; defined by the service. + */ public BigInteger id() { return id; } + /** + * Returns the creation timestamp in RFC3339 text format. + * + * @see RFC3339 + */ public String creationTimestamp() { return creationTimestamp; } + /** + * Returns an optional textual description of the region. + */ public String description() { return description; } + /** + * Returns a service-defined URL for the region. + */ public String selfLink() { return selfLink; } + /** + * Returns the status of the status. + */ public Status status() { return status; } + /** + * Returns a list of identities of zones available in this region. + */ public List zones() { return zones; } + /** + * Returns quotas assigned to this region. + */ public List quotas() { return quotas; } + /** + * Returns the deprecation status of the region. If {@link DeprecationStatus#status()} is either + * {@link DeprecationStatus.Status#DELETED} or {@link DeprecationStatus.Status#OBSOLETE} the + * region should not be used. Returns {@code null} if the region is not deprecated. + */ + public DeprecationStatus deprecationStatus() { + return deprecationStatus; + } + @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -262,6 +305,7 @@ public String toString() { .add("status", status) .add("zones", zones) .add("quotas", quotas) + .add("deprecationStatus", deprecationStatus) .toString(); } @@ -290,6 +334,9 @@ com.google.api.services.compute.model.Region toPb() { if (quotas != null) { regionPb.setQuotas(Lists.transform(quotas, Quota.TO_PB_FUNCTION)); } + if (deprecationStatus != null) { + regionPb.setDeprecated(deprecationStatus.toPb()); + } return regionPb; } @@ -313,6 +360,10 @@ static Region fromPb(com.google.api.services.compute.model.Region regionPb) { if (regionPb.getQuotas() != null) { builder.quotas(Lists.transform(regionPb.getQuotas(), Quota.FROM_PB_FUNCTION)); } + if (regionPb.getDeprecated() != null) { + builder.deprecationStatus( + DeprecationStatus.fromPb(regionPb.getDeprecated(), RegionId.FROM_URL_FUNCTION)); + } return builder.build(); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java index 09c81941ee43..b172b0d46794 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Function; import com.google.common.base.MoreObjects.ToStringHelper; import java.util.Objects; @@ -27,6 +28,19 @@ */ public final class RegionId extends ResourceId { + static final Function FROM_URL_FUNCTION = new Function() { + @Override + public RegionId apply(String pb) { + return RegionId.fromUrl(pb); + } + }; + static final Function TO_URL_FUNCTION = new Function() { + @Override + public String apply(RegionId regionId) { + return regionId.toUrl(); + } + }; + private static final long serialVersionUID = 5569092266957249294L; private final String region; diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java index bb494477bf53..fb8d974dc618 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java @@ -58,6 +58,7 @@ public com.google.api.services.compute.model.Zone apply(Zone region) { private final Status status; private final List maintenanceWindows; private final RegionId region; + private final DeprecationStatus deprecationStatus; /** * Status of the region. @@ -185,6 +186,7 @@ static final class Builder { private Status status; private List maintenanceWindows; private RegionId region; + private DeprecationStatus deprecationStatus; private Builder() {} @@ -228,6 +230,11 @@ Builder region(RegionId region) { return this; } + Builder deprecationStatus(DeprecationStatus deprecationStatus) { + this.deprecationStatus = deprecationStatus; + return this; + } + Zone build() { return new Zone(this); } @@ -242,6 +249,7 @@ private Zone(Builder builder) { this.status = builder.status; this.maintenanceWindows = builder.maintenanceWindows; this.region = builder.region; + this.deprecationStatus = builder.deprecationStatus; } /** @@ -306,6 +314,15 @@ public RegionId region() { return region; } + /** + * Returns the deprecation status of the zone. If {@link DeprecationStatus#status()} is either + * {@link DeprecationStatus.Status#DELETED} or {@link DeprecationStatus.Status#OBSOLETE} the zone + * should not be used. Returns {@code null} if the zone is not deprecated. + */ + public DeprecationStatus deprecationStatus() { + return deprecationStatus; + } + @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -317,6 +334,7 @@ public String toString() { .add("status", status) .add("maintenanceWindows", maintenanceWindows) .add("region", region) + .add("deprecationStatus", deprecationStatus) .toString(); } @@ -346,6 +364,9 @@ com.google.api.services.compute.model.Zone toPb() { if (region != null) { zonePb.setRegion(region.toUrl()); } + if (deprecationStatus != null) { + zonePb.setDeprecated(deprecationStatus.toPb()); + } return zonePb; } @@ -370,6 +391,10 @@ static Zone fromPb(com.google.api.services.compute.model.Zone zonePb) { if (zonePb.getRegion() != null) { builder.region(RegionId.fromUrl(zonePb.getRegion())); } + if (zonePb.getDeprecated() != null) { + builder.deprecationStatus( + DeprecationStatus.fromPb(zonePb.getDeprecated(), ZoneId.FROM_URL_FUNCTION)); + } return builder.build(); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java index 32bda09a5bdd..2b7ec22e36bf 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java @@ -27,9 +27,9 @@ public class DeprecationStatusTest { private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; - private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone","diskType"); + private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType"); private static final MachineTypeId MACHINE_TYPE_ID = - MachineTypeId.of("project", "zone","machineType"); + MachineTypeId.of("project", "zone", "machineType"); private static final Status STATUS = Status.DELETED; private static final DeprecationStatus DISK_TYPE_STATUS = new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, DISK_TYPE_ID, STATUS); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java index fafd80884c7d..89deed2f1444 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java @@ -40,6 +40,11 @@ public class RegionTest { private static final Region.Quota QUOTA2 = new Region.Quota("METRIC2", 4, 3); private static final List QUOTAS = ImmutableList.of(QUOTA1, QUOTA2); + private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; + private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; + private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final DeprecationStatus DEPRECATION_STATUS = new DeprecationStatus<>( + DELETED, DEPRECATED, OBSOLETE, REGION_ID, DeprecationStatus.Status.DELETED); private static final Region REGION = Region.builder() .regionId(REGION_ID) .id(ID) @@ -49,6 +54,7 @@ public class RegionTest { .status(STATUS) .zones(ZONES) .quotas(QUOTAS) + .deprecationStatus(DEPRECATION_STATUS) .build(); @Test @@ -61,6 +67,7 @@ public void testBuilder() { assertEquals(STATUS, REGION.status()); assertEquals(ZONES, REGION.zones()); assertEquals(QUOTAS, REGION.quotas()); + assertEquals(DEPRECATION_STATUS, REGION.deprecationStatus()); } @Test @@ -81,6 +88,7 @@ private void compareRegions(Region expected, Region value) { assertEquals(expected.status(), value.status()); assertEquals(expected.zones(), value.zones()); assertEquals(expected.quotas(), value.quotas()); + assertEquals(expected.deprecationStatus(), value.deprecationStatus()); assertEquals(expected.hashCode(), value.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java index f0020197d699..3fa2b228382a 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java @@ -39,6 +39,11 @@ public class ZoneTest { private static final MaintenanceWindow WINDOW2 = new MaintenanceWindow("NAME2", "DESCRIPTION2", "2016-01-21T04:39:00.210-08:00", "2016-01-22T04:39:00.210-08:00"); private static final List WINDOWS = ImmutableList.of(WINDOW1, WINDOW2); + private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; + private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; + private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final DeprecationStatus DEPRECATION_STATUS = new DeprecationStatus<>( + DELETED, DEPRECATED, OBSOLETE, ZONE_ID, DeprecationStatus.Status.DELETED); private static final Zone ZONE = Zone.builder() .zoneId(ZONE_ID) .id(ID) @@ -47,6 +52,7 @@ public class ZoneTest { .selfLink(ZONE_ID.toUrl()) .status(STATUS) .maintenanceWindows(WINDOWS) + .deprecationStatus(DEPRECATION_STATUS) .region(REGION_ID) .build(); @@ -60,6 +66,7 @@ public void testBuilder() { assertEquals(STATUS, ZONE.status()); assertEquals(WINDOWS, ZONE.maintenanceWindows()); assertEquals(REGION_ID, ZONE.region()); + assertEquals(DEPRECATION_STATUS, ZONE.deprecationStatus()); } @Test @@ -82,6 +89,7 @@ private void compareZones(Zone expected, Zone value) { assertEquals(expected.status(), value.status()); assertEquals(expected.maintenanceWindows(), value.maintenanceWindows()); assertEquals(expected.region(), value.region()); + assertEquals(expected.deprecationStatus(), value.deprecationStatus()); assertEquals(expected.hashCode(), value.hashCode()); } } From 0940f3172732aea71fc7e683b538a3004d17f51d Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 24 Feb 2016 12:03:19 +0100 Subject: [PATCH 06/10] Add test for missing fields, fix NPE, minor docs and lint fixes --- gcloud-java-compute/README.md | 3 ++- .../src/main/java/com/google/gcloud/compute/DiskTypeId.java | 4 ++-- .../src/main/java/com/google/gcloud/compute/MachineType.java | 2 +- .../src/main/java/com/google/gcloud/compute/Region.java | 4 +++- .../src/main/java/com/google/gcloud/compute/Zone.java | 4 +++- .../src/test/java/com/google/gcloud/compute/DiskTypeTest.java | 1 - .../test/java/com/google/gcloud/compute/MachineTypeTest.java | 1 - .../src/test/java/com/google/gcloud/compute/RegionTest.java | 2 ++ .../src/test/java/com/google/gcloud/compute/ZoneTest.java | 2 ++ 9 files changed, 15 insertions(+), 8 deletions(-) diff --git a/gcloud-java-compute/README.md b/gcloud-java-compute/README.md index 653a586d76e6..d1909f904095 100644 --- a/gcloud-java-compute/README.md +++ b/gcloud-java-compute/README.md @@ -10,7 +10,8 @@ Java idiomatic client for [Google Cloud Compute] (https://cloud.google.com/compu [![Dependency Status](https://www.versioneye.com/user/projects/56bd8ee72a29ed002d2b0969/badge.svg?style=flat)](https://www.versioneye.com/user/projects/56bd8ee72a29ed002d2b0969) - [Homepage] (https://googlecloudplatform.github.io/gcloud-java/) - + + > Note: This client is a work-in-progress, and may occasionally > make backwards-incompatible changes. diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java index dfaa58b6f481..93ec44b51acf 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java @@ -99,14 +99,14 @@ public static DiskTypeId of(ZoneId zoneId, String diskType) { } /** - * Returns a disk type identity given the zone disk and disk type. + * Returns a disk type identity given the zone and disk type names. */ public static DiskTypeId of(String zone, String diskType) { return of(ZoneId.of(null, zone), diskType); } /** - * Returns a disk type identity given project disk, zone disk and disk type. + * Returns a disk type identity given project disk, zone and disk type names. */ public static DiskTypeId of(String project, String zone, String diskType) { return of(ZoneId.of(project, zone), diskType); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java index c6bb7780aba9..e8ac54094bf8 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java @@ -164,7 +164,7 @@ public MachineTypeId machineTypeId() { } /** - * Returns an unique identifier for the machin type; defined by the service. + * Returns an unique identifier for the machine type; defined by the service. */ public BigInteger id() { return id; diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java index abc8233ac04e..8edfd869a2b1 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java @@ -327,7 +327,9 @@ com.google.api.services.compute.model.Region toPb() { regionPb.setName(regionId.region()); regionPb.setDescription(description); regionPb.setSelfLink(selfLink); - regionPb.setStatus(status.name()); + if (status != null) { + regionPb.setStatus(status.name()); + } if (zones != null) { regionPb.setZones(Lists.transform(zones, ZoneId.TO_URL_FUNCTION)); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java index fb8d974dc618..073f7f624dae 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java @@ -356,7 +356,9 @@ com.google.api.services.compute.model.Zone toPb() { zonePb.setName(zoneId.zone()); zonePb.setDescription(description); zonePb.setSelfLink(selfLink); - zonePb.setStatus(status.name()); + if (status != null) { + zonePb.setStatus(status.name()); + } if (maintenanceWindows != null) { zonePb.setMaintenanceWindows( Lists.transform(maintenanceWindows, MaintenanceWindow.TO_PB_FUNCTION)); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java index 02dc92d85e93..bbd3ad5207d3 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java @@ -63,7 +63,6 @@ public void testBuilder() { public void testToPbAndFromPb() { compareDiskTypes(DISK_TYPE, DiskType.fromPb(DISK_TYPE.toPb())); DiskType diskType = DiskType.builder() - .id(ID) .diskTypeId(DISK_TYPE_ID) .selfLink(DISK_TYPE_ID.toUrl()) .build(); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java index 8c035a15a3c6..fffacf28f6e9 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java @@ -75,7 +75,6 @@ public void testBuilder() { public void testToPbAndFromPb() { compareMachineTypes(MACHINE_TYPE, MachineType.fromPb(MACHINE_TYPE.toPb())); MachineType machineType = MachineType.builder() - .id(ID) .machineTypeId(MACHINE_TYPE_ID) .selfLink(MACHINE_TYPE_ID.toUrl()) .build(); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java index 89deed2f1444..94ca163f5b30 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java @@ -76,6 +76,8 @@ public void testToAndFromPb() { compareRegions(REGION, region); assertEquals(REGION_ID.project(), region.regionId().project()); assertEquals(REGION_ID.region(), region.regionId().region()); + region = Region.builder().regionId(REGION_ID).selfLink(REGION_ID.toUrl()).build(); + compareRegions(region, Region.fromPb(region.toPb())); } private void compareRegions(Region expected, Region value) { diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java index 3fa2b228382a..30cefd6b380a 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java @@ -77,6 +77,8 @@ public void testToAndFromPb() { compareZones(ZONE, zone); assertEquals(ZONE_ID.project(), zone.zoneId().project()); assertEquals(ZONE_ID.zone(), zone.zoneId().zone()); + zone = Zone.builder().zoneId(ZONE_ID).selfLink(ZONE_ID.toUrl()).build(); + compareZones(zone, Zone.fromPb(zone.toPb())); } private void compareZones(Zone expected, Zone value) { From 046592606f298d2aa3a54df4f1f7e7eb50ab2ac6 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 24 Feb 2016 12:19:22 +0100 Subject: [PATCH 07/10] Add matchesUrl method to identity objects. Add tests --- .../com/google/gcloud/compute/DiskTypeId.java | 12 ++++++++++++ .../com/google/gcloud/compute/LicenseId.java | 12 ++++++++++++ .../com/google/gcloud/compute/MachineTypeId.java | 12 ++++++++++++ .../java/com/google/gcloud/compute/RegionId.java | 11 ++++++++++- .../google/gcloud/compute/RegionResourceId.java | 1 + .../com/google/gcloud/compute/ResourceId.java | 2 ++ .../java/com/google/gcloud/compute/ZoneId.java | 11 ++++++++++- .../google/gcloud/compute/ZoneResourceId.java | 1 + .../google/gcloud/compute/DiskTypeIdTest.java | 16 ++++++++++++++++ .../com/google/gcloud/compute/LicenseIdTest.java | 16 ++++++++++++++++ .../google/gcloud/compute/MachineTypeIdTest.java | 16 ++++++++++++++++ .../com/google/gcloud/compute/RegionIdTest.java | 16 ++++++++++++++++ .../com/google/gcloud/compute/ZoneIdTest.java | 16 ++++++++++++++++ 13 files changed, 140 insertions(+), 2 deletions(-) diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java index 93ec44b51acf..c338dfeb10f1 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java @@ -41,6 +41,7 @@ public String apply(DiskTypeId diskTypeId) { } }; + static final String REGEX = ZoneResourceId.REGEX + "diskTypes/[^/]+"; private static final long serialVersionUID = 7337881474103686219L; private final String diskType; @@ -112,7 +113,18 @@ public static DiskTypeId of(String project, String zone, String diskType) { return of(ZoneId.of(project, zone), diskType); } + /** + * Returns {@code true} if the provided string matches the expected format of a disk type URL. + * Returns {@code false} otherwise. + */ + static boolean matchesUrl(String url) { + return url.matches(REGEX); + } + static DiskTypeId fromUrl(String url) { + if (!matchesUrl(url)) { + throw new IllegalArgumentException(url + " is not a valid disk type URL"); + } int projectsIndex = url.indexOf("/projects/"); int zonesIndex = url.indexOf("/zones/"); int diskTypesIndex = url.indexOf("/diskTypes/"); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java index 2fd2b5eb3058..9bbeea639dc4 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java @@ -41,6 +41,7 @@ public String apply(LicenseId licenseId) { } }; + static final String REGEX = ResourceId.REGEX + "global/licenses/[^/]+"; private static final long serialVersionUID = -2239484554024469651L; private final String license; @@ -105,7 +106,18 @@ public static LicenseId of(String project, String license) { return new LicenseId(project, license); } + /** + * Returns {@code true} if the provided string matches the expected format of a license URL. + * Returns {@code false} otherwise. + */ + static boolean matchesUrl(String url) { + return url.matches(REGEX); + } + static LicenseId fromUrl(String url) { + if (!matchesUrl(url)) { + throw new IllegalArgumentException(url + " is not a valid license URL"); + } int projectsIndex = url.indexOf("/projects/"); int licensesIndex = url.indexOf("/global/licenses/"); String project = url.substring(projectsIndex + 10, licensesIndex); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java index 04073863b814..11428f700481 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java @@ -43,6 +43,7 @@ public String apply(MachineTypeId machineTypeId) { } }; + static final String REGEX = ZoneResourceId.REGEX + "machineTypes/[^/]+"; private static final long serialVersionUID = -5819598544478859608L; private final String machineType; @@ -101,7 +102,18 @@ public static MachineTypeId of(String project, String zone, String machineType) return new MachineTypeId(project, zone, machineType); } + /** + * Returns {@code true} if the provided string matches the expected format of a machine type URL. + * Returns {@code false} otherwise. + */ + static boolean matchesUrl(String url) { + return url.matches(REGEX); + } + static MachineTypeId fromUrl(String url) { + if (!matchesUrl(url)) { + throw new IllegalArgumentException(url + " is not a valid machine type URL"); + } int projectsIndex = url.indexOf("/projects/"); int zonesIndex = url.indexOf("/zones/"); int machineTypesIndex = url.indexOf("/machineTypes/"); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java index b172b0d46794..97d31ba09d99 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java @@ -41,6 +41,7 @@ public String apply(RegionId regionId) { } }; + static final String REGEX = ResourceId.REGEX + "regions/[^/]+"; private static final long serialVersionUID = 5569092266957249294L; private final String region; @@ -105,9 +106,17 @@ public static RegionId of(String region) { } /** - * Returns a new region identity given a region URL. + * Returns {@code true} if the provided string matches the expected format of a region URL. + * Returns {@code false} otherwise. */ + static boolean matchesUrl(String url) { + return url.matches(REGEX); + } + static RegionId fromUrl(String url) { + if (!matchesUrl(url)) { + throw new IllegalArgumentException(url + " is not a valid region URL"); + } int projectsIndex = url.indexOf("/projects/"); int regionsIndex = url.indexOf("/regions/"); String project = url.substring(projectsIndex + 10, regionsIndex); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java index 618ae0a9a4e1..63193f54833b 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java @@ -27,6 +27,7 @@ */ public abstract class RegionResourceId extends ResourceId { + static final String REGEX = ResourceId.REGEX + "regions/[^/]+/"; private static final long serialVersionUID = 5569092266957249294L; private final String region; diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java index dc69d6ae53b4..aa25c4c2b589 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java @@ -26,6 +26,8 @@ */ public abstract class ResourceId implements Serializable { + static final String REGEX = + "(https?://(www|content).googleapis.com/compute/v1/)?projects/[^/]+/"; private static final long serialVersionUID = -8028734746870421573L; private String project; diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java index 14e38c22cad7..bbf55a1c1062 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java @@ -41,6 +41,7 @@ public String apply(ZoneId zoneId) { } }; + static final String REGEX = ResourceId.REGEX + "zones/[^/]+"; private static final long serialVersionUID = -7635391994812946733L; private final String zone; @@ -100,9 +101,17 @@ public static ZoneId of(String zone) { } /** - * Returns a new zone identity given a zone URL. + * Returns {@code true} if the provided string matches the expected format of a zone URL. + * Returns {@code false} otherwise. */ + static boolean matchesUrl(String url) { + return url.matches(REGEX); + } + static ZoneId fromUrl(String url) { + if (!matchesUrl(url)) { + throw new IllegalArgumentException(url + " is not a valid zone URL"); + } int projectsIndex = url.indexOf("/projects/"); int zonesIndex = url.indexOf("/zones/"); String project = url.substring(projectsIndex + 10, zonesIndex); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java index 961ca7cd499b..576ae31aed0c 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java @@ -27,6 +27,7 @@ */ public abstract class ZoneResourceId extends ResourceId { + static final String REGEX = ResourceId.REGEX + "zones/[^/]+/"; private static final long serialVersionUID = -6249546895344926888L; private final String zone; diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java index 0555f7c1d570..43be10eb31a3 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java @@ -17,10 +17,14 @@ package com.google.gcloud.compute; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class DiskTypeIdTest { @@ -30,6 +34,9 @@ public class DiskTypeIdTest { private static final String URL = "https://www.googleapis.com/compute/v1/projects/project/zones/zone/diskTypes/diskType"; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testOf() { DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); @@ -48,6 +55,9 @@ public void testToAndFromUrl() { DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); assertSame(diskTypeId, diskTypeId.setProjectId(PROJECT)); compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.toUrl())); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("notMatchingUrl is not a valid disk type URL"); + diskTypeId = DiskTypeId.fromUrl("notMatchingUrl"); } @Test @@ -57,6 +67,12 @@ public void testSetProjectId() { compareDiskTypeId(diskTypeId, DiskTypeId.of(ZONE, DISK_TYPE).setProjectId(PROJECT)); } + @Test + public void testMatchesUrl() { + assertTrue(DiskTypeId.matchesUrl(DiskTypeId.of(PROJECT, ZONE, DISK_TYPE).toUrl())); + assertFalse(DiskTypeId.matchesUrl("notMatchingUrl")); + } + private void compareDiskTypeId(DiskTypeId expected, DiskTypeId value) { assertEquals(expected, value); assertEquals(expected.project(), expected.project()); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java index eb5c40849956..d3632603b030 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java @@ -17,10 +17,14 @@ package com.google.gcloud.compute; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class LicenseIdTest { @@ -29,6 +33,9 @@ public class LicenseIdTest { private static final String URL = "https://www.googleapis.com/compute/v1/projects/project/global/licenses/license"; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testOf() { LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); @@ -51,6 +58,15 @@ public void testSetProjectId() { LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); assertSame(licenseId, licenseId.setProjectId(PROJECT)); compareLicenseId(licenseId, LicenseId.of(LICENSE).setProjectId(PROJECT)); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("notMatchingUrl is not a valid license URL"); + licenseId = LicenseId.fromUrl("notMatchingUrl"); + } + + @Test + public void testMatchesUrl() { + assertTrue(LicenseId.matchesUrl(LicenseId.of(PROJECT, LICENSE).toUrl())); + assertFalse(LicenseId.matchesUrl("notMatchingUrl")); } private void compareLicenseId(LicenseId expected, LicenseId value) { diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java index 34241fb9e3d1..bddb9c21f67a 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java @@ -17,10 +17,14 @@ package com.google.gcloud.compute; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class MachineTypeIdTest { @@ -30,6 +34,9 @@ public class MachineTypeIdTest { private static final String URL = "https://www.googleapis.com/compute/v1/projects/project/zones/zone/machineTypes/type"; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testOf() { MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE); @@ -47,6 +54,9 @@ public void testOf() { public void testToAndFromUrl() { MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE); compareMachineTypeId(machineTypeId, MachineTypeId.fromUrl(machineTypeId.toUrl())); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("notMatchingUrl is not a valid machine type URL"); + machineTypeId = MachineTypeId.fromUrl("notMatchingUrl"); } @Test @@ -56,6 +66,12 @@ public void testSetProjectId() { compareMachineTypeId(machineTypeId, MachineTypeId.of(ZONE, TYPE).setProjectId(PROJECT)); } + @Test + public void testMatchesUrl() { + assertTrue(MachineTypeId.matchesUrl(MachineTypeId.of(PROJECT, ZONE, TYPE).toUrl())); + assertFalse(MachineTypeId.matchesUrl("notMatchingUrl")); + } + private void compareMachineTypeId(MachineTypeId expected, MachineTypeId value) { assertEquals(expected, value); assertEquals(expected.project(), expected.project()); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java index 74d08aa27ab6..adb94aee08fc 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java @@ -17,10 +17,14 @@ package com.google.gcloud.compute; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class RegionIdTest { @@ -29,6 +33,9 @@ public class RegionIdTest { private static final String URL = "https://www.googleapis.com/compute/v1/projects/project/regions/region"; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testOf() { RegionId regionId = RegionId.of(PROJECT, REGION); @@ -51,6 +58,15 @@ public void testSetProjectId() { RegionId regionId = RegionId.of(PROJECT, REGION); assertSame(regionId, regionId.setProjectId(PROJECT)); compareRegionId(regionId, RegionId.of(REGION).setProjectId(PROJECT)); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("notMatchingUrl is not a valid region URL"); + regionId = RegionId.fromUrl("notMatchingUrl"); + } + + @Test + public void testMatchesUrl() { + assertTrue(RegionId.matchesUrl(RegionId.of(PROJECT, REGION).toUrl())); + assertFalse(RegionId.matchesUrl("notMatchingUrl")); } private void compareRegionId(RegionId expected, RegionId value) { diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java index 5c3ccdac86d0..0583e454690d 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java @@ -17,10 +17,14 @@ package com.google.gcloud.compute; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class ZoneIdTest { @@ -29,6 +33,9 @@ public class ZoneIdTest { private static final String URL = "https://www.googleapis.com/compute/v1/projects/project/zones/zone"; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testOf() { ZoneId zoneId = ZoneId.of(PROJECT, ZONE); @@ -51,6 +58,15 @@ public void testSetProjectId() { ZoneId zoneId = ZoneId.of(PROJECT, ZONE); assertSame(zoneId, zoneId.setProjectId(PROJECT)); compareZoneId(zoneId, ZoneId.of(ZONE).setProjectId(PROJECT)); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("notMatchingUrl is not a valid zone URL"); + zoneId = ZoneId.fromUrl("notMatchingUrl"); + } + + @Test + public void testMatchesUrl() { + assertTrue(ZoneId.matchesUrl(ZoneId.of(PROJECT, ZONE).toUrl())); + assertFalse(ZoneId.matchesUrl("notMatchingUrl")); } private void compareZoneId(ZoneId expected, ZoneId value) { From 9d485286cf64aaff759c497fdf29d0316c1b66a4 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 29 Feb 2016 16:42:46 +0100 Subject: [PATCH 08/10] Refactor Compute immutable resources' identities and classes - Use Pattern and Matcher to parse URLs into identities - Make REGEX private in identity classes - Remove non-necessary javadoc - Change timestamps' type from String to Long - Change id's type from Long to String --- .../gcloud/compute/DeprecationStatus.java | 61 ++++++------ .../com/google/gcloud/compute/DiskType.java | 59 +++++------ .../com/google/gcloud/compute/DiskTypeId.java | 38 +++---- .../com/google/gcloud/compute/License.java | 17 +--- .../com/google/gcloud/compute/LicenseId.java | 34 +++---- .../google/gcloud/compute/MachineType.java | 99 +++++++++---------- .../google/gcloud/compute/MachineTypeId.java | 30 +++--- .../com/google/gcloud/compute/Region.java | 66 ++++++------- .../com/google/gcloud/compute/RegionId.java | 26 ++--- .../gcloud/compute/RegionResourceId.java | 18 ++-- .../com/google/gcloud/compute/ResourceId.java | 19 ++-- .../java/com/google/gcloud/compute/Zone.java | 88 ++++++++--------- .../com/google/gcloud/compute/ZoneId.java | 24 ++--- .../google/gcloud/compute/ZoneResourceId.java | 18 ++-- .../gcloud/compute/DeprecationStatusTest.java | 6 +- .../google/gcloud/compute/DiskTypeIdTest.java | 10 +- .../google/gcloud/compute/DiskTypeTest.java | 18 ++-- .../google/gcloud/compute/LicenseIdTest.java | 10 +- .../google/gcloud/compute/LicenseTest.java | 5 +- .../gcloud/compute/MachineTypeIdTest.java | 10 +- .../gcloud/compute/MachineTypeTest.java | 33 +++---- .../google/gcloud/compute/RegionIdTest.java | 10 +- .../com/google/gcloud/compute/RegionTest.java | 15 ++- .../gcloud/compute/SerializationTest.java | 28 +++--- .../com/google/gcloud/compute/ZoneIdTest.java | 10 +- .../com/google/gcloud/compute/ZoneTest.java | 23 +++-- 26 files changed, 353 insertions(+), 422 deletions(-) diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java index 1e8c5f98c7e6..20123d648598 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java @@ -16,6 +16,7 @@ package com.google.gcloud.compute; +import com.google.api.client.util.DateTime; import com.google.common.base.Function; import com.google.common.base.MoreObjects; @@ -24,14 +25,16 @@ /** * The deprecation status associated to a Google Compute Engine resource. + * + * @param The Google Compute Engine resource to which the deprecation status refers to. */ public final class DeprecationStatus implements Serializable { private static final long serialVersionUID = -2695077634793679794L; - private final String deleted; - private final String deprecated; - private final String obsolete; + private final Long deleted; + private final Long deprecated; + private final Long obsolete; private final T replacement; private final Status status; @@ -58,8 +61,7 @@ public enum Status { DELETED } - DeprecationStatus(String deleted, String deprecated, String obsolete, T replacement, - Status status) { + DeprecationStatus(Long deleted, Long deprecated, Long obsolete, T replacement, Status status) { this.deleted = deleted; this.deprecated = deprecated; this.obsolete = obsolete; @@ -68,32 +70,26 @@ public enum Status { } /** - * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource - * will be changed to {@link Status#DELETED}. - * - * @see RFC3339 + * Returns the timestamp on or after which the deprecation state of this resource will be changed + * to {@link Status#DELETED}. In milliseconds since epoch. */ - public String deleted() { + public Long deleted() { return deleted; } /** - * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource - * will be changed to {@link Status#DEPRECATED}. - * - * @see RFC3339 + * Returns the timestamp on or after which the deprecation state of this resource will be changed + * to {@link Status#DEPRECATED}. In milliseconds since epoch. */ - public String deprecated() { + public Long deprecated() { return deprecated; } /** - * Returns an optional RFC3339 timestamp on or after which the deprecation state of this resource - * will be changed to {@link Status#OBSOLETE}. - * - * @see RFC3339 + * Returns the timestamp on or after which the deprecation state of this resource will be changed + * to {@link Status#OBSOLETE}. In milliseconds since epoch. */ - public String obsolete() { + public Long obsolete() { return obsolete; } @@ -137,11 +133,17 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.DeprecationStatus toPb() { com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb = new com.google.api.services.compute.model.DeprecationStatus(); - deprecationStatusPb.setDeleted(deleted); - deprecationStatusPb.setDeprecated(deprecated); - deprecationStatusPb.setObsolete(obsolete); + if (deleted != null) { + deprecationStatusPb.setDeleted(new DateTime(deleted).toStringRfc3339()); + } + if (deprecated != null) { + deprecationStatusPb.setDeprecated(new DateTime(deprecated).toStringRfc3339()); + } + if (obsolete != null) { + deprecationStatusPb.setObsolete(new DateTime(obsolete).toStringRfc3339()); + } if (replacement != null) { - deprecationStatusPb.setReplacement(replacement.toUrl()); + deprecationStatusPb.setReplacement(replacement.selfLink()); } if (status() != null) { deprecationStatusPb.setState(status.name()); @@ -152,10 +154,13 @@ com.google.api.services.compute.model.DeprecationStatus toPb() { static DeprecationStatus fromPb( com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb, Function fromUrl) { - return new DeprecationStatus( - deprecationStatusPb.getDeleted(), - deprecationStatusPb.getDeprecated(), - deprecationStatusPb.getObsolete(), + return new DeprecationStatus<>( + deprecationStatusPb.getDeleted() != null + ? DateTime.parseRfc3339(deprecationStatusPb.getDeleted()).getValue() : null, + deprecationStatusPb.getDeprecated() != null + ? DateTime.parseRfc3339(deprecationStatusPb.getDeprecated()).getValue() : null, + deprecationStatusPb.getObsolete() != null + ? DateTime.parseRfc3339(deprecationStatusPb.getObsolete()).getValue() : null, deprecationStatusPb.getReplacement() != null ? fromUrl.apply(deprecationStatusPb.getReplacement()) : null, deprecationStatusPb.getState() != null diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java index a9d12fc20813..f25aa3ac65a4 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java @@ -16,6 +16,7 @@ package com.google.gcloud.compute; +import com.google.api.client.util.DateTime; import com.google.common.base.Function; import com.google.common.base.MoreObjects; @@ -48,34 +49,32 @@ public com.google.api.services.compute.model.DiskType apply(DiskType diskType) { private static final long serialVersionUID = -944042261695072026L; - private final BigInteger id; + private final String id; private final DiskTypeId diskTypeId; - private final String creationTimestamp; + private final Long creationTimestamp; private final String description; private final String validDiskSize; - private final String selfLink; private final Long defaultDiskSizeGb; private final DeprecationStatus deprecationStatus; static final class Builder { - private BigInteger id; + private String id; private DiskTypeId diskTypeId; - private String creationTimestamp; + private Long creationTimestamp; private String description; private String validDiskSize; - private String selfLink; private Long defaultDiskSizeGb; private DeprecationStatus deprecationStatus; private Builder() {} - Builder id(BigInteger id) { + Builder id(String id) { this.id = id; return this; } - Builder creationTimestamp(String creationTimestamp) { + Builder creationTimestamp(Long creationTimestamp) { this.creationTimestamp = creationTimestamp; return this; } @@ -95,11 +94,6 @@ Builder validDiskSize(String validDiskSize) { return this; } - Builder selfLink(String selfLink) { - this.selfLink = selfLink; - return this; - } - Builder defaultDiskSizeGb(Long defaultDiskSizeGb) { this.defaultDiskSizeGb = defaultDiskSizeGb; return this; @@ -121,17 +115,14 @@ private DiskType(Builder builder) { this.diskTypeId = builder.diskTypeId; this.description = builder.description; this.validDiskSize = builder.validDiskSize; - this.selfLink = builder.selfLink; this.defaultDiskSizeGb = builder.defaultDiskSizeGb; this.deprecationStatus = builder.deprecationStatus; } /** - * Returns the creation timestamp in RFC3339 text format. - * - * @see RFC3339 + * Returns the creation timestamp in milliseconds since epoch. */ - public String creationTimestamp() { + public Long creationTimestamp() { return creationTimestamp; } @@ -145,7 +136,7 @@ public DiskTypeId diskTypeId() { /** * Returns an unique identifier for the disk type; defined by the service. */ - public BigInteger id() { + public String id() { return id; } @@ -163,13 +154,6 @@ public String validDiskSize() { return validDiskSize; } - /** - * Returns a service-defined URL for the disk type. - */ - public String selfLink() { - return selfLink; - } - /** * Returns the service-defined default disk size in GB. */ @@ -193,7 +177,6 @@ public String toString() { .add("creationTimestamp", creationTimestamp) .add("description", description) .add("validDiskSize", validDiskSize) - .add("selfLink", selfLink) .add("defaultDiskSizeGb", defaultDiskSizeGb) .add("deprecationStatus", deprecationStatus) .toString(); @@ -212,13 +195,17 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.DiskType toPb() { com.google.api.services.compute.model.DiskType diskTypePb = new com.google.api.services.compute.model.DiskType(); - diskTypePb.setId(id); - diskTypePb.setCreationTimestamp(creationTimestamp); + if (id != null) { + diskTypePb.setId(new BigInteger(id)); + } + if (creationTimestamp != null) { + diskTypePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + } diskTypePb.setDescription(description); diskTypePb.setValidDiskSize(validDiskSize); - diskTypePb.setSelfLink(selfLink); + diskTypePb.setSelfLink(diskTypeId.selfLink()); diskTypePb.setDefaultDiskSizeGb(defaultDiskSizeGb); - diskTypePb.setZone(diskTypeId.zoneId().toUrl()); + diskTypePb.setZone(diskTypeId.zoneId().selfLink()); if (deprecationStatus != null) { diskTypePb.setDeprecated(deprecationStatus.toPb()); } @@ -231,12 +218,16 @@ static Builder builder() { static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb) { Builder builder = builder(); - builder.id(diskTypePb.getId()); - builder.creationTimestamp(diskTypePb.getCreationTimestamp()); + if (diskTypePb.getId() != null) { + builder.id(diskTypePb.getId().toString()); + } + if (diskTypePb.getCreationTimestamp() != null) { + builder.creationTimestamp( + DateTime.parseRfc3339(diskTypePb.getCreationTimestamp()).getValue()); + } builder.diskTypeId(DiskTypeId.fromUrl(diskTypePb.getSelfLink())); builder.description(diskTypePb.getDescription()); builder.validDiskSize(diskTypePb.getValidDiskSize()); - builder.selfLink(diskTypePb.getSelfLink()); builder.defaultDiskSizeGb(diskTypePb.getDefaultDiskSizeGb()); if (diskTypePb.getDeprecated() != null) { builder.deprecationStatus( diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java index c338dfeb10f1..cdf1fd42eedc 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskTypeId.java @@ -22,6 +22,8 @@ import com.google.common.base.MoreObjects; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Identity for a Google Compute Engine disk type. @@ -37,11 +39,12 @@ public DiskTypeId apply(String pb) { static final Function TO_URL_FUNCTION = new Function() { @Override public String apply(DiskTypeId diskTypeId) { - return diskTypeId.toUrl(); + return diskTypeId.selfLink(); } }; - static final String REGEX = ZoneResourceId.REGEX + "diskTypes/[^/]+"; + private static final String REGEX = ZoneResourceId.REGEX + "diskTypes/([^/]+)"; + private static final Pattern PATTERN = Pattern.compile(REGEX); private static final long serialVersionUID = 7337881474103686219L; private final String diskType; @@ -52,21 +55,15 @@ private DiskTypeId(String project, String zone, String diskType) { } /** - * Returns the name of the disk type resource. The name must be 1-63 characters long, and comply - * with RFC1035. Specifically, the name must be 1-63 characters long and match the regular - * expression {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a - * lowercase letter, and all following characters must be a dash, lowercase letter, or digit, - * except the last character, which cannot be a dash. - * - * @see RFC1035 + * Returns the name of the disk type. */ public String diskType() { return diskType; } @Override - public String toUrl() { - return super.toUrl() + "/diskTypes/" + diskType; + public String selfLink() { + return super.selfLink() + "/diskTypes/" + diskType; } @Override @@ -76,12 +73,14 @@ MoreObjects.ToStringHelper toStringHelper() { @Override public int hashCode() { - return Objects.hash(super.hashCode(), diskType); + return Objects.hash(super.baseHashCode(), diskType); } @Override public boolean equals(Object obj) { - return obj instanceof DiskTypeId && baseEquals((DiskTypeId) obj); + return obj instanceof DiskTypeId + && baseEquals((DiskTypeId) obj) + && Objects.equals(diskType, ((DiskTypeId) obj).diskType); } @Override @@ -118,19 +117,14 @@ public static DiskTypeId of(String project, String zone, String diskType) { * Returns {@code false} otherwise. */ static boolean matchesUrl(String url) { - return url.matches(REGEX); + return PATTERN.matcher(url).matches(); } static DiskTypeId fromUrl(String url) { - if (!matchesUrl(url)) { + Matcher matcher = PATTERN.matcher(url); + if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid disk type URL"); } - int projectsIndex = url.indexOf("/projects/"); - int zonesIndex = url.indexOf("/zones/"); - int diskTypesIndex = url.indexOf("/diskTypes/"); - String project = url.substring(projectsIndex + 10, zonesIndex); - String zone = url.substring(zonesIndex + 7, diskTypesIndex); - String diskType = url.substring(diskTypesIndex + 11, url.length()); - return DiskTypeId.of(project, zone, diskType); + return DiskTypeId.of(matcher.group(1), matcher.group(2), matcher.group(3)); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java index 5116761a0b2f..2c6cb4ac9422 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/License.java @@ -35,12 +35,10 @@ public final class License implements Serializable { private final LicenseId licenseId; private final Boolean chargesUseFee; - private final String selfLink; - License(LicenseId licenseId, Boolean chargesUseFee, String selfLink) { + License(LicenseId licenseId, Boolean chargesUseFee) { this.licenseId = checkNotNull(licenseId); this.chargesUseFee = chargesUseFee; - this.selfLink = selfLink; } /** @@ -58,19 +56,11 @@ public Boolean chargesUseFee() { return chargesUseFee; } - /** - * Returns a service-defined URL for the license. - */ - public String selfLink() { - return selfLink; - } - @Override public String toString() { return MoreObjects.toStringHelper(this) .add("licenseId", licenseId) .add("chargesUseFee", chargesUseFee) - .add("selfLink", selfLink) .toString(); } @@ -89,12 +79,11 @@ com.google.api.services.compute.model.License toPb() { new com.google.api.services.compute.model.License(); licensePb.setName(licenseId.license()); licensePb.setChargesUseFee(chargesUseFee); - licensePb.setSelfLink(selfLink); + licensePb.setSelfLink(licenseId.selfLink()); return licensePb; } static License fromPb(com.google.api.services.compute.model.License licensePb) { - return new License(LicenseId.fromUrl(licensePb.getSelfLink()), licensePb.getChargesUseFee(), - licensePb.getSelfLink()); + return new License(LicenseId.fromUrl(licensePb.getSelfLink()), licensePb.getChargesUseFee()); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java index 9bbeea639dc4..36d3037bc41b 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/LicenseId.java @@ -22,6 +22,8 @@ import com.google.common.base.MoreObjects; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Identity for a Google Compute Engine license. @@ -37,11 +39,12 @@ public LicenseId apply(String pb) { static final Function TO_URL_FUNCTION = new Function() { @Override public String apply(LicenseId licenseId) { - return licenseId.toUrl(); + return licenseId.selfLink(); } }; - static final String REGEX = ResourceId.REGEX + "global/licenses/[^/]+"; + private static final String REGEX = ResourceId.REGEX + "global/licenses/([^/]+)"; + private static final Pattern PATTERN = Pattern.compile(REGEX); private static final long serialVersionUID = -2239484554024469651L; private final String license; @@ -52,21 +55,15 @@ private LicenseId(String project, String license) { } /** - * Returns the name of the license. The name must be 1-63 characters long, and comply with - * RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression - * {@code [a-z]([-a-z0-9]*[a-z0-9])?} which means the first character must be a lowercase letter, - * and all following characters must be a dash, lowercase letter, or digit, except the last - * character, which cannot be a dash. - * - * @see RFC1035 + * Returns the name of the license. */ public String license() { return license; } @Override - public String toUrl() { - return super.toUrl() + "/global/licenses/" + license; + public String selfLink() { + return super.selfLink() + "/global/licenses/" + license; } @Override @@ -81,7 +78,9 @@ public int hashCode() { @Override public boolean equals(Object obj) { - return obj instanceof LicenseId && baseEquals((LicenseId) obj); + return obj instanceof LicenseId + && baseEquals((LicenseId) obj) + && Objects.equals(license, ((LicenseId) obj).license); } @Override @@ -111,17 +110,14 @@ public static LicenseId of(String project, String license) { * Returns {@code false} otherwise. */ static boolean matchesUrl(String url) { - return url.matches(REGEX); + return PATTERN.matcher(url).matches(); } static LicenseId fromUrl(String url) { - if (!matchesUrl(url)) { + Matcher matcher = PATTERN.matcher(url); + if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid license URL"); } - int projectsIndex = url.indexOf("/projects/"); - int licensesIndex = url.indexOf("/global/licenses/"); - String project = url.substring(projectsIndex + 10, licensesIndex); - String license = url.substring(licensesIndex + 17, url.length()); - return LicenseId.of(project, license); + return LicenseId.of(matcher.group(1), matcher.group(2)); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java index e8ac54094bf8..df0109afa1f5 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java @@ -16,6 +16,7 @@ package com.google.gcloud.compute; +import com.google.api.client.util.DateTime; import com.google.api.services.compute.model.MachineType.ScratchDisks; import com.google.common.base.Function; import com.google.common.base.MoreObjects; @@ -55,13 +56,12 @@ public com.google.api.services.compute.model.MachineType apply(MachineType type) private static final long serialVersionUID = -4210962597502860450L; private final MachineTypeId machineTypeId; - private final BigInteger id; - private final String creationTimestamp; + private final String id; + private final Long creationTimestamp; private final String description; - private final String selfLink; - private final Integer guestCpus; + private final Integer cpus; private final Integer memoryMb; - private final List scratchDisks; + private final List scratchDisksSizeGb; private final Integer maximumPersistentDisks; private final Long maximumPersistentDisksSizeGb; private final DeprecationStatus deprecationStatus; @@ -69,13 +69,12 @@ public com.google.api.services.compute.model.MachineType apply(MachineType type) static final class Builder { private MachineTypeId machineTypeId; - private BigInteger id; - private String creationTimestamp; + private String id; + private Long creationTimestamp; private String description; - private String selfLink; - private Integer guestCpus; + private Integer cpus; private Integer memoryMb; - private List scratchDisks; + private List scratchDisksSizeGb; private Integer maximumPersistentDisks; private Long maximumPersistentDisksSizeGb; private DeprecationStatus deprecationStatus; @@ -87,12 +86,12 @@ Builder machineTypeId(MachineTypeId machineTypeId) { return this; } - Builder id(BigInteger id) { + Builder id(String id) { this.id = id; return this; } - Builder creationTimestamp(String creationTimestamp) { + Builder creationTimestamp(Long creationTimestamp) { this.creationTimestamp = creationTimestamp; return this; } @@ -102,13 +101,8 @@ Builder description(String description) { return this; } - Builder selfLink(String selfLink) { - this.selfLink = selfLink; - return this; - } - - Builder guestCpus(Integer guestCpus) { - this.guestCpus = guestCpus; + Builder cpus(Integer cpus) { + this.cpus = cpus; return this; } @@ -117,8 +111,8 @@ Builder memoryMb(Integer memoryMb) { return this; } - Builder scratchDisks(List scratchDisks) { - this.scratchDisks = scratchDisks; + Builder scratchDisksSizeGb(List scratchDisksSizeGb) { + this.scratchDisksSizeGb = scratchDisksSizeGb; return this; } @@ -147,10 +141,9 @@ private MachineType(Builder builder) { this.id = builder.id; this.creationTimestamp = builder.creationTimestamp; this.description = builder.description; - this.selfLink = builder.selfLink; - this.guestCpus = builder.guestCpus; + this.cpus = builder.cpus; this.memoryMb = builder.memoryMb; - this.scratchDisks = builder.scratchDisks; + this.scratchDisksSizeGb = builder.scratchDisksSizeGb; this.maximumPersistentDisks = builder.maximumPersistentDisks; this.maximumPersistentDisksSizeGb = builder.maximumPersistentDisksSizeGb; this.deprecationStatus = builder.deprecationStatus; @@ -166,16 +159,14 @@ public MachineTypeId machineTypeId() { /** * Returns an unique identifier for the machine type; defined by the service. */ - public BigInteger id() { + public String id() { return id; } /** - * Returns the creation timestamp in RFC3339 text format. - * - * @see RFC3339 + * Returns the creation timestamp in milliseconds since epoch. */ - public String creationTimestamp() { + public Long creationTimestamp() { return creationTimestamp; } @@ -186,18 +177,11 @@ public String description() { return description; } - /** - * Returns a service-defined URL for the machine type. - */ - public String selfLink() { - return selfLink; - } - /** * Returns the number of virtual CPUs that are available to the instance. */ - public Integer guestCpus() { - return guestCpus; + public Integer cpus() { + return cpus; } /** @@ -210,8 +194,8 @@ public Integer memoryMb() { /** * Returns the size of all extended scratch disks assigned to the instance, defined in GB. */ - public List scratchDisks() { - return scratchDisks; + public List scratchDisksSizeGb() { + return scratchDisksSizeGb; } /** @@ -245,10 +229,9 @@ public String toString() { .add("id", id) .add("creationTimestamp", creationTimestamp) .add("description", description) - .add("selfLink", selfLink) - .add("guestCpus", guestCpus) + .add("cpus", cpus) .add("memoryMb", memoryMb) - .add("scratchDisks", scratchDisks) + .add("scratchDisksSizeGb", scratchDisksSizeGb) .add("maximumPersistentDisks", maximumPersistentDisks) .add("maximumPersistentDisksSizeGb", maximumPersistentDisksSizeGb) .add("deprecationStatus", deprecationStatus) @@ -268,15 +251,19 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.MachineType toPb() { com.google.api.services.compute.model.MachineType machineTypePb = new com.google.api.services.compute.model.MachineType(); - machineTypePb.setId(id); - machineTypePb.setCreationTimestamp(creationTimestamp); + if (id != null) { + machineTypePb.setId(new BigInteger(id)); + } + if (creationTimestamp != null) { + machineTypePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + } machineTypePb.setName(machineTypeId.machineType()); machineTypePb.setDescription(description); - machineTypePb.setSelfLink(selfLink); - machineTypePb.setGuestCpus(guestCpus); + machineTypePb.setSelfLink(machineTypeId.selfLink()); + machineTypePb.setGuestCpus(cpus); machineTypePb.setMemoryMb(memoryMb); - if (scratchDisks != null) { - machineTypePb.setScratchDisks(Lists.transform(scratchDisks, + if (scratchDisksSizeGb != null) { + machineTypePb.setScratchDisks(Lists.transform(scratchDisksSizeGb, new Function() { @Override public ScratchDisks apply(Integer diskSize) { @@ -300,14 +287,18 @@ static Builder builder() { static MachineType fromPb(com.google.api.services.compute.model.MachineType machineTypePb) { Builder builder = builder(); builder.machineTypeId(MachineTypeId.fromUrl(machineTypePb.getSelfLink())); - builder.id(machineTypePb.getId()); - builder.creationTimestamp(machineTypePb.getCreationTimestamp()); + if (machineTypePb.getId() != null) { + builder.id(machineTypePb.getId().toString()); + } + if (machineTypePb.getCreationTimestamp() != null) { + builder.creationTimestamp( + DateTime.parseRfc3339(machineTypePb.getCreationTimestamp()).getValue()); + } builder.description(machineTypePb.getDescription()); - builder.selfLink(machineTypePb.getSelfLink()); - builder.guestCpus(machineTypePb.getGuestCpus()); + builder.cpus(machineTypePb.getGuestCpus()); builder.memoryMb(machineTypePb.getMemoryMb()); if (machineTypePb.getScratchDisks() != null) { - builder.scratchDisks( + builder.scratchDisksSizeGb( Lists.transform(machineTypePb.getScratchDisks(), new Function() { @Override public Integer apply(ScratchDisks scratchDiskPb) { diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java index 11428f700481..1252719e05d7 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineTypeId.java @@ -22,6 +22,8 @@ import com.google.common.base.MoreObjects; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Identity for a Google Compute Engine machine type. @@ -39,11 +41,12 @@ public MachineTypeId apply(String pb) { new Function() { @Override public String apply(MachineTypeId machineTypeId) { - return machineTypeId.toUrl(); + return machineTypeId.selfLink(); } }; - static final String REGEX = ZoneResourceId.REGEX + "machineTypes/[^/]+"; + private static final String REGEX = ZoneResourceId.REGEX + "machineTypes/([^/]+)"; + private static final Pattern PATTERN = Pattern.compile(REGEX); private static final long serialVersionUID = -5819598544478859608L; private final String machineType; @@ -61,8 +64,8 @@ public String machineType() { } @Override - public String toUrl() { - return super.toUrl() + "/machineTypes/" + machineType; + public String selfLink() { + return super.selfLink() + "/machineTypes/" + machineType; } @Override @@ -72,12 +75,14 @@ MoreObjects.ToStringHelper toStringHelper() { @Override public int hashCode() { - return Objects.hash(super.hashCode(), machineType); + return Objects.hash(baseHashCode(), machineType); } @Override public boolean equals(Object obj) { - return obj instanceof MachineTypeId && baseEquals((MachineTypeId) obj); + return obj instanceof MachineTypeId + && baseEquals((MachineTypeId) obj) + && Objects.equals(machineType, ((MachineTypeId) obj).machineType); } @Override @@ -107,19 +112,14 @@ public static MachineTypeId of(String project, String zone, String machineType) * Returns {@code false} otherwise. */ static boolean matchesUrl(String url) { - return url.matches(REGEX); + return PATTERN.matcher(url).matches(); } static MachineTypeId fromUrl(String url) { - if (!matchesUrl(url)) { + Matcher matcher = PATTERN.matcher(url); + if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid machine type URL"); } - int projectsIndex = url.indexOf("/projects/"); - int zonesIndex = url.indexOf("/zones/"); - int machineTypesIndex = url.indexOf("/machineTypes/"); - String project = url.substring(projectsIndex + 10, zonesIndex); - String zone = url.substring(zonesIndex + 7, machineTypesIndex); - String machineType = url.substring(machineTypesIndex + 14, url.length()); - return MachineTypeId.of(project, zone, machineType); + return MachineTypeId.of(matcher.group(1), matcher.group(2), matcher.group(3)); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java index 8edfd869a2b1..add58d8009b0 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java @@ -16,6 +16,7 @@ package com.google.gcloud.compute; +import com.google.api.client.util.DateTime; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; @@ -51,10 +52,9 @@ public com.google.api.services.compute.model.Region apply(Region region) { private static final long serialVersionUID = -3578710133393645135L; private final RegionId regionId; - private final BigInteger id; - private final String creationTimestamp; + private final String id; + private final Long creationTimestamp; private final String description; - private final String selfLink; private final Status status; private final List zones; private final List quotas; @@ -102,14 +102,23 @@ public com.google.api.services.compute.model.Quota apply(Quota quota) { this.usage = usage; } + /** + * Returns the name of the quota metric. + */ public String metric() { return metric; } + /** + * Returns the quota limit for this metric. + */ public double limit() { return limit; } + /** + * Returns the current usage for this quota. + */ public double usage() { return usage; } @@ -154,10 +163,10 @@ static Quota fromPb(com.google.api.services.compute.model.Quota quotaPb) { static final class Builder { private RegionId regionId; - private BigInteger id; - private String creationTimestamp; + private String id; + private Long creationTimestamp; private String description; - private String selfLink; + private Status status; private List zones; private List quotas; @@ -170,12 +179,12 @@ Builder regionId(RegionId regionId) { return this; } - Builder id(BigInteger id) { + Builder id(String id) { this.id = id; return this; } - Builder creationTimestamp(String creationTimestamp) { + Builder creationTimestamp(Long creationTimestamp) { this.creationTimestamp = creationTimestamp; return this; } @@ -185,11 +194,6 @@ Builder description(String description) { return this; } - Builder selfLink(String selfLink) { - this.selfLink = selfLink; - return this; - } - Builder status(Status status) { this.status = status; return this; @@ -220,7 +224,6 @@ private Region(Builder builder) { this.id = builder.id; this.creationTimestamp = builder.creationTimestamp; this.description = builder.description; - this.selfLink = builder.selfLink; this.status = builder.status; this.zones = builder.zones; this.quotas = builder.quotas; @@ -237,16 +240,14 @@ public RegionId regionId() { /** * Returns an unique identifier for the region; defined by the service. */ - public BigInteger id() { + public String id() { return id; } /** - * Returns the creation timestamp in RFC3339 text format. - * - * @see RFC3339 + * Returns the creation timestamp in milliseconds since epoch. */ - public String creationTimestamp() { + public Long creationTimestamp() { return creationTimestamp; } @@ -257,13 +258,6 @@ public String description() { return description; } - /** - * Returns a service-defined URL for the region. - */ - public String selfLink() { - return selfLink; - } - /** * Returns the status of the status. */ @@ -301,7 +295,6 @@ public String toString() { .add("id", id) .add("creationTimestamp", creationTimestamp) .add("description", description) - .add("selfLink", selfLink) .add("status", status) .add("zones", zones) .add("quotas", quotas) @@ -322,11 +315,15 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.Region toPb() { com.google.api.services.compute.model.Region regionPb = new com.google.api.services.compute.model.Region(); - regionPb.setId(id); - regionPb.setCreationTimestamp(creationTimestamp); + if (id != null) { + regionPb.setId(new BigInteger(id)); + } + if (creationTimestamp != null) { + regionPb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + } regionPb.setName(regionId.region()); regionPb.setDescription(description); - regionPb.setSelfLink(selfLink); + regionPb.setSelfLink(regionId.selfLink()); if (status != null) { regionPb.setStatus(status.name()); } @@ -349,10 +346,13 @@ static Builder builder() { static Region fromPb(com.google.api.services.compute.model.Region regionPb) { Builder builder = builder(); builder.regionId(RegionId.fromUrl(regionPb.getSelfLink())); - builder.id(regionPb.getId()); - builder.creationTimestamp(regionPb.getCreationTimestamp()); + if (regionPb.getId() != null) { + builder.id(regionPb.getId().toString()); + } + if (regionPb.getCreationTimestamp() != null) { + builder.creationTimestamp(DateTime.parseRfc3339(regionPb.getCreationTimestamp()).getValue()); + } builder.description(regionPb.getDescription()); - builder.selfLink(regionPb.getSelfLink()); if (regionPb.getStatus() != null) { builder.status(Status.valueOf(regionPb.getStatus())); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java index 97d31ba09d99..403edd47ce48 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionId.java @@ -22,6 +22,8 @@ import com.google.common.base.MoreObjects.ToStringHelper; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * A Google Compute Engine region identity. @@ -37,11 +39,12 @@ public RegionId apply(String pb) { static final Function TO_URL_FUNCTION = new Function() { @Override public String apply(RegionId regionId) { - return regionId.toUrl(); + return regionId.selfLink(); } }; - static final String REGEX = ResourceId.REGEX + "regions/[^/]+"; + private static final String REGEX = ResourceId.REGEX + "regions/([^/]+)"; + private static final Pattern PATTERN = Pattern.compile(REGEX); private static final long serialVersionUID = 5569092266957249294L; private final String region; @@ -64,8 +67,8 @@ public final String region() { } @Override - public String toUrl() { - return super.toUrl() + "/regions/" + region; + public String selfLink() { + return super.selfLink() + "/regions/" + region; } @Override @@ -80,7 +83,9 @@ public int hashCode() { @Override public boolean equals(Object obj) { - return obj instanceof RegionId && baseEquals((RegionId) obj); + return obj instanceof RegionId + && baseEquals((RegionId) obj) + && Objects.equals(region, ((RegionId) obj).region); } @Override @@ -110,17 +115,14 @@ public static RegionId of(String region) { * Returns {@code false} otherwise. */ static boolean matchesUrl(String url) { - return url.matches(REGEX); + return PATTERN.matcher(url).matches(); } static RegionId fromUrl(String url) { - if (!matchesUrl(url)) { + Matcher matcher = PATTERN.matcher(url); + if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid region URL"); } - int projectsIndex = url.indexOf("/projects/"); - int regionsIndex = url.indexOf("/regions/"); - String project = url.substring(projectsIndex + 10, regionsIndex); - String region = url.substring(regionsIndex + 9, url.length()); - return RegionId.of(project, region); + return RegionId.of(matcher.group(1), matcher.group(2)); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java index 63193f54833b..eeb288b07be1 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/RegionResourceId.java @@ -27,7 +27,7 @@ */ public abstract class RegionResourceId extends ResourceId { - static final String REGEX = ResourceId.REGEX + "regions/[^/]+/"; + static final String REGEX = ResourceId.REGEX + "regions/([^/]+)/"; private static final long serialVersionUID = 5569092266957249294L; private final String region; @@ -57,8 +57,8 @@ public final RegionId regionId() { } @Override - public String toUrl() { - return super.toUrl() + "/regions/" + region; + public String selfLink() { + return super.selfLink() + "/regions/" + region; } @Override @@ -67,14 +67,14 @@ ToStringHelper toStringHelper() { } @Override - public int hashCode() { - return Objects.hash(baseHashCode(), region); + final int baseHashCode() { + return Objects.hash(super.baseHashCode(), region); } @Override - public boolean equals(Object obj) { - return obj != null - && obj.getClass().equals(RegionResourceId.class) - && baseEquals((RegionResourceId) obj); + final boolean baseEquals(ResourceId resourceId) { + return resourceId instanceof RegionResourceId + && super.baseEquals(resourceId) + && Objects.equals(region, ((RegionResourceId) resourceId).region); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java index aa25c4c2b589..38e0fe576ac8 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ResourceId.java @@ -26,25 +26,20 @@ */ public abstract class ResourceId implements Serializable { - static final String REGEX = - "(https?://(www|content).googleapis.com/compute/v1/)?projects/[^/]+/"; + static final String REGEX = ".*?projects/([^/]+)/"; + private static final String BASE_URL = "https://www.googleapis.com/compute/v1/projects/"; private static final long serialVersionUID = -8028734746870421573L; - private String project; + private final String project; ResourceId(String project) { this.project = project; } - /** - * Base URL for a resource URL. - */ - private static final String BASE_URL = "https://www.googleapis.com/compute/v1/projects/"; - /** * Returns a fully qualified URL to the entity. */ - public String toUrl() { + public String selfLink() { return BASE_URL + project; } @@ -64,12 +59,12 @@ public String toString() { return toStringHelper().toString(); } - final int baseHashCode() { + int baseHashCode() { return Objects.hash(project); } - final boolean baseEquals(ResourceId resourceId) { - return Objects.equals(toUrl(), resourceId.toUrl()); + boolean baseEquals(ResourceId resourceId) { + return Objects.equals(project, resourceId.project); } abstract ResourceId setProjectId(String projectId); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java index 073f7f624dae..48bb5412b605 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java @@ -16,6 +16,7 @@ package com.google.gcloud.compute; +import com.google.api.client.util.DateTime; import com.google.api.services.compute.model.Zone.MaintenanceWindows; import com.google.common.base.Function; import com.google.common.base.MoreObjects; @@ -51,10 +52,9 @@ public com.google.api.services.compute.model.Zone apply(Zone region) { private static final long serialVersionUID = 6113636504417213010L; private final ZoneId zoneId; - private final BigInteger id; - private final String creationTimestamp; + private final String id; + private final Long creationTimestamp; private final String description; - private final String selfLink; private final Status status; private final List maintenanceWindows; private final RegionId region; @@ -96,13 +96,13 @@ public MaintenanceWindows apply(MaintenanceWindow maintenanceWindow) { private final String name; private final String description; - private final String beginTime; - private final String endTime; + private final Long beginTime; + private final Long endTime; /** * Returns a zone maintenance window object. */ - MaintenanceWindow(String name, String description, String beginTime, String endTime) { + MaintenanceWindow(String name, String description, Long beginTime, Long endTime) { this.name = name; this.description = description; this.beginTime = beginTime; @@ -110,7 +110,7 @@ public MaintenanceWindows apply(MaintenanceWindow maintenanceWindow) { } /** - * Returns the disk of the maintanance window. + * Returns the name of the maintenance window. */ public String name() { return name; @@ -124,20 +124,16 @@ public String description() { } /** - * Returns the starting time of the maintenance window in RFC3339 text format. - * - * @see RFC3339 + * Returns the starting time of the maintenance window in milliseconds since epoch. */ - public String beginTime() { + public Long beginTime() { return beginTime; } /** - * Returns the ending time of the maintenance window in RFC3339 text format. - * - * @see RFC3339 + * Returns the ending time of the maintenance window in milliseconds since epoch. */ - public String endTime() { + public Long endTime() { return endTime; } @@ -166,23 +162,26 @@ MaintenanceWindows toPb() { return new MaintenanceWindows() .setName(name) .setDescription(description) - .setBeginTime(beginTime) - .setEndTime(endTime); + .setBeginTime(beginTime != null ? new DateTime(beginTime).toStringRfc3339() : null) + .setEndTime(endTime != null ? new DateTime(endTime).toStringRfc3339() : null); } static MaintenanceWindow fromPb(MaintenanceWindows windowPb) { return new MaintenanceWindow(windowPb.getName(), windowPb.getDescription(), - windowPb.getBeginTime(), windowPb.getEndTime()); + windowPb.getBeginTime() != null + ? DateTime.parseRfc3339(windowPb.getBeginTime()).getValue() : null, + windowPb.getEndTime() != null + ? DateTime.parseRfc3339(windowPb.getEndTime()).getValue() : null); } } static final class Builder { private ZoneId zoneId; - private BigInteger id; - private String creationTimestamp; + private String id; + private Long creationTimestamp; private String description; - private String selfLink; + private Status status; private List maintenanceWindows; private RegionId region; @@ -195,12 +194,12 @@ Builder zoneId(ZoneId zoneId) { return this; } - Builder id(BigInteger id) { + Builder id(String id) { this.id = id; return this; } - Builder creationTimestamp(String creationTimestamp) { + Builder creationTimestamp(Long creationTimestamp) { this.creationTimestamp = creationTimestamp; return this; } @@ -210,11 +209,6 @@ Builder description(String description) { return this; } - Builder selfLink(String selfLink) { - this.selfLink = selfLink; - return this; - } - Builder status(Status status) { this.status = status; return this; @@ -245,7 +239,6 @@ private Zone(Builder builder) { this.id = builder.id; this.creationTimestamp = builder.creationTimestamp; this.description = builder.description; - this.selfLink = builder.selfLink; this.status = builder.status; this.maintenanceWindows = builder.maintenanceWindows; this.region = builder.region; @@ -260,11 +253,9 @@ public ZoneId zoneId() { } /** - * Returns the creation timestamp in RFC3339 text format. - * - * @see RFC3339 + * Returns the creation timestamp in milliseconds since epoch. */ - public String creationTimestamp() { + public Long creationTimestamp() { return creationTimestamp; } @@ -278,17 +269,10 @@ public String description() { /** * Returns an unique identifier for the zone; defined by the service. */ - public BigInteger id() { + public String id() { return id; } - /** - * Returns a service-defined URL for the zone. - */ - public String selfLink() { - return selfLink; - } - /** * Returns the status of the zone. */ @@ -330,7 +314,6 @@ public String toString() { .add("id", id) .add("creationTimestamp", creationTimestamp) .add("description", description) - .add("selfLink", selfLink) .add("status", status) .add("maintenanceWindows", maintenanceWindows) .add("region", region) @@ -351,11 +334,15 @@ public boolean equals(Object obj) { com.google.api.services.compute.model.Zone toPb() { com.google.api.services.compute.model.Zone zonePb = new com.google.api.services.compute.model.Zone(); - zonePb.setId(id); - zonePb.setCreationTimestamp(creationTimestamp); + if (id != null) { + zonePb.setId(new BigInteger(id)); + } + if (creationTimestamp != null) { + zonePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + } zonePb.setName(zoneId.zone()); zonePb.setDescription(description); - zonePb.setSelfLink(selfLink); + zonePb.setSelfLink(zoneId.selfLink()); if (status != null) { zonePb.setStatus(status.name()); } @@ -364,7 +351,7 @@ com.google.api.services.compute.model.Zone toPb() { Lists.transform(maintenanceWindows, MaintenanceWindow.TO_PB_FUNCTION)); } if (region != null) { - zonePb.setRegion(region.toUrl()); + zonePb.setRegion(region.selfLink()); } if (deprecationStatus != null) { zonePb.setDeprecated(deprecationStatus.toPb()); @@ -379,10 +366,13 @@ static Builder builder() { static Zone fromPb(com.google.api.services.compute.model.Zone zonePb) { Builder builder = builder(); builder.zoneId(ZoneId.fromUrl(zonePb.getSelfLink())); - builder.id(zonePb.getId()); - builder.creationTimestamp(zonePb.getCreationTimestamp()); + if (zonePb.getId() != null) { + builder.id(zonePb.getId().toString()); + } + if (zonePb.getCreationTimestamp() != null) { + builder.creationTimestamp(DateTime.parseRfc3339(zonePb.getCreationTimestamp()).getValue()); + } builder.description(zonePb.getDescription()); - builder.selfLink(zonePb.getSelfLink()); if (zonePb.getStatus() != null) { builder.status(Status.valueOf(zonePb.getStatus())); } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java index bbf55a1c1062..74ac5be58201 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneId.java @@ -22,6 +22,8 @@ import com.google.common.base.MoreObjects.ToStringHelper; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * A Google Compute Engine zone identity. @@ -37,11 +39,12 @@ public ZoneId apply(String pb) { static final Function TO_URL_FUNCTION = new Function() { @Override public String apply(ZoneId zoneId) { - return zoneId.toUrl(); + return zoneId.selfLink(); } }; - static final String REGEX = ResourceId.REGEX + "zones/[^/]+"; + private static final String REGEX = ResourceId.REGEX + "zones/([^/]+)"; + private static final Pattern PATTERN = Pattern.compile(REGEX); private static final long serialVersionUID = -7635391994812946733L; private final String zone; @@ -59,8 +62,8 @@ public final String zone() { } @Override - public String toUrl() { - return super.toUrl() + "/zones/" + zone; + public String selfLink() { + return super.selfLink() + "/zones/" + zone; } @Override @@ -75,7 +78,9 @@ public int hashCode() { @Override public boolean equals(Object obj) { - return obj instanceof ZoneId && baseEquals((ZoneId) obj); + return obj instanceof ZoneId + && baseEquals((ZoneId) obj) + && Objects.equals(zone, ((ZoneId) obj).zone); } @Override @@ -109,13 +114,10 @@ static boolean matchesUrl(String url) { } static ZoneId fromUrl(String url) { - if (!matchesUrl(url)) { + Matcher matcher = PATTERN.matcher(url); + if (!matcher.matches()) { throw new IllegalArgumentException(url + " is not a valid zone URL"); } - int projectsIndex = url.indexOf("/projects/"); - int zonesIndex = url.indexOf("/zones/"); - String project = url.substring(projectsIndex + 10, zonesIndex); - String zone = url.substring(zonesIndex + 7, url.length()); - return ZoneId.of(project, zone); + return ZoneId.of(matcher.group(1), matcher.group(2)); } } diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java index 576ae31aed0c..60117684c056 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ZoneResourceId.java @@ -27,7 +27,7 @@ */ public abstract class ZoneResourceId extends ResourceId { - static final String REGEX = ResourceId.REGEX + "zones/[^/]+/"; + static final String REGEX = ResourceId.REGEX + "zones/([^/]+)/"; private static final long serialVersionUID = -6249546895344926888L; private final String zone; @@ -57,8 +57,8 @@ public final ZoneId zoneId() { } @Override - public String toUrl() { - return super.toUrl() + "/zones/" + zone; + public String selfLink() { + return super.selfLink() + "/zones/" + zone; } @Override @@ -67,14 +67,14 @@ ToStringHelper toStringHelper() { } @Override - public int hashCode() { - return Objects.hash(baseHashCode(), zone); + final int baseHashCode() { + return Objects.hash(super.baseHashCode(), zone); } @Override - public boolean equals(Object obj) { - return obj != null - && obj.getClass().equals(ZoneResourceId.class) - && baseEquals((RegionResourceId) obj); + final boolean baseEquals(ResourceId resourceId) { + return resourceId instanceof ZoneResourceId + && super.baseEquals(resourceId) + && Objects.equals(zone, ((ZoneResourceId) resourceId).zone); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java index 2b7ec22e36bf..e8d29669da7b 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DeprecationStatusTest.java @@ -24,9 +24,9 @@ public class DeprecationStatusTest { - private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; - private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; - private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final Long DELETED = 1453293540000L; + private static final Long DEPRECATED = 1453293420000L; + private static final Long OBSOLETE = 1453293480000L; private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType"); private static final MachineTypeId MACHINE_TYPE_ID = MachineTypeId.of("project", "zone", "machineType"); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java index 43be10eb31a3..5ff61d5777ba 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeIdTest.java @@ -43,7 +43,7 @@ public void testOf() { assertEquals(PROJECT, diskTypeId.project()); assertEquals(ZONE, diskTypeId.zone()); assertEquals(DISK_TYPE, diskTypeId.diskType()); - assertEquals(URL, diskTypeId.toUrl()); + assertEquals(URL, diskTypeId.selfLink()); diskTypeId = DiskTypeId.of(ZONE, DISK_TYPE); assertNull(diskTypeId.project()); assertEquals(ZONE, diskTypeId.zone()); @@ -54,10 +54,10 @@ public void testOf() { public void testToAndFromUrl() { DiskTypeId diskTypeId = DiskTypeId.of(PROJECT, ZONE, DISK_TYPE); assertSame(diskTypeId, diskTypeId.setProjectId(PROJECT)); - compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.toUrl())); + compareDiskTypeId(diskTypeId, DiskTypeId.fromUrl(diskTypeId.selfLink())); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid disk type URL"); - diskTypeId = DiskTypeId.fromUrl("notMatchingUrl"); + DiskTypeId.fromUrl("notMatchingUrl"); } @Test @@ -69,7 +69,7 @@ public void testSetProjectId() { @Test public void testMatchesUrl() { - assertTrue(DiskTypeId.matchesUrl(DiskTypeId.of(PROJECT, ZONE, DISK_TYPE).toUrl())); + assertTrue(DiskTypeId.matchesUrl(DiskTypeId.of(PROJECT, ZONE, DISK_TYPE).selfLink())); assertFalse(DiskTypeId.matchesUrl("notMatchingUrl")); } @@ -78,7 +78,7 @@ private void compareDiskTypeId(DiskTypeId expected, DiskTypeId value) { assertEquals(expected.project(), expected.project()); assertEquals(expected.zone(), expected.zone()); assertEquals(expected.diskType(), expected.diskType()); - assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.selfLink(), expected.selfLink()); assertEquals(expected.hashCode(), expected.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java index bbd3ad5207d3..7cacf256523a 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/DiskTypeTest.java @@ -24,15 +24,15 @@ public class DiskTypeTest { - private static final BigInteger ID = BigInteger.valueOf(42L); - private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String ID = "42"; + private static final Long CREATION_TIMESTAMP = 1453293540000L; private static final String DESCRIPTION = "description"; private static final String VALID_DISK_SIZE = "10GB-10TB"; private static final Long DEFAULT_DISK_SIZE_GB = 10L; private static final DiskTypeId DISK_TYPE_ID = DiskTypeId.of("project", "zone", "diskType"); - private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; - private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; - private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final Long DELETED = 1453293540000L; + private static final Long DEPRECATED = 1453293420000L; + private static final Long OBSOLETE = 1453293480000L; private static final DeprecationStatus.Status STATUS = DeprecationStatus.Status.DELETED; private static final DeprecationStatus DEPRECATION_STATUS = new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, DISK_TYPE_ID, STATUS); @@ -42,7 +42,6 @@ public class DiskTypeTest { .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) .validDiskSize(VALID_DISK_SIZE) - .selfLink(DISK_TYPE_ID.toUrl()) .defaultDiskSizeGb(DEFAULT_DISK_SIZE_GB) .deprecationStatus(DEPRECATION_STATUS) .build(); @@ -54,7 +53,6 @@ public void testBuilder() { assertEquals(CREATION_TIMESTAMP, DISK_TYPE.creationTimestamp()); assertEquals(DESCRIPTION, DISK_TYPE.description()); assertEquals(VALID_DISK_SIZE, DISK_TYPE.validDiskSize()); - assertEquals(DISK_TYPE_ID.toUrl(), DISK_TYPE.selfLink()); assertEquals(DEFAULT_DISK_SIZE_GB, DISK_TYPE.defaultDiskSizeGb()); assertEquals(DEPRECATION_STATUS, DISK_TYPE.deprecationStatus()); } @@ -62,10 +60,7 @@ public void testBuilder() { @Test public void testToPbAndFromPb() { compareDiskTypes(DISK_TYPE, DiskType.fromPb(DISK_TYPE.toPb())); - DiskType diskType = DiskType.builder() - .diskTypeId(DISK_TYPE_ID) - .selfLink(DISK_TYPE_ID.toUrl()) - .build(); + DiskType diskType = DiskType.builder().diskTypeId(DISK_TYPE_ID).build(); compareDiskTypes(diskType, DiskType.fromPb(diskType.toPb())); } @@ -76,7 +71,6 @@ private void compareDiskTypes(DiskType expected, DiskType value) { assertEquals(expected.creationTimestamp(), value.creationTimestamp()); assertEquals(expected.description(), value.description()); assertEquals(expected.validDiskSize(), value.validDiskSize()); - assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.defaultDiskSizeGb(), value.defaultDiskSizeGb()); assertEquals(expected.deprecationStatus(), value.deprecationStatus()); assertEquals(expected.hashCode(), value.hashCode()); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java index d3632603b030..d06fd48cff9a 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseIdTest.java @@ -41,7 +41,7 @@ public void testOf() { LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); assertEquals(PROJECT, licenseId.project()); assertEquals(LICENSE, licenseId.license()); - assertEquals(URL, licenseId.toUrl()); + assertEquals(URL, licenseId.selfLink()); licenseId = LicenseId.of(LICENSE); assertNull(licenseId.project()); assertEquals(LICENSE, licenseId.license()); @@ -50,7 +50,7 @@ public void testOf() { @Test public void testToAndFromUrl() { LicenseId licenseId = LicenseId.of(PROJECT, LICENSE); - compareLicenseId(licenseId, LicenseId.fromUrl(licenseId.toUrl())); + compareLicenseId(licenseId, LicenseId.fromUrl(licenseId.selfLink())); } @Test @@ -60,12 +60,12 @@ public void testSetProjectId() { compareLicenseId(licenseId, LicenseId.of(LICENSE).setProjectId(PROJECT)); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid license URL"); - licenseId = LicenseId.fromUrl("notMatchingUrl"); + LicenseId.fromUrl("notMatchingUrl"); } @Test public void testMatchesUrl() { - assertTrue(LicenseId.matchesUrl(LicenseId.of(PROJECT, LICENSE).toUrl())); + assertTrue(LicenseId.matchesUrl(LicenseId.of(PROJECT, LICENSE).selfLink())); assertFalse(LicenseId.matchesUrl("notMatchingUrl")); } @@ -73,7 +73,7 @@ private void compareLicenseId(LicenseId expected, LicenseId value) { assertEquals(expected, value); assertEquals(expected.project(), expected.project()); assertEquals(expected.license(), expected.license()); - assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.selfLink(), expected.selfLink()); assertEquals(expected.hashCode(), expected.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java index 450979d8ecc8..c9fb95fab9c6 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/LicenseTest.java @@ -24,14 +24,12 @@ public class LicenseTest { private static final LicenseId LICENSE_ID = LicenseId.of("project", "license"); private static final Boolean CHARGES_USE_FEE = true; - private static final String SELF_LINK = LICENSE_ID.toUrl(); - private static final License LICENSE = new License(LICENSE_ID, CHARGES_USE_FEE, SELF_LINK); + private static final License LICENSE = new License(LICENSE_ID, CHARGES_USE_FEE); @Test public void testBuilder() { assertEquals(LICENSE_ID, LICENSE.licenseId()); assertEquals(CHARGES_USE_FEE, LICENSE.chargesUseFee()); - assertEquals(LICENSE_ID.toUrl(), LICENSE.selfLink()); } @Test @@ -46,7 +44,6 @@ private void compareLicenses(License expected, License value) { assertEquals(expected, value); assertEquals(expected.licenseId(), value.licenseId()); assertEquals(expected.chargesUseFee(), value.chargesUseFee()); - assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.hashCode(), value.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java index bddb9c21f67a..7e3f4d7eae35 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeIdTest.java @@ -43,7 +43,7 @@ public void testOf() { assertEquals(PROJECT, machineTypeId.project()); assertEquals(ZONE, machineTypeId.zone()); assertEquals(TYPE, machineTypeId.machineType()); - assertEquals(URL, machineTypeId.toUrl()); + assertEquals(URL, machineTypeId.selfLink()); machineTypeId = MachineTypeId.of(ZONE, TYPE); assertNull(machineTypeId.project()); assertEquals(ZONE, machineTypeId.zone()); @@ -53,10 +53,10 @@ public void testOf() { @Test public void testToAndFromUrl() { MachineTypeId machineTypeId = MachineTypeId.of(PROJECT, ZONE, TYPE); - compareMachineTypeId(machineTypeId, MachineTypeId.fromUrl(machineTypeId.toUrl())); + compareMachineTypeId(machineTypeId, MachineTypeId.fromUrl(machineTypeId.selfLink())); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid machine type URL"); - machineTypeId = MachineTypeId.fromUrl("notMatchingUrl"); + MachineTypeId.fromUrl("notMatchingUrl"); } @Test @@ -68,7 +68,7 @@ public void testSetProjectId() { @Test public void testMatchesUrl() { - assertTrue(MachineTypeId.matchesUrl(MachineTypeId.of(PROJECT, ZONE, TYPE).toUrl())); + assertTrue(MachineTypeId.matchesUrl(MachineTypeId.of(PROJECT, ZONE, TYPE).selfLink())); assertFalse(MachineTypeId.matchesUrl("notMatchingUrl")); } @@ -77,7 +77,7 @@ private void compareMachineTypeId(MachineTypeId expected, MachineTypeId value) { assertEquals(expected.project(), expected.project()); assertEquals(expected.zone(), expected.zone()); assertEquals(expected.machineType(), expected.machineType()); - assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.selfLink(), expected.selfLink()); assertEquals(expected.hashCode(), expected.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java index fffacf28f6e9..969b370ddce8 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MachineTypeTest.java @@ -22,23 +22,22 @@ import org.junit.Test; -import java.math.BigInteger; import java.util.List; public class MachineTypeTest { - private static final BigInteger ID = BigInteger.valueOf(42L); - private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String ID = "42"; + private static final Long CREATION_TIMESTAMP = 1453293540000L; private static final String DESCRIPTION = "description"; private static final MachineTypeId MACHINE_TYPE_ID = MachineTypeId.of("project", "zone", "type"); - private static final Integer GUEST_CPUS = 1; + private static final Integer CPUS = 1; private static final Integer MEMORY_MB = 2; private static final List SCRATCH_DISKS = ImmutableList.of(3); private static final Integer MAXIMUM_PERSISTENT_DISKS = 4; private static final Long MAXIMUM_PERSISTENT_DISKS_SIZE_GB = 5L; - private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; - private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; - private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final Long DELETED = 1453293540000L; + private static final Long DEPRECATED = 1453293420000L; + private static final Long OBSOLETE = 1453293480000L; private static final DeprecationStatus.Status STATUS = DeprecationStatus.Status.DELETED; private static final DeprecationStatus DEPRECATION_STATUS = new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, MACHINE_TYPE_ID, STATUS); @@ -47,10 +46,9 @@ public class MachineTypeTest { .machineTypeId(MACHINE_TYPE_ID) .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) - .selfLink(MACHINE_TYPE_ID.toUrl()) - .guestCpus(GUEST_CPUS) + .cpus(CPUS) .memoryMb(MEMORY_MB) - .scratchDisks(SCRATCH_DISKS) + .scratchDisksSizeGb(SCRATCH_DISKS) .maximumPersistentDisks(MAXIMUM_PERSISTENT_DISKS) .maximumPersistentDisksSizeGb(MAXIMUM_PERSISTENT_DISKS_SIZE_GB) .deprecationStatus(DEPRECATION_STATUS) @@ -62,10 +60,9 @@ public void testBuilder() { assertEquals(MACHINE_TYPE_ID, MACHINE_TYPE.machineTypeId()); assertEquals(CREATION_TIMESTAMP, MACHINE_TYPE.creationTimestamp()); assertEquals(DESCRIPTION, MACHINE_TYPE.description()); - assertEquals(MACHINE_TYPE_ID.toUrl(), MACHINE_TYPE.selfLink()); - assertEquals(GUEST_CPUS, MACHINE_TYPE.guestCpus()); + assertEquals(CPUS, MACHINE_TYPE.cpus()); assertEquals(MEMORY_MB, MACHINE_TYPE.memoryMb()); - assertEquals(SCRATCH_DISKS, MACHINE_TYPE.scratchDisks()); + assertEquals(SCRATCH_DISKS, MACHINE_TYPE.scratchDisksSizeGb()); assertEquals(MAXIMUM_PERSISTENT_DISKS, MACHINE_TYPE.maximumPersistentDisks()); assertEquals(MAXIMUM_PERSISTENT_DISKS_SIZE_GB, MACHINE_TYPE.maximumPersistentDisksSizeGb()); assertEquals(DEPRECATION_STATUS, MACHINE_TYPE.deprecationStatus()); @@ -74,10 +71,7 @@ public void testBuilder() { @Test public void testToPbAndFromPb() { compareMachineTypes(MACHINE_TYPE, MachineType.fromPb(MACHINE_TYPE.toPb())); - MachineType machineType = MachineType.builder() - .machineTypeId(MACHINE_TYPE_ID) - .selfLink(MACHINE_TYPE_ID.toUrl()) - .build(); + MachineType machineType = MachineType.builder().machineTypeId(MACHINE_TYPE_ID).build(); compareMachineTypes(machineType, MachineType.fromPb(machineType.toPb())); } @@ -87,10 +81,9 @@ private void compareMachineTypes(MachineType expected, MachineType value) { assertEquals(expected.id(), value.id()); assertEquals(expected.creationTimestamp(), value.creationTimestamp()); assertEquals(expected.description(), value.description()); - assertEquals(expected.selfLink(), value.selfLink()); - assertEquals(expected.guestCpus(), value.guestCpus()); + assertEquals(expected.cpus(), value.cpus()); assertEquals(expected.memoryMb(), value.memoryMb()); - assertEquals(expected.scratchDisks(), value.scratchDisks()); + assertEquals(expected.scratchDisksSizeGb(), value.scratchDisksSizeGb()); assertEquals(expected.maximumPersistentDisks(), value.maximumPersistentDisks()); assertEquals(expected.maximumPersistentDisksSizeGb(), value.maximumPersistentDisksSizeGb()); assertEquals(expected.deprecationStatus(), value.deprecationStatus()); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java index adb94aee08fc..3474a73c3030 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionIdTest.java @@ -41,7 +41,7 @@ public void testOf() { RegionId regionId = RegionId.of(PROJECT, REGION); assertEquals(PROJECT, regionId.project()); assertEquals(REGION, regionId.region()); - assertEquals(URL, regionId.toUrl()); + assertEquals(URL, regionId.selfLink()); regionId = RegionId.of(REGION); assertNull(regionId.project()); assertEquals(REGION, regionId.region()); @@ -50,7 +50,7 @@ public void testOf() { @Test public void testToAndFromUrl() { RegionId regionId = RegionId.of(PROJECT, REGION); - compareRegionId(regionId, RegionId.fromUrl(regionId.toUrl())); + compareRegionId(regionId, RegionId.fromUrl(regionId.selfLink())); } @Test @@ -60,12 +60,12 @@ public void testSetProjectId() { compareRegionId(regionId, RegionId.of(REGION).setProjectId(PROJECT)); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid region URL"); - regionId = RegionId.fromUrl("notMatchingUrl"); + RegionId.fromUrl("notMatchingUrl"); } @Test public void testMatchesUrl() { - assertTrue(RegionId.matchesUrl(RegionId.of(PROJECT, REGION).toUrl())); + assertTrue(RegionId.matchesUrl(RegionId.of(PROJECT, REGION).selfLink())); assertFalse(RegionId.matchesUrl("notMatchingUrl")); } @@ -73,7 +73,7 @@ private void compareRegionId(RegionId expected, RegionId value) { assertEquals(expected, value); assertEquals(expected.project(), expected.project()); assertEquals(expected.region(), expected.region()); - assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.selfLink(), expected.selfLink()); assertEquals(expected.hashCode(), expected.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java index 94ca163f5b30..072823933110 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/RegionTest.java @@ -28,8 +28,8 @@ public class RegionTest { private static final RegionId REGION_ID = RegionId.of("project", "region"); - private static final BigInteger ID = BigInteger.valueOf(42L); - private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String ID = "42"; + private static final Long CREATION_TIMESTAMP = 1453293540000L; private static final String DESCRIPTION = "description"; private static final Region.Status STATUS = Region.Status.DOWN; private static final ZoneId ZONE_ID1 = ZoneId.of("project", "zone1"); @@ -40,9 +40,9 @@ public class RegionTest { private static final Region.Quota QUOTA2 = new Region.Quota("METRIC2", 4, 3); private static final List QUOTAS = ImmutableList.of(QUOTA1, QUOTA2); - private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; - private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; - private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final Long DELETED = 1453293540000L; + private static final Long DEPRECATED = 1453293420000L; + private static final Long OBSOLETE = 1453293480000L; private static final DeprecationStatus DEPRECATION_STATUS = new DeprecationStatus<>( DELETED, DEPRECATED, OBSOLETE, REGION_ID, DeprecationStatus.Status.DELETED); private static final Region REGION = Region.builder() @@ -50,7 +50,6 @@ public class RegionTest { .id(ID) .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) - .selfLink(REGION_ID.toUrl()) .status(STATUS) .zones(ZONES) .quotas(QUOTAS) @@ -63,7 +62,6 @@ public void testBuilder() { assertEquals(ID, REGION.id()); assertEquals(CREATION_TIMESTAMP, REGION.creationTimestamp()); assertEquals(DESCRIPTION, REGION.description()); - assertEquals(REGION_ID.toUrl(), REGION.selfLink()); assertEquals(STATUS, REGION.status()); assertEquals(ZONES, REGION.zones()); assertEquals(QUOTAS, REGION.quotas()); @@ -76,7 +74,7 @@ public void testToAndFromPb() { compareRegions(REGION, region); assertEquals(REGION_ID.project(), region.regionId().project()); assertEquals(REGION_ID.region(), region.regionId().region()); - region = Region.builder().regionId(REGION_ID).selfLink(REGION_ID.toUrl()).build(); + region = Region.builder().regionId(REGION_ID).build(); compareRegions(region, Region.fromPb(region.toPb())); } @@ -86,7 +84,6 @@ private void compareRegions(Region expected, Region value) { assertEquals(expected.id(), value.id()); assertEquals(expected.creationTimestamp(), value.creationTimestamp()); assertEquals(expected.description(), value.description()); - assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.status(), value.status()); assertEquals(expected.zones(), value.zones()); assertEquals(expected.quotas(), value.quotas()); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java index 04f52d9aea17..74c062fd6c27 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SerializationTest.java @@ -32,13 +32,12 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.math.BigInteger; import java.util.List; public class SerializationTest { - private static final BigInteger ID = BigInteger.valueOf(42L); - private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String ID = "42"; + private static final Long CREATION_TIMESTAMP = 1453293540000L; private static final String DESCRIPTION = "description"; private static final String VALID_DISK_SIZE = "10GB-10TB"; private static final Long DEFAULT_DISK_SIZE_GB = 10L; @@ -49,7 +48,6 @@ public class SerializationTest { .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) .validDiskSize(VALID_DISK_SIZE) - .selfLink(DISK_TYPE_ID.toUrl()) .defaultDiskSizeGb(DEFAULT_DISK_SIZE_GB) .build(); private static final MachineTypeId MACHINE_TYPE_ID = MachineTypeId.of("project", "zone", "type"); @@ -63,10 +61,9 @@ public class SerializationTest { .machineTypeId(MACHINE_TYPE_ID) .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) - .selfLink(MACHINE_TYPE_ID.toUrl()) - .guestCpus(GUEST_CPUS) + .cpus(GUEST_CPUS) .memoryMb(MEMORY_MB) - .scratchDisks(SCRATCH_DISKS) + .scratchDisksSizeGb(SCRATCH_DISKS) .maximumPersistentDisks(MAXIMUM_PERSISTENT_DISKS) .maximumPersistentDisksSizeGb(MAXIMUM_PERSISTENT_DISKS_SIZE_GB) .build(); @@ -85,38 +82,37 @@ public class SerializationTest { .id(ID) .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) - .selfLink(REGION_ID.toUrl()) .status(REGION_STATUS) .zones(ZONES) .quotas(QUOTAS) .build(); private static final ZoneId ZONE_ID = ZoneId.of("project", "zone"); private static final Zone.Status ZONE_STATUS = Zone.Status.DOWN; + private static final Long BEGIN_TIME = 1453293420000L; + private static final Long END_TIME = 1453293480000L; private static final MaintenanceWindow WINDOW1 = new MaintenanceWindow("NAME1", "DESCRIPTION1", - "2016-01-20T04:39:00.210-08:00", "2016-01-21T04:39:00.210-08:00"); + BEGIN_TIME, END_TIME); private static final MaintenanceWindow WINDOW2 = new MaintenanceWindow("NAME2", "DESCRIPTION2", - "2016-01-21T04:39:00.210-08:00", "2016-01-22T04:39:00.210-08:00"); + BEGIN_TIME, END_TIME); private static final List WINDOWS = ImmutableList.of(WINDOW1, WINDOW2); private static final Zone ZONE = Zone.builder() .zoneId(ZONE_ID) .id(ID) .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) - .selfLink(ZONE_ID.toUrl()) .status(ZONE_STATUS) .maintenanceWindows(WINDOWS) .region(REGION_ID) .build(); - private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; - private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; - private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final Long DELETED = 1453293540000L; + private static final Long DEPRECATED = 1453293420000L; + private static final Long OBSOLETE = 1453293480000L; private static final DeprecationStatus DEPRECATION_STATUS = new DeprecationStatus<>(DELETED, DEPRECATED, OBSOLETE, MACHINE_TYPE_ID, DeprecationStatus.Status.DELETED); private static final LicenseId LICENSE_ID = LicenseId.of("project", "license"); private static final Boolean CHARGES_USE_FEE = true; - private static final String SELF_LINK = LICENSE_ID.toUrl(); - private static final License LICENSE = new License(LICENSE_ID, CHARGES_USE_FEE, SELF_LINK); + private static final License LICENSE = new License(LICENSE_ID, CHARGES_USE_FEE); @Test public void testServiceOptions() throws Exception { diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java index 0583e454690d..a147f0e21f63 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneIdTest.java @@ -41,7 +41,7 @@ public void testOf() { ZoneId zoneId = ZoneId.of(PROJECT, ZONE); assertEquals(PROJECT, zoneId.project()); assertEquals(ZONE, zoneId.zone()); - assertEquals(URL, zoneId.toUrl()); + assertEquals(URL, zoneId.selfLink()); zoneId = ZoneId.of(ZONE); assertNull(zoneId.project()); assertEquals(ZONE, zoneId.zone()); @@ -50,7 +50,7 @@ public void testOf() { @Test public void testToAndFromUrl() { ZoneId zoneId = ZoneId.of(PROJECT, ZONE); - compareZoneId(zoneId, ZoneId.fromUrl(zoneId.toUrl())); + compareZoneId(zoneId, ZoneId.fromUrl(zoneId.selfLink())); } @Test @@ -60,12 +60,12 @@ public void testSetProjectId() { compareZoneId(zoneId, ZoneId.of(ZONE).setProjectId(PROJECT)); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("notMatchingUrl is not a valid zone URL"); - zoneId = ZoneId.fromUrl("notMatchingUrl"); + ZoneId.fromUrl("notMatchingUrl"); } @Test public void testMatchesUrl() { - assertTrue(ZoneId.matchesUrl(ZoneId.of(PROJECT, ZONE).toUrl())); + assertTrue(ZoneId.matchesUrl(ZoneId.of(PROJECT, ZONE).selfLink())); assertFalse(ZoneId.matchesUrl("notMatchingUrl")); } @@ -73,7 +73,7 @@ private void compareZoneId(ZoneId expected, ZoneId value) { assertEquals(expected, value); assertEquals(expected.project(), expected.project()); assertEquals(expected.zone(), expected.zone()); - assertEquals(expected.toUrl(), expected.toUrl()); + assertEquals(expected.selfLink(), expected.selfLink()); assertEquals(expected.hashCode(), expected.hashCode()); } } diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java index 30cefd6b380a..bdc96d3d9069 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ZoneTest.java @@ -30,18 +30,20 @@ public class ZoneTest { private static final ZoneId ZONE_ID = ZoneId.of("project", "zone"); private static final RegionId REGION_ID = RegionId.of("project", "region"); - private static final BigInteger ID = BigInteger.valueOf(42L); - private static final String CREATION_TIMESTAMP = "2016-01-20T04:39:00.210-08:00"; + private static final String ID = "42"; + private static final Long CREATION_TIMESTAMP = 1453293540000L; private static final String DESCRIPTION = "description"; private static final Zone.Status STATUS = Zone.Status.DOWN; + private static final Long BEGIN_TIME = 1453293420000L; + private static final Long END_TIME = 1453293480000L; private static final MaintenanceWindow WINDOW1 = new MaintenanceWindow("NAME1", "DESCRIPTION1", - "2016-01-20T04:39:00.210-08:00", "2016-01-21T04:39:00.210-08:00"); + BEGIN_TIME, END_TIME); private static final MaintenanceWindow WINDOW2 = new MaintenanceWindow("NAME2", "DESCRIPTION2", - "2016-01-21T04:39:00.210-08:00", "2016-01-22T04:39:00.210-08:00"); + BEGIN_TIME, END_TIME); private static final List WINDOWS = ImmutableList.of(WINDOW1, WINDOW2); - private static final String DELETED = "2016-01-20T04:39:00.210-08:00"; - private static final String DEPRECATED = "2016-01-20T04:37:00.210-08:00"; - private static final String OBSOLETE = "2016-01-20T04:38:00.210-08:00"; + private static final Long DELETED = 1453293540000L; + private static final Long DEPRECATED = 1453293420000L; + private static final Long OBSOLETE = 1453293480000L; private static final DeprecationStatus DEPRECATION_STATUS = new DeprecationStatus<>( DELETED, DEPRECATED, OBSOLETE, ZONE_ID, DeprecationStatus.Status.DELETED); private static final Zone ZONE = Zone.builder() @@ -49,7 +51,6 @@ public class ZoneTest { .id(ID) .creationTimestamp(CREATION_TIMESTAMP) .description(DESCRIPTION) - .selfLink(ZONE_ID.toUrl()) .status(STATUS) .maintenanceWindows(WINDOWS) .deprecationStatus(DEPRECATION_STATUS) @@ -62,7 +63,6 @@ public void testBuilder() { assertEquals(ID, ZONE.id()); assertEquals(CREATION_TIMESTAMP, ZONE.creationTimestamp()); assertEquals(DESCRIPTION, ZONE.description()); - assertEquals(ZONE_ID.toUrl(), ZONE.selfLink()); assertEquals(STATUS, ZONE.status()); assertEquals(WINDOWS, ZONE.maintenanceWindows()); assertEquals(REGION_ID, ZONE.region()); @@ -72,12 +72,12 @@ public void testBuilder() { @Test public void testToAndFromPb() { com.google.api.services.compute.model.Zone zonePb = ZONE.toPb(); - assertEquals(REGION_ID.toUrl(), zonePb.getRegion()); + assertEquals(REGION_ID.selfLink(), zonePb.getRegion()); Zone zone = Zone.fromPb(zonePb); compareZones(ZONE, zone); assertEquals(ZONE_ID.project(), zone.zoneId().project()); assertEquals(ZONE_ID.zone(), zone.zoneId().zone()); - zone = Zone.builder().zoneId(ZONE_ID).selfLink(ZONE_ID.toUrl()).build(); + zone = Zone.builder().zoneId(ZONE_ID).build(); compareZones(zone, Zone.fromPb(zone.toPb())); } @@ -87,7 +87,6 @@ private void compareZones(Zone expected, Zone value) { assertEquals(expected.id(), value.id()); assertEquals(expected.creationTimestamp(), value.creationTimestamp()); assertEquals(expected.description(), value.description()); - assertEquals(expected.selfLink(), value.selfLink()); assertEquals(expected.status(), value.status()); assertEquals(expected.maintenanceWindows(), value.maintenanceWindows()); assertEquals(expected.region(), value.region()); From 5c6d5ca8eb899e74f56c3858326e631247d78bdd Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 29 Feb 2016 19:50:42 +0100 Subject: [PATCH 09/10] Remove tab indentation from compute's pom.xml --- gcloud-java-compute/pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gcloud-java-compute/pom.xml b/gcloud-java-compute/pom.xml index 419c7c4780a2..5bf336e01a2f 100644 --- a/gcloud-java-compute/pom.xml +++ b/gcloud-java-compute/pom.xml @@ -27,10 +27,10 @@ v1-rev97-1.21.0 compile - - com.google.guava - guava-jdk5 - + + com.google.guava + guava-jdk5 + From 9873ef6afdf8e09073370586f8fed9b83798ef1e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 1 Mar 2016 12:01:37 +0100 Subject: [PATCH 10/10] Replace com.google.api.client.util.DateTime with Joda formatter --- .../gcloud/compute/DeprecationStatus.java | 17 ++++++++++------- .../com/google/gcloud/compute/DiskType.java | 10 ++++++---- .../com/google/gcloud/compute/MachineType.java | 9 ++++++--- .../java/com/google/gcloud/compute/Region.java | 9 ++++++--- .../java/com/google/gcloud/compute/Zone.java | 17 ++++++++++------- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java index 20123d648598..c80071dde10a 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DeprecationStatus.java @@ -16,10 +16,12 @@ package com.google.gcloud.compute; -import com.google.api.client.util.DateTime; import com.google.common.base.Function; import com.google.common.base.MoreObjects; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + import java.io.Serializable; import java.util.Objects; @@ -31,6 +33,7 @@ public final class DeprecationStatus implements Serializable { private static final long serialVersionUID = -2695077634793679794L; + private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime(); private final Long deleted; private final Long deprecated; @@ -134,13 +137,13 @@ com.google.api.services.compute.model.DeprecationStatus toPb() { com.google.api.services.compute.model.DeprecationStatus deprecationStatusPb = new com.google.api.services.compute.model.DeprecationStatus(); if (deleted != null) { - deprecationStatusPb.setDeleted(new DateTime(deleted).toStringRfc3339()); + deprecationStatusPb.setDeleted(TIMESTAMP_FORMATTER.print(deleted)); } if (deprecated != null) { - deprecationStatusPb.setDeprecated(new DateTime(deprecated).toStringRfc3339()); + deprecationStatusPb.setDeprecated(TIMESTAMP_FORMATTER.print(deprecated)); } if (obsolete != null) { - deprecationStatusPb.setObsolete(new DateTime(obsolete).toStringRfc3339()); + deprecationStatusPb.setObsolete(TIMESTAMP_FORMATTER.print(obsolete)); } if (replacement != null) { deprecationStatusPb.setReplacement(replacement.selfLink()); @@ -156,11 +159,11 @@ static DeprecationStatus fromPb( Function fromUrl) { return new DeprecationStatus<>( deprecationStatusPb.getDeleted() != null - ? DateTime.parseRfc3339(deprecationStatusPb.getDeleted()).getValue() : null, + ? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getDeleted()) : null, deprecationStatusPb.getDeprecated() != null - ? DateTime.parseRfc3339(deprecationStatusPb.getDeprecated()).getValue() : null, + ? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getDeprecated()) : null, deprecationStatusPb.getObsolete() != null - ? DateTime.parseRfc3339(deprecationStatusPb.getObsolete()).getValue() : null, + ? TIMESTAMP_FORMATTER.parseMillis(deprecationStatusPb.getObsolete()) : null, deprecationStatusPb.getReplacement() != null ? fromUrl.apply(deprecationStatusPb.getReplacement()) : null, deprecationStatusPb.getState() != null diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java index f25aa3ac65a4..ce57067786c4 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/DiskType.java @@ -16,10 +16,12 @@ package com.google.gcloud.compute; -import com.google.api.client.util.DateTime; import com.google.common.base.Function; import com.google.common.base.MoreObjects; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + import java.io.Serializable; import java.math.BigInteger; import java.util.Objects; @@ -48,6 +50,7 @@ public com.google.api.services.compute.model.DiskType apply(DiskType diskType) { }; private static final long serialVersionUID = -944042261695072026L; + private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime(); private final String id; private final DiskTypeId diskTypeId; @@ -199,7 +202,7 @@ com.google.api.services.compute.model.DiskType toPb() { diskTypePb.setId(new BigInteger(id)); } if (creationTimestamp != null) { - diskTypePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + diskTypePb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp)); } diskTypePb.setDescription(description); diskTypePb.setValidDiskSize(validDiskSize); @@ -222,8 +225,7 @@ static DiskType fromPb(com.google.api.services.compute.model.DiskType diskTypePb builder.id(diskTypePb.getId().toString()); } if (diskTypePb.getCreationTimestamp() != null) { - builder.creationTimestamp( - DateTime.parseRfc3339(diskTypePb.getCreationTimestamp()).getValue()); + builder.creationTimestamp(TIMESTAMP_FORMATTER.parseMillis(diskTypePb.getCreationTimestamp())); } builder.diskTypeId(DiskTypeId.fromUrl(diskTypePb.getSelfLink())); builder.description(diskTypePb.getDescription()); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java index df0109afa1f5..ee242c0d1ef0 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/MachineType.java @@ -16,12 +16,14 @@ package com.google.gcloud.compute; -import com.google.api.client.util.DateTime; import com.google.api.services.compute.model.MachineType.ScratchDisks; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.Lists; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + import java.io.Serializable; import java.math.BigInteger; import java.util.List; @@ -52,6 +54,7 @@ public com.google.api.services.compute.model.MachineType apply(MachineType type) return type.toPb(); } }; + private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime(); private static final long serialVersionUID = -4210962597502860450L; @@ -255,7 +258,7 @@ com.google.api.services.compute.model.MachineType toPb() { machineTypePb.setId(new BigInteger(id)); } if (creationTimestamp != null) { - machineTypePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + machineTypePb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp)); } machineTypePb.setName(machineTypeId.machineType()); machineTypePb.setDescription(description); @@ -292,7 +295,7 @@ static MachineType fromPb(com.google.api.services.compute.model.MachineType mach } if (machineTypePb.getCreationTimestamp() != null) { builder.creationTimestamp( - DateTime.parseRfc3339(machineTypePb.getCreationTimestamp()).getValue()); + TIMESTAMP_FORMATTER.parseMillis(machineTypePb.getCreationTimestamp())); } builder.description(machineTypePb.getDescription()); builder.cpus(machineTypePb.getGuestCpus()); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java index add58d8009b0..38f79a691721 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Region.java @@ -16,12 +16,14 @@ package com.google.gcloud.compute; -import com.google.api.client.util.DateTime; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + import java.io.Serializable; import java.math.BigInteger; import java.util.List; @@ -50,6 +52,7 @@ public com.google.api.services.compute.model.Region apply(Region region) { }; private static final long serialVersionUID = -3578710133393645135L; + private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime(); private final RegionId regionId; private final String id; @@ -319,7 +322,7 @@ com.google.api.services.compute.model.Region toPb() { regionPb.setId(new BigInteger(id)); } if (creationTimestamp != null) { - regionPb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + regionPb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp)); } regionPb.setName(regionId.region()); regionPb.setDescription(description); @@ -350,7 +353,7 @@ static Region fromPb(com.google.api.services.compute.model.Region regionPb) { builder.id(regionPb.getId().toString()); } if (regionPb.getCreationTimestamp() != null) { - builder.creationTimestamp(DateTime.parseRfc3339(regionPb.getCreationTimestamp()).getValue()); + builder.creationTimestamp(TIMESTAMP_FORMATTER.parseMillis(regionPb.getCreationTimestamp())); } builder.description(regionPb.getDescription()); if (regionPb.getStatus() != null) { diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java index 48bb5412b605..20c64946d7ce 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Zone.java @@ -16,12 +16,14 @@ package com.google.gcloud.compute; -import com.google.api.client.util.DateTime; import com.google.api.services.compute.model.Zone.MaintenanceWindows; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.Lists; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + import java.io.Serializable; import java.math.BigInteger; import java.util.List; @@ -50,6 +52,7 @@ public com.google.api.services.compute.model.Zone apply(Zone region) { }; private static final long serialVersionUID = 6113636504417213010L; + private static final DateTimeFormatter TIMESTAMP_FORMATTER = ISODateTimeFormat.dateTime(); private final ZoneId zoneId; private final String id; @@ -162,16 +165,16 @@ MaintenanceWindows toPb() { return new MaintenanceWindows() .setName(name) .setDescription(description) - .setBeginTime(beginTime != null ? new DateTime(beginTime).toStringRfc3339() : null) - .setEndTime(endTime != null ? new DateTime(endTime).toStringRfc3339() : null); + .setBeginTime(beginTime != null ? TIMESTAMP_FORMATTER.print(beginTime) : null) + .setEndTime(endTime != null ? TIMESTAMP_FORMATTER.print(endTime) : null); } static MaintenanceWindow fromPb(MaintenanceWindows windowPb) { return new MaintenanceWindow(windowPb.getName(), windowPb.getDescription(), windowPb.getBeginTime() != null - ? DateTime.parseRfc3339(windowPb.getBeginTime()).getValue() : null, + ? TIMESTAMP_FORMATTER.parseMillis(windowPb.getBeginTime()) : null, windowPb.getEndTime() != null - ? DateTime.parseRfc3339(windowPb.getEndTime()).getValue() : null); + ? TIMESTAMP_FORMATTER.parseMillis(windowPb.getEndTime()) : null); } } @@ -338,7 +341,7 @@ com.google.api.services.compute.model.Zone toPb() { zonePb.setId(new BigInteger(id)); } if (creationTimestamp != null) { - zonePb.setCreationTimestamp(new DateTime(creationTimestamp).toStringRfc3339()); + zonePb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp)); } zonePb.setName(zoneId.zone()); zonePb.setDescription(description); @@ -370,7 +373,7 @@ static Zone fromPb(com.google.api.services.compute.model.Zone zonePb) { builder.id(zonePb.getId().toString()); } if (zonePb.getCreationTimestamp() != null) { - builder.creationTimestamp(DateTime.parseRfc3339(zonePb.getCreationTimestamp()).getValue()); + builder.creationTimestamp(TIMESTAMP_FORMATTER.parseMillis(zonePb.getCreationTimestamp())); } builder.description(zonePb.getDescription()); if (zonePb.getStatus() != null) {