ImmutableMap.Builder<String, String> metadataBuilder = metatadaFromTemplateOptions.apply(options);
instanceTemplate.metadata(metadataBuilder.build());
instanceTemplate.serviceAccounts(options.getServiceAccounts());
instanceTemplate.image(checkNotNull(template.getImage().getUri(), "image URI is null"));
Operation operation = api.getInstanceApiForProject(userProject.get())
.createInZone(name, template.getLocation().getId(), instanceTemplate);
if (options.shouldBlockUntilRunning()) {
waitOperationDone(operation);
}
// some times the newly created instances are not immediately returned
AtomicReference<Instance> instance = new AtomicReference<Instance>();
retry(new Predicate<AtomicReference<Instance>>() {
@Override
public boolean apply(AtomicReference<Instance> input) {
input.set(api.getInstanceApiForProject(userProject.get()).getInZone(template.getLocation().getId(),
name));
return input.get() != null;
}
}, operationCompleteCheckTimeout, operationCompleteCheckInterval, MILLISECONDS).apply(instance);
if (options.getTags().size() > 0) {
Operation tagsOperation = api.getInstanceApiForProject(userProject.get()).setTagsInZone(template.getLocation().getId(),
name, options.getTags(), instance.get().getTags().getFingerprint());
waitOperationDone(tagsOperation);
retry(new Predicate<AtomicReference<Instance>>() {