use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState 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);
}
});
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class VSphereNetworkEnumerationHelper method updateNetwork.
private static void updateNetwork(VSphereIncrementalEnumerationService service, NetworkState oldDocument, EnumerationProgress enumerationProgress, NetworkOverlay net) {
NetworkState networkState = makeNetworkStateFromResults(service, enumerationProgress, net);
// restore original selfLink
networkState.documentSelfLink = oldDocument.documentSelfLink;
networkState.resourcePoolLink = null;
if (oldDocument.tenantLinks == null) {
networkState.tenantLinks = enumerationProgress.getTenantLinks();
}
service.logFine(() -> String.format("Syncing Network %s", net.getName()));
Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), oldDocument.documentSelfLink)).setBody(networkState).setCompletion(trackNetwork(enumerationProgress, net)).sendWith(service);
// represent a Network also as a subnet
SubnetState subnet = createSubnetStateForNetwork(networkState, enumerationProgress, net);
if (subnet != null) {
Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), subnet.documentSelfLink)).setBody(subnet).sendWith(service);
}
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class VSphereNetworkEnumerationHelper method makeSubnetStateFromResults.
private static SubnetState makeSubnetStateFromResults(EnumerationProgress enumerationProgress, NetworkOverlay net) {
ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
SubnetState state = new SubnetState();
state.id = state.name = net.getName();
state.endpointLink = enumerationProgress.getRequest().endpointLink;
AdapterUtils.addToEndpointLinks(state, enumerationProgress.getRequest().endpointLink);
ManagedObjectReference parentSwitch = net.getParentSwitch();
state.networkLink = buildStableDvsLink(parentSwitch, request.endpointLink);
state.regionId = enumerationProgress.getRegionId();
CustomProperties customProperties = CustomProperties.of(state).put(CustomProperties.MOREF, net.getId()).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink()).put(CustomProperties.TYPE, net.getId().getType());
customProperties.put(DvsProperties.PORT_GROUP_KEY, net.getPortgroupKey());
return state;
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class VSphereNetworkEnumerationHelper method createNewNetwork.
private static void createNewNetwork(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, NetworkOverlay net) {
NetworkState networkState = makeNetworkStateFromResults(service, enumerationProgress, net);
networkState.tenantLinks = enumerationProgress.getTenantLinks();
Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), NetworkService.FACTORY_LINK)).setBody(networkState).setCompletion((o, e) -> {
trackNetwork(enumerationProgress, net).handle(o, e);
Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ResourceGroupService.FACTORY_LINK)).setBody(makeNetworkGroup(net, enumerationProgress)).sendWith(service);
}).sendWith(service);
service.logFine(() -> String.format("Found new Network %s", net.getName()));
SubnetState subnet = createSubnetStateForNetwork(networkState, enumerationProgress, net);
if (subnet != null) {
Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), SubnetService.FACTORY_LINK)).setBody(subnet).sendWith(service);
}
}
use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.
the class VSphereNetworkEnumerationHelper method updateSubnet.
private static void updateSubnet(VSphereIncrementalEnumerationService service, SubnetState oldDocument, EnumerationProgress enumerationProgress, NetworkOverlay net, boolean fullUpdate) {
SubnetState state;
if (fullUpdate) {
state = makeSubnetStateFromResults(enumerationProgress, net);
} else {
state = makeSubnetStateFromChanges(net);
}
state.documentSelfLink = oldDocument.documentSelfLink;
if (oldDocument.tenantLinks == null) {
state.tenantLinks = enumerationProgress.getTenantLinks();
}
service.logFine(() -> String.format("Syncing Subnet(Portgroup) %s", net.getName()));
Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), oldDocument.documentSelfLink)).setBody(state).setCompletion(trackNetwork(enumerationProgress, net)).sendWith(service);
}
Aggregations