Search in sources :

Example 71 with Instance

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);
    }
}
Also used : AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Instance(com.amazonaws.services.ec2.model.Instance) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) ArrayList(java.util.ArrayList) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 72 with Instance

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);
    }
}
Also used : AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Instance(com.amazonaws.services.ec2.model.Instance) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 73 with Instance

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);
    }
}
Also used : AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) Instance(com.amazonaws.services.ec2.model.Instance) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) HashSet(java.util.HashSet)

Example 74 with Instance

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;
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) Volume(com.amazonaws.services.ec2.model.Volume) DescribeNatGatewaysRequest(com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest) ArrayList(java.util.ArrayList) NatGateway(com.amazonaws.services.ec2.model.NatGateway) ArrayList(java.util.ArrayList) List(java.util.List) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest)

Example 75 with Instance

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;
        }
    };
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) Instance(com.amazonaws.services.ec2.model.Instance) DescribeNatGatewaysResult(com.amazonaws.services.ec2.model.DescribeNatGatewaysResult) NatGateway(com.amazonaws.services.ec2.model.NatGateway) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AmazonWebServiceResult(com.amazonaws.AmazonWebServiceResult) Volume(com.amazonaws.services.ec2.model.Volume) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)86 ArrayList (java.util.ArrayList)43 Reservation (com.amazonaws.services.ec2.model.Reservation)39 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)31 List (java.util.List)23 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)22 HashMap (java.util.HashMap)22 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18 Operation (com.vmware.xenon.common.Operation)18 Tag (com.amazonaws.services.ec2.model.Tag)17 Map (java.util.Map)17 Test (org.junit.Test)16 Filter (com.amazonaws.services.ec2.model.Filter)14 HashSet (java.util.HashSet)13 Volume (com.amazonaws.services.ec2.model.Volume)12 Utils (com.vmware.xenon.common.Utils)12 TimeUnit (java.util.concurrent.TimeUnit)12 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)11 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)11 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)10