mirror of https://github.com/apache/jclouds.git
Making tags Immutable
This commit is contained in:
parent
465a62aa27
commit
aa33619c92
|
@ -26,6 +26,7 @@ import static org.jclouds.googlecomputeengine.config.GoogleComputeEngineProperti
|
||||||
import static org.jclouds.googlecomputeengine.compute.strategy.CreateNodesWithGroupEncodedIntoNameThenAddToSet.simplifyPorts;
|
import static org.jclouds.googlecomputeengine.compute.strategy.CreateNodesWithGroupEncodedIntoNameThenAddToSet.simplifyPorts;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.NewInstance;
|
||||||
import org.jclouds.googlecomputeengine.domain.Operation;
|
import org.jclouds.googlecomputeengine.domain.Operation;
|
||||||
import org.jclouds.googlecomputeengine.domain.Region;
|
import org.jclouds.googlecomputeengine.domain.Region;
|
||||||
|
import org.jclouds.googlecomputeengine.domain.Tags;
|
||||||
import org.jclouds.googlecomputeengine.domain.Zone;
|
import org.jclouds.googlecomputeengine.domain.Zone;
|
||||||
import org.jclouds.googlecomputeengine.features.InstanceApi;
|
import org.jclouds.googlecomputeengine.features.InstanceApi;
|
||||||
import org.jclouds.location.suppliers.all.JustProvider;
|
import org.jclouds.location.suppliers.all.JustProvider;
|
||||||
|
@ -126,24 +128,26 @@ public final class GoogleComputeEngineServiceAdapter
|
||||||
URI network = URI.create(networks.next());
|
URI network = URI.create(networks.next());
|
||||||
assert !networks.hasNext() : "Error: Options should specify only one network";
|
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
|
// Add tags from template
|
||||||
newInstance.tags().items().addAll(options.getTags());
|
ArrayList<String> tags = new ArrayList<String>(options.getTags());
|
||||||
|
|
||||||
// Add tags for firewalls
|
// Add tags for firewalls
|
||||||
FirewallTagNamingConvention naming = firewallTagNamingConvention.get(group);
|
FirewallTagNamingConvention naming = firewallTagNamingConvention.get(group);
|
||||||
List<String> ports = simplifyPorts(options.getInboundPorts());
|
List<String> ports = simplifyPorts(options.getInboundPorts());
|
||||||
if (ports != null){
|
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
|
// Add metadata from template and for ssh key and image id
|
||||||
newInstance.metadata().putAll(options.getUserMetadata());
|
newInstance.metadata().putAll(options.getUserMetadata());
|
||||||
|
|
||||||
|
|
|
@ -77,10 +77,10 @@ public abstract class NewInstance {
|
||||||
|
|
||||||
/** Convenience for creating a new instance with only a boot disk and minimal parameters. */
|
/** 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) {
|
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);
|
checkArgument(disks.get(0).boot(), "disk 0 must be a boot disk! %s", disks);
|
||||||
boolean foundBoot = false;
|
boolean foundBoot = false;
|
||||||
for (AttachDisk disk : disks) {
|
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),
|
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",
|
@SerializedNames({ "name", "machineType", "canIpForward", "networkInterfaces", "disks", "description", "tags", "metadata",
|
||||||
|
|
|
@ -16,13 +16,11 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.googlecomputeengine.domain;
|
package org.jclouds.googlecomputeengine.domain;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.jclouds.javax.annotation.Nullable;
|
import org.jclouds.javax.annotation.Nullable;
|
||||||
import org.jclouds.json.SerializedNames;
|
import org.jclouds.json.SerializedNames;
|
||||||
|
|
||||||
import com.google.auto.value.AutoValue;
|
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
|
* 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. */
|
/** The fingerprint for the items - needed for updating them. */
|
||||||
@Nullable public abstract String fingerprint();
|
@Nullable public abstract String fingerprint();
|
||||||
|
|
||||||
/** Mutable list of tags. */
|
/** Immutable list of tags. */
|
||||||
public abstract List<String> items();
|
public abstract ImmutableList<String> items();
|
||||||
|
|
||||||
/** Convenience method for chaining adds. */
|
|
||||||
public Tags add(String tag) {
|
|
||||||
items().add(tag);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Tags create() {
|
public static Tags create() {
|
||||||
return Tags.create(null, null);
|
return Tags.create(null, null);
|
||||||
|
@ -53,14 +45,15 @@ public abstract class Tags implements Cloneable {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SerializedNames({ "fingerprint", "items" })
|
@SerializedNames({ "fingerprint", "items" })
|
||||||
static Tags create(String fingerprint, ArrayList<String> items) { // Dictates the type when created from json!
|
public static Tags create(String fingerprint, ImmutableList<String> items) { // Dictates the type when created from json!
|
||||||
return new AutoValue_Tags(fingerprint, items != null ? items : new ArrayList<String>());
|
ImmutableList<String> empty = ImmutableList.of();
|
||||||
|
return new AutoValue_Tags(fingerprint, items != null ? items : empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
Tags() {
|
Tags() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public Tags clone() {
|
@Override public Tags clone() {
|
||||||
return Tags.create(fingerprint(), new ArrayList<String>(items()));
|
return Tags.create(fingerprint(), items());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ import org.jclouds.googlecomputeengine.domain.Metadata;
|
||||||
import org.jclouds.googlecomputeengine.domain.NewInstance;
|
import org.jclouds.googlecomputeengine.domain.NewInstance;
|
||||||
import org.jclouds.googlecomputeengine.domain.AttachDisk;
|
import org.jclouds.googlecomputeengine.domain.AttachDisk;
|
||||||
import org.jclouds.googlecomputeengine.domain.Operation;
|
import org.jclouds.googlecomputeengine.domain.Operation;
|
||||||
|
import org.jclouds.googlecomputeengine.domain.Tags;
|
||||||
import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiLiveTest;
|
import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiLiveTest;
|
||||||
import org.jclouds.googlecomputeengine.options.DiskCreationOptions;
|
import org.jclouds.googlecomputeengine.options.DiskCreationOptions;
|
||||||
import org.testng.annotations.AfterClass;
|
import org.testng.annotations.AfterClass;
|
||||||
|
@ -93,9 +94,9 @@ public class InstanceApiLiveTest extends BaseGoogleComputeEngineApiLiveTest {
|
||||||
getNetworkUrl(INSTANCE_NETWORK_NAME), // network
|
getNetworkUrl(INSTANCE_NETWORK_NAME), // network
|
||||||
Arrays.asList(AttachDisk.newBootDisk(imageUri),
|
Arrays.asList(AttachDisk.newBootDisk(imageUri),
|
||||||
AttachDisk.existingDisk(getDiskUrl(DISK_NAME))), // disks
|
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");
|
instance.metadata().put("mykey", "myvalue");
|
||||||
|
|
||||||
instance2 = NewInstance.create(
|
instance2 = NewInstance.create(
|
||||||
|
|
|
@ -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/zones/us-central1-a/machineTypes/n1-standard-1")), // machineType
|
||||||
URI.create(url("/projects/party/global/networks/default")), // network
|
URI.create(url("/projects/party/global/networks/default")), // network
|
||||||
Arrays.asList(AttachDisk.existingBootDisk(URI.create(url("/projects/party/zones/us-central1-a/disks/test")))),
|
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");
|
newInstance.metadata().put("aKey", "aValue");
|
||||||
|
|
|
@ -57,7 +57,7 @@ public class ParseInstanceTest extends BaseGoogleComputeEngineParseTest<Instance
|
||||||
URI.create(baseUrl + "/party/zones/us-central1-a/instances/test-0"), // selfLink
|
URI.create(baseUrl + "/party/zones/us-central1-a/instances/test-0"), // selfLink
|
||||||
"test-0", // name
|
"test-0", // name
|
||||||
"desc", // description
|
"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
|
URI.create(baseUrl + "/party/zones/us-central1-a/machineTypes/n1-standard-1"), // machineType
|
||||||
Instance.Status.RUNNING, // status
|
Instance.Status.RUNNING, // status
|
||||||
null, // statusMessage
|
null, // statusMessage
|
||||||
|
|
Loading…
Reference in New Issue