private async Task <bool> IsApplicationOrServiceExisting(FabricBackupResourceType fabricBackupResourceType, string applicationOrServiceUri, string partitionId, TimeSpan timeout, CancellationToken cancellationToken) { bool isApplicationOrServiceExisting; try { if (fabricBackupResourceType == FabricBackupResourceType.ApplicationUri) { isApplicationOrServiceExisting = await FabricClientHelper.ValidateApplicationUri(applicationOrServiceUri, timeout, cancellationToken); } else if (fabricBackupResourceType == FabricBackupResourceType.ServiceUri) { isApplicationOrServiceExisting = await FabricClientHelper.ValidateServiceUri(applicationOrServiceUri, timeout, cancellationToken); } else if (fabricBackupResourceType == FabricBackupResourceType.PartitionUri) { isApplicationOrServiceExisting = await FabricClientHelper.ValidatePartition(applicationOrServiceUri, partitionId, timeout, cancellationToken); } else { throw new ArgumentException(StringResources.InvalidArguments); } } catch (FabricException) { //Since the Application Or Service is not existing we will return false isApplicationOrServiceExisting = false; } return(isApplicationOrServiceExisting); }
protected internal async Task ValidateTheRequestFabricUri(string fabricUri, TimeSpan timeout, CancellationToken cancellationToken) { string applicationNameUri = null; string serviceNameUri = null; string partitionId = null; FabricBackupResourceType fabricResourceType = UtilityHelper.GetApplicationAndServicePartitionUri(fabricUri, out applicationNameUri, out serviceNameUri, out partitionId); bool isInputValid = false; switch (fabricResourceType) { case FabricBackupResourceType.ApplicationUri: isInputValid = await FabricClientHelper.ValidateApplicationUri(applicationNameUri, timeout, cancellationToken); break; case FabricBackupResourceType.ServiceUri: isInputValid = await FabricClientHelper.ValidateServiceUri(serviceNameUri, timeout, cancellationToken); break; case FabricBackupResourceType.PartitionUri: isInputValid = await FabricClientHelper.ValidateServiceUri(serviceNameUri, timeout, cancellationToken); if (isInputValid) { isInputValid = await FabricClientHelper.ValidatePartition(serviceNameUri, partitionId, timeout, cancellationToken); } break; case FabricBackupResourceType.Error: throw new ArgumentException("Invalid argument"); } if (!isInputValid) { throw new ArgumentException("Invalid Arguments for Application / Service / Partitions Details."); } }
internal override async Task <HttpResponseMessage> RunAsync(TimeSpan timeout, CancellationToken cancellationToken) { WorkItem workItem = null; string applicationNameUri = null; string serviceNameUri = null; string partitionId = null; this.ThrowInvalidArgumentIfNull(this.backupMapping); var appOrServiceUri = await UtilityHelper.GetFabricUriFromRequstHeader(this.fabricRequestHeader, timeout, cancellationToken); BackupMappingModel backupMappingModel = BackupMappingModel.FromBackupMappingView(this.backupMapping, appOrServiceUri); FabricBackupResourceType fabricResourceType = UtilityHelper.GetApplicationAndServicePartitionUri(backupMappingModel.ApplicationOrServiceUri, out applicationNameUri, out serviceNameUri, out partitionId); bool isInputValid = false; string key = backupMappingModel.ApplicationOrServiceUri; BackupMappingModel existingBackupMapping = await this.BackupMappingStore.GetValueAsync(key, timeout, cancellationToken); BackupPolicyModel backupPolicy = await this.BackupPolicyStore.GetValueAsync(backupMappingModel.BackupPolicyName, timeout, cancellationToken); if (backupPolicy == null) { throw new FabricException(StringResources.BackupPolicyDoesNotExist, FabricErrorCode.BackupPolicyDoesNotExist); } WorkItemPropogationType workItemPropogationType = existingBackupMapping == null? WorkItemPropogationType.Enable : WorkItemPropogationType.UpdateProtection; WorkItemInfo workItemInfo = new WorkItemInfo { WorkItemType = workItemPropogationType, ProctectionGuid = backupMappingModel.ProtectionId, BackupPolicyUpdateGuid = backupPolicy.UniqueId }; switch (fabricResourceType) { case FabricBackupResourceType.ApplicationUri: isInputValid = await FabricClientHelper.ValidateApplicationUri(applicationNameUri, timeout, cancellationToken); workItem = new ResolveToPartitionWorkItem(fabricResourceType, backupMappingModel.ApplicationOrServiceUri, workItemInfo); break; case FabricBackupResourceType.ServiceUri: isInputValid = await FabricClientHelper.ValidateServiceUri(serviceNameUri, timeout, cancellationToken); workItem = new ResolveToPartitionWorkItem(fabricResourceType, backupMappingModel.ApplicationOrServiceUri, workItemInfo); break; case FabricBackupResourceType.PartitionUri: isInputValid = await FabricClientHelper.ValidateServiceUri(serviceNameUri, timeout, cancellationToken); if (isInputValid) { isInputValid = await FabricClientHelper.ValidatePartition(serviceNameUri, partitionId, timeout, cancellationToken); workItem = new SendToServiceNodeWorkItem(serviceNameUri, partitionId, workItemInfo); } break; case FabricBackupResourceType.Error: throw new ArgumentException("Invalid argument"); } if (!isInputValid) { throw new ArgumentException("Invalid Arguments for Application / Service / Partitions Details."); } using (var transaction = this.StatefulService.StateManager.CreateTransaction()) { await this.BackupMappingStore.AddOrUpdateAsync(key, backupMappingModel, (key1, BackupMappingModel1) => backupMappingModel, timeout, cancellationToken); await this.WorkItemQueue.AddWorkItem(workItem, timeout, cancellationToken, transaction); BackupPolicyModel existingBackupPolicy = await this.BackupPolicyStore.GetValueWithUpdateLockModeAsync(backupMappingModel.BackupPolicyName, timeout, cancellationToken, transaction); if (existingBackupMapping != null) { BackupPolicyModel previousBackupPolicy = await this.BackupPolicyStore.GetValueWithUpdateLockModeAsync(existingBackupMapping.BackupPolicyName, timeout, cancellationToken, transaction); await this.BackupPolicyStore.RemoveProtectionEntity(previousBackupPolicy, key, timeout, cancellationToken, transaction); } await this.BackupPolicyStore.AddProtectionEntity(existingBackupPolicy, key, timeout, cancellationToken, transaction); cancellationToken.ThrowIfCancellationRequested(); await transaction.CommitAsync(); } return(new HttpResponseMessage(HttpStatusCode.Accepted)); }
private async Task <bool> IsFabricEntityExisting(string fabricUri, TimeSpan timeout, CancellationToken cancellationToken) { string applicationUri = null; string serviceUri = null; string partitionId = null; FabricBackupResourceType fabricBackupResourceType = UtilityHelper.GetApplicationAndServicePartitionUri(fabricUri, out applicationUri, out serviceUri, out partitionId); switch (fabricBackupResourceType) { case FabricBackupResourceType.ApplicationUri: try { return (await FabricClientHelper.ValidateApplicationUri(applicationUri, timeout, cancellationToken)); } catch (Exception exception) { FabricException fabricException = exception as FabricException; if (fabricException != null && fabricException.ErrorCode == FabricErrorCode.ApplicationNotFound) { return(false); } } break; case FabricBackupResourceType.ServiceUri: try { return (await FabricClientHelper.ValidateServiceUri(serviceUri, timeout, cancellationToken)); } catch (Exception exception) { FabricException fabricException = exception as FabricException; if (fabricException != null && (fabricException.ErrorCode == FabricErrorCode.ServiceNotFound || fabricException.ErrorCode == FabricErrorCode.ApplicationNotFound)) { return(false); } } break; case FabricBackupResourceType.PartitionUri: try { return (await FabricClientHelper.ValidatePartition(serviceUri, partitionId, timeout, cancellationToken)); } catch (Exception exception) { FabricException fabricException = exception as FabricException; if (fabricException != null && fabricException.ErrorCode == FabricErrorCode.PartitionNotFound) { return(false); } } break; } return(true); }