Search in sources :

Example 1 with InstanceDescKey

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey in project photon-model by vmware.

the class AWSComputeStateCreationAdapterService method getRelatedComputeDescriptions.

/**
 * Looks up the compute descriptions associated with the compute states to be created in the
 * system.
 */
private void getRelatedComputeDescriptions(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
    // Get the related compute descriptions for all the compute states are to be updated and
    // created.
    Set<InstanceDescKey> representativeCDSet = getRepresentativeListOfCDsFromInstanceList(context.request.instancesToBeCreated, context.request.zones);
    representativeCDSet.addAll(getRepresentativeListOfCDsFromInstanceList(context.request.instancesToBeUpdated.values(), context.request.zones));
    if (representativeCDSet.isEmpty()) {
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
        return;
    }
    QueryTask queryTask = getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery(representativeCDSet, context.request.tenantLinks, context.request.regionId, context.request.parentComputeLink, context.request.endpointLink);
    queryTask.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + QUERY_TASK_EXPIRY_MICROS;
    // create the query to find an existing compute description
    QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
        if (e != null) {
            logWarning(() -> String.format("Failure retrieving query results: %s", e.toString()));
            finishWithFailure(context, e);
            return;
        }
        if (qrt != null && qrt.results.documentCount > 0) {
            for (Object s : qrt.results.documents.values()) {
                ComputeDescription localComputeDescription = Utils.fromJson(s, ComputeDescription.class);
                context.computeDescriptionMap.put(getKeyForComputeDescriptionFromCD(localComputeDescription), localComputeDescription.documentSelfLink);
            }
            logFine(() -> String.format("%d compute descriptions found", context.computeDescriptionMap.size()));
        } else {
            logFine(() -> "No compute descriptions found");
        }
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey)

Example 2 with InstanceDescKey

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey 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 3 with InstanceDescKey

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey 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);
}
Also used : ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) HashMap(java.util.HashMap) AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance) Instance(com.amazonaws.services.ec2.model.Instance) Placement(com.amazonaws.services.ec2.model.Placement) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey) Test(org.junit.Test)

Aggregations

InstanceDescKey (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey)3 Instance (com.amazonaws.services.ec2.model.Instance)2 ZoneData (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData)2 InstanceNetworkInterface (com.amazonaws.services.ec2.model.InstanceNetworkInterface)1 Placement (com.amazonaws.services.ec2.model.Placement)1 AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getKeyForComputeDescriptionFromInstance)1 AWSEnumerationUtils.mapInstanceToComputeState (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState)1 AdapterUtils.createDeleteOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation)1 AdapterUtils.createPatchOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation)1 AdapterUtils.createPostOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation)1 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)1 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)1 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)1 Operation (com.vmware.xenon.common.Operation)1 QueryTask (com.vmware.xenon.services.common.QueryTask)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1