use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method populateUpdateOperations.
private void populateUpdateOperations(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
if (context.request.instancesToBeUpdated == null || context.request.instancesToBeUpdated.size() == 0) {
logFine(() -> "No local compute states to be updated");
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
} else {
logFine(() -> String.format("Need to update %d local compute states", context.request.instancesToBeUpdated.size()));
for (String instanceId : context.request.instancesToBeUpdated.keySet()) {
Instance instance = context.request.instancesToBeUpdated.get(instanceId);
ComputeState existingComputeState = context.request.computeStatesToBeUpdated.get(instanceId);
if (StringUtils.isEmpty(existingComputeState.endpointLink)) {
existingComputeState.endpointLink = context.request.endpointLink;
}
Set<String> endpointLinks = new HashSet<>();
if (existingComputeState.endpointLinks != null) {
endpointLinks.addAll(existingComputeState.endpointLinks);
}
endpointLinks.add(context.request.endpointLink);
// Calculate NICs delta - collection of NIC States to add, to update and to delete
Map<String, List<Integer>> deviceIndexesDelta = new HashMap<>();
Map<String, Map<String, Collection<Object>>> linksToNICSToAddOrRemove = new HashMap<>();
// The stopped or stopping instance does not have full network settings.
if (!AWSEnumerationUtils.instanceIsInStoppedState(instance)) {
// Get existing NetworkInterfaceStates for this ComputeState
List<NetworkInterfaceState> existingNicStates = context.request.nicStatesToBeUpdated.get(instanceId);
deviceIndexesDelta = calculateNICsDeviceIndexesDelta(instance, existingNicStates);
linksToNICSToAddOrRemove = addUpdateOrRemoveNICStates(context, instance, deviceIndexesDelta, existingComputeState.endpointLink, endpointLinks);
}
// Create dedicated PATCH operation for updating NIC Links {{
if (linksToNICSToAddOrRemove.get(ADD_NIC_STATES) != null || linksToNICSToAddOrRemove.get(REMOVE_NIC_STATES) != null) {
ServiceStateCollectionUpdateRequest updateComputeStateRequest = ServiceStateCollectionUpdateRequest.create(linksToNICSToAddOrRemove.get(ADD_NIC_STATES), linksToNICSToAddOrRemove.get(REMOVE_NIC_STATES));
Operation patchComputeStateNICLinks = Operation.createPatch(UriUtils.buildUri(this.getHost(), existingComputeState.documentSelfLink)).setBody(updateComputeStateRequest).setReferer(this.getUri());
context.enumerationOperations.add(patchComputeStateNICLinks);
}
// Update ComputeState
String zoneId = instance.getPlacement().getAvailabilityZone();
ZoneData zoneData = context.request.zones.get(zoneId);
ComputeState computeStateToBeUpdated = mapInstanceToComputeState(this.getHost(), instance, context.request.parentComputeLink, zoneData.computeLink, existingComputeState.resourcePoolLink != null ? existingComputeState.resourcePoolLink : context.request.resourcePoolLink, existingComputeState.endpointLink, endpointLinks, existingComputeState.descriptionLink, context.request.parentCDStatsAdapterReferences, context.internalTagLinksMap.get(ec2_instance.toString()), zoneData.regionId, zoneId, context.request.tenantLinks, null, false, null);
computeStateToBeUpdated.documentSelfLink = existingComputeState.documentSelfLink;
Operation patchComputeState = createPatchOperation(this, computeStateToBeUpdated, existingComputeState.documentSelfLink);
context.enumerationOperations.add(patchComputeState);
// Reconcile remote diskLinks with current diskLinks of existing computeState and
// update them with collection update request if needed.
List<String> remoteDiskLinks = context.diskLinksByInstances.get(instance);
List<String> currentDiskLinks = existingComputeState.diskLinks;
Collection<Object> linksToAdd = new ArrayList<>();
Collection<Object> linksToRemove = new ArrayList<>();
if (remoteDiskLinks == null && currentDiskLinks != null) {
linksToRemove.addAll(currentDiskLinks);
} else if (remoteDiskLinks != null && currentDiskLinks == null) {
linksToAdd.addAll(remoteDiskLinks);
} else if (remoteDiskLinks != null && currentDiskLinks != null) {
currentDiskLinks.stream().forEach(link -> {
if (!remoteDiskLinks.contains(link)) {
linksToRemove.add(link);
}
});
remoteDiskLinks.removeAll(currentDiskLinks);
linksToAdd.addAll(remoteDiskLinks);
}
// If existing computeState does not have an internal tag then we need to add it
// to tagLinks with collection update request patch.
String ec2ComputeInternalTagLink = context.internalTagLinksMap.get(ec2_instance.toString()).iterator().next();
// and send a combined collection update request for both of these.
if (existingComputeState.tagLinks == null || (existingComputeState.tagLinks != null && !existingComputeState.tagLinks.contains(ec2ComputeInternalTagLink)) || !linksToAdd.isEmpty() || !linksToRemove.isEmpty()) {
Map<String, Collection<Object>> itemsToAdd = new HashMap<>();
Map<String, Collection<Object>> itemsToRemove = new HashMap<>();
itemsToAdd.put(ComputeState.FIELD_NAME_DISK_LINKS, linksToAdd);
itemsToRemove.put(ComputeState.FIELD_NAME_DISK_LINKS, linksToRemove);
itemsToAdd.put(ComputeState.FIELD_NAME_TAG_LINKS, Collections.singletonList(ec2ComputeInternalTagLink));
ServiceStateCollectionUpdateRequest updateTagLinksAndDiskLinks = ServiceStateCollectionUpdateRequest.create(itemsToAdd, itemsToRemove);
context.enumerationOperations.add(Operation.createPatch(this.getHost(), existingComputeState.documentSelfLink).setBody(updateTagLinksAndDiskLinks).setReferer(this.getUri()));
}
}
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
}
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method populateCreateOperations.
/**
* Method to create Compute States associated with the instances received from the AWS host.
*/
private void populateCreateOperations(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
if (context.request.instancesToBeCreated == null || context.request.instancesToBeCreated.size() == 0) {
logFine(() -> "No local compute states to be created");
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
} else {
logFine(() -> String.format("Need to create %d local compute states", context.request.instancesToBeCreated.size()));
for (int i = 0; i < context.request.instancesToBeCreated.size(); i++) {
Instance instance = context.request.instancesToBeCreated.get(i);
String zoneId = instance.getPlacement().getAvailabilityZone();
ZoneData zoneData = context.request.zones.get(zoneId);
String regionId = zoneData.regionId;
InstanceDescKey descKey = InstanceDescKey.build(regionId, zoneId, instance.getInstanceType());
Set<String> endpointLinks = new HashSet<>();
endpointLinks.add(context.request.endpointLink);
ComputeState computeStateToBeCreated = mapInstanceToComputeState(this.getHost(), instance, context.request.parentComputeLink, zoneData.computeLink, context.request.resourcePoolLink, null, endpointLinks, context.computeDescriptionMap.get(descKey), context.request.parentCDStatsAdapterReferences, context.internalTagLinksMap.get(ec2_instance.toString()), regionId, zoneId, context.request.tenantLinks, context.createdExternalTags, true, context.diskLinksByInstances.get(instance));
computeStateToBeCreated.networkInterfaceLinks = new ArrayList<>();
if (!AWSEnumerationUtils.instanceIsInStoppedState(instance)) {
// ComputeState to be created to the NIC State
for (InstanceNetworkInterface awsNic : instance.getNetworkInterfaces()) {
if (context.request.enumeratedNetworks != null && context.request.enumeratedNetworks.subnets != null && context.request.enumeratedNetworks.subnets.containsKey(awsNic.getSubnetId())) {
NetworkInterfaceState nicState = createNICStateAndDescription(context, awsNic, null, endpointLinks);
computeStateToBeCreated.networkInterfaceLinks.add(UriUtils.buildUriPath(NetworkInterfaceService.FACTORY_LINK, nicState.documentSelfLink));
}
}
}
Operation postComputeState = createPostOperation(this, computeStateToBeCreated, ComputeService.FACTORY_LINK);
context.enumerationOperations.add(postComputeState);
}
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
}
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData in project photon-model by vmware.
the class TestAWSEnumerationUtils method testGetComputeDescriptionKeyFromAWSInstance.
@Test
public void testGetComputeDescriptionKeyFromAWSInstance() throws Throwable {
Map<String, ZoneData> zones = new HashMap<>();
zones.put(AWS_ZONE_ID, ZoneData.build(AWS_REGION_ID, AWS_ZONE_ID, ""));
Instance awsInstance = new Instance();
awsInstance.setInstanceId(AWS_INSTANCE_ID);
Placement placement = new Placement();
placement.setAvailabilityZone(AWS_ZONE_ID);
awsInstance.setPlacement(placement);
String regionId = getRegionId(awsInstance);
awsInstance.setInstanceType(AWS_INSTANCE_TYPE);
awsInstance.setVpcId(AWS_VPC_ID);
assertEquals(AWS_REGION_ID, regionId);
InstanceDescKey computeDescriptionKey = getKeyForComputeDescriptionFromInstance(awsInstance, zones);
assertEquals(AWS_COMPUTE_DESCRIPTION_KEY, computeDescriptionKey);
}
Aggregations