From f7b74301548aa93f0798dda73d76719875c46da1 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 26 Apr 2016 15:04:26 +0200 Subject: [PATCH] Move InstanceInfo inner classes outside --- .../google/gcloud/compute/InstanceInfo.java | 608 ------------------ .../com/google/gcloud/compute/Metadata.java | 212 ++++++ .../gcloud/compute/NetworkInterface.java | 2 +- .../gcloud/compute/SchedulingOptions.java | 150 +++++ .../google/gcloud/compute/ServiceAccount.java | 123 ++++ .../java/com/google/gcloud/compute/Tags.java | 227 +++++++ .../gcloud/compute/InstanceInfoTest.java | 144 +---- .../google/gcloud/compute/MetadataTest.java | 81 +++ .../gcloud/compute/SchedulingOptionsTest.java | 57 ++ .../gcloud/compute/SerializationTest.java | 4 - .../gcloud/compute/ServiceAccountTest.java | 47 ++ .../com/google/gcloud/compute/TagsTest.java | 69 ++ 12 files changed, 968 insertions(+), 756 deletions(-) create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/Metadata.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/SchedulingOptions.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/ServiceAccount.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/Tags.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/MetadataTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/SchedulingOptionsTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/ServiceAccountTest.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/TagsTest.java diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/InstanceInfo.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/InstanceInfo.java index b26d0994e7af..0ce3e8f1bb76 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/InstanceInfo.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/InstanceInfo.java @@ -22,9 +22,7 @@ import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; @@ -33,7 +31,6 @@ import java.math.BigInteger; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.Objects; /** @@ -123,611 +120,6 @@ public enum Status { TERMINATED } - /** - * A list of tags for a Google Compute Engine Instance; with associated fingerprint. Tags are used - * to identify valid sources or targets for network firewalls and are specified by the client - * during instance creation. Each tag within the list must comply with RFC1035. - * - * @see RFC1035 - */ - public static final class Tags implements Serializable { - - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public Tags apply(com.google.api.services.compute.model.Tags pb) { - return Tags.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public com.google.api.services.compute.model.Tags apply(Tags tags) { - return tags.toPb(); - } - }; - - private static final long serialVersionUID = 5627093820497225322L; - - private final List values; - private final String fingerprint; - - /** - * A builder for {@code Tags} objects. - */ - public static final class Builder { - - private List values; - private String fingerprint; - - private Builder() { - values = Lists.newArrayList(); - } - - private Builder(Tags tags) { - this.values = tags.values != null ? Lists.newArrayList(tags.values) - : Lists.newArrayList(); - this.fingerprint = tags.fingerprint; - } - - /** - * Sets a list of tags to apply to an instance. Tags are used to identify valid sources or - * targets for network firewalls. Each tag within the list must comply with RFC1035. - * - * @see RFC1035 - */ - public Builder values(Iterable values) { - this.values = Lists.newArrayList(values); - return this; - } - - /** - * Sets a list of tags to apply to an instance. Tags are used to identify valid sources or - * targets for network firewalls. Each tag within the list must comply with RFC1035. - * - * @see RFC1035 - */ - public Builder values(String... values) { - this.values = Lists.newArrayList(Arrays.asList(checkNotNull(values))); - return this; - } - - /** - * Adds a tag to the list of tags. Tags are used to identify valid sources or targets for - * network firewalls. The tag must comply with RFC1035. - * - * @see RFC1035 - */ - public Builder add(String tag) { - this.values.add(tag); - return this; - } - - /** - * Sets the fingerprint for the tags. This value is needed to update instance's tags. - */ - public Builder fingerprint(String fingerprint) { - this.fingerprint = fingerprint; - return this; - } - - /** - * Creates a {@code Tags} object. - */ - public Tags build() { - return new Tags(this); - } - } - - private Tags(Builder builder) { - this.values = ImmutableList.copyOf(builder.values); - this.fingerprint = builder.fingerprint; - } - - /** - * Returns a list of tags to apply to an instance. Tags are used to identify valid sources or - * targets for network firewalls. Each tag within the list must comply with RFC1035. - * - * @see RFC1035 - */ - public List values() { - return values; - } - - /** - * Returns the fingerprint for the tags. This value is needed to update instance's tags. - */ - public String fingerprint() { - return fingerprint; - } - - /** - * Returns a builder for the current instance tags. - */ - public Builder toBuilder() { - return new Builder(this); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("values", values) - .add("fingerprint", fingerprint) - .toString(); - } - - @Override - public int hashCode() { - return Objects.hash(values, fingerprint); - } - - @Override - public boolean equals(Object obj) { - return obj == this || obj instanceof Tags && Objects.equals(toPb(), ((Tags) obj).toPb()); - } - - com.google.api.services.compute.model.Tags toPb() { - com.google.api.services.compute.model.Tags tagsPb = - new com.google.api.services.compute.model.Tags(); - tagsPb.setFingerprint(fingerprint); - tagsPb.setItems(values); - return tagsPb; - } - - /** - * Returns a builder for a {@code Tags} object given the tags to apply to the instance. Each tag - * within the list must comply with RFC1035. - * - * @see RFC1035 - */ - public static Builder builder(Iterable values) { - return new Builder().values(values); - } - - /** - * Returns a builder for a {@code Tags} object given the tags to apply to the instance. Each tag - * within the list must comply with RFC1035. - * - * @see RFC1035 - */ - public static Builder builder(String... values) { - return new Builder().values(values); - } - - /** - * Returns a {@code Tags} object given the tags to apply to the instance. Each tag within the - * list must comply with RFC1035. - * - * @see RFC1035 - */ - public static Tags of(Iterable values) { - return builder(values).build(); - } - - /** - * Returns a {@code Tags} object given the tags to apply to the instance. Each tag within the - * list must comply with RFC1035. - * - * @see RFC1035 - */ - public static Tags of(String... values) { - return builder(values).build(); - } - - static Tags fromPb(com.google.api.services.compute.model.Tags tagsPb) { - Builder builder = - builder(tagsPb.getItems() != null ? tagsPb.getItems() : ImmutableList.of()); - return builder.fingerprint(tagsPb.getFingerprint()).build(); - } - } - - /** - * Metadata for Google Compute Engine Instance as ket/value pairs. This includes custom metadata - * and predefined keys. - * - * @see Metadata - */ - public static final class Metadata implements Serializable { - - static final Function - FROM_PB_FUNCTION = - new Function() { - @Override - public Metadata apply(com.google.api.services.compute.model.Metadata pb) { - return Metadata.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public com.google.api.services.compute.model.Metadata apply(Metadata metadata) { - return metadata.toPb(); - } - }; - - private static final long serialVersionUID = -945038809838910107L; - - private final Map values; - private final String fingerprint; - - /** - * A builder for {@code Metadata} objects. - */ - public static final class Builder { - - private Map values; - private String fingerprint; - - Builder() { - values = Maps.newHashMap(); - } - - Builder(Metadata metadata) { - this.values = metadata.values != null ? Maps.newHashMap(metadata.values) - : Maps.newHashMap(); - this.fingerprint = metadata.fingerprint; - } - - /** - * Sets the metadata for the instance as key/value pairs. The total size of all keys and - * values must be less than 512 KB. Keys must conform to the following regexp: - * {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of - * a URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with - * any other metadata keys for the project. Values must be less than or equal to 32768 bytes. - */ - public Builder values(Map values) { - this.values = Maps.newHashMap(checkNotNull(values)); - return this; - } - - /** - * Adds a key/value pair to the instance metadata. The total size of all keys and values must - * be less than 512 KB. Keys must conform to the following regexp: {@code [a-zA-Z0-9-_]+}, and - * be less than 128 bytes in length. This is reflected as part of a URL in the metadata - * server. Additionally, to avoid ambiguity, keys must not conflict with any other metadata - * keys for the project. Values must be less than or equal to 32768 bytes. - */ - public Builder add(String key, String value) { - this.values.put(key, value); - return this; - } - - /** - * Sets the fingerprint for the metadata. This value can be used to update instance's - * metadata. - */ - public Builder fingerprint(String fingerprint) { - this.fingerprint = fingerprint; - return this; - } - - /** - * Creates a {@code Metadata} object. - */ - public Metadata build() { - return new Metadata(this); - } - } - - private Metadata(Builder builder) { - this.values = ImmutableMap.copyOf(builder.values); - this.fingerprint = builder.fingerprint; - } - - /** - * Returns instance's metadata as key/value pairs. - */ - public Map values() { - return values; - } - - /** - * Returns the fingerprint for the metadata. This value can be used to update instance's - * metadata. - */ - public String fingerprint() { - return fingerprint; - } - - /** - * Returns a builder for the current instance metadata. - */ - public Builder toBuilder() { - return new Builder(this); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("values", values) - .add("fingerprint", fingerprint) - .toString(); - } - - @Override - public int hashCode() { - return Objects.hash(values, fingerprint); - } - - @Override - public boolean equals(Object obj) { - return obj == this - || obj instanceof Metadata - && Objects.equals(toPb(), ((Metadata) obj).toPb()); - } - - com.google.api.services.compute.model.Metadata toPb() { - com.google.api.services.compute.model.Metadata metadataPb = - new com.google.api.services.compute.model.Metadata(); - metadataPb.setFingerprint(fingerprint); - List itemsPb = - Lists.newArrayListWithCapacity(values.size()); - for (Map.Entry entry : values.entrySet()) { - itemsPb.add(new com.google.api.services.compute.model.Metadata.Items() - .setKey(entry.getKey()).setValue(entry.getValue())); - } - metadataPb.setItems(itemsPb); - metadataPb.setFingerprint(fingerprint); - return metadataPb; - } - - /** - * Returns a builder for a {@code Metadata} object. - */ - public static Builder builder() { - return new Builder(); - } - - /** - * Returns a {@code Metadata} object given the the metadata as a map. The total size of all keys - * and values must be less than 512 KB. Keys must conform to the following regexp: - * {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of a - * URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with any - * other metadata keys for the project. Values must be less than or equal to 32768 bytes. - */ - public static Metadata of(Map values) { - return builder().values(values).build(); - } - - static Metadata fromPb(com.google.api.services.compute.model.Metadata metadataPb) { - Builder builder = builder(); - if (metadataPb.getItems() != null) { - Map metadataValues = - Maps.newHashMapWithExpectedSize(metadataPb.getItems().size()); - for (com.google.api.services.compute.model.Metadata.Items item : metadataPb.getItems()) { - metadataValues.put(item.getKey(), item.getValue()); - } - builder.values(metadataValues); - } - return builder.fingerprint(metadataPb.getFingerprint()).build(); - } - } - - /** - * A service account, with its specified scopes, authorized for this instance. - * - * @see Authenticating from Google - * Compute Engine - */ - public static final class ServiceAccount implements Serializable { - - static final Function - FROM_PB_FUNCTION = - new Function() { - @Override - public ServiceAccount apply(com.google.api.services.compute.model.ServiceAccount pb) { - return ServiceAccount.fromPb(pb); - } - }; - static final Function - TO_PB_FUNCTION = - new Function() { - @Override - public com.google.api.services.compute.model.ServiceAccount apply( - ServiceAccount metadata) { - return metadata.toPb(); - } - }; - - private static final long serialVersionUID = 4199610694227857331L; - - private final String email; - private final List scopes; - - private ServiceAccount(String email, List scopes) { - this.email = email; - this.scopes = ImmutableList.copyOf(scopes); - } - - /** - * Returns the email address of the service account. - */ - public String email() { - return email; - } - - /** - * Returns the list of scopes to be made available for this service account. - */ - public List scopes() { - return scopes; - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("email", email) - .add("scopes", scopes) - .toString(); - } - - @Override - public int hashCode() { - return Objects.hash(email, scopes); - } - - @Override - public boolean equals(Object obj) { - return obj == this - || obj instanceof ServiceAccount - && Objects.equals(toPb(), ((ServiceAccount) obj).toPb()); - } - - com.google.api.services.compute.model.ServiceAccount toPb() { - com.google.api.services.compute.model.ServiceAccount serviceAccountPb = - new com.google.api.services.compute.model.ServiceAccount(); - serviceAccountPb.setEmail(email); - serviceAccountPb.setScopes(scopes); - return serviceAccountPb; - } - - /** - * Returns a {@code ServiceAccount} object for the provided email and scopes. - */ - public static ServiceAccount of(String email, List scopes) { - return new ServiceAccount(email, scopes); - } - - /** - * Returns a {@code ServiceAccount} object for the provided email and scopes. - */ - public static ServiceAccount of(String email, String... scopes) { - return of(email, Arrays.asList(scopes)); - } - - static ServiceAccount fromPb(com.google.api.services.compute.model.ServiceAccount accountPb) { - return new ServiceAccount(accountPb.getEmail(), accountPb.getScopes()); - } - } - - /** - * A Google Compute Engine instance scheduling options. When there are system events that might - * cause your instances to be disrupted, Google Compute Engine automatically manages the - * scheduling decisions for your instances. Use {@code SchedulingOptions.preemptible()} to create - * a preemptible instance. Use {@code SchedulingOptions.standard()} to configure scheduling - * options for a standard instance. - * - * @see - * Setting Instance Scheduling Options - */ - public static final class SchedulingOptions implements Serializable { - - private static final long serialVersionUID = 4199610694227857331L; - - private final boolean automaticRestart; - private final Maintenance maintenance; - private final boolean isPreemptible; - - /** - * Defines the maintenance behavior for this instance. - */ - public enum Maintenance { - /** - * The default behavior for standard instances. - */ - MIGRATE, - - /** - * The default and only possible behavior for preemptible instances. - */ - TERMINATE - } - - private SchedulingOptions(Boolean automaticRestart, Maintenance maintenance, - Boolean isPreemptible) { - this.automaticRestart = automaticRestart; - this.maintenance = maintenance; - this.isPreemptible = isPreemptible; - } - - /** - * Returns whether the instance should be automatically restarted if it is terminated by Compute - * Engine (not terminated by a user). - */ - public Boolean automaticRestart() { - return automaticRestart; - } - - /** - * Returns the maintenance behavior for the instance. - */ - public Maintenance maintenance() { - return maintenance; - } - - /** - * Returns {@code true} if the instance is preemptible, {@code false} otherwhise. - * - * @see Preemptible - * Instance - */ - public boolean isPreemptible() { - return isPreemptible; - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("automaticRestart", automaticRestart) - .add("maintenance", maintenance) - .add("isPreemptible", isPreemptible) - .toString(); - } - - @Override - public int hashCode() { - return Objects.hash(automaticRestart, maintenance, isPreemptible); - } - - @Override - public boolean equals(Object obj) { - return obj == this - || obj instanceof SchedulingOptions - && Objects.equals(toPb(), ((SchedulingOptions) obj).toPb()); - } - - com.google.api.services.compute.model.Scheduling toPb() { - com.google.api.services.compute.model.Scheduling schedulingPb = - new com.google.api.services.compute.model.Scheduling(); - schedulingPb.setAutomaticRestart(automaticRestart); - schedulingPb.setPreemptible(isPreemptible); - if (maintenance != null) { - schedulingPb.setOnHostMaintenance(maintenance.name()); - } - return schedulingPb; - } - - /** - * Returns a {@code SchedulingOptions} object for a preemptible instance. - * - * @see Preemptible - * Instance - */ - public static SchedulingOptions preemptible() { - return new SchedulingOptions(false, Maintenance.TERMINATE, true); - } - - /** - * Returns a {@code SchedulingOptions} object for a standard instance. - * - * @param automaticRestart specifies whether the instance should be automatically restarted if - * it is terminated by Compute Engine (not terminated by a user) - * @param maintenance defines the maintenance behavior for the instance - */ - public static SchedulingOptions standard(boolean automaticRestart, Maintenance maintenance) { - return new SchedulingOptions(automaticRestart, maintenance, false); - } - - static SchedulingOptions fromPb(com.google.api.services.compute.model.Scheduling schedPb) { - Maintenance maintenance = null; - if (schedPb.getOnHostMaintenance() != null) { - maintenance = Maintenance.valueOf(schedPb.getOnHostMaintenance()); - } - return new SchedulingOptions(schedPb.getAutomaticRestart(), maintenance, - schedPb.getPreemptible()); - } - } - /** * A builder for {@code InstanceInfo} objects. */ diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Metadata.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Metadata.java new file mode 100644 index 000000000000..bf489caf141d --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Metadata.java @@ -0,0 +1,212 @@ +/* + * 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 com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * Metadata for Google Compute Engine Instance as ket/value pairs. This includes custom metadata + * and predefined keys. + * + * @see Metadata + */ +public final class Metadata implements Serializable { + + static final Function + FROM_PB_FUNCTION = + new Function() { + @Override + public Metadata apply(com.google.api.services.compute.model.Metadata pb) { + return Metadata.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.Metadata apply(Metadata metadata) { + return metadata.toPb(); + } + }; + + private static final long serialVersionUID = -945038809838910107L; + + private final Map values; + private final String fingerprint; + + /** + * A builder for {@code Metadata} objects. + */ + public static final class Builder { + + private Map values; + private String fingerprint; + + Builder() { + values = Maps.newHashMap(); + } + + Builder(Metadata metadata) { + this.values = metadata.values != null ? Maps.newHashMap(metadata.values) + : Maps.newHashMap(); + this.fingerprint = metadata.fingerprint; + } + + /** + * Sets the metadata for the instance as key/value pairs. The total size of all keys and + * values must be less than 512 KB. Keys must conform to the following regexp: + * {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of + * a URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with + * any other metadata keys for the project. Values must be less than or equal to 32768 bytes. + */ + public Builder values(Map values) { + this.values = Maps.newHashMap(checkNotNull(values)); + return this; + } + + /** + * Adds a key/value pair to the instance metadata. The total size of all keys and values must + * be less than 512 KB. Keys must conform to the following regexp: {@code [a-zA-Z0-9-_]+}, and + * be less than 128 bytes in length. This is reflected as part of a URL in the metadata + * server. Additionally, to avoid ambiguity, keys must not conflict with any other metadata + * keys for the project. Values must be less than or equal to 32768 bytes. + */ + public Builder add(String key, String value) { + this.values.put(key, value); + return this; + } + + /** + * Sets the fingerprint for the metadata. This value can be used to update instance's + * metadata. + */ + public Builder fingerprint(String fingerprint) { + this.fingerprint = fingerprint; + return this; + } + + /** + * Creates a {@code Metadata} object. + */ + public Metadata build() { + return new Metadata(this); + } + } + + private Metadata(Builder builder) { + this.values = ImmutableMap.copyOf(builder.values); + this.fingerprint = builder.fingerprint; + } + + /** + * Returns instance's metadata as key/value pairs. + */ + public Map values() { + return values; + } + + /** + * Returns the fingerprint for the metadata. This value can be used to update instance's + * metadata. + */ + public String fingerprint() { + return fingerprint; + } + + /** + * Returns a builder for the current instance metadata. + */ + public Builder toBuilder() { + return new Builder(this); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("values", values) + .add("fingerprint", fingerprint) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(values, fingerprint); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj instanceof Metadata + && Objects.equals(toPb(), ((Metadata) obj).toPb()); + } + + com.google.api.services.compute.model.Metadata toPb() { + com.google.api.services.compute.model.Metadata metadataPb = + new com.google.api.services.compute.model.Metadata(); + metadataPb.setFingerprint(fingerprint); + List itemsPb = + Lists.newArrayListWithCapacity(values.size()); + for (Map.Entry entry : values.entrySet()) { + itemsPb.add(new com.google.api.services.compute.model.Metadata.Items() + .setKey(entry.getKey()).setValue(entry.getValue())); + } + metadataPb.setItems(itemsPb); + metadataPb.setFingerprint(fingerprint); + return metadataPb; + } + + /** + * Returns a builder for a {@code Metadata} object. + */ + public static Builder builder() { + return new Builder(); + } + + /** + * Returns a {@code Metadata} object given the the metadata as a map. The total size of all keys + * and values must be less than 512 KB. Keys must conform to the following regexp: + * {@code [a-zA-Z0-9-_]+}, and be less than 128 bytes in length. This is reflected as part of a + * URL in the metadata server. Additionally, to avoid ambiguity, keys must not conflict with any + * other metadata keys for the project. Values must be less than or equal to 32768 bytes. + */ + public static Metadata of(Map values) { + return builder().values(values).build(); + } + + static Metadata fromPb(com.google.api.services.compute.model.Metadata metadataPb) { + Builder builder = builder(); + if (metadataPb.getItems() != null) { + Map metadataValues = + Maps.newHashMapWithExpectedSize(metadataPb.getItems().size()); + for (com.google.api.services.compute.model.Metadata.Items item : metadataPb.getItems()) { + metadataValues.put(item.getKey(), item.getValue()); + } + builder.values(metadataValues); + } + return builder.fingerprint(metadataPb.getFingerprint()).build(); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/NetworkInterface.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/NetworkInterface.java index cf937de68ed6..8226eae7b56c 100644 --- a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/NetworkInterface.java +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/NetworkInterface.java @@ -493,7 +493,7 @@ static NetworkInterface fromPb( com.google.api.services.compute.model.NetworkInterface interfacePb) { Builder builder = builder(NetworkId.fromUrl(interfacePb.getNetwork())) .name(interfacePb.getName()); - if (interfacePb.getSubnetwork() != null){ + if (interfacePb.getSubnetwork() != null) { builder.subnetwork(SubnetworkId.fromUrl(interfacePb.getSubnetwork())); } builder.networkIp(interfacePb.getNetworkIP()); diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/SchedulingOptions.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/SchedulingOptions.java new file mode 100644 index 000000000000..c5d7c293cc10 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/SchedulingOptions.java @@ -0,0 +1,150 @@ +/* + * 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; + +/** + * A Google Compute Engine instance scheduling options. When there are system events that might + * cause your instances to be disrupted, Google Compute Engine automatically manages the + * scheduling decisions for your instances. Use {@code SchedulingOptions.preemptible()} to create + * a preemptible instance. Use {@code SchedulingOptions.standard()} to configure scheduling + * options for a standard instance. + * + * @see + * Setting Instance Scheduling Options + */ +public final class SchedulingOptions implements Serializable { + + private static final long serialVersionUID = 4199610694227857331L; + + private final boolean automaticRestart; + private final Maintenance maintenance; + private final boolean isPreemptible; + + /** + * Defines the maintenance behavior for this instance. + */ + public enum Maintenance { + /** + * The default behavior for standard instances. + */ + MIGRATE, + + /** + * The default and only possible behavior for preemptible instances. + */ + TERMINATE + } + + private SchedulingOptions(Boolean automaticRestart, Maintenance maintenance, + Boolean isPreemptible) { + this.automaticRestart = automaticRestart; + this.maintenance = maintenance; + this.isPreemptible = isPreemptible; + } + + /** + * Returns whether the instance should be automatically restarted if it is terminated by Compute + * Engine (not terminated by a user). + */ + public Boolean automaticRestart() { + return automaticRestart; + } + + /** + * Returns the maintenance behavior for the instance. + */ + public Maintenance maintenance() { + return maintenance; + } + + /** + * Returns {@code true} if the instance is preemptible, {@code false} otherwhise. + * + * @see Preemptible + * Instance + */ + public boolean isPreemptible() { + return isPreemptible; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("automaticRestart", automaticRestart) + .add("maintenance", maintenance) + .add("isPreemptible", isPreemptible) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(automaticRestart, maintenance, isPreemptible); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj instanceof SchedulingOptions + && Objects.equals(toPb(), ((SchedulingOptions) obj).toPb()); + } + + com.google.api.services.compute.model.Scheduling toPb() { + com.google.api.services.compute.model.Scheduling schedulingPb = + new com.google.api.services.compute.model.Scheduling(); + schedulingPb.setAutomaticRestart(automaticRestart); + schedulingPb.setPreemptible(isPreemptible); + if (maintenance != null) { + schedulingPb.setOnHostMaintenance(maintenance.name()); + } + return schedulingPb; + } + + /** + * Returns a {@code SchedulingOptions} object for a preemptible instance. + * + * @see Preemptible + * Instance + */ + public static SchedulingOptions preemptible() { + return new SchedulingOptions(false, Maintenance.TERMINATE, true); + } + + /** + * Returns a {@code SchedulingOptions} object for a standard instance. + * + * @param automaticRestart specifies whether the instance should be automatically restarted if + * it is terminated by Compute Engine (not terminated by a user) + * @param maintenance defines the maintenance behavior for the instance + */ + public static SchedulingOptions standard(boolean automaticRestart, Maintenance maintenance) { + return new SchedulingOptions(automaticRestart, maintenance, false); + } + + static SchedulingOptions fromPb(com.google.api.services.compute.model.Scheduling schedPb) { + Maintenance maintenance = null; + if (schedPb.getOnHostMaintenance() != null) { + maintenance = Maintenance.valueOf(schedPb.getOnHostMaintenance()); + } + return new SchedulingOptions(schedPb.getAutomaticRestart(), maintenance, + schedPb.getPreemptible()); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ServiceAccount.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ServiceAccount.java new file mode 100644 index 000000000000..ccdb2ecb0d79 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/ServiceAccount.java @@ -0,0 +1,123 @@ +/* + * 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.ImmutableList; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +/** + * A service account, with its specified scopes, authorized for this instance. + * + * @see Authenticating from Google + * Compute Engine + */ +public final class ServiceAccount implements Serializable { + + static final Function + FROM_PB_FUNCTION = + new Function() { + @Override + public ServiceAccount apply(com.google.api.services.compute.model.ServiceAccount pb) { + return ServiceAccount.fromPb(pb); + } + }; + static final Function + TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.ServiceAccount apply( + ServiceAccount metadata) { + return metadata.toPb(); + } + }; + + private static final long serialVersionUID = 4199610694227857331L; + + private final String email; + private final List scopes; + + private ServiceAccount(String email, List scopes) { + this.email = email; + this.scopes = ImmutableList.copyOf(scopes); + } + + /** + * Returns the email address of the service account. + */ + public String email() { + return email; + } + + /** + * Returns the list of scopes to be made available for this service account. + */ + public List scopes() { + return scopes; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("email", email) + .add("scopes", scopes) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(email, scopes); + } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj instanceof ServiceAccount + && Objects.equals(toPb(), ((ServiceAccount) obj).toPb()); + } + + com.google.api.services.compute.model.ServiceAccount toPb() { + com.google.api.services.compute.model.ServiceAccount serviceAccountPb = + new com.google.api.services.compute.model.ServiceAccount(); + serviceAccountPb.setEmail(email); + serviceAccountPb.setScopes(scopes); + return serviceAccountPb; + } + + /** + * Returns a {@code ServiceAccount} object for the provided email and scopes. + */ + public static ServiceAccount of(String email, List scopes) { + return new ServiceAccount(email, scopes); + } + + /** + * Returns a {@code ServiceAccount} object for the provided email and scopes. + */ + public static ServiceAccount of(String email, String... scopes) { + return of(email, Arrays.asList(scopes)); + } + + static ServiceAccount fromPb(com.google.api.services.compute.model.ServiceAccount accountPb) { + return new ServiceAccount(accountPb.getEmail(), accountPb.getScopes()); + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Tags.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Tags.java new file mode 100644 index 000000000000..275bad05f4fe --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/Tags.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 static com.google.common.base.Preconditions.checkNotNull; + +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; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +/** + * A list of tags for a Google Compute Engine Instance; with associated fingerprint. Tags are used + * to identify valid sources or targets for network firewalls and are specified by the client + * during instance creation. Each tag within the list must comply with RFC1035. + * + * @see RFC1035 + */ +public final class Tags implements Serializable { + + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public Tags apply(com.google.api.services.compute.model.Tags pb) { + return Tags.fromPb(pb); + } + }; + static final Function TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.compute.model.Tags apply(Tags tags) { + return tags.toPb(); + } + }; + + private static final long serialVersionUID = 5627093820497225322L; + + private final List values; + private final String fingerprint; + + /** + * A builder for {@code Tags} objects. + */ + public static final class Builder { + + private List values; + private String fingerprint; + + private Builder() { + values = Lists.newArrayList(); + } + + private Builder(Tags tags) { + this.values = tags.values != null ? Lists.newArrayList(tags.values) + : Lists.newArrayList(); + this.fingerprint = tags.fingerprint; + } + + /** + * Sets a list of tags to apply to an instance. Tags are used to identify valid sources or + * targets for network firewalls. Each tag within the list must comply with RFC1035. + * + * @see RFC1035 + */ + public Builder values(Iterable values) { + this.values = Lists.newArrayList(values); + return this; + } + + /** + * Sets a list of tags to apply to an instance. Tags are used to identify valid sources or + * targets for network firewalls. Each tag within the list must comply with RFC1035. + * + * @see RFC1035 + */ + public Builder values(String... values) { + this.values = Lists.newArrayList(Arrays.asList(checkNotNull(values))); + return this; + } + + /** + * Adds a tag to the list of tags. Tags are used to identify valid sources or targets for + * network firewalls. The tag must comply with RFC1035. + * + * @see RFC1035 + */ + public Builder add(String tag) { + this.values.add(tag); + return this; + } + + /** + * Sets the fingerprint for the tags. This value is needed to update instance's tags. + */ + public Builder fingerprint(String fingerprint) { + this.fingerprint = fingerprint; + return this; + } + + /** + * Creates a {@code Tags} object. + */ + public Tags build() { + return new Tags(this); + } + } + + private Tags(Builder builder) { + this.values = ImmutableList.copyOf(builder.values); + this.fingerprint = builder.fingerprint; + } + + /** + * Returns a list of tags to apply to an instance. Tags are used to identify valid sources or + * targets for network firewalls. Each tag within the list must comply with RFC1035. + * + * @see RFC1035 + */ + public List values() { + return values; + } + + /** + * Returns the fingerprint for the tags. This value is needed to update instance's tags. + */ + public String fingerprint() { + return fingerprint; + } + + /** + * Returns a builder for the current instance tags. + */ + public Builder toBuilder() { + return new Builder(this); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("values", values) + .add("fingerprint", fingerprint) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(values, fingerprint); + } + + @Override + public boolean equals(Object obj) { + return obj == this || obj instanceof Tags && Objects.equals(toPb(), ((Tags) obj).toPb()); + } + + com.google.api.services.compute.model.Tags toPb() { + com.google.api.services.compute.model.Tags tagsPb = + new com.google.api.services.compute.model.Tags(); + tagsPb.setFingerprint(fingerprint); + tagsPb.setItems(values); + return tagsPb; + } + + /** + * Returns a builder for a {@code Tags} object given the tags to apply to the instance. Each tag + * within the list must comply with RFC1035. + * + * @see RFC1035 + */ + public static Builder builder(Iterable values) { + return new Builder().values(values); + } + + /** + * Returns a builder for a {@code Tags} object given the tags to apply to the instance. Each tag + * within the list must comply with RFC1035. + * + * @see RFC1035 + */ + public static Builder builder(String... values) { + return new Builder().values(values); + } + + /** + * Returns a {@code Tags} object given the tags to apply to the instance. Each tag within the + * list must comply with RFC1035. + * + * @see RFC1035 + */ + public static Tags of(Iterable values) { + return builder(values).build(); + } + + /** + * Returns a {@code Tags} object given the tags to apply to the instance. Each tag within the + * list must comply with RFC1035. + * + * @see RFC1035 + */ + public static Tags of(String... values) { + return builder(values).build(); + } + + static Tags fromPb(com.google.api.services.compute.model.Tags tagsPb) { + Builder builder = + builder(tagsPb.getItems() != null ? tagsPb.getItems() : ImmutableList.of()); + return builder.fingerprint(tagsPb.getFingerprint()).build(); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/InstanceInfoTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/InstanceInfoTest.java index 604624d91c7f..51b2dcb91c72 100644 --- a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/InstanceInfoTest.java +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/InstanceInfoTest.java @@ -17,23 +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.assertTrue; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.gcloud.compute.AttachedDisk.PersistentDiskConfiguration; -import com.google.gcloud.compute.InstanceInfo.Metadata; -import com.google.gcloud.compute.InstanceInfo.SchedulingOptions; -import com.google.gcloud.compute.InstanceInfo.SchedulingOptions.Maintenance; -import com.google.gcloud.compute.InstanceInfo.ServiceAccount; -import com.google.gcloud.compute.InstanceInfo.Tags; import org.junit.Test; import java.util.List; -import java.util.Map; public class InstanceInfoTest { @@ -61,8 +52,7 @@ public class InstanceInfoTest { private static final ServiceAccount SERVICE_ACCOUNT = ServiceAccount.of("email", ImmutableList.of("scope1")); private static final List SERVICE_ACCOUNTS = ImmutableList.of(SERVICE_ACCOUNT); - private static final SchedulingOptions SCHEDULING_OPTIONS = - SchedulingOptions.preemptible(); + private static final SchedulingOptions SCHEDULING_OPTIONS = SchedulingOptions.preemptible(); private static final String CPU_PLATFORM = "cpuPlatform"; private static final InstanceInfo INSTANCE_INFO = InstanceInfo.builder(INSTANCE_ID, MACHINE_TYPE) .id(ID) @@ -80,20 +70,6 @@ public class InstanceInfoTest { .cpuPlatform(CPU_PLATFORM) .build(); - @Test - public void testMetadataToBuilder() { - Metadata metadata = METADATA.toBuilder().fingerprint("newFingerprint").build(); - assertEquals("newFingerprint", metadata.fingerprint()); - comparevalues(METADATA, metadata.toBuilder().fingerprint(null).build()); - } - - @Test - public void testTagsToBuilder() { - Tags tags = TAGS.toBuilder().values("tag1").build(); - assertEquals(ImmutableList.of("tag1"), tags.values()); - compareTags(TAGS, tags.toBuilder().values("tag1", "tag2").build()); - } - @Test public void testToBuilder() { compareInstanceInfo(INSTANCE_INFO, INSTANCE_INFO.toBuilder().build()); @@ -110,36 +86,6 @@ public void testToBuilderIncomplete() { assertEquals(instanceInfo, instanceInfo.toBuilder().build()); } - @Test - public void testMetadataBuilder() { - assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), METADATA.values()); - assertNull(METADATA.fingerprint()); - Metadata metadata = Metadata.builder() - .values(ImmutableMap.of("key1", "value1", "key2", "value2")) - .build(); - assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), metadata.values()); - assertNull(metadata.fingerprint()); - metadata = Metadata.builder() - .values(ImmutableMap.of("key1", "value1", "key2", "value2")) - .fingerprint("fingerprint") - .build(); - assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), metadata.values()); - assertEquals("fingerprint", metadata.fingerprint()); - } - - @Test - public void testTagsBuilder() { - Tags tags = Tags.builder().values(ImmutableList.of("tag1", "tag2")).build(); - assertEquals(ImmutableList.of("tag1", "tag2"), tags.values()); - assertNull(tags.fingerprint()); - tags = Tags.builder().add("tag1").add("tag2").build(); - assertEquals(ImmutableList.of("tag1", "tag2"), tags.values()); - assertNull(tags.fingerprint()); - tags = Tags.builder().add("tag1").add("tag2").fingerprint("fingerprint").build(); - assertEquals(ImmutableList.of("tag1", "tag2"), tags.values()); - assertEquals("fingerprint", tags.fingerprint()); - } - @Test public void testBuilder() { assertEquals(ID, INSTANCE_INFO.id()); @@ -175,35 +121,6 @@ public void testBuilder() { compareInstanceInfo(INSTANCE_INFO, instanceInfo); } - @Test - public void testMetadataOf() { - Map map = ImmutableMap.of("key1", "value1", "key2", "value2"); - comparevalues(METADATA, Metadata.of(map)); - } - - @Test - public void testTagsOf() { - compareTags(TAGS, Tags.of("tag1", "tag2")); - compareTags(TAGS, Tags.of(ImmutableList.of("tag1", "tag2"))); - } - - @Test - public void testServiceAccountOf() { - compareServiceAccount(SERVICE_ACCOUNT, ServiceAccount.of("email", ImmutableList.of("scope1"))); - compareServiceAccount(SERVICE_ACCOUNT, ServiceAccount.of("email", "scope1")); - } - - @Test - public void testSchedulingOptionsFactoryMethods() { - assertTrue(SCHEDULING_OPTIONS.isPreemptible()); - assertFalse(SCHEDULING_OPTIONS.automaticRestart()); - assertEquals(Maintenance.TERMINATE, SCHEDULING_OPTIONS.maintenance()); - SchedulingOptions schedulingOptions = SchedulingOptions.standard(true, Maintenance.MIGRATE); - assertFalse(schedulingOptions.isPreemptible()); - assertTrue(schedulingOptions.automaticRestart()); - assertEquals(Maintenance.MIGRATE, schedulingOptions.maintenance()); - } - @Test public void testOf() { InstanceInfo instance = @@ -225,36 +142,6 @@ public void testOf() { assertNull(instance.cpuPlatform()); } - @Test - public void testMetadataToAndFromPb() { - comparevalues(METADATA, Metadata.fromPb(METADATA.toPb())); - Metadata metadata = Metadata.builder() - .values(ImmutableMap.of("key1", "value1", "key2", "value2")) - .fingerprint("fingerprint") - .build(); - comparevalues(metadata, Metadata.fromPb(metadata.toPb())); - } - - @Test - public void testTagsToAndFromPb() { - compareTags(TAGS, Tags.fromPb(TAGS.toPb())); - Tags tags = Tags.builder().add("tag1").add("tag2").fingerprint("fingerprint").build(); - compareTags(tags, Tags.fromPb(tags.toPb())); - } - - @Test - public void testServiceAccountToAndFromPb() { - compareServiceAccount(SERVICE_ACCOUNT, ServiceAccount.fromPb(SERVICE_ACCOUNT.toPb())); - } - - @Test - public void testSchedulingOptionsToAndFromPb() { - compareSchedulingOptions(SCHEDULING_OPTIONS, - SchedulingOptions.fromPb(SCHEDULING_OPTIONS.toPb())); - SchedulingOptions schedulingOptions = SchedulingOptions.standard(true, Maintenance.MIGRATE); - compareSchedulingOptions(schedulingOptions, SchedulingOptions.fromPb(schedulingOptions.toPb())); - } - @Test public void testToAndFromPb() { compareInstanceInfo(INSTANCE_INFO, InstanceInfo.fromPb(INSTANCE_INFO.toPb())); @@ -275,35 +162,6 @@ public void testSetProjectId() { compareInstanceInfo(instanceWithProject, instance.setProjectId("project")); } - public void comparevalues(Metadata expected, Metadata value) { - assertEquals(expected, value); - assertEquals(expected.fingerprint(), value.fingerprint()); - assertEquals(expected.values(), value.values()); - assertEquals(expected.hashCode(), value.hashCode()); - } - - public void compareTags(Tags expected, Tags value) { - assertEquals(expected, value); - assertEquals(expected.fingerprint(), value.fingerprint()); - assertEquals(expected.values(), value.values()); - assertEquals(expected.hashCode(), value.hashCode()); - } - - public void compareServiceAccount(ServiceAccount expected, ServiceAccount value) { - assertEquals(expected, value); - assertEquals(expected.email(), value.email()); - assertEquals(expected.scopes(), value.scopes()); - assertEquals(expected.hashCode(), value.hashCode()); - } - - public void compareSchedulingOptions(SchedulingOptions expected, SchedulingOptions value) { - assertEquals(expected, value); - assertEquals(expected.isPreemptible(), value.isPreemptible()); - assertEquals(expected.maintenance(), value.maintenance()); - assertEquals(expected.automaticRestart(), value.automaticRestart()); - assertEquals(expected.hashCode(), value.hashCode()); - } - public void compareInstanceInfo(InstanceInfo expected, InstanceInfo value) { assertEquals(expected, value); assertEquals(expected.id(), value.id()); diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MetadataTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MetadataTest.java new file mode 100644 index 000000000000..be43c4fd433f --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/MetadataTest.java @@ -0,0 +1,81 @@ +/* + * 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 com.google.common.collect.ImmutableMap; + +import org.junit.Test; + +import java.util.Map; + +public class MetadataTest { + + private static final Metadata METADATA = Metadata.builder() + .add("key1", "value1") + .add("key2", "value2") + .build(); + + @Test + public void testToBuilder() { + Metadata metadata = METADATA.toBuilder().fingerprint("newFingerprint").build(); + assertEquals("newFingerprint", metadata.fingerprint()); + compareMetadata(METADATA, metadata.toBuilder().fingerprint(null).build()); + } + + @Test + public void testBuilder() { + assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), METADATA.values()); + assertNull(METADATA.fingerprint()); + Metadata metadata = Metadata.builder() + .values(ImmutableMap.of("key1", "value1", "key2", "value2")) + .build(); + assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), metadata.values()); + assertNull(metadata.fingerprint()); + metadata = Metadata.builder() + .values(ImmutableMap.of("key1", "value1", "key2", "value2")) + .fingerprint("fingerprint") + .build(); + assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), metadata.values()); + assertEquals("fingerprint", metadata.fingerprint()); + } + + @Test + public void testOf() { + Map map = ImmutableMap.of("key1", "value1", "key2", "value2"); + compareMetadata(METADATA, Metadata.of(map)); + } + + @Test + public void testToAndFromPb() { + compareMetadata(METADATA, Metadata.fromPb(METADATA.toPb())); + Metadata metadata = Metadata.builder() + .values(ImmutableMap.of("key1", "value1", "key2", "value2")) + .fingerprint("fingerprint") + .build(); + compareMetadata(metadata, Metadata.fromPb(metadata.toPb())); + } + + public void compareMetadata(Metadata expected, Metadata value) { + assertEquals(expected, value); + assertEquals(expected.fingerprint(), value.fingerprint()); + assertEquals(expected.values(), value.values()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SchedulingOptionsTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SchedulingOptionsTest.java new file mode 100644 index 000000000000..258c880d783f --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/SchedulingOptionsTest.java @@ -0,0 +1,57 @@ +/* + * 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.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class SchedulingOptionsTest { + + private static final SchedulingOptions SCHEDULING_OPTIONS = SchedulingOptions.preemptible(); + + @Test + public void testFactoryMethods() { + assertTrue(SCHEDULING_OPTIONS.isPreemptible()); + assertFalse(SCHEDULING_OPTIONS.automaticRestart()); + assertEquals(SchedulingOptions.Maintenance.TERMINATE, SCHEDULING_OPTIONS.maintenance()); + SchedulingOptions schedulingOptions = + SchedulingOptions.standard(true, SchedulingOptions.Maintenance.MIGRATE); + assertFalse(schedulingOptions.isPreemptible()); + assertTrue(schedulingOptions.automaticRestart()); + assertEquals(SchedulingOptions.Maintenance.MIGRATE, schedulingOptions.maintenance()); + } + + @Test + public void testToAndFromPb() { + compareSchedulingOptions(SCHEDULING_OPTIONS, + SchedulingOptions.fromPb(SCHEDULING_OPTIONS.toPb())); + SchedulingOptions schedulingOptions = + SchedulingOptions.standard(true, SchedulingOptions.Maintenance.MIGRATE); + compareSchedulingOptions(schedulingOptions, SchedulingOptions.fromPb(schedulingOptions.toPb())); + } + + public void compareSchedulingOptions(SchedulingOptions expected, SchedulingOptions value) { + assertEquals(expected, value); + assertEquals(expected.isPreemptible(), value.isPreemptible()); + assertEquals(expected.maintenance(), value.maintenance()); + assertEquals(expected.automaticRestart(), value.automaticRestart()); + 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 index e31280f94ec3..92b1128ac6c4 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 @@ -26,10 +26,6 @@ import com.google.gcloud.compute.AttachedDisk.CreateDiskConfiguration; import com.google.gcloud.compute.AttachedDisk.PersistentDiskConfiguration; import com.google.gcloud.compute.AttachedDisk.ScratchDiskConfiguration; -import com.google.gcloud.compute.InstanceInfo.Metadata; -import com.google.gcloud.compute.InstanceInfo.SchedulingOptions; -import com.google.gcloud.compute.InstanceInfo.ServiceAccount; -import com.google.gcloud.compute.InstanceInfo.Tags; import com.google.gcloud.compute.NetworkInterface.AccessConfig; import org.junit.Test; diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ServiceAccountTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ServiceAccountTest.java new file mode 100644 index 000000000000..4d7c08d79f55 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/ServiceAccountTest.java @@ -0,0 +1,47 @@ +/* + * 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; + +public class ServiceAccountTest { + + private static final ServiceAccount SERVICE_ACCOUNT = + ServiceAccount.of("email", ImmutableList.of("scope1")); + + @Test + public void testOf() { + compareServiceAccount(SERVICE_ACCOUNT, ServiceAccount.of("email", ImmutableList.of("scope1"))); + compareServiceAccount(SERVICE_ACCOUNT, ServiceAccount.of("email", "scope1")); + } + + @Test + public void testToAndFromPb() { + compareServiceAccount(SERVICE_ACCOUNT, ServiceAccount.fromPb(SERVICE_ACCOUNT.toPb())); + } + + public void compareServiceAccount(ServiceAccount expected, ServiceAccount value) { + assertEquals(expected, value); + assertEquals(expected.email(), value.email()); + assertEquals(expected.scopes(), value.scopes()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/TagsTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/TagsTest.java new file mode 100644 index 000000000000..d9e998fd47e7 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/TagsTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.compute; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +public class TagsTest { + + private static final Tags TAGS = Tags.of("tag1", "tag2"); + + @Test + public void testToBuilder() { + Tags tags = TAGS.toBuilder().values("tag1").build(); + assertEquals(ImmutableList.of("tag1"), tags.values()); + compareTags(TAGS, tags.toBuilder().values("tag1", "tag2").build()); + } + + @Test + public void testBuilder() { + Tags tags = Tags.builder().values(ImmutableList.of("tag1", "tag2")).build(); + assertEquals(ImmutableList.of("tag1", "tag2"), tags.values()); + assertNull(tags.fingerprint()); + tags = Tags.builder().add("tag1").add("tag2").build(); + assertEquals(ImmutableList.of("tag1", "tag2"), tags.values()); + assertNull(tags.fingerprint()); + tags = Tags.builder().add("tag1").add("tag2").fingerprint("fingerprint").build(); + assertEquals(ImmutableList.of("tag1", "tag2"), tags.values()); + assertEquals("fingerprint", tags.fingerprint()); + } + + @Test + public void testOf() { + compareTags(TAGS, Tags.of("tag1", "tag2")); + compareTags(TAGS, Tags.of(ImmutableList.of("tag1", "tag2"))); + } + + @Test + public void testToAndFromPb() { + compareTags(TAGS, Tags.fromPb(TAGS.toPb())); + Tags tags = Tags.builder().add("tag1").add("tag2").fingerprint("fingerprint").build(); + compareTags(tags, Tags.fromPb(tags.toPb())); + } + + public void compareTags(Tags expected, Tags value) { + assertEquals(expected, value); + assertEquals(expected.fingerprint(), value.fingerprint()); + assertEquals(expected.values(), value.values()); + assertEquals(expected.hashCode(), value.hashCode()); + } +}