use of com.vmware.photon.controller.model.adapters.vsphere.CustomProperties.DATACENTER_SELF_LINK in project photon-model by vmware.
the class VSphereAdapterInstanceService method handleCreateInstance.
private void handleCreateInstance(ProvisionContext ctx) {
ctx.pool.submit(ctx.getAdapterManagementReference(), ctx.vSphereCredentials, (connection, ce) -> {
if (ctx.fail(ce)) {
return;
}
try {
InstanceClient client = new InstanceClient(connection, ctx);
ComputeState state;
if (ctx.templateMoRef != null) {
state = client.createInstanceFromTemplate(ctx.templateMoRef);
} else if (ctx.image != null) {
ManagedObjectReference moRef = CustomProperties.of(ctx.image).getMoRef(CustomProperties.MOREF);
if (moRef != null) {
// the image is backed by a template VM
state = client.createInstanceFromTemplate(moRef);
} else {
// library item
state = client.createInstanceFromLibraryItem(ctx.image);
}
} else if (ctx.snapshotMoRef != null) {
state = client.createInstanceFromSnapshot();
} else {
state = client.createInstance();
}
if (state == null) {
// assume they will patch the task if they have provisioned the vm
return;
}
// populate state, MAC address being very important
VmOverlay vmOverlay = client.enrichStateFromVm(state);
Operation[] finishTask = new Operation[1];
for (NetworkInterfaceStateWithDetails nic : ctx.nics) {
// request guest customization while vm of powered off
SubnetState subnet = nic.subnet;
if (subnet != null && nic.description != null && nic.description.assignment == IpAssignment.STATIC) {
CustomizationClient cc = new CustomizationClient(connection, ctx.child, vmOverlay.getGuestId());
CustomizationSpec template = new CustomizationSpec();
cc.customizeNic(vmOverlay.getPrimaryMac(), ctx.child.hostName, nic.address, subnet, template);
cc.customizeDns(subnet.dnsServerAddresses, subnet.dnsSearchDomains, template);
ManagedObjectReference task = cc.customizeGuest(client.getVm(), template);
TaskInfo taskInfo = VimUtils.waitTaskEnd(connection, task);
if (taskInfo.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(taskInfo.getError());
}
}
}
// power on machine before enrichment
if (ctx.child.powerState == PowerState.ON) {
new PowerStateClient(connection).changePowerState(client.getVm(), PowerState.ON, null, 0);
state.powerState = PowerState.ON;
Operation op = ctx.mgr.createTaskPatch(TaskStage.FINISHED);
Boolean awaitIp = CustomProperties.of(ctx.child).getBoolean(ComputeProperties.CUSTOM_PROP_COMPUTE_AWAIT_IP, true);
if (awaitIp) {
Runnable runnable = createCheckForIpTask(ctx.pool, op, client.getVm(), connection.createUnmanagedCopy(), ctx.child.documentSelfLink, ctx);
ctx.pool.schedule(runnable, IP_CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS);
} else {
finishTask[0] = op;
}
} else {
// only finish the task without waiting for IP
finishTask[0] = ctx.mgr.createTaskPatch(TaskStage.FINISHED);
}
updateNicsAfterProvisionSuccess(vmOverlay.getNics(), ctx);
updateDiskLinksAfterProvisionSuccess(state, vmOverlay.getDisks(), ctx);
state.lifecycleState = LifecycleState.READY;
// Find the host link where the computed is provisioned and patch the
// compute state.
queryHostDocumentAndUpdateCompute(ctx, vmOverlay.getHost()).thenCompose(computeState -> {
ComputeState hostState = computeState.iterator().next();
CustomProperties.of(state).put(VC_UUID, CustomProperties.of(hostState).getString(VC_UUID)).put(DATACENTER_SELF_LINK, CustomProperties.of(hostState).getString(DATACENTER_SELF_LINK)).put(COMPUTE_HOST_LINK_PROP_NAME, hostState.documentSelfLink);
return createComputeResourcePatch(state, ctx.computeReference);
}).whenComplete((o, e) -> {
if (e != null) {
ctx.fail(e);
return;
}
if (finishTask.length > 0) {
finishTask[0].sendWith(this);
}
});
} catch (Exception e) {
ctx.fail(e);
}
});
}
Aggregations