Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google-cloud-compute/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-compute</artifactId>
<version>v1-rev103-1.21.0</version>
<version>v1-rev157-1.22.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ public Builder setMetadata(Metadata metadata) {
return this;
}

@Override
public Builder setLabels(Map<String, String> labels) {
this.infoBuilder.setLabels(labels);
return this;
}

@Override
public Builder setServiceAccounts(List<ServiceAccount> serviceAccounts) {
this.infoBuilder.setServiceAccounts(serviceAccounts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -86,6 +88,7 @@ public Instance apply(InstanceInfo instance) {
private final List<ServiceAccount> serviceAccounts;
private final SchedulingOptions schedulingOptions;
private final String cpuPlatform;
private final Map<String, String> labels;

/**
* The status of the instance.
Expand Down Expand Up @@ -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<String, String> labels);

/**
* Sets a list of service accounts, with their specified scopes, authorized for this instance.
Expand Down Expand Up @@ -234,6 +242,7 @@ public static final class BuilderImpl extends Builder {
private List<ServiceAccount> serviceAccounts;
private SchedulingOptions schedulingOptions;
private String cpuPlatform;
private Map<String, String> labels;

BuilderImpl(InstanceId instanceId) {
this.instanceId = checkNotNull(instanceId);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -381,6 +394,12 @@ public Builder setMetadata(Metadata metadata) {
return this;
}

@Override
public Builder setLabels(Map<String, String> labels) {
this.labels = labels != null ? ImmutableMap.copyOf(labels) : null;
return this;
}

@Override
public Builder setServiceAccounts(List<ServiceAccount> serviceAccounts) {
this.serviceAccounts = ImmutableList.copyOf(checkNotNull(serviceAccounts));
Expand Down Expand Up @@ -423,6 +442,7 @@ public InstanceInfo build() {
this.serviceAccounts = builder.serviceAccounts;
this.schedulingOptions = builder.schedulingOptions;
this.cpuPlatform = builder.cpuPlatform;
this.labels = builder.labels;
}

/**
Expand Down Expand Up @@ -515,6 +535,13 @@ public Metadata getMetadata() {
return metadata;
}

/**
* Returns the labels for this bucket.
*/
public Map<String, String> 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
Expand Down Expand Up @@ -566,14 +593,15 @@ public String toString() {
.add("serviceAccounts", serviceAccounts)
.add("schedulingOptions", schedulingOptions)
.add("cpuPlatform", cpuPlatform)
.add("labels", labels)
.toString();
}

@Override
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
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,6 +26,8 @@
import org.junit.Test;

import java.util.List;
import java.util.Map;


public class InstanceInfoTest {

Expand Down Expand Up @@ -53,6 +57,7 @@ public class InstanceInfoTest {
private static final List<ServiceAccount> 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<String, String> LABELS = ImmutableMap.of("label1", "value1");
private static final InstanceInfo INSTANCE_INFO =
InstanceInfo.newBuilder(INSTANCE_ID, MACHINE_TYPE)
.setGeneratedId(GENERATED_ID)
Expand All @@ -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)
Expand Down Expand Up @@ -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());
Expand All @@ -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)
Expand All @@ -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());
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class InstanceTest {
.add("key2", "value2")
.setFingerprint("fingerprint")
.build();
private static final Map<String, String> LABELS = ImmutableMap.of("label1", "value1");
private static final ServiceAccount SERVICE_ACCOUNT =
ServiceAccount.of("email", ImmutableList.of("scope1"));
private static final List<ServiceAccount> SERVICE_ACCOUNTS =
Expand Down Expand Up @@ -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);
}
Expand All @@ -109,6 +111,7 @@ private void initializeInstance() {
.setServiceAccounts(SERVICE_ACCOUNTS)
.setSchedulingOptions(SCHEDULING_OPTIONS)
.setCpuPlatform(CPU_PLATFORM)
.setLabels(LABELS)
.build();
}

Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand Down