use of com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK 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;
}
use of com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method createComputeDescriptions.
/**
* Creates relevant resources for given VMs.
*/
private void createComputeDescriptions(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.virtualMachines.size() == 0 && ctx.regions.isEmpty()) {
// nothing to create
if (ctx.enumNextPageLink != null) {
ctx.subStage = ComputeEnumerationSubStages.LISTVMS;
handleSubStage(ctx);
return;
}
logFine(() -> "No virtual machine found for creation.");
ctx.subStage = ComputeEnumerationSubStages.PATCH_ADDITIONAL_FIELDS;
handleSubStage(ctx);
return;
}
logFine(() -> String.format("%d compute description with states to be created", ctx.virtualMachines.size()));
Iterator<Entry<String, VirtualMachineInner>> iterator = ctx.virtualMachines.entrySet().iterator();
Collection<Operation> opCollection = new ArrayList<>();
while (iterator.hasNext()) {
Entry<String, VirtualMachineInner> vmEntry = iterator.next();
VirtualMachineInner virtualMachine = vmEntry.getValue();
AuthCredentialsServiceState auth = new AuthCredentialsServiceState();
if (virtualMachine.osProfile() != null) {
auth.userEmail = virtualMachine.osProfile().adminUsername();
auth.privateKey = virtualMachine.osProfile().adminPassword();
}
auth.documentSelfLink = UUID.randomUUID().toString();
auth.tenantLinks = ctx.parentCompute.tenantLinks;
auth.customProperties = new HashMap<>();
if (ctx.request.endpointLink != null) {
auth.customProperties.put(CUSTOM_PROP_ENDPOINT_LINK, ctx.request.endpointLink);
}
String authLink = UriUtils.buildUriPath(AuthCredentialsService.FACTORY_LINK, auth.documentSelfLink);
Operation authOp = Operation.createPost(createInventoryUri(getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
opCollection.add(authOp);
// TODO VSYM-631: Match existing descriptions for new VMs discovered on Azure
ComputeDescription computeDescription = new ComputeDescription();
computeDescription.id = UUID.randomUUID().toString();
computeDescription.name = virtualMachine.name();
computeDescription.regionId = virtualMachine.location();
computeDescription.authCredentialsLink = authLink;
computeDescription.endpointLink = ctx.request.endpointLink;
AdapterUtils.addToEndpointLinks(computeDescription, ctx.request.endpointLink);
computeDescription.documentSelfLink = computeDescription.id;
computeDescription.environmentName = ENVIRONMENT_NAME_AZURE;
if (virtualMachine.hardwareProfile() != null && virtualMachine.hardwareProfile().vmSize() != null) {
computeDescription.instanceType = virtualMachine.hardwareProfile().vmSize().toString();
}
computeDescription.instanceAdapterReference = ctx.parentCompute.description.instanceAdapterReference;
computeDescription.statsAdapterReference = ctx.parentCompute.description.statsAdapterReference;
computeDescription.diskAdapterReference = ctx.parentCompute.description.diskAdapterReference;
computeDescription.computeHostLink = ctx.parentCompute.documentSelfLink;
computeDescription.customProperties = new HashMap<>();
computeDescription.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK);
// TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-1268
String resourceGroupName = getResourceGroupName(virtualMachine.id());
computeDescription.customProperties.put(AZURE_RESOURCE_GROUP_NAME, resourceGroupName);
computeDescription.tenantLinks = ctx.parentCompute.tenantLinks;
Operation compDescOp = Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK).setBody(computeDescription);
ctx.computeDescriptionIds.put(virtualMachine.name(), computeDescription.id);
opCollection.add(compDescOp);
}
for (RegionInfo region : ctx.regions.values()) {
ComputeDescription computeDescriptionForRegion = createComputeDescriptionForRegion(ctx, region);
Operation compDescOp = Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK).setBody(computeDescriptionForRegion);
ctx.computeDescriptionIds.put(region.regionId, computeDescriptionForRegion.id);
opCollection.add(compDescOp);
}
OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
logFine(() -> "Continue on to updating disks.");
ctx.subStage = next;
handleSubStage(ctx);
}).sendWith(this);
}
Aggregations