Exemple #1
0
 /// <summary>
 /// Constructor class
 /// </summary>
 /// <param name="parameters">Input parameters for import functionality.</param>
 /// <param name="dcMigration">DCMigration class object to report progress.</param>
 public ResourceImporter(ImportParameters parameters, DCMigrationManager dcMigration)
 {
     // Set export parameters.
     importParameters = parameters;
     this.dcMigration = dcMigration;
     helper = new ResourceNameMapperHelper();
 }
Exemple #2
0
        /// <summary>
        /// Exports information about source subscription and stores the metadata into 'SourceDataCenterName-MM-DD-YYYY-hh-mm.json format
        /// on specified ExportMetadataFolderPath.
        /// </summary>
        /// <param name="parameters"> Collection of key value paired input parameters <example> Operation "Export"
        /// SourceSubscriptionID "5d14d4a2-8c5a-4fc5-8d7d-86efb48b3a07" SourceDCName "East Asia" ExportMetadataFolderPath "D:\\DataCenterMigration"
        /// SourcePublishSettingsFilePath "D:\\PublishSettings.PublishSettings" SourceCertificateThumbprint "2180d782768926ee0e5ddbcc6e8d2efa8ddb98c7"
        /// QuietMode "True" GenerateMapperXml "True" RetryCount "5" MinBackoff "3" MaxBackoff "3" DeltaBackoff "90" </example> </param>
        public void ExportSubscriptionMetadata(IDictionary <string, string> parameters)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted);
            bool boolValue;

            if (!parameters.Keys.Contains(Constants.Parameters.QuietMode))
            {
                quietMode = false;
            }
            else
            {
                quietMode = bool.TryParse(parameters[Constants.Parameters.QuietMode], out boolValue) ? boolValue : false;
            }

            ReportProgress(ProgressResources.ExportMetadataStarted);
            Logger.Info(methodName, ProgressResources.ExportMetadataStarted);

            // Validate the input paramters and export them into parameters class.
            ExportParameters exportParameters = ConfigurationReader.ValidateAndConvertExportParameters(parameters);

            // Export metadata.
            ResourceExporter exporter     = new ResourceExporter(exportParameters, this);
            Subscription     subscription = exporter.ExportSubscriptionMetadata();

            // Export metadata into json file.
            File.WriteAllText(exportParameters.ExportMetadataFolderPath,
                              JsonConvert.SerializeObject(subscription, Formatting.Indented));

            if (exportParameters.GenerateMapperXml)
            {
                ResourceNameMapperHelper resourceHelper = new ResourceNameMapperHelper();
                File.WriteAllText(
                    Path.ChangeExtension(exportParameters.ExportMetadataFolderPath, Constants.MapperFileExtension),
                    resourceHelper.GenerateMapperXml(subscription, exportParameters.DestinationPrefixName));
            }

            ReportProgress(string.Format(ProgressResources.ExportMetadataCompleted, exportParameters.ExportMetadataFolderPath));
            Logger.Info(methodName, string.Format(ProgressResources.ExportMetadataCompleted, exportParameters.ExportMetadataFolderPath));
            Logger.Info(methodName, ProgressResources.ExecutionCompleted);
        }
Exemple #3
0
        /// <summary>
        /// Exports information about source subscription and stores the metadata into 'SourceDataCenterName-MM-DD-YYYY-hh-mm.json format 
        /// on specified ExportMetadataFolderPath.
        /// </summary>
        /// <param name="parameters"> Collection of key value paired input parameters <example> Operation "Export" 
        /// SourceSubscriptionID "5d14d4a2-8c5a-4fc5-8d7d-86efb48b3a07" SourceDCName "East Asia" ExportMetadataFolderPath "D:\\DataCenterMigration" 
        /// SourcePublishSettingsFilePath "D:\\PublishSettings.PublishSettings" SourceCertificateThumbprint "2180d782768926ee0e5ddbcc6e8d2efa8ddb98c7" 
        /// QuietMode "True" GenerateMapperXml "True" RetryCount "5" MinBackoff "3" MaxBackoff "3" DeltaBackoff "90" </example> </param>
        public void ExportSubscriptionMetadata(IDictionary<string, string> parameters)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            Logger.Info(methodName, ProgressResources.ExecutionStarted);
            bool boolValue;
            if (!parameters.Keys.Contains(Constants.Parameters.QuietMode))
            {
                quietMode = false;
            }
            else
            {
                quietMode = bool.TryParse(parameters[Constants.Parameters.QuietMode], out boolValue) ? boolValue : false;
            }

            ReportProgress(ProgressResources.ExportMetadataStarted);
            Logger.Info(methodName, ProgressResources.ExportMetadataStarted);

            // Validate the input paramters and export them into parameters class.
            ExportParameters exportParameters = ConfigurationReader.ValidateAndConvertExportParameters(parameters);

            // Export metadata.
            ResourceExporter exporter = new ResourceExporter(exportParameters, this);
            Subscription subscription = exporter.ExportSubscriptionMetadata();

            // Export metadata into json file.
            File.WriteAllText(exportParameters.ExportMetadataFolderPath,
               JsonConvert.SerializeObject(subscription, Formatting.Indented));

            if (exportParameters.GenerateMapperXml)
            {
                ResourceNameMapperHelper resourceHelper = new ResourceNameMapperHelper();
                File.WriteAllText(
                    Path.ChangeExtension(exportParameters.ExportMetadataFolderPath, Constants.MapperFileExtension),
                    resourceHelper.GenerateMapperXml(subscription, exportParameters.DestinationPrefixName));
            }

            ReportProgress(string.Format(ProgressResources.ExportMetadataCompleted, exportParameters.ExportMetadataFolderPath));
            Logger.Info(methodName, string.Format(ProgressResources.ExportMetadataCompleted, exportParameters.ExportMetadataFolderPath));
            Logger.Info(methodName, ProgressResources.ExecutionCompleted);
        }
Exemple #4
0
        /// <summary>
        /// Import Subscription metadata.
        /// </summary>
        internal void ImportSubscriptionMetadata()
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string importFileContents = File.ReadAllText(importParameters.ImportMetadataFilePath);

            try
            {
                Logger.Info(methodName, string.Format(ProgressResources.ImportDataCenterStarted, importParameters.DestinationDCName),
                    ResourceType.DataCenter.ToString(), importParameters.DestinationDCName);
                dcMigration.ReportProgress(string.Format(ProgressResources.ImportDataCenterStarted, importParameters.DestinationDCName));

                // Deserialize the import metadata file contents
                destSubscriptionMetadata = JsonConvert.DeserializeObject<Subscription>(importFileContents);

                // Copy of deserialize data to update metadata file with import status
                sourceSubscriptionMetadata = JsonConvert.DeserializeObject<Subscription>(importFileContents);

                #region Mapper Xml
                if (String.IsNullOrEmpty(importParameters.MapperXmlFilePath))
                {
                    ResourceNameMapperHelper resourceHelper = new ResourceNameMapperHelper();
                    importParameters.MapperXmlFilePath =
                        Path.ChangeExtension(importParameters.ImportMetadataFilePath, Constants.MapperFileExtension);
                    File.WriteAllText(importParameters.MapperXmlFilePath,
                        resourceHelper.GenerateMapperXml(destSubscriptionMetadata, importParameters.DestinationPrefixName));
                }
                string destinationPrefixValue;

                resourceNameCollection = helper.GetDestinationResourceNames(
                    importParameters.MapperXmlFilePath, out destinationPrefixValue);

                importParameters.DestinationPrefixName = destinationPrefixValue;
                #endregion

                // If Resume Import false, make a copy of metadata file for import status update
                if (!importParameters.ResumeImport)
                {
                    // Generate new metadata file name
                    importParameters.ImportMetadataFilePath = Path.Combine(
                        Path.GetDirectoryName(importParameters.ImportMetadataFilePath),
                        string.Format(Constants.MetadataFileNewName,
                            Path.GetFileNameWithoutExtension(importParameters.ImportMetadataFilePath)));

                    // Make a copy of metadata file
                    File.WriteAllText(importParameters.ImportMetadataFilePath,
                              JsonConvert.SerializeObject(sourceSubscriptionMetadata, Newtonsoft.Json.Formatting.Indented));
                }

                // Validate Metadata file resources.
                dcMigration.ReportProgress(ProgressResources.ValidateMetadataFileResources);
                int stageCount = 1;
                ChangeAndValidateMetadataFileResources();

                dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, stageCount, Constants.ImportTotalStages));
                Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, stageCount, Constants.ImportTotalStages));
                //Create Destination Resources
                foreach (var datacenter in destSubscriptionMetadata.DataCenters)
                {
                    // Check if all reources are already imported
                    if (!datacenter.IsImported)
                    {
                        AffinityGroupListResponse affinityGroupResponse = GetAffinityGroupListResponseFromMSAzure(
                            importParameters.DestinationSubscriptionSettings.Credentials);
                        // Create affinity groups
                        dcMigration.ReportProgress(ProgressResources.CreateAffinityGroups);
                        CreateAffinityGroups(datacenter.AffinityGroups, affinityGroupResponse);
                        dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ImportTotalStages));
                        Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, stageCount, Constants.ImportTotalStages));

                        // Create storage accounts
                        dcMigration.ReportProgress(ProgressResources.CreateStorageAccounts);
                        CreateStorageAccounts(datacenter.StorageAccounts);
                        dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ImportTotalStages));
                        Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, stageCount, Constants.ImportTotalStages));

                        // Copy all blobs to destination
                        dcMigration.ReportProgress(ProgressResources.CopyAllBlobsToDestination);
                        ShutDownVMsAndCopyBlobToDestination();
                        dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ImportTotalStages));
                        Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, stageCount, Constants.ImportTotalStages));

                        // Create virtual networks with local networks and DNS servers
                        dcMigration.ReportProgress(ProgressResources.CreateVirtualNetworks);
                        CreateVirtualNetworks(datacenter.NetworkConfiguration);
                        dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ImportTotalStages));
                        Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, stageCount, Constants.ImportTotalStages));

                        // Create cloud services and there deployments
                        dcMigration.ReportProgress(ProgressResources.CreateCloudServices);
                        CreateCloudServices(datacenter.CloudServices);
                        dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ImportTotalStages));
                        Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, stageCount, Constants.ImportTotalStages));
                        //  throw new Exception();
                        // Update datacenter status as imported after successful import and update metadata file
                        datacenter.IsImported = true;
                        UpdateMedatadaFile(ResourceType.DataCenter);
                        Logger.Info(methodName, string.Format(ProgressResources.ImportDataCenterCompleted, importParameters.DestinationDCName));
                    }
                }
            }
            catch (ValidationException vex)
            {
                throw vex;
            }
            catch
            {
                // Update Metadata File.
                UpdateMedatadaFile(ResourceType.None);

                if (importParameters.RollBackOnFailure)
                {
                    RollBack rollback = new RollBack(importParameters, sourceSubscriptionMetadata, dcMigration, this);
                    rollback.RollBackResources();
                }
                // If Resume Import false, Inform user about updated import status file
                else if (!importParameters.ResumeImport)
                {
                    dcMigration.ReportProgress(string.Format(ProgressResources.ResumeImport, importParameters.ImportMetadataFilePath));
                }
                throw;
            }
        }