コード例 #1
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
                string vaultName         = resourceIdentifier.ResourceName;
                string resourceGroupName = resourceIdentifier.ResourceGroupName;

                if (!OriginalWorkloadRestore.IsPresent && !AlternateWorkloadRestore.IsPresent)
                {
                    throw new ArgumentException(string.Format(Resources.AzureWorkloadRestoreLocationException));
                }

                AzureWorkloadRecoveryConfig azureWorkloadRecoveryConfig = GetConfigObject();
                azureWorkloadRecoveryConfig.SourceResourceId            = Item != null ? Item.SourceResourceId : GetResourceId();
                DateTime currentTime = DateTime.Now;
                TimeSpan timeSpan    = DateTime.UtcNow - new DateTime(1970, 1, 1);
                int offset           = (int)timeSpan.TotalSeconds;
                string targetDb      = "";
                if (ParameterSetName == RpParameterSet)
                {
                    Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(RecoveryPoint.Id);
                    string itemUri = HelperUtils.GetProtectedItemUri(keyValueDict, RecoveryPoint.Id);
                    targetDb       = itemUri.Split(new string[] { ";" }, StringSplitOptions.None)[2];
                }

                if (OriginalWorkloadRestore.IsPresent)
                {
                    azureWorkloadRecoveryConfig.RestoredDBName = Item != null ?
                                                                 ((AzureWorkloadSQLDatabaseProtectedItem)Item).FriendlyName : targetDb;
                    azureWorkloadRecoveryConfig.OverwriteWLIfpresent = "No";
                    azureWorkloadRecoveryConfig.NoRecoveryMode       = "Disabled";
                    azureWorkloadRecoveryConfig.ContainerId          = Item != null ?
                                                                       GetContainerId(Item.Id) : GetContainerId(GetItemId(RecoveryPoint.Id));
                }
                else if (AlternateWorkloadRestore.IsPresent && Item == null)
                {
                    if (string.Compare(((AzureWorkloadProtectableItem)TargetItem).ProtectableItemType,
                                       ProtectableItemType.SQLInstance.ToString()) != 0)
                    {
                        throw new ArgumentException(string.Format(Resources.AzureWorkloadRestoreProtectableItemException));
                    }

                    azureWorkloadRecoveryConfig.RestoredDBName =
                        GetRestoredDBName(RecoveryPoint.ItemName, currentTime);
                    azureWorkloadRecoveryConfig.OverwriteWLIfpresent  = "No";
                    azureWorkloadRecoveryConfig.NoRecoveryMode        = "Disabled";
                    List <SQLDataDirectoryMapping> targetPhysicalPath = new List <SQLDataDirectoryMapping>();

                    //Get target workload item
                    ODataQuery <BMSWorkloadItemQueryObject> queryParams = null;
                    string backupManagementType = ServiceClientModel.BackupManagementType.AzureWorkload;
                    queryParams = new ODataQuery <BMSWorkloadItemQueryObject>(
                        q => q.WorkloadItemType == WorkloadItemType.SQLInstance &&
                        q.BackupManagementType == backupManagementType);

                    var itemResponses = ServiceClientAdapter.ListWorkloadItem(
                        TargetItem.ContainerName,
                        queryParams,
                        vaultName: vaultName,
                        resourceGroupName: resourceGroupName);

                    foreach (var itemResponse in itemResponses)
                    {
                        if (string.Compare(((AzureVmWorkloadSQLInstanceWorkloadItem)itemResponse.Properties).FriendlyName,
                                           ((AzureWorkloadProtectableItem)TargetItem).FriendlyName) == 0 &&
                            string.Compare(((AzureVmWorkloadSQLInstanceWorkloadItem)itemResponse.Properties).ServerName,
                                           ((AzureWorkloadProtectableItem)TargetItem).ServerName) == 0)
                        {
                            string itemId = GetItemId(RecoveryPoint.Id);
                            IList <SQLDataDirectory> dataDirectoryPaths = GetRpDetails(vaultName, resourceGroupName);
                            foreach (var dataDirectoryPath in dataDirectoryPaths)
                            {
                                targetPhysicalPath.Add(new SQLDataDirectoryMapping()
                                {
                                    MappingType       = dataDirectoryPath.Type,
                                    SourceLogicalName = dataDirectoryPath.LogicalName,
                                    SourcePath        = dataDirectoryPath.Path,
                                    TargetPath        = GetTargetPath(dataDirectoryPath.Path, dataDirectoryPath.LogicalName, dataDirectoryPath.Type,
                                                                      ((AzureVmWorkloadSQLInstanceWorkloadItem)itemResponse.Properties).DataDirectoryPaths
                                                                      as List <SQLDataDirectory>, offset)
                                });
                            }
                            break;
                        }
                    }
                    azureWorkloadRecoveryConfig.targetPhysicalPath = targetPhysicalPath;
                    azureWorkloadRecoveryConfig.ContainerId        = GetContainerId(TargetItem.Id);
                }
                else if (Item != null && TargetItem != null)
                {
                    if (string.Compare(((AzureWorkloadProtectableItem)TargetItem).ProtectableItemType,
                                       ProtectableItemType.SQLDataBase.ToString()) == 0)
                    {
                        throw new ArgumentException(string.Format(Resources.AzureWorkloadRestoreProtectableItemException));
                    }

                    azureWorkloadRecoveryConfig.RestoredDBName =
                        GetRestoredDBName(Item.Name, currentTime);
                    azureWorkloadRecoveryConfig.OverwriteWLIfpresent  = "No";
                    azureWorkloadRecoveryConfig.NoRecoveryMode        = "Disabled";
                    List <SQLDataDirectoryMapping> targetPhysicalPath = new List <SQLDataDirectoryMapping>();

                    //Get target workload item
                    ODataQuery <BMSWorkloadItemQueryObject> queryParams = null;
                    string backupManagementType = ServiceClientModel.BackupManagementType.AzureWorkload;
                    queryParams = new ODataQuery <BMSWorkloadItemQueryObject>(
                        q => q.WorkloadItemType == WorkloadItemType.SQLInstance &&
                        q.BackupManagementType == backupManagementType);

                    var itemResponses = ServiceClientAdapter.ListWorkloadItem(
                        TargetItem.ContainerName,
                        queryParams,
                        vaultName: vaultName,
                        resourceGroupName: resourceGroupName);

                    foreach (var itemResponse in itemResponses)
                    {
                        if (string.Compare(((AzureVmWorkloadSQLInstanceWorkloadItem)itemResponse.Properties).FriendlyName,
                                           ((AzureWorkloadProtectableItem)TargetItem).FriendlyName) == 0 &&
                            string.Compare(((AzureVmWorkloadSQLInstanceWorkloadItem)itemResponse.Properties).ServerName,
                                           ((AzureWorkloadProtectableItem)TargetItem).ServerName) == 0)
                        {
                            List <SQLDataDirectory> dataDirectory = GetDataDirectory(vaultName, resourceGroupName, Item.Id, PointInTime);
                            foreach (var dataDirectoryPath in dataDirectory)
                            {
                                targetPhysicalPath.Add(new SQLDataDirectoryMapping()
                                {
                                    MappingType       = dataDirectoryPath.Type,
                                    SourceLogicalName = dataDirectoryPath.LogicalName,
                                    SourcePath        = dataDirectoryPath.Path,
                                    TargetPath        = GetTargetPath(dataDirectoryPath.Path, dataDirectoryPath.LogicalName, dataDirectoryPath.Type,
                                                                      ((AzureVmWorkloadSQLInstanceWorkloadItem)itemResponse.Properties).DataDirectoryPaths
                                                                      as List <SQLDataDirectory>, offset)
                                });
                            }
                            break;
                        }
                    }

                    azureWorkloadRecoveryConfig.targetPhysicalPath = targetPhysicalPath;
                    azureWorkloadRecoveryConfig.ContainerId        = GetContainerId(TargetItem.Id);
                }
                RecoveryConfigBase baseobj = azureWorkloadRecoveryConfig;
                WriteObject(baseobj);
            });
        }