Search in sources :

Example 51 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.

the class AWSComputeDiskDay2Service method getAvailableDeviceName.

private String getAvailableDeviceName(DiskContext context, String instanceId) {
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceId);
    DescribeInstancesResult instancesResult = context.amazonEC2Client.describeInstances(describeInstancesRequest);
    List<InstanceBlockDeviceMapping> blockDeviceMappings = null;
    AWSSupportedOS platform = null;
    AWSSupportedVirtualizationTypes virtualizationTypes = null;
    String instanceType = null;
    for (Reservation reservation : instancesResult.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (instance.getInstanceId().equals(instanceId)) {
                blockDeviceMappings = instance.getBlockDeviceMappings();
                platform = AWSSupportedOS.get(instance.getPlatform());
                virtualizationTypes = AWSSupportedVirtualizationTypes.get(instance.getVirtualizationType());
                instanceType = instance.getInstanceType();
                break;
            }
        }
    }
    String deviceName = null;
    if (blockDeviceMappings != null) {
        List<String> usedDeviceNames = getUsedDeviceNames(blockDeviceMappings);
        List<String> availableDiskNames = AWSBlockDeviceNameMapper.getAvailableNames(platform, virtualizationTypes, AWSStorageType.EBS, instanceType, usedDeviceNames);
        deviceName = availableDiskNames.get(0);
    }
    return deviceName;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS)

Example 52 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.

the class AWSComputeDiskDay2Service method startInstance.

/**
 * start the instance and on success updates the disk and compute state to reflect the detach information.
 */
private void startInstance(AmazonEC2AsyncClient client, DiskContext c, DeferredResult<DiskContext> dr, OperationContext opCtx) {
    StartInstancesRequest startRequest = new StartInstancesRequest();
    startRequest.withInstanceIds(c.baseAdapterContext.child.id);
    client.startInstancesAsync(startRequest, new AWSAsyncHandler<StartInstancesRequest, StartInstancesResult>() {

        @Override
        protected void handleError(Exception e) {
            service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start the instance %s. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
            OperationContext.restoreOperationContext(opCtx);
            c.error = e;
            dr.complete(c);
        }

        @Override
        protected void handleSuccess(StartInstancesRequest request, StartInstancesResult result) {
            AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
                if (e != null) {
                    service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Instance %s failed to reach " + "running state. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
                    OperationContext.restoreOperationContext(opCtx);
                    c.error = e;
                    dr.complete(c);
                    return;
                }
                logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully started the " + "instance %s", result.getStartingInstances().get(0).getInstanceId()));
                updateComputeAndDiskState(dr, c, opCtx);
            });
        }
    });
}
Also used : Service(com.vmware.xenon.common.Service) Arrays(java.util.Arrays) DetachVolumeRequest(com.amazonaws.services.ec2.model.DetachVolumeRequest) Pair(com.vmware.photon.controller.model.adapters.util.Pair) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) ServiceDocument(com.vmware.xenon.common.ServiceDocument) DetachVolumeResult(com.amazonaws.services.ec2.model.DetachVolumeResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) AWS_DISK_OPERATION_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_DISK_OPERATION_TIMEOUT_MINUTES) DEVICE_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_NAME) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) Utils(com.vmware.xenon.common.Utils) AttachVolumeResult(com.amazonaws.services.ec2.model.AttachVolumeResult) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AWSBlockDeviceNameMapper(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSBlockDeviceNameMapper) Map(java.util.Map) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) Reservation(com.amazonaws.services.ec2.model.Reservation) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) AWS_INSTANCE_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_INSTANCE_ID_PREFIX) List(java.util.List) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) Optional(java.util.Optional) FactoryService(com.vmware.xenon.common.FactoryService) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationContext(com.vmware.xenon.common.OperationContext) HashMap(java.util.HashMap) AWS_VOLUME_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VOLUME_ID_PREFIX) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Volume(com.amazonaws.services.ec2.model.Volume) Instance(com.amazonaws.services.ec2.model.Instance) AWSStorageType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSStorageType) Operation(com.vmware.xenon.common.Operation) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest)

Example 53 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.

the class AWSComputeDiskDay2Service method performDetachOperation.

private DeferredResult<DiskContext> performDetachOperation(DiskContext context) {
    DeferredResult<DiskContext> dr = new DeferredResult<>();
    try {
        validateDetachInfo(context.diskState);
        if (context.request.isMockRequest) {
            updateComputeAndDiskState(dr, context, null);
            return dr;
        }
        String instanceId = context.computeState.id;
        if (instanceId == null || !instanceId.startsWith(AWS_INSTANCE_ID_PREFIX)) {
            return logAndGetFailedDr(context, "compute id cannot be empty");
        }
        String diskId = context.diskState.id;
        if (diskId == null || !diskId.startsWith(AWS_VOLUME_ID_PREFIX)) {
            return logAndGetFailedDr(context, "disk id cannot be empty");
        }
        // stop the instance, detach the disk and then start the instance.
        if (context.baseAdapterContext.child.powerState.equals(ComputeService.PowerState.ON)) {
            StopInstancesRequest stopRequest = new StopInstancesRequest();
            stopRequest.withInstanceIds(context.baseAdapterContext.child.id);
            context.amazonEC2Client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {

                @Override
                protected void handleError(Exception e) {
                    service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start compute. %s", Utils.toString(e)));
                    OperationContext.restoreOperationContext(this.opContext);
                    context.error = e;
                    dr.complete(context);
                }

                @Override
                protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
                    OperationContext.restoreOperationContext(this.opContext);
                    AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", context.amazonEC2Client, (is, e) -> {
                        if (e != null) {
                            service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to stop " + "the compute. %s", Utils.toString(e)));
                            context.error = e;
                            dr.complete(context);
                            return;
                        }
                        logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully stopped " + "the instance %s", instanceId));
                        // detach disk from the instance.
                        detachVolume(context, dr, instanceId, diskId, true);
                    });
                }
            });
        } else {
            detachVolume(context, dr, instanceId, diskId, false);
        }
    } catch (Exception e) {
        context.error = e;
        return DeferredResult.completed(context);
    }
    return dr;
}
Also used : Service(com.vmware.xenon.common.Service) Arrays(java.util.Arrays) DetachVolumeRequest(com.amazonaws.services.ec2.model.DetachVolumeRequest) Pair(com.vmware.photon.controller.model.adapters.util.Pair) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) ServiceDocument(com.vmware.xenon.common.ServiceDocument) DetachVolumeResult(com.amazonaws.services.ec2.model.DetachVolumeResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) AWS_DISK_OPERATION_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_DISK_OPERATION_TIMEOUT_MINUTES) DEVICE_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_NAME) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) Utils(com.vmware.xenon.common.Utils) AttachVolumeResult(com.amazonaws.services.ec2.model.AttachVolumeResult) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AWSBlockDeviceNameMapper(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSBlockDeviceNameMapper) Map(java.util.Map) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) Reservation(com.amazonaws.services.ec2.model.Reservation) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) AWS_INSTANCE_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_INSTANCE_ID_PREFIX) List(java.util.List) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) Optional(java.util.Optional) FactoryService(com.vmware.xenon.common.FactoryService) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationContext(com.vmware.xenon.common.OperationContext) HashMap(java.util.HashMap) AWS_VOLUME_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VOLUME_ID_PREFIX) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Volume(com.amazonaws.services.ec2.model.Volume) Instance(com.amazonaws.services.ec2.model.Instance) AWSStorageType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSStorageType) Operation(com.vmware.xenon.common.Operation) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 54 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.

the class AWSComputeDiskDay2ServiceTest method assertAndSetVMSecurityGroupsToBeDeleted.

private void assertAndSetVMSecurityGroupsToBeDeleted(Instance instance, ComputeState vm) {
    // This assert is only suitable for real (non-mocking env).
    if (this.isMock) {
        return;
    }
    this.host.log(Level.INFO, "%s: Assert security groups configuration for [%s] VM", this.currentTestName.getMethodName(), this.vmState.name);
    // Get the SecurityGroupStates that were provided in the request ComputeState
    Collector<SecurityGroupService.SecurityGroupState, ?, Map<String, SecurityGroupService.SecurityGroupState>> convertToMap = Collectors.<SecurityGroupService.SecurityGroupState, String, SecurityGroupService.SecurityGroupState>toMap(sg -> sg.name, sg -> sg);
    Map<String, SecurityGroupService.SecurityGroupState> currentSGNamesToStates = vm.networkInterfaceLinks.stream().map(nicLink -> this.host.getServiceState(null, NetworkInterfaceService.NetworkInterfaceState.class, UriUtils.buildUri(this.host, nicLink))).<// collect all SecurityGroup States from all NIC states
    SecurityGroupService.SecurityGroupState>flatMap(nicState -> nicState.securityGroupLinks.stream().map(sgLink -> {
        SecurityGroupService.SecurityGroupState sgState = this.host.getServiceState(null, SecurityGroupService.SecurityGroupState.class, UriUtils.buildUri(this.host, sgLink));
        return sgState;
    })).collect(convertToMap);
    // Compare ComputeState after provisioning to the ComputeState in the request
    assertNotNull("Instance should have security groups attached.", instance.getSecurityGroups());
    // Provisioned Instance should have the same number of SecurityGroups as requested
    assertEquals(instance.getSecurityGroups().size(), currentSGNamesToStates.size());
    for (SecurityGroupService.SecurityGroupState currentSGState : currentSGNamesToStates.values()) {
        // Get corresponding requested state
        GroupIdentifier provisionedGroupIdentifier = null;
        for (GroupIdentifier awsGroupIdentifier : instance.getSecurityGroups()) {
            if (awsGroupIdentifier.getGroupId().equals(currentSGState.id)) {
                provisionedGroupIdentifier = awsGroupIdentifier;
                break;
            }
        }
        // Ensure that the requested SecurityGroup was actually provisioned
        assertNotNull(provisionedGroupIdentifier);
        if (currentSGState.name.contains(TestAWSSetupUtils.AWS_NEW_GROUP_PREFIX)) {
            this.sgToCleanUp = currentSGState.id;
            SecurityGroup awsSecurityGroup = getSecurityGroupsIdUsingEC2Client(this.client, provisionedGroupIdentifier.getGroupId());
            assertNotNull(awsSecurityGroup);
            // Validate rules are correctly created as requested
            IpPermission awsIngressRule = awsSecurityGroup.getIpPermissions().get(0);
            IpPermission awsEgressRule = awsSecurityGroup.getIpPermissionsEgress().get(1);
            assertNotNull(awsIngressRule);
            assertNotNull(awsEgressRule);
            assertEquals("Error in created ingress rule", awsIngressRule.getIpProtocol(), currentSGState.ingress.get(0).protocol);
            assertEquals("Error in created ingress rule", awsIngressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.ingress.get(0).ipRangeCidr);
            assertEquals("Error in created egress rule", awsEgressRule.getIpProtocol(), currentSGState.egress.get(0).protocol);
            assertEquals("Error in created egress rule", awsEgressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.egress.get(0).ipRangeCidr);
        }
    }
}
Also used : Arrays(java.util.Arrays) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) TestAWSSetupUtils.createAWSComputeHost(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost) ServiceMetadata(com.vmware.photon.controller.model.util.StartServicesHelper.ServiceMetadata) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TestAWSSetupUtils.createAWSResourcePool(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) DEVICE_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_NAME) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) StartServicesHelper(com.vmware.photon.controller.model.util.StartServicesHelper) Gson(com.google.gson.Gson) AWSBlockDeviceNameMapper(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSBlockDeviceNameMapper) TestAWSSetupUtils.verifyRemovalOfResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.verifyRemovalOfResourceState) Duration(java.time.Duration) Map(java.util.Map) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) After(org.junit.After) ResourcePoolService(com.vmware.photon.controller.model.resources.ResourcePoolService) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) ProvisionDiskTaskService(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) Collector(java.util.stream.Collector) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) AWS_VM_REQUEST_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AWS_VM_REQUEST_TIMEOUT_MINUTES) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) CompletionException(java.util.concurrent.CompletionException) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Collectors(java.util.stream.Collectors) TestAWSSetupUtils.getResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getResourceState) TestAWSSetupUtils.avalabilityZoneIdentifier(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.avalabilityZoneIdentifier) TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client) List(java.util.List) TestAWSSetupUtils.getAwsDisksByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsDisksByIds) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) FactoryService(com.vmware.xenon.common.FactoryService) DiskService(com.vmware.photon.controller.model.resources.DiskService) TaskService(com.vmware.xenon.services.common.TaskService) ServiceMetadata.factoryService(com.vmware.photon.controller.model.util.StartServicesHelper.ServiceMetadata.factoryService) TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) TestAWSSetupUtils.getAwsInstancesByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsInstancesByIds) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) UriPaths(com.vmware.photon.controller.model.UriPaths) TestProvisionAWSDisk.createAWSDiskState(com.vmware.photon.controller.model.adapters.awsadapter.TestProvisionAWSDisk.createAWSDiskState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestName(org.junit.rules.TestName) Volume(com.amazonaws.services.ec2.model.Volume) PLACEMENT_LINK(com.vmware.photon.controller.model.ComputeProperties.PLACEMENT_LINK) DEVICE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_TYPE) Instance(com.amazonaws.services.ec2.model.Instance) Before(org.junit.Before) TestAWSSetupUtils.createAWSAuthentication(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) AWSStorageType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSStorageType) Operation(com.vmware.xenon.common.Operation) SecurityGroupService(com.vmware.photon.controller.model.resources.SecurityGroupService) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) TimeUnit(java.util.concurrent.TimeUnit) TestAWSSetupUtils.createAWSVMResource(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource) Rule(org.junit.Rule) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) TestAWSSetupUtils.regionId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.regionId) TestAWSSetupUtils.setAwsClientMockInfo(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setAwsClientMockInfo) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) IpPermission(com.amazonaws.services.ec2.model.IpPermission) SecurityGroupService(com.vmware.photon.controller.model.resources.SecurityGroupService) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Map(java.util.Map) HashMap(java.util.HashMap) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Example 55 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project photon-model by vmware.

the class AWSRebootServiceTest method assertAndSetVMSecurityGroupsToBeDeleted.

private void assertAndSetVMSecurityGroupsToBeDeleted(Instance instance, ComputeState vm) {
    // This assert is only suitable for real (non-mocking env).
    if (this.isMock) {
        return;
    }
    this.host.log(Level.INFO, "%s: Assert security groups configuration for [%s] VM", this.currentTestName.getMethodName(), this.vmState.name);
    // Get the SecurityGroupStates that were provided in the request ComputeState
    Collector<SecurityGroupState, ?, Map<String, SecurityGroupState>> convertToMap = Collectors.<SecurityGroupState, String, SecurityGroupState>toMap(sg -> sg.name, sg -> sg);
    Map<String, SecurityGroupState> currentSGNamesToStates = vm.networkInterfaceLinks.stream().map(nicLink -> this.host.getServiceState(null, NetworkInterfaceState.class, UriUtils.buildUri(this.host, nicLink))).<// collect all SecurityGroup States from all NIC states
    SecurityGroupState>flatMap(nicState -> nicState.securityGroupLinks.stream().map(sgLink -> {
        SecurityGroupState sgState = this.host.getServiceState(null, SecurityGroupState.class, UriUtils.buildUri(this.host, sgLink));
        return sgState;
    })).collect(convertToMap);
    // Compare ComputeState after provisioning to the ComputeState in the request
    assertNotNull("Instance should have security groups attached.", instance.getSecurityGroups());
    // Provisioned Instance should have the same number of SecurityGroups as requested
    assertEquals(instance.getSecurityGroups().size(), currentSGNamesToStates.size());
    for (SecurityGroupState currentSGState : currentSGNamesToStates.values()) {
        // Get corresponding requested state
        GroupIdentifier provisionedGroupIdentifier = null;
        for (GroupIdentifier awsGroupIdentifier : instance.getSecurityGroups()) {
            if (awsGroupIdentifier.getGroupId().equals(currentSGState.id)) {
                provisionedGroupIdentifier = awsGroupIdentifier;
                break;
            }
        }
        // Ensure that the requested SecurityGroup was actually provisioned
        assertNotNull(provisionedGroupIdentifier);
        if (currentSGState.name.contains(TestAWSSetupUtils.AWS_NEW_GROUP_PREFIX)) {
            this.sgToCleanUp = currentSGState.id;
            SecurityGroup awsSecurityGroup = getSecurityGroupsIdUsingEC2Client(this.client, provisionedGroupIdentifier.getGroupId());
            assertNotNull(awsSecurityGroup);
            // Validate rules are correctly created as requested
            IpPermission awsIngressRule = awsSecurityGroup.getIpPermissions().get(0);
            IpPermission awsEgressRule = awsSecurityGroup.getIpPermissionsEgress().get(1);
            assertNotNull(awsIngressRule);
            assertNotNull(awsEgressRule);
            assertEquals("Error in created ingress rule", awsIngressRule.getIpProtocol(), currentSGState.ingress.get(0).protocol);
            assertEquals("Error in created ingress rule", awsIngressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.ingress.get(0).ipRangeCidr);
            assertEquals("Error in created egress rule", awsEgressRule.getIpProtocol(), currentSGState.egress.get(0).protocol);
            assertEquals("Error in created egress rule", awsEgressRule.getIpv4Ranges().get(0).getCidrIp(), currentSGState.egress.get(0).ipRangeCidr);
        }
    }
}
Also used : Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) TestAWSSetupUtils.createAWSComputeHost(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TestAWSSetupUtils.createAWSResourcePool(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) TestAWSSetupUtils.verifyRemovalOfResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.verifyRemovalOfResourceState) Map(java.util.Map) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) After(org.junit.After) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) Collector(java.util.stream.Collector) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) AwsNicSpecs(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) AWS_VM_REQUEST_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AWS_VM_REQUEST_TIMEOUT_MINUTES) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) UUID(java.util.UUID) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Collectors(java.util.stream.Collectors) TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client) List(java.util.List) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TaskState(com.vmware.xenon.common.TaskState) TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) TestAWSSetupUtils.zoneId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.zoneId) Function(java.util.function.Function) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) TestAWSSetupUtils.getAwsInstancesByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsInstancesByIds) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestName(org.junit.rules.TestName) Instance(com.amazonaws.services.ec2.model.Instance) Before(org.junit.Before) TestAWSSetupUtils.createAWSAuthentication(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) Operation(com.vmware.xenon.common.Operation) TestAWSSetupUtils.getCompute(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getCompute) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) TestAWSSetupUtils.createAWSVMResource(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource) Rule(org.junit.Rule) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) TestAWSSetupUtils.regionId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.regionId) TestAWSSetupUtils.setAwsClientMockInfo(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setAwsClientMockInfo) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) IpPermission(com.amazonaws.services.ec2.model.IpPermission) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Map(java.util.Map) HashMap(java.util.HashMap) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)185 Reservation (com.amazonaws.services.ec2.model.Reservation)84 ArrayList (java.util.ArrayList)82 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)71 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)48 List (java.util.List)48 Tag (com.amazonaws.services.ec2.model.Tag)41 Test (org.junit.jupiter.api.Test)38 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)36 Map (java.util.Map)36 Collectors (java.util.stream.Collectors)32 HashMap (java.util.HashMap)26 Filter (com.amazonaws.services.ec2.model.Filter)25 Set (java.util.Set)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)22 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)18 InstanceState (com.amazonaws.services.ec2.model.InstanceState)18 Volume (com.amazonaws.services.ec2.model.Volume)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18