public NodeAndInitialCredentials<InstanceInZone> createNodeWithGroupEncodedIntoName(
final String group, final String name, final Template template) {
checkNotNull(template, "template");
GoogleComputeEngineTemplateOptions options = GoogleComputeEngineTemplateOptions.class.cast(template.getOptions()).clone();
checkState(options.getNetwork().isPresent(), "network was not present in template options");
Hardware hardware = checkNotNull(template.getHardware(), "hardware must be set");
checkNotNull(hardware.getUri(), "hardware must have a URI");
checkNotNull(template.getImage().getUri(), "image URI is null");
// Note that the ordering is significant here - the first disk must be the boot disk.
List<PersistentDisk> disks = Lists.newArrayList();
if (!tryFind(options.getDisks(), isBootDisk()).isPresent()) {
Disk bootDisk = createBootDisk(template, name);
disks.add(new PersistentDisk(Mode.READ_WRITE,
bootDisk.getSelfLink(),
null,
true,
true));
}
disks.addAll(options.getDisks());
InstanceTemplate instanceTemplate = InstanceTemplate.builder()
.forMachineType(hardware.getUri());
if (options.isEnableNat()) {
instanceTemplate.addNetworkInterface(options.getNetwork().get(), Type.ONE_TO_ONE_NAT);
} else {
instanceTemplate.addNetworkInterface(options.getNetwork().get());
}
instanceTemplate.disks(disks);
LoginCredentials credentials = getFromImageAndOverrideIfRequired(template.getImage(), options);
ImmutableMap.Builder<String, String> metadataBuilder = metatadaFromTemplateOptions.apply(options);
metadataBuilder.put(GCE_IMAGE_METADATA_KEY, template.getImage().getUri().toString());
if (!options.shouldKeepBootDisk()) {
metadataBuilder.put(GCE_DELETE_BOOT_DISK_METADATA_KEY, Boolean.TRUE.toString());
}
instanceTemplate.metadata(metadataBuilder.build());
instanceTemplate.serviceAccounts(options.getServiceAccounts());
final InstanceApi instanceApi = api.getInstanceApiForProject(userProject.get());
final String zone = template.getLocation().getId();
Operation operation = instanceApi.createInZone(name, zone, instanceTemplate);
if (options.shouldBlockUntilRunning()) {
waitOperationDone(operation);
}
// some times the newly created instances are not immediately returned
AtomicReference<Instance> instance = Atomics.newReference();
retry(new Predicate<AtomicReference<Instance>>() {
@Override
public boolean apply(AtomicReference<Instance> input) {
input.set(instanceApi.getInZone(zone, name));
return input.get() != null;
}
}, operationCompleteCheckTimeout, operationCompleteCheckInterval, MILLISECONDS).apply(instance);
if (!options.getTags().isEmpty()) {
Operation tagsOperation = instanceApi.setTagsInZone(zone,
name, options.getTags(), instance.get().getTags().getFingerprint());
waitOperationDone(tagsOperation);
retry(new Predicate<AtomicReference<Instance>>() {
@Override
public boolean apply(AtomicReference<Instance> input) {
input.set(instanceApi.getInZone(zone, name));
return input.get() != null;
}
}, operationCompleteCheckTimeout, operationCompleteCheckInterval, MILLISECONDS).apply(instance);
}
// Add tags for security groups
final FirewallTagNamingConvention naming = firewallTagNamingConvention.get(group);
Set<String> tags = FluentIterable.from(Ints.asList(options.getInboundPorts()))
.transform(new Function<Integer, String>(){
@Override
public String apply(Integer input) {
return input != null
? naming.name(input)