Making tags Immutable

This commit is contained in:
Daniel Broudy 2015-05-20 16:36:35 -07:00 committed by Ignasi Barrera
parent 465a62aa27
commit aa33619c92
6 changed files with 30 additions and 31 deletions

View File

@ -26,6 +26,7 @@ import static org.jclouds.googlecomputeengine.config.GoogleComputeEngineProperti
import static org.jclouds.googlecomputeengine.compute.strategy.CreateNodesWithGroupEncodedIntoNameThenAddToSet.simplifyPorts;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -56,6 +57,7 @@ import org.jclouds.googlecomputeengine.domain.MachineType;
import org.jclouds.googlecomputeengine.domain.NewInstance;
import org.jclouds.googlecomputeengine.domain.Operation;
import org.jclouds.googlecomputeengine.domain.Region;
import org.jclouds.googlecomputeengine.domain.Tags;
import org.jclouds.googlecomputeengine.domain.Zone;
import org.jclouds.googlecomputeengine.features.InstanceApi;
import org.jclouds.location.suppliers.all.JustProvider;
@ -126,24 +128,26 @@ public final class GoogleComputeEngineServiceAdapter
URI network = URI.create(networks.next());
assert !networks.hasNext() : "Error: Options should specify only one network";
NewInstance newInstance = NewInstance.create(
name, // name
template.getHardware().getUri(), // machineType
network, // network
disks, // disks
group // description
);
// Add tags from template
newInstance.tags().items().addAll(options.getTags());
ArrayList<String> tags = new ArrayList<String>(options.getTags());
// Add tags for firewalls
FirewallTagNamingConvention naming = firewallTagNamingConvention.get(group);
List<String> ports = simplifyPorts(options.getInboundPorts());
if (ports != null){
newInstance.tags().items().add(naming.name(ports));
tags.add(naming.name(ports));
}
NewInstance newInstance = NewInstance.create(
name, // name
template.getHardware().getUri(), // machineType
network, // network
disks, // disks
group, // description
Tags.create(null, ImmutableList.copyOf(tags)) // tags
);
// Add metadata from template and for ssh key and image id
newInstance.metadata().putAll(options.getUserMetadata());

View File

@ -77,10 +77,10 @@ public abstract class NewInstance {
/** Convenience for creating a new instance with only a boot disk and minimal parameters. */
public static NewInstance create(String name, URI machineType, URI network, URI sourceImage) {
return create(name, machineType, network, Arrays.asList(AttachDisk.newBootDisk(sourceImage)), null);
return create(name, machineType, network, Arrays.asList(AttachDisk.newBootDisk(sourceImage)), null, null);
}
public static NewInstance create(String name, URI machineType, URI network, List<AttachDisk> disks, String description) {
public static NewInstance create(String name, URI machineType, URI network, List<AttachDisk> disks, @Nullable String description, @Nullable Tags tags) {
checkArgument(disks.get(0).boot(), "disk 0 must be a boot disk! %s", disks);
boolean foundBoot = false;
for (AttachDisk disk : disks) {
@ -90,7 +90,7 @@ public abstract class NewInstance {
}
}
return create(name, machineType, null, ImmutableList.of(NetworkInterface.create(network)), ImmutableList.copyOf(disks),
description, Tags.create(), Metadata.create(), null, null);
description, tags != null ? tags : Tags.create(), Metadata.create(), null, null);
}
@SerializedNames({ "name", "machineType", "canIpForward", "networkInterfaces", "disks", "description", "tags", "metadata",

View File

@ -16,13 +16,11 @@
*/
package org.jclouds.googlecomputeengine.domain;
import java.util.ArrayList;
import java.util.List;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
/**
* Tags for an instance or project, with their fingerprint. Each tag must be unique, must be 1-63 characters long, and
@ -35,14 +33,8 @@ public abstract class Tags implements Cloneable {
/** The fingerprint for the items - needed for updating them. */
@Nullable public abstract String fingerprint();
/** Mutable list of tags. */
public abstract List<String> items();
/** Convenience method for chaining adds. */
public Tags add(String tag) {
items().add(tag);
return this;
}
/** Immutable list of tags. */
public abstract ImmutableList<String> items();
public static Tags create() {
return Tags.create(null, null);
@ -53,14 +45,15 @@ public abstract class Tags implements Cloneable {
}
@SerializedNames({ "fingerprint", "items" })
static Tags create(String fingerprint, ArrayList<String> items) { // Dictates the type when created from json!
return new AutoValue_Tags(fingerprint, items != null ? items : new ArrayList<String>());
public static Tags create(String fingerprint, ImmutableList<String> items) { // Dictates the type when created from json!
ImmutableList<String> empty = ImmutableList.of();
return new AutoValue_Tags(fingerprint, items != null ? items : empty);
}
Tags() {
}
@Override public Tags clone() {
return Tags.create(fingerprint(), new ArrayList<String>(items()));
return Tags.create(fingerprint(), items());
}
}

View File

@ -40,6 +40,7 @@ import org.jclouds.googlecomputeengine.domain.Metadata;
import org.jclouds.googlecomputeengine.domain.NewInstance;
import org.jclouds.googlecomputeengine.domain.AttachDisk;
import org.jclouds.googlecomputeengine.domain.Operation;
import org.jclouds.googlecomputeengine.domain.Tags;
import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiLiveTest;
import org.jclouds.googlecomputeengine.options.DiskCreationOptions;
import org.testng.annotations.AfterClass;
@ -93,9 +94,9 @@ public class InstanceApiLiveTest extends BaseGoogleComputeEngineApiLiveTest {
getNetworkUrl(INSTANCE_NETWORK_NAME), // network
Arrays.asList(AttachDisk.newBootDisk(imageUri),
AttachDisk.existingDisk(getDiskUrl(DISK_NAME))), // disks
"a description" // description
"a description", // description
Tags.create(null, ImmutableList.of("foo", "bar")) // tags
);
instance.tags().items().addAll(Arrays.asList("foo", "bar"));
instance.metadata().put("mykey", "myvalue");
instance2 = NewInstance.create(

View File

@ -88,7 +88,8 @@ public class InstanceApiMockTest extends BaseGoogleComputeEngineApiMockTest {
URI.create(url("/projects/party/zones/us-central1-a/machineTypes/n1-standard-1")), // machineType
URI.create(url("/projects/party/global/networks/default")), // network
Arrays.asList(AttachDisk.existingBootDisk(URI.create(url("/projects/party/zones/us-central1-a/disks/test")))),
"desc" // description
"desc", // description
null // tags
);
newInstance.metadata().put("aKey", "aValue");

View File

@ -57,7 +57,7 @@ public class ParseInstanceTest extends BaseGoogleComputeEngineParseTest<Instance
URI.create(baseUrl + "/party/zones/us-central1-a/instances/test-0"), // selfLink
"test-0", // name
"desc", // description
Tags.create("abcd").add("aTag").add("Group-port-42"), // tags
Tags.create("abcd", ImmutableList.of("aTag", "Group-port-42")), // tags
URI.create(baseUrl + "/party/zones/us-central1-a/machineTypes/n1-standard-1"), // machineType
Instance.Status.RUNNING, // status
null, // statusMessage