Search in sources :

Example 1 with AWS_TAG_NAME

use of com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME 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)

Example 2 with AWS_TAG_NAME

use of com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME in project photon-model by vmware.

the class AWSNetworkUtils method mapVPCToNetworkState.

public static NetworkState mapVPCToNetworkState(Vpc vpc, String regionId, String resourcePoolLink, String endpointLink, String authCredentialsLink, String parentComputeLink, List<String> tenantLinks, URI adapterUri) {
    if (vpc == null) {
        throw new IllegalArgumentException("Cannot map VPC to network state for null instance");
    }
    NetworkState networkState = new NetworkState();
    networkState.id = vpc.getVpcId();
    // calculate vpc name
    if (vpc.getTags() == null) {
        networkState.name = vpc.getVpcId();
    } else {
        networkState.name = vpc.getTags().stream().filter(tag -> tag.getKey().equals(AWS_TAG_NAME)).map(tag -> tag.getValue()).findFirst().orElse(vpc.getVpcId());
    }
    networkState.subnetCIDR = vpc.getCidrBlock();
    networkState.regionId = regionId;
    networkState.resourcePoolLink = resourcePoolLink;
    networkState.endpointLink = endpointLink;
    if (networkState.endpointLinks == null) {
        networkState.endpointLinks = new HashSet<>();
    }
    networkState.endpointLinks.add(endpointLink);
    networkState.authCredentialsLink = authCredentialsLink;
    networkState.instanceAdapterReference = adapterUri;
    networkState.tenantLinks = tenantLinks;
    networkState.computeHostLink = parentComputeLink;
    networkState.customProperties = new HashMap<>();
    networkState.customProperties.put("defaultInstance", String.valueOf(vpc.isDefault()));
    return networkState;
}
Also used : AWSEnumerationUtils.getTagValue(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getTagValue) Arrays(java.util.Arrays) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) StringUtil(io.netty.util.internal.StringUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) Subnet(com.amazonaws.services.ec2.model.Subnet) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) 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) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) List(java.util.List) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Example 3 with AWS_TAG_NAME

use of com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME in project photon-model by vmware.

the class AWSDiskService method updateDiskState.

/**
 * Update photon-model disk state with the properties of ebs volume.
 */
private void updateDiskState(Volume volume, AWSDiskContext context, AwsDiskStage next) {
    DiskState diskState = context.disk;
    diskState.id = volume.getVolumeId();
    if (volume.getCreateTime() != null) {
        diskState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(volume.getCreateTime().getTime());
    }
    diskState.status = DiskService.DiskStatus.AVAILABLE;
    diskState.origin = DiskService.DiskOrigin.DEPLOYED;
    diskState.encrypted = volume.getEncrypted();
    // calculate disk name, default to volume-id if 'Name' tag is not present
    if (diskState.name == null) {
        if (volume.getTags() == null) {
            diskState.name = volume.getVolumeId();
        } else {
            diskState.name = volume.getTags().stream().filter(tag -> tag.getKey().equals(AWS_TAG_NAME)).map(tag -> tag.getValue()).findFirst().orElse(volume.getVolumeId());
        }
    }
    diskState.zoneId = volume.getAvailabilityZone();
    sendRequest(Operation.createPatch(createInventoryUri(this.getHost(), context.diskRequest.resourceLink())).setBody(diskState).setCompletion((o, e) -> {
        if (e != null) {
            handleStages(context, e);
            return;
        }
        handleStages(context, next);
    }));
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) DeleteVolumeResult(com.amazonaws.services.ec2.model.DeleteVolumeResult) OperationContext(com.vmware.xenon.common.OperationContext) Date(java.util.Date) DISK_IOPS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DISK_IOPS) AWSUtils.validateSizeSupportedByVolumeType(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.validateSizeSupportedByVolumeType) DiskInstanceRequest(com.vmware.photon.controller.model.adapterapi.DiskInstanceRequest) AWS_VOLUME_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VOLUME_ID_PREFIX) ArrayList(java.util.ArrayList) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) MAX_IOPS_PER_GiB(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.MAX_IOPS_PER_GiB) Utils(com.vmware.xenon.common.Utils) CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) BiConsumer(java.util.function.BiConsumer) Volume(com.amazonaws.services.ec2.model.Volume) DEVICE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_TYPE) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) AwsDiskClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AwsDiskClient) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) UUID(java.util.UUID) VOLUME_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.VOLUME_TYPE) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) AsyncHandler(com.amazonaws.handlers.AsyncHandler) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) AWS_DISK_REQUEST_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_DISK_REQUEST_TIMEOUT_MINUTES) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState)

Aggregations

AWS_TAG_NAME (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)2 Operation (com.vmware.xenon.common.Operation)2 StatelessService (com.vmware.xenon.common.StatelessService)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)1 AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)1 CreateVolumeRequest (com.amazonaws.services.ec2.model.CreateVolumeRequest)1 CreateVolumeResult (com.amazonaws.services.ec2.model.CreateVolumeResult)1 DeleteVolumeRequest (com.amazonaws.services.ec2.model.DeleteVolumeRequest)1 DeleteVolumeResult (com.amazonaws.services.ec2.model.DeleteVolumeResult)1 Instance (com.amazonaws.services.ec2.model.Instance)1 Subnet (com.amazonaws.services.ec2.model.Subnet)1 Tag (com.amazonaws.services.ec2.model.Tag)1 Volume (com.amazonaws.services.ec2.model.Volume)1 Vpc (com.amazonaws.services.ec2.model.Vpc)1 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)1 CUSTOM_OS_TYPE (com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE)1