diff --git a/google-cloud-compute/pom.xml b/google-cloud-compute/pom.xml index fb4ace10c50e..cbce4b8d3293 100644 --- a/google-cloud-compute/pom.xml +++ b/google-cloud-compute/pom.xml @@ -30,7 +30,7 @@ com.google.apis google-api-services-compute - v1-rev103-1.21.0 + v1-rev157-1.22.0 compile diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/Instance.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/Instance.java index 50b419dff22e..5a60ac01b424 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/Instance.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/Instance.java @@ -153,6 +153,12 @@ public Builder setMetadata(Metadata metadata) { return this; } + @Override + public Builder setLabels(Map labels) { + this.infoBuilder.setLabels(labels); + return this; + } + @Override public Builder setServiceAccounts(List serviceAccounts) { this.infoBuilder.setServiceAccounts(serviceAccounts); diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/InstanceInfo.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/InstanceInfo.java index a10ad091b646..39676bb8e7b0 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/InstanceInfo.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/InstanceInfo.java @@ -22,6 +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 org.joda.time.format.DateTimeFormatter; @@ -31,6 +32,7 @@ import java.math.BigInteger; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -86,6 +88,7 @@ public Instance apply(InstanceInfo instance) { private final List serviceAccounts; private final SchedulingOptions schedulingOptions; private final String cpuPlatform; + private final Map labels; /** * The status of the instance. @@ -193,6 +196,11 @@ public abstract static class Builder { * Sets the instance metadata. */ public abstract Builder setMetadata(Metadata metadata); + + /** + * Sets the label of the instance. + */ + public abstract Builder setLabels(Map labels); /** * Sets a list of service accounts, with their specified scopes, authorized for this instance. @@ -234,6 +242,7 @@ public static final class BuilderImpl extends Builder { private List serviceAccounts; private SchedulingOptions schedulingOptions; private String cpuPlatform; + private Map labels; BuilderImpl(InstanceId instanceId) { this.instanceId = checkNotNull(instanceId); @@ -255,6 +264,7 @@ public static final class BuilderImpl extends Builder { this.serviceAccounts = instance.serviceAccounts; this.schedulingOptions = instance.schedulingOptions; this.cpuPlatform = instance.cpuPlatform; + this.labels = instance.labels; } BuilderImpl(Instance instancePb) { @@ -287,6 +297,9 @@ public static final class BuilderImpl extends Builder { if (instancePb.getMetadata() != null) { this.metadata = Metadata.fromPb(instancePb.getMetadata()); } + if (instancePb.getLabels() != null) { + this.labels = instancePb.getLabels(); + } if (instancePb.getServiceAccounts() != null) { this.serviceAccounts = Lists.transform(instancePb.getServiceAccounts(), ServiceAccount.FROM_PB_FUNCTION); @@ -381,6 +394,12 @@ public Builder setMetadata(Metadata metadata) { return this; } + @Override + public Builder setLabels(Map labels) { + this.labels = labels != null ? ImmutableMap.copyOf(labels) : null; + return this; + } + @Override public Builder setServiceAccounts(List serviceAccounts) { this.serviceAccounts = ImmutableList.copyOf(checkNotNull(serviceAccounts)); @@ -423,6 +442,7 @@ public InstanceInfo build() { this.serviceAccounts = builder.serviceAccounts; this.schedulingOptions = builder.schedulingOptions; this.cpuPlatform = builder.cpuPlatform; + this.labels = builder.labels; } /** @@ -515,6 +535,13 @@ public Metadata getMetadata() { return metadata; } + /** + * Returns the labels for this bucket. + */ + public Map getLabels() { + return labels; + } + /** * Returns a list of service accounts, with their specified scopes, authorized for this instance. * Service accounts generate access tokens that can be accessed through the metadata server and @@ -566,6 +593,7 @@ public String toString() { .add("serviceAccounts", serviceAccounts) .add("schedulingOptions", schedulingOptions) .add("cpuPlatform", cpuPlatform) + .add("labels", labels) .toString(); } @@ -573,7 +601,7 @@ public String toString() { public int hashCode() { return Objects.hash(generatedId, instanceId, creationTimestamp, description, status, statusMessage, tags, machineType, canIpForward, networkInterfaces, attachedDisks, metadata, - serviceAccounts, schedulingOptions, cpuPlatform); + serviceAccounts, schedulingOptions, cpuPlatform, labels); } @Override @@ -638,6 +666,9 @@ Instance toPb() { if (metadata != null) { instancePb.setMetadata(metadata.toPb()); } + if (labels != null) { + instancePb.setLabels(labels); + } if (serviceAccounts != null) { instancePb.setServiceAccounts( Lists.transform(serviceAccounts, ServiceAccount.TO_PB_FUNCTION)); diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceInfoTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceInfoTest.java index 1a4d8ee7085a..fe2a7302d2e3 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceInfoTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceInfoTest.java @@ -16,6 +16,8 @@ package com.google.cloud.compute; +import com.google.common.collect.ImmutableMap; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -24,6 +26,8 @@ import org.junit.Test; import java.util.List; +import java.util.Map; + public class InstanceInfoTest { @@ -53,6 +57,7 @@ public class InstanceInfoTest { private static final List SERVICE_ACCOUNTS = ImmutableList.of(SERVICE_ACCOUNT); private static final SchedulingOptions SCHEDULING_OPTIONS = SchedulingOptions.preemptible(); private static final String CPU_PLATFORM = "cpuPlatform"; + private static final Map LABELS = ImmutableMap.of("label1", "value1"); private static final InstanceInfo INSTANCE_INFO = InstanceInfo.newBuilder(INSTANCE_ID, MACHINE_TYPE) .setGeneratedId(GENERATED_ID) @@ -65,6 +70,7 @@ public class InstanceInfoTest { .setNetworkInterfaces(NETWORK_INTERFACES) .setAttachedDisks(ATTACHED_DISKS) .setMetadata(METADATA) + .setLabels(LABELS) .setServiceAccounts(SERVICE_ACCOUNTS) .setSchedulingOptions(SCHEDULING_OPTIONS) .setCpuPlatform(CPU_PLATFORM) @@ -100,6 +106,7 @@ public void testBuilder() { assertEquals(NETWORK_INTERFACES, INSTANCE_INFO.getNetworkInterfaces()); assertEquals(ATTACHED_DISKS, INSTANCE_INFO.getAttachedDisks()); assertEquals(METADATA, INSTANCE_INFO.getMetadata()); + assertEquals(LABELS, INSTANCE_INFO.getLabels()); assertEquals(SERVICE_ACCOUNTS, INSTANCE_INFO.getServiceAccounts()); assertEquals(SCHEDULING_OPTIONS, INSTANCE_INFO.getSchedulingOptions()); assertEquals(CPU_PLATFORM, INSTANCE_INFO.getCpuPlatform()); @@ -114,6 +121,7 @@ public void testBuilder() { .setNetworkInterfaces(NETWORK_INTERFACE) .setAttachedDisks(ATTACHED_DISK) .setMetadata(METADATA) + .setLabels(LABELS) .setServiceAccounts(SERVICE_ACCOUNTS) .setSchedulingOptions(SCHEDULING_OPTIONS) .setCpuPlatform(CPU_PLATFORM) @@ -137,6 +145,7 @@ public void testOf() { assertEquals(NETWORK_INTERFACES, instance.getNetworkInterfaces()); assertEquals(ATTACHED_DISKS, instance.getAttachedDisks()); assertNull(instance.getMetadata()); + assertNull(instance.getLabels()); assertNull(instance.getServiceAccounts()); assertNull(instance.getSchedulingOptions()); assertNull(instance.getCpuPlatform()); @@ -180,5 +189,6 @@ public void compareInstanceInfo(InstanceInfo expected, InstanceInfo value) { assertEquals(expected.getSchedulingOptions(), value.getSchedulingOptions()); assertEquals(expected.getCpuPlatform(), value.getCpuPlatform()); assertEquals(expected.hashCode(), value.hashCode()); + assertEquals(expected.getLabels(), value.getLabels()); } } diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceTest.java index 9c7acd816c70..0531d48f357c 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/InstanceTest.java @@ -62,6 +62,7 @@ public class InstanceTest { .add("key2", "value2") .setFingerprint("fingerprint") .build(); + private static final Map LABELS = ImmutableMap.of("label1", "value1"); private static final ServiceAccount SERVICE_ACCOUNT = ServiceAccount.of("email", ImmutableList.of("scope1")); private static final List SERVICE_ACCOUNTS = @@ -91,6 +92,7 @@ private void initializeExpectedInstance(int optionsCalls) { .setServiceAccounts(SERVICE_ACCOUNTS) .setSchedulingOptions(SCHEDULING_OPTIONS) .setCpuPlatform(CPU_PLATFORM) + .setLabels(LABELS) .build(); compute = createStrictMock(Compute.class); } @@ -109,6 +111,7 @@ private void initializeInstance() { .setServiceAccounts(SERVICE_ACCOUNTS) .setSchedulingOptions(SCHEDULING_OPTIONS) .setCpuPlatform(CPU_PLATFORM) + .setLabels(LABELS) .build(); } @@ -147,6 +150,7 @@ public void testBuilder() { assertEquals(NETWORK_INTERFACES, expectedInstance.getNetworkInterfaces()); assertEquals(ATTACHED_DISKS, expectedInstance.getAttachedDisks()); assertEquals(METADATA, expectedInstance.getMetadata()); + assertEquals(LABELS, expectedInstance.getLabels()); assertEquals(SERVICE_ACCOUNTS, expectedInstance.getServiceAccounts()); assertEquals(SCHEDULING_OPTIONS, expectedInstance.getSchedulingOptions()); assertEquals(CPU_PLATFORM, expectedInstance.getCpuPlatform()); @@ -167,6 +171,7 @@ public void testBuilder() { assertEquals(NETWORK_INTERFACES, instance.getNetworkInterfaces()); assertEquals(ATTACHED_DISKS, instance.getAttachedDisks()); assertNull(instance.getMetadata()); + assertNull(instance.getLabels()); assertNull(instance.getServiceAccounts()); assertNull(instance.getSchedulingOptions()); assertNull(instance.getCpuPlatform()); @@ -909,6 +914,7 @@ public void compareInstance(Instance expected, Instance value) { assertEquals(expected.getNetworkInterfaces(), value.getNetworkInterfaces()); assertEquals(expected.getAttachedDisks(), value.getAttachedDisks()); assertEquals(expected.getMetadata(), value.getMetadata()); + assertEquals(expected.getLabels(), value.getLabels()); assertEquals(expected.getServiceAccounts(), value.getServiceAccounts()); assertEquals(expected.getSchedulingOptions(), value.getSchedulingOptions()); assertEquals(expected.getCpuPlatform(), value.getCpuPlatform()); diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/it/ITComputeTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/it/ITComputeTest.java index 781cc949d56f..e3888b4236d6 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/it/ITComputeTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/it/ITComputeTest.java @@ -1641,6 +1641,7 @@ public void testCreateGetAndDeleteInstance() throws InterruptedException, Timeou assertEquals("NAT", remoteAccessConfig.getName()); assertNotNull(remoteInstance.getMetadata()); assertNotNull(remoteInstance.getTags()); + assertNotNull(remoteInstance.getLabels()); // test get with selected fields remoteInstance = compute.getInstance(instanceId, Compute.InstanceOption.fields(Compute.InstanceField.CREATION_TIMESTAMP)); @@ -1652,6 +1653,7 @@ public void testCreateGetAndDeleteInstance() throws InterruptedException, Timeou assertNull(remoteInstance.getNetworkInterfaces()); assertNull(remoteInstance.getMetadata()); assertNull(remoteInstance.getTags()); + assertNotNull(remoteInstance.getLabels()); // test get default serial port output String serialPortOutput = remoteInstance.getSerialPortOutput(); assertNotNull(serialPortOutput);