public RollBack(ImportParameters importParameters, Subscription subscription, DCMigrationManager dcMigrationManager, ResourceImporter resourceImporter) { this.subscription = subscription; this.importParameters = importParameters; this.dcMigrationManager = dcMigrationManager; this.resourceImporter = resourceImporter; }
/// <summary> /// return xml string for the resource name mapping /// </summary> /// <param name="subscription"></param> /// <param name="destinationPrefixValue"></param> /// <returns></returns> internal string GenerateMapperXml(Subscription subscription, string destinationPrefixValue) { foreach (var datacenter in subscription.DataCenters) { foreach (var affinityGroup in datacenter.AffinityGroups) { GenerateNewResourceName(ResourceType.AffinityGroup, affinityGroup.AffinityGroupDetails.Name, destinationPrefixValue); } foreach (var storageAccount in datacenter.StorageAccounts) { GenerateNewResourceName(ResourceType.StorageAccount, storageAccount.StorageAccountDetails.Name, destinationPrefixValue); } foreach (var cloudService in datacenter.CloudServices) { GenerateNewResourceName(ResourceType.CloudService, cloudService.CloudServiceDetails.ServiceName, destinationPrefixValue); ResourceNameMapper resourceCloudService = resourceNameCollection[ResourceType.CloudService].Where (s => s.SourceName.Equals(cloudService.CloudServiceDetails.ServiceName)).FirstOrDefault(); if (cloudService.DeploymentDetails != null) { if (resourceCloudService.ChildResources == null) { resourceCloudService.ChildResources = new List<ResourceNameMapper>(); } ResourceNameMapper resourceDeployment = new ResourceNameMapper { SourceName = cloudService.DeploymentDetails.Name, DestinationName = GenerateNewResourceName(ResourceType.Deployment, cloudService.DeploymentDetails.Name, destinationPrefixValue, false), //Import = true, ResourceType = ResourceType.Deployment.ToString() }; foreach (var virtualMachine in cloudService.DeploymentDetails.VirtualMachines) { ResourceNameMapper resourceVirtaulMachine = new ResourceNameMapper { SourceName = virtualMachine.VirtualMachineDetails.RoleName, DestinationName = virtualMachine.VirtualMachineDetails.RoleName, // Import = true, ResourceType = ResourceType.VirtualMachine.ToString() }; resourceVirtaulMachine.ChildResources.Add(new ResourceNameMapper { SourceName = virtualMachine.VirtualMachineDetails.OSVirtualHardDisk.Name, DestinationName = string.Format("{0}{1}", destinationPrefixValue, virtualMachine.VirtualMachineDetails.OSVirtualHardDisk.Name), //Import = true, ResourceType = ResourceType.OSDisk.ToString() }); foreach (var disk in virtualMachine.VirtualMachineDetails.DataVirtualHardDisks) { resourceVirtaulMachine.ChildResources.Add(new ResourceNameMapper { SourceName = disk.Name, DestinationName = string.Format("{0}{1}", destinationPrefixValue, disk.Name), // Import = true, ResourceType = ResourceType.HardDisk.ToString() }); } resourceDeployment.ChildResources.Add(resourceVirtaulMachine); } resourceCloudService.ChildResources.Add( resourceDeployment); } } #region Network Configuration if (datacenter.NetworkConfiguration != null && datacenter.NetworkConfiguration.VirtualNetworkConfiguration != null) { if (datacenter.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites != null) { foreach (var virtualNetworkSite in datacenter.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites) { GenerateNewResourceName(ResourceType.VirtualNetworkSite, virtualNetworkSite.name, destinationPrefixValue); ResourceNameMapper resourceVirtualNetworkSite = resourceNameCollection[ResourceType.VirtualNetworkSite].Where (s => s.SourceName.Equals(virtualNetworkSite.name)).FirstOrDefault(); if (resourceVirtualNetworkSite.ChildResources == null) { resourceVirtualNetworkSite.ChildResources = new List<ResourceNameMapper>(); } if (virtualNetworkSite.DnsServersRef != null) { foreach (var dns in virtualNetworkSite.DnsServersRef) { ResourceNameMapper resourceDNS = new ResourceNameMapper { SourceName = dns.name, DestinationName = GenerateNewResourceName(ResourceType.DnsServer, dns.name, destinationPrefixValue, false), //Import = true, ResourceType = ResourceType.DnsServer.ToString() }; resourceVirtualNetworkSite.ChildResources.Add(resourceDNS); } } if (virtualNetworkSite.Gateway != null && virtualNetworkSite.Gateway.ConnectionsToLocalNetwork != null && virtualNetworkSite.Gateway.ConnectionsToLocalNetwork.LocalNetworkSiteRef != null) { ResourceNameMapper resourceLocalNetwork = new ResourceNameMapper { SourceName = virtualNetworkSite.Gateway.ConnectionsToLocalNetwork.LocalNetworkSiteRef.name, DestinationName = GenerateNewResourceName(ResourceType.LocalNetworkSite, virtualNetworkSite.Gateway.ConnectionsToLocalNetwork.LocalNetworkSiteRef.name, destinationPrefixValue, false), //Import = true, ResourceType = ResourceType.LocalNetworkSite.ToString() }; resourceVirtualNetworkSite.ChildResources.Add(resourceLocalNetwork); } } } } #endregion } var xmlRoot = new XElement("Resources", new XAttribute(Constants.Parameters.DestinationPrefixName, destinationPrefixValue)); foreach (var item in resourceNameCollection) { XElement child = new XElement(item.Key + "s"); foreach (var resourceMapper in item.Value) { GetAllChildResources(resourceMapper, child); } xmlRoot.Add(child); } return (new XDocument(xmlRoot)).ToString(); }
/// <summary> /// Exports Subscription metadata. /// </summary> /// <returns>Subscription details/></returns> internal Subscription ExportSubscriptionMetadata() { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; Logger.Info(methodName, ProgressResources.ExecutionStarted); Subscription subscription = new Subscription() { Name = exportParameters.SourceSubscriptionSettings.Name }; Logger.Info(methodName, string.Format(ProgressResources.ExportDataCenterStarted, exportParameters.SourceDCName), ResourceType.DataCenter.ToString(), exportParameters.SourceDCName); dcMigration.ReportProgress(string.Format(ProgressResources.ExportDataCenterStarted, exportParameters.SourceDCName)); AffinityGroupListResponse affinityGroupResponse = GetAffinityGroupListResponseFromMSAzure(exportParameters.SourceSubscriptionSettings.Credentials); HostedServiceListResponse cloudserviceResponse = GetCloudServiceListResponseFromMSAzure(exportParameters.SourceSubscriptionSettings.Credentials, exportParameters.SourceSubscriptionSettings.ServiceUrl); NetworkGetConfigurationResponse networkResponse = GetNetworkConfigurationFromMSAzure(exportParameters.SourceSubscriptionSettings.Credentials, exportParameters.SourceSubscriptionSettings.ServiceUrl); StorageAccountListResponse storageAccountResponse = GetStorageAccountListResponseFromMSAzure(exportParameters.SourceSubscriptionSettings.Credentials); // Create an instance of data center. var dataCenter = new DataCenter { LocationName = exportParameters.SourceDCName, }; // Get all affinity groups. Logger.Info(methodName, ProgressResources.ExportAffinityGroupStarted, ResourceType.AffinityGroup.ToString()); dcMigration.ReportProgress(ProgressResources.ExportAffinityGroupStarted); var affinityGroups = ExportAffinityGroups(affinityGroupResponse); dataCenter.AffinityGroups.AddRange(affinityGroups.ToList()); int stageCount = 1; Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, stageCount, Constants.ExportTotalStages)); dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, stageCount, Constants.ExportTotalStages)); List<string> affinityGroupNamesInDC = affinityGroups.Select(ag => ag.AffinityGroupDetails.Name).ToList(); Logger.Info(methodName, ProgressResources.ExportVNetConfigurationStarted, ResourceType.VirtualNetwork.ToString()); dcMigration.ReportProgress(ProgressResources.ExportVNetConfigurationStarted); // Filter and Export network configuration file. dataCenter.NetworkConfiguration = ExportVNetConfiguration(networkResponse, affinityGroupNamesInDC); Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ExportTotalStages)); dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, stageCount, Constants.ExportTotalStages)); Logger.Info(methodName, ProgressResources.ExportCloudServicesStarted, ResourceType.CloudService.ToString()); dcMigration.ReportProgress(ProgressResources.ExportCloudServicesStarted); // Get cloud services for affinityGroupNamesInDC or for SourceDCName dataCenter.CloudServices.AddRange(ExportCloudServices(affinityGroupNamesInDC, cloudserviceResponse)); Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ExportTotalStages)); dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, stageCount, Constants.ExportTotalStages)); Logger.Info(methodName, ProgressResources.ExportStorageAccountStarted, ResourceType.StorageAccount.ToString()); dcMigration.ReportProgress(ProgressResources.ExportStorageAccountStarted); // Get list of storage accounts dataCenter.StorageAccounts.AddRange(ExportStorageAccounts(affinityGroupNamesInDC, storageAccountResponse)); Logger.Info(methodName, string.Format(ProgressResources.CompletedStages, ++stageCount, Constants.ExportTotalStages)); dcMigration.ReportProgress(string.Format(ProgressResources.CompletedStages, stageCount, Constants.ExportTotalStages)); // Add the data center into subscription. subscription.DataCenters.Add(dataCenter); Logger.Info(methodName, string.Format(ProgressResources.ExportDataCenterCompleted, dataCenter.LocationName), ResourceType.DataCenter.ToString()); Logger.Info(methodName, ProgressResources.ExecutionCompleted); return subscription; }
/// <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; } }