use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureInstanceContext method getNetworks.
/**
* For every NIC lookup associated Azure Subnets as specified by
* {@code AzureNicContext.networkState.name} and {@code AzureNicContext.subnetState.name}. If
* any of the subnets is not found leave the {@link AzureNicContext#subnet} as null and proceed
* without an exception.
*/
private DeferredResult<AzureInstanceContext> getNetworks(AzureInstanceContext context) {
if (context.nics.isEmpty()) {
return DeferredResult.completed(context);
}
SubnetsInner azureClient = service().getNetworkManagementClientImpl(context).subnets();
List<DeferredResult<SubnetInner>> getSubnetDRs = context.nics.stream().filter(nicCtx -> nicCtx.networkRGState != null).map(nicCtx -> {
String msg = "Getting Azure Subnet [" + nicCtx.networkRGState.name + "/" + nicCtx.networkState.name + "/" + nicCtx.subnetState.name + "] for [" + nicCtx.nicStateWithDesc.name + "] NIC for [" + context.vmName + "] VM";
AzureDeferredResultServiceCallback<SubnetInner> handler = new AzureDeferredResultServiceCallback<SubnetInner>(service(), msg) {
@Override
protected DeferredResult<SubnetInner> consumeSuccess(SubnetInner subnet) {
nicCtx.subnet = subnet;
return DeferredResult.completed(subnet);
}
};
azureClient.getAsync(nicCtx.networkRGState.name, nicCtx.networkState.name, nicCtx.subnetState.name, null, /* expand */
handler);
return handler.toDeferredResult();
}).collect(Collectors.toList());
return DeferredResult.allOf(getSubnetDRs).handle((all, exc) -> {
if (exc != null) {
String msg = String.format("Error getting Subnets 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 AzureNetworkEnumerationAdapterService method updateNetworkTagLinks.
private DeferredResult<NetworkEnumContext> updateNetworkTagLinks(NetworkEnumContext context) {
logFine(() -> "Create or update Network States' tags with the actual tags in Azure.");
if (context.virtualNetworks.size() == 0) {
logFine(() -> "No local networks to be updated so there are no tags to update.");
return DeferredResult.completed(context);
} else {
List<DeferredResult<Set<String>>> updateNetwLinksOps = new ArrayList<>();
// update tag links for the existing NetworkStates
for (String vnetId : context.networkStates.keySet()) {
if (!context.virtualNetworks.containsKey(vnetId)) {
// this is not a network to update
continue;
}
VirtualNetwork vNet = context.virtualNetworks.get(vnetId);
NetworkState existingNetworkState = context.networkStates.get(vnetId);
Map<String, String> remoteTags = new HashMap<>();
if (vNet.tags != null && !vNet.tags.isEmpty()) {
for (Entry<String, String> vNetTagEntry : vNet.tags.entrySet()) {
remoteTags.put(vNetTagEntry.getKey(), vNetTagEntry.getValue());
}
}
updateNetwLinksOps.add(updateLocalTagStates(this, existingNetworkState, remoteTags, null));
}
return DeferredResult.allOf(updateNetwLinksOps).thenApply(gnore -> context);
}
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class QueryUtilsTest method doTest.
private void doTest(ComputeDescription cd, Set<String> expected, List<String> tenantLinks) {
Query queryForReferrers = QueryUtils.queryForReferrers(cd.documentSelfLink, ComputeState.class, ComputeState.FIELD_NAME_DESCRIPTION_LINK);
// The classes under testing: QueryByPages and QueryTop
List<QueryTemplate<?, ComputeState>> queryStrategies = Arrays.asList(new QueryByPages<>(getHost(), queryForReferrers, ComputeState.class, tenantLinks), new QueryTop<>(getHost(), queryForReferrers, ComputeState.class, tenantLinks));
// Test collectDocuments/queryDocuments/collectLinks/queryLinks per strategy
for (QueryTemplate<?, ComputeState> queryStrategy : queryStrategies) {
for (boolean isDirect : Arrays.asList(true, false)) {
final String msg = queryStrategy.getClass().getSimpleName() + ":" + isDirect;
{
// Test collectDocuments, which internally also tests queryDocuments
DeferredResult<Set<String>> documentLinksDR = queryStrategy.setDirect(isDirect).collectDocuments(mapping(cs -> cs.documentSelfLink, toSet()));
Set<String> actual = waitToComplete(documentLinksDR);
assertThat(msg, actual, equalTo(expected));
}
{
// Test collectLinks, which internally also tests queryLinks
DeferredResult<Set<String>> documentLinksDR = queryStrategy.setDirect(isDirect).collectLinks(toSet());
Set<String> actual = waitToComplete(documentLinksDR);
assertThat(msg, actual, equalTo(expected));
}
}
}
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createStorageDescriptions.
/*
* Create all storage descriptions Storage descriptions mapping to Azure storage accounts have
* an additional custom property {"storageType" : "Microsoft.Storage/storageAccounts"}
*/
private void createStorageDescriptions(StorageEnumContext context, StorageEnumStages next) {
if (context.storageAccountsToUpdateCreate.size() == 0) {
if (context.enumNextPageLink != null) {
context.subStage = StorageEnumStages.GET_STORAGE_ACCOUNTS;
handleSubStage(context);
return;
}
logFine(() -> "No storage account found for creation");
context.subStage = next;
handleSubStage(context);
return;
}
logFine(() -> String.format("%d storage description to be created", context.storageAccountsToUpdateCreate.size()));
Consumer<Throwable> failure = e -> {
logWarning("Failure getting Azure storage accounts [EndpointLink:%s] [Exception:%s]", context.request.endpointLink, e.getMessage());
handleError(context, e);
};
PhotonModelUtils.runInExecutor(this.executorService, () -> {
StorageAccountsInner stOps = context.azureSdkClients.getAzureClient().storageAccounts().inner();
List<DeferredResult<StorageDescription>> results = context.storageAccountsToUpdateCreate.values().stream().map(sa -> createStorageDescription(context, sa, stOps)).collect(java.util.stream.Collectors.toList());
DeferredResult.allOf(results).whenComplete((sds, e) -> {
if (e != null) {
logWarning(() -> String.format("Error: %s", e.getMessage()));
}
context.subStage = next;
handleSubStage(context);
});
}, failure);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createStorageDescription.
private DeferredResult<StorageDescription> createStorageDescription(StorageEnumContext context, StorageAccount storageAccount, StorageAccountsInner stOps) {
String resourceGroupName = getResourceGroupName(storageAccount.id);
AzureDeferredResultServiceCallback<StorageAccountListKeysResultInner> handler = new Default<>(this, "Load account keys for storage: " + storageAccount.name);
PhotonModelUtils.runInExecutor(this.executorService, () -> {
stOps.listKeysAsync(resourceGroupName, storageAccount.name, handler);
}, handler::failure);
return handler.toDeferredResult().thenCompose(keys -> AzureUtils.storeKeys(getHost(), keys, context.request.endpointLink, context.parentCompute.tenantLinks)).thenApply(auth -> {
String connectionString = String.format(STORAGE_CONNECTION_STRING, storageAccount.name, auth.customProperties.get(AZURE_STORAGE_ACCOUNT_KEY1));
context.storageConnectionStrings.put(storageAccount.id, connectionString);
return auth;
}).thenApply(auth -> {
StorageDescription storageDesc = AzureUtils.constructStorageDescription(context.parentCompute, context.request, storageAccount, auth.documentSelfLink);
return storageDesc;
}).thenCompose(sd -> sendWithDeferredResult(Operation.createPost(context.request.buildUri(StorageDescriptionService.FACTORY_LINK)).setBody(sd).setCompletion((o, e) -> {
if (e != null) {
logWarning("Unable to store storage description for storage account:[%s], reason: %s", storageAccount.name, Utils.toJsonHtml(e));
} else {
StorageDescription storageDescription = o.getBody(StorageDescription.class);
context.storageDescriptionsForPatching.put(storageDescription.id, storageDescription);
}
}), StorageDescription.class));
}
Aggregations