use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AWSInstanceContext method getSecurityGroups.
/**
* For every NIC's security group states obtain existing SecurityGroup objects from AWS and
* store their IDs and Names in context.
*/
private DeferredResult<AWSInstanceContext> getSecurityGroups(AWSInstanceContext context) {
if (context.nics.isEmpty()) {
return DeferredResult.completed(context);
}
List<DeferredResult<Void>> getSecurityGroupsDRs = new ArrayList<>();
AWSSecurityGroupClient client = new AWSSecurityGroupClient(context.amazonEC2Client);
for (AWSNicContext nicCtx : context.nics) {
getSecurityGroupsDRs.add(getSecurityGroupsPerNIC(client, nicCtx, context.child.name));
}
return DeferredResult.allOf(getSecurityGroupsDRs).handle((all, exc) -> {
if (exc != null) {
String msg = String.format("Error getting SecurityGroups from AWS for [%s] VM.", context.child.name);
throw new IllegalStateException(msg, exc);
}
return context;
});
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AWSInstanceTypeService method getInstanceTypes.
/**
* Return the instance types by loading them from AWS SDK {@link InstanceType}.
*/
private DeferredResult<Context> getInstanceTypes(Context context) {
AssertUtil.assertNotNull(context.endpointState, "Endpoint state was not retrieved.");
context.instanceTypes = new InstanceTypeList();
// Set tenant links as specified in the endpoint.
context.instanceTypes.tenantLinks = context.endpointState.tenantLinks;
context.instanceTypes.instanceTypes = // Use AWS SDK InstanceType enum as primary source of instance type data.
Arrays.stream(InstanceType.values()).map(instanceType -> {
InstanceTypeList.InstanceType result = new InstanceTypeList.InstanceType(instanceType.toString(), instanceType.toString());
InstanceTypeList.InstanceType instanceTypeInfo = this.instanceTypeInfo.get(instanceType.toString());
if (instanceTypeInfo != null) {
// We have additional information -> populate additional fields.
result.cpuCount = instanceTypeInfo.cpuCount;
result.memoryInMB = instanceTypeInfo.memoryInMB;
result.networkType = instanceTypeInfo.networkType;
result.storageType = instanceTypeInfo.storageType;
result.dataDiskMaxCount = instanceTypeInfo.dataDiskMaxCount;
result.dataDiskSizeInMB = instanceTypeInfo.dataDiskSizeInMB;
}
return result;
}).filter(instanceType -> !Integer.valueOf(-1).equals(instanceType.cpuCount)).collect(Collectors.toList());
return DeferredResult.completed(context);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method processCreateUpdateNicRequest.
/**
* Processes request for creating and updating Network interface resources.
*/
private void processCreateUpdateNicRequest(NetworkInterfaceState nic, NetworkInterfaceInner remoteNic, EnumerationContext ctx, List<DeferredResult<NetworkInterfaceState>> ops, Map<String, String> subnetPerNicId, boolean isCreate) {
nic.name = remoteNic.name();
nic.subnetLink = subnetPerNicId.get(remoteNic.id());
NicMetadata nicMeta = new NicMetadata();
nicMeta.state = nic;
nicMeta.macAddress = remoteNic.macAddress();
// else will default to original ID for PATCH requests
if (isCreate) {
nic.id = remoteNic.id();
nic.endpointLink = ctx.request.endpointLink;
AdapterUtils.addToEndpointLinks(nic, ctx.request.endpointLink);
nic.tenantLinks = ctx.parentCompute.tenantLinks;
nic.regionId = remoteNic.location();
nic.computeHostLink = ctx.parentCompute.documentSelfLink;
} else {
if (StringUtils.isEmpty(nic.endpointLink)) {
nic.endpointLink = ctx.request.endpointLink;
}
nic.endpointLinks.add(ctx.request.endpointLink);
}
List<NetworkInterfaceIPConfigurationInner> ipConfigurations = remoteNic.ipConfigurations();
if (ipConfigurations == null || ipConfigurations.isEmpty()) {
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
return;
}
NetworkInterfaceIPConfigurationInner nicIPConf = ipConfigurations.get(0);
nic.address = nicIPConf.privateIPAddress();
if (nicIPConf.publicIPAddress() == null) {
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
return;
}
// IP address is not directly available in NetworkInterfaceIPConfigurationInner.
// It is available as a SubResource, We use the SubResource ID of IP address from
// NetworkInterfaceIPConfigurationInner to obtain the IP address.
Consumer<Throwable> failure = e -> {
logWarning("Error getting public IP address from Azure [endpointLink:%s], [Exception:%s]", ctx.request.endpointLink, e.getMessage());
handleError(ctx, e);
};
PhotonModelUtils.runInExecutor(this.executorService, () -> {
Azure azure = ctx.azureSdkClients.getAzureClient();
azure.publicIPAddresses().getByIdAsync(nicIPConf.publicIPAddress().id()).subscribe(injectOperationContext(new Action1<PublicIPAddress>() {
@Override
public void call(PublicIPAddress publicIPAddress) {
nicMeta.publicIp = publicIPAddress.ipAddress();
if (publicIPAddress.inner().dnsSettings() != null) {
nicMeta.publicDnsName = publicIPAddress.inner().dnsSettings().fqdn();
}
executeNicCreateUpdateRequest(nic, remoteNic, ctx, ops, nicMeta, isCreate);
}
}));
}, failure);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureInstanceContext method getSecurityGroups.
/**
* For every NIC lookup associated Azure Security Groups as specified by
* {@code AzureNicContext.securityGroupState.name}. If any of the security groups is not found
* leave the {@code AzureNicContext.securityGroup} as null and proceed without an exception.
*/
private DeferredResult<AzureInstanceContext> getSecurityGroups(AzureInstanceContext context) {
if (context.nics.isEmpty()) {
return DeferredResult.completed(context);
}
NetworkSecurityGroupsInner azureClient = context.azureSdkClients.getNetworkManagementClientImpl().networkSecurityGroups();
List<DeferredResult<NetworkSecurityGroupInner>> getSecurityGroupDRs = context.nics.stream().filter(nicCtx -> nicCtx.securityGroupState() != null && nicCtx.securityGroupRGState != null).map(nicCtx -> {
String sgName = nicCtx.securityGroupState().name;
String msg = "Getting Azure Security Group [" + nicCtx.securityGroupRGState.name + "/" + sgName + "] for [" + nicCtx.nicStateWithDesc.name + "] NIC for [" + context.vmName + "] VM";
return AzureSecurityGroupUtils.getSecurityGroup(service(), azureClient, nicCtx.securityGroupRGState.name, sgName, msg).thenApply(sg -> {
nicCtx.securityGroup = sg;
return sg;
});
}).collect(Collectors.toList());
return DeferredResult.allOf(getSecurityGroupDRs).handle((all, exc) -> {
if (exc != null) {
String msg = String.format("Error getting Security Group from Azure for [%s] VM.", context.child.name);
throw new IllegalStateException(msg, exc);
}
return context;
});
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class ResourcePoolQueryHelper method executeRpQueries.
/**
* Executes the resource pool queries in parallel and then collects the result.
*/
private DeferredResult<Void> executeRpQueries() {
List<DeferredResult<Void>> rpQueryDRs = new ArrayList<>(this.result.resourcesPools.size());
Map<String, Map<String, ComputeState>> computeMapByRpLink = new ConcurrentHashMap<>();
for (ResourcePoolData rpData : this.result.resourcesPools.values()) {
String rpLink = rpData.resourcePoolState.documentSelfLink;
Query rpQuery = rpData.resourcePoolState.query;
Query.Builder queryBuilder = Query.Builder.create().addClause(rpQuery);
if (this.computeLinks != null && !this.computeLinks.isEmpty()) {
queryBuilder.addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, this.computeLinks);
} else if (this.additionalQueryClausesProvider != null) {
this.additionalQueryClausesProvider.accept(queryBuilder);
}
QueryByPages<ComputeState> computeQuery = new QueryByPages<>(this.host, queryBuilder.build(), ComputeState.class, this.tenantLinks).setMaxPageSize(PAGE_SIZE);
computeQuery.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
DeferredResult<Map<String, ComputeState>> rpQueryDR;
if (this.expandComputes) {
rpQueryDR = computeQuery.collectDocuments(Collectors.toMap(cs -> cs.documentSelfLink, cs -> cs));
} else {
// manually collect links since Collectors.toMap() does not allow null values
Map<String, ComputeState> computesMap = new HashMap<>();
rpQueryDR = computeQuery.queryLinks(csLink -> computesMap.put(csLink, null)).thenApply(ignore -> computesMap);
}
rpQueryDRs.add(rpQueryDR.thenAccept(computesMap -> computeMapByRpLink.put(rpLink, computesMap)));
}
return DeferredResult.allOf(rpQueryDRs).thenAccept(ignore -> computeMapByRpLink.forEach(this::storeComputes)).thenApply(ignore -> (Void) null);
}
Aggregations