Search in sources :

Example 1 with AzureDecommissionCallback

use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDecommissionCallback in project photon-model by vmware.

the class AzureInstanceService method errorHandler.

/**
 * The ultimate error handler that should handle errors from all sources.
 * <p>
 * NOTE: Do not use directly. Use it through
 * {@link #handleError(AzureInstanceContext, Throwable)}.
 */
private void errorHandler(AzureInstanceContext ctx) {
    logSevere(ctx.error);
    if (ctx.computeRequest.isMockRequest) {
        finishWithFailure(ctx);
        return;
    }
    if (ctx.computeRequest.requestType != ComputeInstanceRequest.InstanceRequestType.CREATE) {
        finishWithFailure(ctx);
        return;
    }
    if (ctx.resourceGroup == null) {
        finishWithFailure(ctx);
        return;
    }
    // CREATE request has resulted in RG creation -> clear RG and its content.
    String rgName = ctx.resourceGroup.name();
    String msg = "Rollback provisioning for [" + ctx.vmName + "] Azure VM";
    ResourceGroupsInner azureClient = getResourceManagementClientImpl(ctx).resourceGroups();
    AzureDecommissionCallback callback = new AzureDecommissionCallback(this, msg) {

        @Override
        protected DeferredResult<Void> consumeDecommissionSuccess(Void body) {
            return DeferredResult.completed(body);
        }

        @Override
        protected Throwable consumeError(Throwable e) {
            String rollbackError = String.format(msg + ": FAILED. Details: %s", Utils.toString(e));
            // Wrap original ctx.error with rollback error details.
            ctx.error = new IllegalStateException(rollbackError, ctx.error);
            return RECOVERED;
        }

        @Override
        protected Runnable checkExistenceCall(ServiceCallback<Boolean> checkExistenceCallback) {
            return () -> azureClient.checkExistenceAsync(rgName, checkExistenceCallback);
        }
    };
    azureClient.deleteAsync(rgName, callback);
    callback.toDeferredResult().whenComplete((o, e) -> finishWithFailure(ctx));
}
Also used : ServiceCallback(com.microsoft.rest.ServiceCallback) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) AzureDecommissionCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDecommissionCallback) ResourceGroupsInner(com.microsoft.azure.management.resources.implementation.ResourceGroupsInner)

Aggregations

ResourceGroupsInner (com.microsoft.azure.management.resources.implementation.ResourceGroupsInner)1 ServiceCallback (com.microsoft.rest.ServiceCallback)1 AzureDecommissionCallback (com.vmware.photon.controller.model.adapters.azure.utils.AzureDecommissionCallback)1 AzureDeferredResultServiceCallback (com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback)1