public Instance createInstance(GoogleCloud cloud, MachineCreationRequest request, PublicKey sshPublicKey)
throws OpsException {
// GoogleComputeClient computeClient = getComputeClient(cloud);
try {
Image foundImage = null;
{
DiskImageRecipe recipe = null;
if (request.recipeId != null) {
recipe = platformLayerClient.getItem(request.recipeId, DiskImageRecipe.class);
}
OperatingSystemRecipe operatingSystem = null;
if (recipe != null) {
operatingSystem = recipe.getOperatingSystem();
}
log.info("Listing images to pick best image");
Iterable<Image> images = listImages(PROJECTID_GOOGLE);
// TODO: We need a better solution here!!
log.warn("Hard coding image names");
Set<String> imageNames = Sets.newHashSet("ubuntu-12-04-v20120621");
// Set<String> imageNames = Sets.newHashSet("centos-6-2-v20120621");
for (Image image : images) {
if (imageNames.contains(image.getName())) {
foundImage = image;
break;
}
}
if (foundImage == null) {
throw new IllegalArgumentException("Could not find image");
}
}
// GCE requires that the name comply with RFC1035, which I think means a valid DNS
// For now, just use a UUID, with a pl- prefix so it doesn't start with a number
// TODO: Fix this!
String instanceName = "pl-" + UUID.randomUUID().toString();
Operation createServerOperation;
{
Instance create = new Instance();
create.setName(instanceName);
create.setZone(buildZoneUrl(projectId, ZONE_US_CENTRAL1_A));
{
NetworkInterface networkInterface = new NetworkInterface();
networkInterface.setNetwork(buildNetworkUrl(projectId, "default"));
AccessConfig networkAccessConfig = new AccessConfig();
networkAccessConfig.setType("ONE_TO_ONE_NAT");
networkInterface.setAccessConfigs(Lists.newArrayList(networkAccessConfig));
create.setNetworkInterfaces(Lists.newArrayList(networkInterface));
}
Metadata metadata = new Metadata();
metadata.setItems(Lists.<Items> newArrayList());
create.setMetadata(metadata);
if (request.tags != null) {
for (Tag tag : request.tags) {
Metadata.Items meta = new Metadata.Items();
meta.setKey(tag.getKey());
meta.setValue(tag.getValue());
metadata.getItems().add(meta);
}
}
if (request.sshPublicKey != null) {
Metadata.Items meta = new Metadata.Items();
meta.setKey("sshKeys");
meta.setValue(USER_NAME + ":" + OpenSshUtils.serialize(sshPublicKey));
metadata.getItems().add(meta);
}
create.setImage(foundImage.getSelfLink());
MachineType flavor = getClosestInstanceType(request);
if (flavor == null) {
throw new OpsException("Cannot determine machine type for request");
}