Search in sources :

Example 1 with InstanceType

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

the class AWSInstanceContext method getInstanceTypeInfo.

private DeferredResult<AWSInstanceContext> getInstanceTypeInfo(AWSInstanceContext context) {
    String instanceType = context.child.description.instanceType;
    if (instanceType == null) {
        instanceType = context.child.description.name;
    }
    if (instanceType == null) {
        String msg = String.format("AWS Instance type not specified for [%s] VM.", context.child.name);
        return DeferredResult.failed(new IllegalStateException(msg));
    }
    URI instanceTypeServiceURI = UriUtils.buildUri(context.service.getHost(), AWSInstanceTypeService.SELF_LINK);
    instanceTypeServiceURI = UriUtils.appendQueryParam(instanceTypeServiceURI, URI_PARAM_ENDPOINT, context.child.endpointLink);
    instanceTypeServiceURI = UriUtils.appendQueryParam(instanceTypeServiceURI, URI_PARAM_INSTANCE_TYPE, instanceType);
    Operation op = Operation.createGet(instanceTypeServiceURI).setReferer(context.service.getHost().getUri());
    DeferredResult<InstanceType> dr = context.service.sendWithDeferredResult(op, InstanceType.class);
    return dr.thenAccept(type -> {
        context.instanceTypeInfo = type;
    }).handle((all, err) -> {
        if (err != null) {
            String msg = String.format("Error getting instance-type info for [%s] VM. Reason [%s]", context.child.name, err.getMessage());
            throw new IllegalStateException(msg, err);
        }
        return context;
    });
}
Also used : InstanceNetworkInterfaceSpecification(com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) CREATE_CONTEXT_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.CREATE_CONTEXT_PROP_NAME) Function(java.util.function.Function) CreateSubnetResult(com.amazonaws.services.ec2.model.CreateSubnetResult) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) HashSet(java.util.HashSet) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) URI_PARAM_INSTANCE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_INSTANCE_TYPE) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Filter(com.amazonaws.services.ec2.model.Filter) Collections.singletonMap(java.util.Collections.singletonMap) URI(java.net.URI) Subnet(com.amazonaws.services.ec2.model.Subnet) StatelessService(com.vmware.xenon.common.StatelessService) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) Vpc(com.amazonaws.services.ec2.model.Vpc) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Collectors(java.util.stream.Collectors) AWS_VPC_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID_FILTER) URI_PARAM_ENDPOINT(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_ENDPOINT) InstanceType(com.vmware.photon.controller.model.support.InstanceTypeList.InstanceType) List(java.util.List) BaseComputeInstanceContext(com.vmware.photon.controller.model.adapters.util.instance.BaseComputeInstanceContext) AWS_SUBNET_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_SUBNET_ID_FILTER) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) DiskService(com.vmware.photon.controller.model.resources.DiskService) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) CreateSubnetRequest(com.amazonaws.services.ec2.model.CreateSubnetRequest) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) Operation(com.vmware.xenon.common.Operation) InstanceType(com.vmware.photon.controller.model.support.InstanceTypeList.InstanceType) URI(java.net.URI)

Example 2 with InstanceType

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

the class AWSInstanceService method createInstanceStoreMappings.

/**
 * Creates device mappings for the instance-store disks.
 */
private List<BlockDeviceMapping> createInstanceStoreMappings(List<DiskState> instanceStoreDisks, List<String> usedDeviceNames, List<String> usedVirtualNames, String instanceType, Integer capacityMBytes, String platform, String virtualizationType) {
    List<BlockDeviceMapping> deviceMappings = new ArrayList<>();
    if (!instanceStoreDisks.isEmpty()) {
        this.logInfo(() -> String.format("[AWSInstanceService] Ignores the size and type of the " + "additional disk. Instance-store type of additional disks are " + "provisioned with the capacity supported by %s", instanceType));
        List<String> availableDeviceNames = AWSBlockDeviceNameMapper.getAvailableNames(AWSSupportedOS.get(platform), AWSSupportedVirtualizationTypes.get(virtualizationType), AWSStorageType.INSTANCE_STORE, instanceType, usedDeviceNames);
        List<String> availableVirtualNames = getAvailableVirtualNames(usedVirtualNames);
        if (availableDeviceNames.size() >= instanceStoreDisks.size() && availableVirtualNames.size() >= instanceStoreDisks.size()) {
            for (DiskState diskState : instanceStoreDisks) {
                BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
                String deviceName = availableDeviceNames.get(0);
                blockDeviceMapping.setDeviceName(deviceName);
                availableDeviceNames.remove(0);
                usedDeviceNames.add(deviceName);
                String virtualName = availableVirtualNames.get(0);
                blockDeviceMapping.setVirtualName(virtualName);
                availableVirtualNames.remove(0);
                deviceMappings.add(blockDeviceMapping);
                if (diskState.customProperties == null) {
                    diskState.customProperties = new HashMap<>();
                }
                diskState.customProperties.put(DEVICE_NAME, deviceName);
                diskState.customProperties.put(DEVICE_TYPE, AWSStorageType.INSTANCE_STORE.getName());
                diskState.capacityMBytes = capacityMBytes;
            }
        } else {
            String message = "Additional instance-store disks cannot be attached. " + "Not sufficient device names are available.";
            this.logWarning(() -> "[AWSInstanceService] " + message);
            throw new IllegalArgumentException(message);
        }
    }
    return deviceMappings;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)

Example 3 with InstanceType

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

the class AWSInstanceService method addMandatoryProperties.

/**
 * Add the disk information to disk state so that the disk state reflects the volume
 * information
 */
private void addMandatoryProperties(DiskState diskState, BlockDeviceMapping deviceMapping, AWSInstanceContext instanceType) {
    if (diskState.customProperties == null) {
        diskState.customProperties = new HashMap<>();
    }
    String deviceName = deviceMapping.getDeviceName();
    diskState.customProperties.put(DEVICE_NAME, deviceName);
    diskState.persistent = Boolean.FALSE;
    EbsBlockDevice ebs = deviceMapping.getEbs();
    if (ebs != null) {
        diskState.capacityMBytes = ebs.getVolumeSize() * 1024;
        diskState.customProperties.put(DEVICE_TYPE, AWSStorageType.EBS.getName());
    } else {
        diskState.capacityMBytes = instanceType.instanceTypeInfo.dataDiskSizeInMB;
        diskState.customProperties.put(DEVICE_TYPE, AWSStorageType.INSTANCE_STORE.getName());
    }
    // add the endpointLinks
    diskState.endpointLinks = instanceType.child.endpointLinks;
}
Also used : EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice)

Example 4 with InstanceType

use of com.amazonaws.services.ec2.model.InstanceType 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 5 with InstanceType

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

the class AWSEnumerationUtils method mapInstanceToComputeState.

/**
 * Maps the instance discovered on AWS to a local compute state that will be persisted.
 */
public static ComputeState mapInstanceToComputeState(ServiceHost host, Instance instance, String parentComputeLink, String placementComputeLink, String resourcePoolLink, String existingEndpointLink, Set<String> endpointLinks, String computeDescriptionLink, Set<URI> parentCDStatsAdapterReferences, Set<String> internalTagLinks, String regionId, String zoneId, List<String> tenantLinks, List<Tag> createdExternalTags, Boolean isNewState, List<String> diskLinks) {
    ComputeState computeState = new ComputeState();
    computeState.id = instance.getInstanceId();
    computeState.name = instance.getInstanceId();
    computeState.parentLink = parentComputeLink;
    computeState.computeHostLink = parentComputeLink;
    computeState.type = ComputeType.VM_GUEST;
    computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
    computeState.regionId = regionId;
    computeState.zoneId = zoneId;
    computeState.instanceType = instance.getInstanceType();
    computeState.instanceAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_INSTANCE_ADAPTER);
    computeState.enumerationAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_ENUMERATION_CREATION_ADAPTER);
    computeState.statsAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_STATS_ADAPTER);
    computeState.statsAdapterReferences = parentCDStatsAdapterReferences;
    computeState.resourcePoolLink = resourcePoolLink;
    if (computeState.endpointLinks == null) {
        computeState.endpointLinks = new HashSet<>();
    }
    computeState.endpointLinks.addAll(endpointLinks);
    // assign existing one, if exists
    if (existingEndpointLink != null) {
        computeState.endpointLink = existingEndpointLink;
    } else {
        computeState.endpointLink = endpointLinks.iterator().next();
    }
    // Compute descriptions are looked up by the instanceType in the local list of CDs.
    computeState.descriptionLink = computeDescriptionLink;
    computeState.hostName = instance.getPublicDnsName();
    // TODO VSYM-375 for adding disk information
    computeState.address = instance.getPublicIpAddress();
    computeState.powerState = AWSUtils.mapToPowerState(instance.getState());
    computeState.customProperties = new HashMap<>();
    computeState.customProperties.put(CUSTOM_OS_TYPE, getNormalizedOSType(instance));
    computeState.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK);
    computeState.customProperties.put(ComputeProperties.PLACEMENT_LINK, placementComputeLink);
    // Network State. Create one network state mapping to each VPC that is discovered during
    // enumeration.
    computeState.customProperties.put(AWS_VPC_ID, instance.getVpcId());
    computeState.tagLinks = new HashSet<>();
    // PATCH to update tagLinks of existing disks.
    if (!instance.getTags().isEmpty() && isNewState) {
        // we have already made sure that the tags exist and we can build their links ourselves
        computeState.tagLinks = instance.getTags().stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey()) && createdExternalTags.contains(t)).map(t -> newTagState(t.getKey(), t.getValue(), true, tenantLinks)).map(TagFactoryService::generateSelfLink).collect(Collectors.toSet());
        if (computeState.tagLinks != null && computeState.tagLinks.contains(null)) {
            host.log(Level.SEVERE, "Null tag link inserted in new ComputeState for instance ID: %s", instance.getInstanceId());
            host.log(Level.SEVERE, "Removing null tag link from new ComputeState");
            computeState.tagLinks.remove(null);
        }
    }
    // The name of the compute state is the value of the AWS_TAG_NAME tag
    String nameTag = getTagValue(instance.getTags(), AWS_TAG_NAME);
    if (nameTag != null && !nameTag.equals(EMPTY_STRING)) {
        computeState.name = nameTag;
    }
    // append internal tagLinks to any existing ones
    if (internalTagLinks != null) {
        computeState.tagLinks.addAll(internalTagLinks);
    }
    if (instance.getLaunchTime() != null) {
        computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
    }
    if (diskLinks != null && !diskLinks.isEmpty()) {
        computeState.diskLinks = new ArrayList<>();
        computeState.diskLinks.addAll(diskLinks);
    }
    computeState.tenantLinks = tenantLinks;
    return computeState;
}
Also used : ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) QueryTask(com.vmware.xenon.services.common.QueryTask) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) Query(com.vmware.xenon.services.common.QueryTask.Query) TagFactoryService(com.vmware.photon.controller.model.resources.TagFactoryService) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) WINDOWS_PLATFORM(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.WINDOWS_PLATFORM) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) Instance(com.amazonaws.services.ec2.model.Instance) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService) AWSConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryResultLimit) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) Collection(java.util.Collection) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) UUID(java.util.UUID) AWS_VPC_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) Tag(com.amazonaws.services.ec2.model.Tag) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) URI_PATH_CHAR(com.vmware.xenon.common.UriUtils.URI_PATH_CHAR) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TagFactoryService(com.vmware.photon.controller.model.resources.TagFactoryService)

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