public void WillBeHandledAsCreateOrUpdate_ShouldReturnFalse() { var result = _createAndUpdateService.CanHandle(new CloudResourceOperationDto { }); Assert.False(result); }
public async Task HandleWork(ProvisioningQueueParentDto queueParentItem) { _provisioningLogService.HandlingQueueParent(queueParentItem); //One per child item in queue item CloudResourceOperationDto currentOperation = null; //Get's re-used amonong child elements because the operations might share variables var currentProvisioningParameters = new ResourceProvisioningParameters(); ResourceProvisioningResult currentProvisioningResult = null; try { //If more than one child/operation, run basic checks on all operations before starting if (queueParentItem.Children.Count > 1) { _provisioningLogService.QueueParentProgressInformation(queueParentItem, "Multiple child item, running pre checks"); foreach (var queueChildItem in queueParentItem.Children) { currentOperation = await _resourceOperationReadService.GetByIdAsync(queueChildItem.ResourceOperationId); _operationCheckService.ThrowIfPossiblyInProgress(currentOperation); } } foreach (var queueChildItem in queueParentItem.Children) { try { currentOperation = await _resourceOperationReadService.GetByIdAsync(queueChildItem.ResourceOperationId); _provisioningLogService.OperationInformation(currentOperation, "Starting operation"); _operationCheckService.ThrowIfTryCountExceededOrAborted(currentOperation); _operationCheckService.ThrowIfResourceIsDeletedAndOperationIsNotADelete(currentOperation); _operationCheckService.ThrowIfPossiblyInProgress(currentOperation); await _operationCheckService.ThrowIfDependentOnUnfinishedOperationAsync(currentOperation, queueParentItem); string networkSecurityGroupName = null; //Only relevant for Sandbox Resource Creation if (currentOperation.Resource.SandboxId.HasValue) { var nsg = CloudResourceUtil.GetSibilingResource(await _resourceReadService.GetByIdNoAccessCheckAsync(currentOperation.Resource.Id), AzureResourceType.NetworkSecurityGroup); networkSecurityGroupName = nsg?.ResourceName; } ProvisioningParamaterUtil.PrepareForNewOperation(currentProvisioningParameters, currentOperation, currentProvisioningResult, networkSecurityGroupName); await _provisioningQueueService.IncreaseInvisibleBasedOnResource(currentOperation, queueParentItem); _provisioningLogService.OperationInformation(currentOperation, "Initial checks passed"); if (_createAndUpdateService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is CREATE or UPDATE"); var provisioningService = AzureResourceServiceResolver.GetProvisioningServiceOrThrow(_serviceProvider, currentOperation.Resource.ResourceType); if (await _operationCompletedService.HandledAsAllreadyCompletedAsync(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is allready completed"); currentProvisioningResult = await provisioningService.GetSharedVariables(currentProvisioningParameters); continue; } else { currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); currentProvisioningResult = await _createAndUpdateService.Handle(currentOperation, currentProvisioningParameters, provisioningService); } currentOperation = await _resourceOperationUpdateService.TouchAsync(currentOperation.Id); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL, updatedProvisioningState : currentProvisioningResult.CurrentProvisioningState); } else if (_deleteOperationService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is DELETE"); var provisioningService = AzureResourceServiceResolver.GetProvisioningServiceOrThrow(_serviceProvider, currentOperation.Resource.ResourceType); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); currentProvisioningResult = await _deleteOperationService.Handle(currentOperation, currentProvisioningParameters, provisioningService); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL, updatedProvisioningState : null); } else if (_roleProvisioningService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is ENSURE ROLES"); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); await _roleProvisioningService.Handle(currentOperation); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL); } else if (_firewallService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is ENSURE FIREWALL"); var firewallRuleService = AzureResourceServiceResolver.GetFirewallRuleService(_serviceProvider, currentOperation.Resource.ResourceType); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); if (firewallRuleService is IHasFirewallRules) { await _firewallService.Handle(currentOperation, firewallRuleService ); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL); } else { throw new ProvisioningException($"Service {firewallRuleService.GetType().Name} does not support firewall operations", CloudResourceOperationState.ABORTED, deleteFromQueue: true); } } else if (_corsRuleProvisioningService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is ENSURE CORS RULES"); var corsRuleService = AzureResourceServiceResolver.GetCorsRuleServiceOrThrow(_serviceProvider, currentOperation.Resource.ResourceType); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); if (corsRuleService is IHasCorsRules) { await _corsRuleProvisioningService.Handle(currentOperation, corsRuleService); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL); } else { throw new ProvisioningException($"Service {corsRuleService.GetType().Name} does not support CORS operations", CloudResourceOperationState.ABORTED, deleteFromQueue: true); } } else { throw new ProvisioningException("Unknown operation type", CloudResourceOperationState.ABORTED); } _provisioningLogService.OperationInformation(currentOperation, "Successfully handeled operation"); } catch (ProvisioningException ex) //Inner loop, ordinary exception is not catched { if (ex.LogAsWarning) { if (ex.IncludeExceptionInWarningLog) { _provisioningLogService.OperationWarning(currentOperation, "Operation aborted", ex); } else { _provisioningLogService.OperationWarning(currentOperation, $"Operation aborted: {ex.Message}"); } } else { _provisioningLogService.OperationError(ex, currentOperation, "Operation failed"); } currentOperation = await _resourceOperationUpdateService.SetErrorMessageAsync(currentOperation.Id, ex); if (!String.IsNullOrWhiteSpace(ex.NewOperationStatus)) { currentOperation = await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, ex.NewOperationStatus); } if (!ex.ProceedWithOtherOperations) { throw; } } } //foreach _provisioningLogService.QueueParentProgressInformation(queueParentItem, "Done"); await _provisioningQueueService.DeleteMessageAsync(queueParentItem); await MoveUpAnyDependentOperations(queueParentItem); } catch (ProvisioningException ex) //Outer loop catch 1 { if (ex.DeleteFromQueue) { _provisioningLogService.QueueParentProgressWarning(queueParentItem, "Deleting due to exception"); await _provisioningQueueService.DeleteMessageAsync(queueParentItem); } else if (ex.PostponeQueueItemFor.HasValue && ex.PostponeQueueItemFor.Value > 0) { if (currentOperation.TryCount < currentOperation.MaxTryCount) { if (queueParentItem.DequeueCount == 5) { _provisioningLogService.QueueParentProgressWarning(queueParentItem, "Re-queuing after exception"); await _provisioningQueueService.ReQueueMessageAsync(queueParentItem, ex.PostponeQueueItemFor.Value); } else { _provisioningLogService.QueueParentProgressWarning(queueParentItem, "Increasing invisibility after exception"); await _provisioningQueueService.IncreaseInvisibilityAsync(queueParentItem, ex.PostponeQueueItemFor.Value); } } } if (ex.StoreQueueInfoOnOperation) { if (!queueParentItem.NextVisibleOn.HasValue) { _provisioningLogService.QueueParentProgressError(queueParentItem, "Could not store queue info on operation, no next visible time exist"); } else { currentOperation = await _resourceOperationUpdateService.SetQueueInformationAsync(currentOperation.Id, queueParentItem.MessageId, queueParentItem.PopReceipt, queueParentItem.NextVisibleOn.Value); } } } catch (Exception ex) //Outer loop catch 2 { _provisioningLogService.QueueParentProgressError(queueParentItem, "Unhandled exception occured"); await _provisioningQueueService.DeleteMessageAsync(queueParentItem); } }