use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback 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.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback in project photon-model by vmware.
the class AzureEndpointAdapterService method validateCredentials.
private DeferredResult<SubscriptionInner> validateCredentials(AuthCredentialsServiceState credentials) {
String msg = "Getting Azure Subscription [" + credentials.userLink + "] for endpoint validation";
AzureDeferredResultServiceCallback<SubscriptionInner> handler = new AzureDeferredResultServiceCallback<SubscriptionInner>(this, msg) {
@Override
protected DeferredResult<SubscriptionInner> consumeSuccess(SubscriptionInner subscription) {
logFine(() -> String.format("Got subscription %s with id %s", subscription.displayName(), subscription.id()));
if (!SubscriptionState.ENABLED.equals(subscription.state())) {
logFine(() -> String.format("Subscription with id %s is not in active" + " state but in %s", subscription.id(), subscription.state()));
return DeferredResult.failed(new IllegalStateException("Subscription is not active"));
}
return DeferredResult.completed(subscription);
}
};
AzureSdkClients azureSdkClients = new AzureSdkClients(credentials);
SubscriptionClientImpl subscriptionClient = azureSdkClients.getSubscriptionClientImpl();
subscriptionClient.subscriptions().getAsync(credentials.userLink, handler);
return handler.toDeferredResult().whenComplete((res, exc) -> azureSdkClients.close());
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback 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));
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback in project photon-model by vmware.
the class AzureInstanceService method deleteResourceGroup.
private DeferredResult<AzureInstanceContext> deleteResourceGroup(AzureInstanceContext ctx) {
String msg = "Deleting resource group [" + ctx.resourceGroupName + "]";
if (ctx.availabilitySetInners == null || ctx.availabilitySetInners.size() == 0 || ctx.availabilitySetInners.get(0).virtualMachines() == null || ctx.availabilitySetInners.get(0).virtualMachines().size() == 0) {
// there are no other machine in this cluster, delete the resource group
ResourceGroupsInner resourceGoups = getResourceManagementClientImpl(ctx).resourceGroups();
AzureDeferredResultServiceCallback<Void> deleteResourceGroupCallback = new AzureDeferredResultServiceCallback<Void>(this, msg) {
@Override
protected DeferredResult<Void> consumeSuccess(Void result) {
logInfo("Successfully deleted resource group: " + ctx.resourceGroupName);
return DeferredResult.completed(result);
}
};
resourceGoups.deleteAsync(ctx.resourceGroupName, deleteResourceGroupCallback);
return deleteResourceGroupCallback.toDeferredResult().thenApply(result -> ctx);
}
return DeferredResult.completed(ctx);
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback in project photon-model by vmware.
the class AzureInstanceService method getPublicIPAddress.
private DeferredResult<AzureInstanceContext> getPublicIPAddress(AzureInstanceContext ctx) {
if (ctx.getPrimaryNic().publicIP == null) {
// No public IP address created -> do nothing.
return DeferredResult.completed(ctx);
}
NetworkManagementClientImpl client = getNetworkManagementClientImpl(ctx);
String msg = "Get public IP address for resource group [" + ctx.resourceGroup.name() + "] and name [" + ctx.getPrimaryNic().publicIP.name() + "].";
AzureDeferredResultServiceCallback<PublicIPAddressInner> callback = new AzureDeferredResultServiceCallback<PublicIPAddressInner>(ctx.service, msg) {
@Override
protected DeferredResult<PublicIPAddressInner> consumeSuccess(PublicIPAddressInner result) {
ctx.getPrimaryNic().publicIP = result;
return DeferredResult.completed(result);
}
};
client.publicIPAddresses().getByResourceGroupAsync(ctx.resourceGroup.name(), ctx.getPrimaryNic().publicIP.name(), null, /* expand */
callback);
return callback.toDeferredResult().thenApply(ignored -> ctx);
}
Aggregations