Search in sources :

Example 6 with InstanceType

use of com.amazonaws.services.ec2.model.InstanceType in project photon-model by vmware.

the class TestAWSSetupUtils method provisionAWSEBSVMWithEC2Client.

public static String provisionAWSEBSVMWithEC2Client(VerificationHost host, AmazonEC2Client client, String ami, String subnetId, String securityGroupId, BlockDeviceMapping blockDeviceMapping) {
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withSubnetId(subnetId).withImageId(ami).withInstanceType(instanceType).withMinCount(1).withMaxCount(1).withSecurityGroupIds(securityGroupId).withBlockDeviceMappings(blockDeviceMapping);
    // handler invoked once the EC2 runInstancesAsync commands completes
    RunInstancesResult result = null;
    try {
        result = client.runInstances(runInstancesRequest);
    } catch (Exception e) {
        host.log(Level.SEVERE, "Error encountered in provisioning machine on AWS", Utils.toString(e));
    }
    assertNotNull(result);
    assertNotNull(result.getReservation());
    assertNotNull(result.getReservation().getInstances());
    assertEquals(1, result.getReservation().getInstances().size());
    String instanceId = result.getReservation().getInstances().get(0).getInstanceId();
    tagResources(client, Arrays.asList(instanceId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_INSTANCE);
    return instanceId;
}
Also used : RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with InstanceType

use of com.amazonaws.services.ec2.model.InstanceType in project photon-model by vmware.

the class TestAWSSetupUtils method provisionAWSVMWithEC2Client.

public static String provisionAWSVMWithEC2Client(VerificationHost host, AmazonEC2Client client, String ami, String subnetId, String securityGroupId) {
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withSubnetId(subnetId).withImageId(ami).withInstanceType(instanceType).withMinCount(1).withMaxCount(1).withSecurityGroupIds(securityGroupId);
    // handler invoked once the EC2 runInstancesAsync commands completes
    RunInstancesResult result = null;
    try {
        result = client.runInstances(runInstancesRequest);
    } catch (Exception e) {
        host.log(Level.SEVERE, "Error encountered in provisioning machine on AWS", Utils.toString(e));
    }
    assertNotNull(result);
    assertNotNull(result.getReservation());
    assertNotNull(result.getReservation().getInstances());
    assertEquals(1, result.getReservation().getInstances().size());
    String instanceId = result.getReservation().getInstances().get(0).getInstanceId();
    tagResources(client, Arrays.asList(instanceId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_INSTANCE);
    return instanceId;
}
Also used : RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) UnknownHostException(java.net.UnknownHostException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with InstanceType

use of com.amazonaws.services.ec2.model.InstanceType in project photon-model by vmware.

the class TestAWSEnumerationTask method testEnumeration.

// Runs the enumeration task on the AWS endpoint to list all the instances on the endpoint.
@Test
public void testEnumeration() throws Throwable {
    this.host.log("Running test: " + this.currentTestName.getMethodName());
    ComputeState vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, TestAWSSetupUtils.class, zoneId, regionId, null, this.singleNicSpec, this.awsTestContext);
    if (this.isMock) {
        // Just make a call to the enumeration service and make sure that the adapter patches
        // the parent with completion.
        enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_MOCK_MODE);
        return;
    }
    // Overriding the page size to test the pagination logic with limited instances on AWS.
    // This is a functional test
    // so the latency numbers maybe higher from this test due to low page size.
    setQueryPageSize(DEFAULT_TEST_PAGE_SIZE);
    setQueryResultLimit(DEFAULT_TEST_PAGE_SIZE);
    // Provision a single VM . Check initial state.
    vmState = provisionMachine(this.host, vmState, this.isMock, this.instancesToCleanUp);
    queryComputeInstances(this.host, count2);
    queryDocumentsAndAssertExpectedCount(this.host, count2, ComputeDescriptionService.FACTORY_LINK, false);
    if (ENABLE_LOAD_BALANCER_ENUMERATION) {
        this.lbToCleanUp = provisionAWSLoadBalancerWithEC2Client(this.host, this.lbClient, null, this.subnetId, this.securityGroupId, Collections.singletonList(vmState.id));
    }
    // CREATION directly on AWS
    List<String> instanceIdsToDeleteFirstTime = provisionAWSVMWithEC2Client(this.client, this.host, count4, T2_MICRO_INSTANCE_TYPE, this.subnetId, this.securityGroupId);
    List<String> instanceIds = provisionAWSVMWithEC2Client(this.client, this.host, count1, instanceType, this.subnetId, this.securityGroupId);
    instanceIdsToDeleteFirstTime.addAll(instanceIds);
    this.instancesToCleanUp.addAll(instanceIdsToDeleteFirstTime);
    waitForProvisioningToComplete(instanceIdsToDeleteFirstTime, this.host, this.client, ZERO);
    // Xenon does not know about the new instances.
    ProvisioningUtils.queryComputeInstances(this.host, count2);
    // Create S3 bucket on amazon
    Map<String, String> tags = new HashMap<>();
    tags.put(S3_TAG_KEY_1, S3_TAG_VALUE_1);
    tags.put(S3_TAG_KEY_2, S3_TAG_VALUE_2);
    createS3BucketAndTags(tags);
    this.bucketToBeDeleted = TEST_BUCKET_NAME;
    enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
    // Get a count of how many EBS disks are attached to a VM initially.
    ComputeState csForDiskLinkValidation = getComputeStateFromId(this.instancesToCleanUp.get(0));
    this.initialEbsDiskLinkCount = csForDiskLinkValidation.diskLinks.size();
    // Create a volume to be attached to the VM later.
    CreateVolumeRequest createVolumeRequest = new CreateVolumeRequest().withAvailabilityZone(csForDiskLinkValidation.zoneId).withVolumeType(VolumeType.Gp2).withSize(10);
    CreateVolumeResult createVolumeResult = this.client.createVolume(createVolumeRequest);
    this.testEbsId = createVolumeResult.getVolume().getVolumeId();
    // Validate if the S3 bucket is enumerated.
    validateS3Enumeration(count1, count3);
    // Validate S3 tag state count.
    validateS3TagStatesCreated();
    if (ENABLE_LOAD_BALANCER_ENUMERATION) {
        // Validate Load Balancer State
        validateLoadBalancerState(this.lbToCleanUp, vmState.documentSelfLink);
    }
    // Remove a tag from test S3 bucket.
    tags.clear();
    tags.put(S3_TAG_KEY_1, S3_TAG_VALUE_1);
    createS3BucketAndTags(tags);
    // 5 new resources should be discovered. Mapping to 2 new compute description and 5 new
    // compute states.
    // Even though the "t2.micro" is common to the VM provisioned from Xenon
    // service and the one directly provisioned on EC2, there is no Compute description
    // linking of discovered resources to user defined compute descriptions. So a new system
    // generated compute description will be created for "t2.micro"
    queryDocumentsAndAssertExpectedCount(this.host, count4, ComputeDescriptionService.FACTORY_LINK, false);
    queryDocumentsAndAssertExpectedCount(this.host, count7, ComputeService.FACTORY_LINK, false);
    queryDocumentsAndAssertExpectedCount(this.host, count7, DiskService.FACTORY_LINK, false);
    // Validate at least 4 availability zones were enumerated
    ProvisioningUtils.queryComputeInstancesByType(this.host, count4, ComputeType.ZONE.toString(), false);
    // Update Scenario : Check that the tag information is present for the VM tagged above.
    String vpCId = validateTagAndNetworkAndComputeDescriptionInformation(vmState);
    validateVPCInformation(vpCId);
    // Count should be 1 NICs per discovered VM.
    int totalNetworkInterfaceStateCount = count6 * this.singleNicSpec.numberOfNics();
    validateNetworkInterfaceCount(totalNetworkInterfaceStateCount);
    // One VPC should be discovered in the test.
    queryDocumentsAndAssertExpectedCount(this.host, count1, NetworkService.FACTORY_LINK, false);
    // Verify that the SecurityGroups of the newly created VM has been enumerated and exists
    // locally
    validateSecurityGroupsInformation(vmState.groupLinks);
    // Verify stop flow
    // The first instance of instanceIdsToDeleteFirstTime will be stopped.
    String instanceIdsToStop = instanceIdsToDeleteFirstTime.get(0);
    tagResourcesWithName(this.client, VM_STOPPED_NAME, instanceIdsToStop);
    // Stop one instance
    stopVMsUsingEC2Client(this.client, this.host, new ArrayList<>(Arrays.asList(instanceIdsToStop)));
    // Create stale resources, that later should be deleted by the enumeration
    String staleSubnetDocumentSelfLink = markFirstResourceStateAsStale(host, SubnetState.class, SubnetService.FACTORY_LINK);
    String staleNetworkDocumentSelfLink = markFirstResourceStateAsStale(host, NetworkState.class, NetworkService.FACTORY_LINK);
    // During the enumeration, if one instance is stopped, its public ip address
    // will disappear, then the corresponding link of local ComputeState's public
    // network interface and its document will be removed.
    enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_STOP_VM);
    // Validate that test VM still has same number of diskLinks.
    csForDiskLinkValidation = getComputeStateFromId(this.instancesToCleanUp.get(0));
    assertEquals(csForDiskLinkValidation.diskLinks.size(), this.initialEbsDiskLinkCount);
    // Attach volume to test VM.
    AttachVolumeRequest attachVolumeRequest = new AttachVolumeRequest().withVolumeId(createVolumeResult.getVolume().getVolumeId()).withInstanceId(csForDiskLinkValidation.id).withDevice("/dev/sdh");
    this.client.attachVolume(attachVolumeRequest);
    // Validate stale resources have been deleted
    validateStaleResourceStateDeletion(staleSubnetDocumentSelfLink, staleNetworkDocumentSelfLink);
    // After two enumeration cycles, validate that we did not create duplicate documents for existing
    // S3 bucket and validate that we did not add duplicate tagLink in diskState and removed the tagLink
    // for tag deleted from AWS.
    validateS3Enumeration(count1, count2);
    // Remove region from S3 bucket DiskState.
    removeS3BucketRegionFromDiskState();
    // Validate that deleted S3 tag's local state is deleted.
    validateS3TagStatesCreated();
    enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_ADDITIONAL_VM);
    // Check that newly attached volume got enumerated and the instance now has 1 more diskLink than before.
    csForDiskLinkValidation = getComputeStateFromId(this.instancesToCleanUp.get(0));
    assertEquals(csForDiskLinkValidation.diskLinks.size(), this.initialEbsDiskLinkCount + 1);
    // Validate that diskState of S3 bucket with null region got deleted
    validateBucketStateDeletionForNullRegion();
    // Delete the S3 bucket created in the test
    this.s3Client.deleteBucket(TEST_BUCKET_NAME);
    this.bucketToBeDeleted = null;
    // Because one public NIC and its document are removed,
    // the totalNetworkInterfaceStateCount should go down by 1
    validateRemovalOfPublicNetworkInterface(instanceIdsToStop, totalNetworkInterfaceStateCount - 1);
    // Provision an additional VM with a different instance type. It should re-use the
    // existing compute description created by the enumeration task above.
    List<String> instanceIdsToDeleteSecondTime = provisionAWSVMWithEC2Client(this.client, this.host, count1, TestAWSSetupUtils.instanceType, this.subnetId, this.securityGroupId);
    this.instancesToCleanUp.addAll(instanceIdsToDeleteSecondTime);
    waitForProvisioningToComplete(instanceIdsToDeleteSecondTime, this.host, this.client, ZERO);
    enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_ADDITIONAL_VM);
    // Validate that we do not add duplicate diskLinks after multiple enumerations.
    csForDiskLinkValidation = getComputeStateFromId(this.instancesToCleanUp.get(0));
    assertEquals(csForDiskLinkValidation.diskLinks.size(), this.initialEbsDiskLinkCount + 1);
    // Detach and delete test EBS volume.
    DetachVolumeRequest detachVolumeRequest = new DetachVolumeRequest().withVolumeId(this.testEbsId);
    this.client.detachVolume(detachVolumeRequest);
    // One additional compute state and no additional compute description should be
    // created. 1) compute host CD 2) t2.nano-system generated 3) t2.micro-system generated
    // 4) t2.micro-created from test code.
    queryDocumentsAndAssertExpectedCount(this.host, count4, ComputeDescriptionService.FACTORY_LINK, false);
    ServiceDocumentQueryResult computesResult1 = queryDocumentsAndAssertExpectedCount(this.host, count8, ComputeService.FACTORY_LINK, false);
    // validate the internal tag tor type=ec2_instance is set
    // query for the existing internal tag state for type=ec2_instance.
    // There should be only one internal tag.
    validateTagInEntity(computesResult1, ComputeState.class, ec2_instance.toString());
    ServiceDocumentQueryResult networkInterfaceResult = queryDocumentsAndAssertExpectedCount(this.host, totalNetworkInterfaceStateCount - 1, NetworkInterfaceService.FACTORY_LINK, false);
    validateTagInEntity(networkInterfaceResult, NetworkInterfaceState.class, ec2_net_interface.toString());
    ServiceDocumentQueryResult networkStateResult = queryDocumentsAndAssertExpectedCount(this.host, count1, NetworkService.FACTORY_LINK, false);
    validateTagInEntity(networkStateResult, NetworkState.class, ec2_vpc.toString());
    ServiceDocumentQueryResult subnetStateResult = queryDocumentsAndAssertExpectedCount(this.host, count1, SubnetService.FACTORY_LINK, false);
    // TODO Remove. This is to help debug an intermittent test failure.
    host.log(Level.INFO, "The subnet result state that I am working with is " + Utils.toJsonHtml(subnetStateResult));
    validateTagInEntity(subnetStateResult, SubnetState.class, ec2_subnet.toString());
    queryDocumentsAndAssertExpectedCount(this.host, count8, DiskService.FACTORY_LINK, false);
    // Verify Deletion flow
    // Delete 5 VMs spawned above of type T2_NANO
    deleteVMsUsingEC2Client(this.client, this.host, instanceIdsToDeleteFirstTime);
    enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_DELETE_VMS);
    // Counts should go down. 5 compute states and 5 disk states.
    ServiceDocumentQueryResult computesResult2 = queryDocumentsAndAssertExpectedCount(this.host, count3, ComputeService.FACTORY_LINK, false);
    queryDocumentsAndAssertExpectedCount(this.host, count3, DiskService.FACTORY_LINK, false);
    // Validate that detached test EBS is removed from diskLinks of test instance.
    csForDiskLinkValidation = getComputeStateFromId(this.instancesToCleanUp.get(0));
    assertEquals(csForDiskLinkValidation.diskLinks.size(), this.initialEbsDiskLinkCount);
    // validate the internal tag tor type=ec2_instance is set
    // query for the existing internal tag state for type=ec2_instance.
    // There should be only one internal tag.
    validateTagInEntity(computesResult2, ComputeState.class, ec2_instance.toString());
    // Delete 1 VMs spawned above of type T2_Micro
    deleteVMsUsingEC2Client(this.client, this.host, instanceIdsToDeleteSecondTime);
    enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_DELETE_VM);
    // Compute state and disk state count should go down by 1
    ServiceDocumentQueryResult computesResult3 = queryDocumentsAndAssertExpectedCount(this.host, count2, ComputeService.FACTORY_LINK, false);
    queryDocumentsAndAssertExpectedCount(this.host, count2, DiskService.FACTORY_LINK, false);
    // validate the internal tag tor type=ec2_instance is set
    // query for the existing internal tag state for type=ec2_instance.
    // There should be only one internal tag.
    validateTagInEntity(computesResult3, ComputeState.class, ec2_instance.toString());
    // Validate that the document for the deleted S3 bucket is deleted after enumeration.
    validateS3Enumeration(ZERO, ZERO);
    // Delete test EBS volume.
    DeleteVolumeRequest deleteVolumeRequest = new DeleteVolumeRequest().withVolumeId(this.testEbsId);
    this.client.deleteVolume(deleteVolumeRequest);
    this.testEbsId = null;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) HashMap(java.util.HashMap) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) TestAWSSetupUtils.deleteVMsOnThisEndpoint(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsOnThisEndpoint) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) DetachVolumeRequest(com.amazonaws.services.ec2.model.DetachVolumeRequest) Test(org.junit.Test)

Example 9 with InstanceType

use of com.amazonaws.services.ec2.model.InstanceType in project camel by apache.

the class EC2Producer method createAndRunInstance.

private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) {
    String ami;
    InstanceType instanceType;
    int minCount;
    int maxCount;
    boolean monitoring;
    String kernelId;
    boolean ebsOptimized;
    Collection securityGroups;
    String keyName;
    String clientToken;
    Placement placement;
    RunInstancesRequest request = new RunInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) {
        ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class);
        request.withImageId(ami);
    } else {
        throw new IllegalArgumentException("AMI must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) {
        instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class);
        request.withInstanceType(instanceType.toString());
    } else {
        throw new IllegalArgumentException("Instance Type must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) {
        minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class);
        request.withMinCount(minCount);
    } else {
        throw new IllegalArgumentException("Min instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) {
        maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class);
        request.withMaxCount(maxCount);
    } else {
        throw new IllegalArgumentException("Max instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) {
        monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class);
        request.withMonitoring(monitoring);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) {
        kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class);
        request.withKernelId(kernelId);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) {
        ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class);
        request.withEbsOptimized(ebsOptimized);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) {
        securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class);
        request.withSecurityGroups(securityGroups);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) {
        keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class);
        request.withKeyName(keyName);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) {
        clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class);
        request.withClientToken(clientToken);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) {
        placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class);
        request.withPlacement(placement);
    }
    RunInstancesResult result;
    try {
        result = ec2Client.runInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString());
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) Endpoint(org.apache.camel.Endpoint) Placement(com.amazonaws.services.ec2.model.Placement) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) InstanceType(com.amazonaws.services.ec2.model.InstanceType)

Example 10 with InstanceType

use of com.amazonaws.services.ec2.model.InstanceType in project photon-model by vmware.

the class TestAWSEnumerationTask method testEnumerationPreserveLocalStates.

@Test
public void testEnumerationPreserveLocalStates() throws Throwable {
    this.host.log("Running test: " + this.currentTestName.getMethodName());
    ComputeState vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, TestAWSSetupUtils.class, zoneId, regionId, null, this.singleNicSpec, this.awsTestContext);
    if (this.isMock) {
        // Just make a call to the enumeration service and make sure that the adapter patches
        // the parent with completion.
        enumerateResourcesPreserveMissing(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_MOCK_MODE);
        return;
    }
    // Overriding the page size to test the pagination logic with limited instances on AWS.
    // This is a functional test
    // so the latency numbers maybe higher from this test due to low page size.
    setQueryPageSize(DEFAULT_TEST_PAGE_SIZE);
    setQueryResultLimit(DEFAULT_TEST_PAGE_SIZE);
    // Provision a single VM . Check initial state.
    vmState = provisionMachine(this.host, vmState, this.isMock, this.instancesToCleanUp);
    queryComputeInstances(this.host, count2);
    queryDocumentsAndAssertExpectedCount(this.host, count2, ComputeDescriptionService.FACTORY_LINK, false);
    // CREATION directly on AWS
    List<String> instanceIdsToDelete = provisionAWSVMWithEC2Client(this.client, this.host, count4, T2_MICRO_INSTANCE_TYPE, this.subnetId, this.securityGroupId);
    List<String> instanceIds = provisionAWSVMWithEC2Client(this.client, this.host, count1, instanceType, this.subnetId, this.securityGroupId);
    instanceIdsToDelete.addAll(instanceIds);
    this.instancesToCleanUp.addAll(instanceIdsToDelete);
    waitForProvisioningToComplete(instanceIdsToDelete, this.host, this.client, ZERO);
    // Xenon does not know about the new instances.
    ProvisioningUtils.queryComputeInstances(this.host, count2);
    enumerateResourcesPreserveMissing(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
    // 5 new resources should be discovered. Mapping to 2 new compute description and 5 new
    // compute states.
    // Even though the "t2.micro" is common to the VM provisioned from Xenon
    // service and the one directly provisioned on EC2, there is no Compute description
    // linking of discovered resources to user defined compute descriptions. So a new system
    // generated compute description will be created for "t2.micro"
    queryDocumentsAndAssertExpectedCount(this.host, count4, ComputeDescriptionService.FACTORY_LINK, false);
    queryDocumentsAndAssertExpectedCount(this.host, count7, ComputeService.FACTORY_LINK, false);
    // Verify Deletion flow
    // Delete 5 VMs spawned above of type T2_NANO
    deleteVMsUsingEC2Client(this.client, this.host, instanceIdsToDelete);
    enumerateResourcesPreserveMissing(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_DELETE_VMS);
    // Counts should go down 5 compute states.
    ServiceDocumentQueryResult queryResult = queryDocumentsAndAssertExpectedCount(this.host, count7, ComputeService.FACTORY_LINK, false);
    List<ComputeState> localInstances = queryResult.documents.values().stream().map(d -> Utils.fromJson(d, ComputeState.class)).filter(c -> instanceIdsToDelete.contains(c.id)).collect(Collectors.toList());
    assertEquals(instanceIdsToDelete.size(), localInstances.size());
    for (ComputeState c : localInstances) {
        assertEquals(LifecycleState.RETIRED, c.lifecycleState);
    }
}
Also used : Arrays(java.util.Arrays) DetachVolumeRequest(com.amazonaws.services.ec2.model.DetachVolumeRequest) AWSResourceType.ec2_vpc(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType.ec2_vpc) TestAWSSetupUtils.createAWSComputeHost(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) VerificationHost(com.vmware.xenon.common.test.VerificationHost) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) TestAWSSetupUtils.createAWSEndpointState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSEndpointState) TestAWSSetupUtils.createAWSResourcePool(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool) TestAWSSetupUtils.provisionAWSVMWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.provisionAWSVMWithEC2Client) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) TagSet(com.amazonaws.services.s3.model.TagSet) TestAWSSetupUtils.provisionAWSLoadBalancerWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.provisionAWSLoadBalancerWithEC2Client) Utils(com.vmware.xenon.common.Utils) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) LoadBalancerState(com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Duration(java.time.Duration) Map(java.util.Map) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) ProvisioningUtils.queryDocumentsAndAssertExpectedCount(com.vmware.photon.controller.model.tasks.ProvisioningUtils.queryDocumentsAndAssertExpectedCount) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) TestAWSSetupUtils.enumerateResources(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.enumerateResources) TestAWSSetupUtils.tearDownTestDisk(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestDisk) TestAWSSetupUtils.waitForInstancesToBeTerminated(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.waitForInstancesToBeTerminated) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Set(java.util.Set) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) TagService(com.vmware.photon.controller.model.resources.TagService) Assert.assertFalse(org.junit.Assert.assertFalse) Tag(com.amazonaws.services.ec2.model.Tag) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TestAWSSetupUtils.enumerateResourcesPreserveMissing(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.enumerateResourcesPreserveMissing) TestAWSSetupUtils.getComputeByAWSId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getComputeByAWSId) TestUtils.doPatch(com.vmware.photon.controller.model.tasks.TestUtils.doPatch) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) StringUtil(io.netty.util.internal.StringUtil) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) TestAWSSetupUtils.getNICByAWSId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getNICByAWSId) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) AWSUtils.unTagResources(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.unTagResources) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) TestName(org.junit.rules.TestName) TestAWSSetupUtils.getLoadBalancerByAWSId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getLoadBalancerByAWSId) Before(org.junit.Before) TestAWSSetupUtils.stopVMsUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.stopVMsUsingEC2Client) TestAWSSetupUtils.createAWSAuthentication(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) TestAWSSetupUtils.createNICDirectlyWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createNICDirectlyWithEC2Client) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) SecurityGroupService(com.vmware.photon.controller.model.resources.SecurityGroupService) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AWS_VPC_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID) EC2_WINDOWS_AMI(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.EC2_WINDOWS_AMI) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) TestContext(com.vmware.xenon.common.test.TestContext) AWSUtils.tagResourcesWithName(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.tagResourcesWithName) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ProvisioningUtils.queryComputeInstances(com.vmware.photon.controller.model.tasks.ProvisioningUtils.queryComputeInstances) TestAWSSetupUtils.setAwsClientMockInfo(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setAwsClientMockInfo) RegionEnumerationResponse(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) VolumeType(com.amazonaws.services.ec2.model.VolumeType) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) EC2_LINUX_AMI(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.EC2_LINUX_AMI) TestAWSSetupUtils.deleteVMsUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsUsingEC2Client) BucketTaggingConfiguration(com.amazonaws.services.s3.model.BucketTaggingConfiguration) AWSResourceType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType) QueryTask(com.vmware.xenon.services.common.QueryTask) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) After(org.junit.After) AWSResourceType.ec2_subnet(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType.ec2_subnet) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) TestAWSSetupUtils.getInternalTagsByType(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getInternalTagsByType) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) TestAWSSetupUtils.provisionAWSEBSVMWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.provisionAWSEBSVMWithEC2Client) AwsNicSpecs(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) TestAWSSetupUtils.waitForProvisioningToComplete(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.waitForProvisioningToComplete) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) CompletionException(java.util.concurrent.CompletionException) TestAWSSetupUtils.deleteLBsUsingLBClient(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteLBsUsingLBClient) UUID(java.util.UUID) TestUtils.getSubnetStates(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getSubnetStates) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) AWS_GATEWAY_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_GATEWAY_ID) TestAWSSetupUtils.deleteNICDirectlyWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteNICDirectlyWithEC2Client) AmazonElasticLoadBalancingAsyncClient(com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingAsyncClient) List(java.util.List) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) TestAWSSetupUtils.setUpTestVolume(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVolume) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) DiskService(com.vmware.photon.controller.model.resources.DiskService) TestAWSSetupUtils.addNICDirectlyWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.addNICDirectlyWithEC2Client) TestAWSSetupUtils.deleteVMsOnThisEndpoint(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsOnThisEndpoint) AWSResourceType.ec2_instance(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType.ec2_instance) BasicTestCase(com.vmware.xenon.common.BasicTestCase) RouteConfiguration(com.vmware.photon.controller.model.resources.LoadBalancerDescriptionService.LoadBalancerDescription.RouteConfiguration) HashMap(java.util.HashMap) AWSUtils.tagResources(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.tagResources) TestAWSSetupUtils.zoneId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.zoneId) TestAWSSetupUtils.detachNICDirectlyWithEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.detachNICDirectlyWithEC2Client) AWSConstants.setQueryPageSize(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.setQueryPageSize) Level(java.util.logging.Level) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) AWSResourceType.ec2_net_interface(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType.ec2_net_interface) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AWS_VPC_ROUTE_TABLE_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ROUTE_TABLE_ID) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) ENABLE_LOAD_BALANCER_PROPERTY(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSLoadBalancerEnumerationAdapterService.ENABLE_LOAD_BALANCER_PROPERTY) Assert.assertNotNull(org.junit.Assert.assertNotNull) Operation(com.vmware.xenon.common.Operation) AWSConstants.setQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.setQueryResultLimit) ProvisioningUtils.queryAllFactoryResources(com.vmware.photon.controller.model.tasks.ProvisioningUtils.queryAllFactoryResources) TestAWSSetupUtils.instanceType(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.instanceType) TestAWSSetupUtils.createAWSVMResource(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource) Rule(org.junit.Rule) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) TestAWSSetupUtils.regionId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.regionId) TestAWSSetupUtils.provisionMachine(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.provisionMachine) Collections(java.util.Collections) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) Test(org.junit.Test)

Aggregations

RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)5 ArrayList (java.util.ArrayList)5 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)4 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)4 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 BlockDeviceMapping (com.amazonaws.services.ec2.model.BlockDeviceMapping)3 EbsBlockDevice (com.amazonaws.services.ec2.model.EbsBlockDevice)3 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)3 Tag (com.amazonaws.services.ec2.model.Tag)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)2 AttachVolumeRequest (com.amazonaws.services.ec2.model.AttachVolumeRequest)2 CreateVolumeRequest (com.amazonaws.services.ec2.model.CreateVolumeRequest)2 CreateVolumeResult (com.amazonaws.services.ec2.model.CreateVolumeResult)2 DeleteVolumeRequest (com.amazonaws.services.ec2.model.DeleteVolumeRequest)2 DetachVolumeRequest (com.amazonaws.services.ec2.model.DetachVolumeRequest)2 Instance (com.amazonaws.services.ec2.model.Instance)2 InstanceType (com.amazonaws.services.ec2.model.InstanceType)2