use of software.amazon.awssdk.services.ec2.model.Instance 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 software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method updateTagLinks.
/**
* Updates tag links of existing computes using TagsUtil.
*/
private DeferredResult<AWSComputeStateCreationContext> updateTagLinks(AWSComputeStateCreationContext context) {
if (context.request.instancesToBeUpdated == null || context.request.instancesToBeUpdated.size() == 0) {
logFine(() -> "No local compute states to be updated so there are no tags to update.");
return DeferredResult.completed(context);
} else {
List<DeferredResult<Set<String>>> updateCSTagLinksOps = new ArrayList<>();
for (String instanceId : context.request.instancesToBeUpdated.keySet()) {
Instance instance = context.request.instancesToBeUpdated.get(instanceId);
ComputeState existingComputeState = context.request.computeStatesToBeUpdated.get(instanceId);
Map<String, String> remoteTags = new HashMap<>();
for (Tag awsInstanceTag : instance.getTags()) {
if (!awsInstanceTag.getKey().equals(AWSConstants.AWS_TAG_NAME)) {
remoteTags.put(awsInstanceTag.getKey(), awsInstanceTag.getValue());
}
}
updateCSTagLinksOps.add(updateLocalTagStates(this, existingComputeState, remoteTags, null));
}
return DeferredResult.allOf(updateCSTagLinksOps).thenApply(gnore -> context);
}
}
use of software.amazon.awssdk.services.ec2.model.Instance 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 software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSTaskStatusChecker method buildRequest.
private AmazonWebServiceRequest buildRequest(T type) {
if (type instanceof Instance) {
DescribeInstancesRequest descRequest = new DescribeInstancesRequest();
List<String> instanceIdList = new ArrayList<>();
instanceIdList.add(this.instanceId);
descRequest.setInstanceIds(instanceIdList);
return descRequest;
} else if (type instanceof NatGateway) {
DescribeNatGatewaysRequest descRequest = new DescribeNatGatewaysRequest();
List<String> instanceIdList = new ArrayList<>();
instanceIdList.add(this.instanceId);
descRequest.setNatGatewayIds(instanceIdList);
return descRequest;
} else if (type instanceof Volume) {
DescribeVolumesRequest descRequest = new DescribeVolumesRequest();
List<String> volumeIdList = new ArrayList<>();
volumeIdList.add(this.instanceId);
descRequest.setVolumeIds(volumeIdList);
return descRequest;
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
return null;
}
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSTaskStatusChecker method buildHandler.
private AsyncHandler buildHandler(T type) {
return new AsyncHandler<AmazonWebServiceRequest, AmazonWebServiceResult>() {
@Override
public void onError(Exception exception) {
// particular instanceId.
if (exception instanceof AmazonServiceException && ((AmazonServiceException) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_INSTANCE_ID_ERROR_CODE)) {
AWSTaskStatusChecker.this.service.logWarning("Could not retrieve status for instance %s. Retrying... Exception on AWS is %s", AWSTaskStatusChecker.this.instanceId, exception);
AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
return;
} else if (exception instanceof AmazonEC2Exception && ((AmazonEC2Exception) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
AWSTaskStatusChecker.this.consumer.accept(null);
return;
}
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(exception);
return;
}
@Override
public void onSuccess(AmazonWebServiceRequest request, AmazonWebServiceResult result) {
String status;
Object instance;
String failureMessage = null;
String stateReason = null;
if (result instanceof DescribeInstancesResult) {
instance = ((DescribeInstancesResult) result).getReservations().get(0).getInstances().get(0);
Instance vm = (Instance) instance;
status = vm.getState().getName();
stateReason = vm.getStateReason() != null ? vm.getStateReason().getMessage() : null;
} else if (result instanceof DescribeNatGatewaysResult) {
instance = ((DescribeNatGatewaysResult) result).getNatGateways().get(0);
status = ((NatGateway) instance).getState();
// if NAT gateway creation fails, the status is still "pending";
// rather than keep checking for status and eventually time out, get the
// failure message and fail the task
failureMessage = ((NatGateway) instance).getFailureMessage();
} else if (result instanceof DescribeVolumesResult) {
instance = ((DescribeVolumesResult) result).getVolumes().get(0);
status = ((Volume) instance).getState().toLowerCase();
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + result));
return;
}
if (failureMessage != null) {
// operation failed; no need to keep checking for desired status
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException(failureMessage));
return;
} else if (AWSTaskStatusChecker.this.failureStates.contains(status)) {
// operation failed; no need to keep checking for desired status
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException("Resource is state:[" + status + "]," + "reason:" + stateReason));
return;
} else if (!status.equals(AWSTaskStatusChecker.this.desiredState)) {
AWSTaskStatusChecker.this.service.logInfo("Instance %s not yet in desired state %s. Current state %s, failure states %s, waiting 5s", AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, status, AWSTaskStatusChecker.this.failureStates);
// if the instance is not in the desired state, schedule thread
// to run again in 5 seconds
AWSTaskStatusChecker.this.service.getHost().schedule(() -> {
AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
}, 5, TimeUnit.SECONDS);
return;
}
AWSTaskStatusChecker.this.consumer.accept(instance);
return;
}
};
}
Aggregations