/// <summary> /// Gets list of hosted service operation response from MS azure using API call. /// </summary> /// <param name="credentials">credentials</param> /// <param name="serviceUrl">serviceUrl</param> /// <returns>List of hosted service operation response for subscription </returns> private HostedServiceListResponse GetCloudServiceListResponseFromMSAzure(SubscriptionCloudCredentials credentials, Uri serviceUrl) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; Logger.Info(methodName, ProgressResources.GetServicesFromMSAzureStarted, ResourceType.CloudService.ToString()); dcMigration.ReportProgress(ProgressResources.GetServicesFromMSAzureStarted); try { using (var client = new ComputeManagementClient(credentials, serviceUrl)) { ////Call management API to get list of CloudServices. ////HostedServiceListResponse serviceResponse = Retry.RetryOperation(() => client.HostedServices.List(), exportParameters.RetryCount, exportParameters.MinBackOff, exportParameters.MaxBackOff, exportParameters.DeltaBackOff, ResourceType.CloudService); HostedServiceListResponse serviceResponse = Retry.RetryOperation(() => client.HostedServices.List(), (BaseParameters)exportParameters, ResourceType.CloudService); Logger.Info(methodName, ProgressResources.GetServicesFromMSAzureCompleted, ResourceType.CloudService.ToString()); return(serviceResponse); } } catch (Exception ex) { Logger.Error(methodName, ex, ResourceType.CloudService.ToString()); throw; } }
internal static void LogObject(this TestEasyLog log, HostedServiceListResponse.HostedService hostedService) { if (hostedService == null) return; if (!string.IsNullOrEmpty(hostedService.ServiceName)) { log.Info(string.Format("HostedService Name:{0}", hostedService.ServiceName)); } log.Info(string.Format("HostedService Url:{0}", hostedService.Uri)); LogObject(log, hostedService.Properties); }
/// <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> /// Exports list of production cloud services. /// </summary> /// <param name="dcAffinityGroupNames">List of affinity group names for which cloud service to be filtered out</param> /// <param name="services">Hosted service response to filter services by SourceDC name</param> /// <returns>List of production cloud services</returns> private List <CloudService> ExportCloudServices(List <string> dcAffinityGroupNames, HostedServiceListResponse services) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; Logger.Info(methodName, ProgressResources.ExecutionStarted, ResourceType.CloudService.ToString()); List <CloudService> cloudServices = new List <CloudService>(); using (var client = new ComputeManagementClient(exportParameters.SourceSubscriptionSettings.Credentials, exportParameters.SourceSubscriptionSettings.ServiceUrl)) { DeploymentGetResponse deployment = null; CloudService cloudService = null; foreach (var service in services) { if ((string.Compare(service.Properties.Location, exportParameters.SourceDCName, StringComparison.CurrentCultureIgnoreCase) == 0) || (service.Properties.Location == null && (dcAffinityGroupNames.Contains(service.Properties.AffinityGroup)))) { Logger.Info(methodName, string.Format(ProgressResources.ExportCloudServiceStarted, service.ServiceName), ResourceType.CloudService.ToString(), service.ServiceName); try { // Get deployment of production slot for specific service. deployment = client.Deployments.GetBySlot(service.ServiceName, DeploymentSlot.Production); } catch (CloudException ex) { deployment = null; if (string.Compare(ex.Error.ToString(), Constants.ResourceNotFound) != 0) { Logger.Error(methodName, ex, ResourceType.CloudService.ToString(), service.ServiceName); throw; } } finally { //// Create CloudService object if cloud service exist in SourceDCName location. ////Or create when service exist in affinity group and affinity group location matches with SourceDName. var deploymentDetails = deployment != null?ExportDeployment(service.ServiceName, deployment) : null; cloudService = new CloudService { IsImported = false, CloudServiceDetails = service, DeploymentDetails = deploymentDetails, }; cloudServices.Add(cloudService); Logger.Info(methodName, string.Format(ProgressResources.ExportCloudServiceCompleted, service.ServiceName), ResourceType.CloudService.ToString(), service.ServiceName); } } } } Logger.Info(methodName, ProgressResources.ExecutionCompleted, ResourceType.CloudService.ToString()); return(cloudServices); }
/// <summary> /// Exports list of production cloud services. /// </summary> /// <param name="dcAffinityGroupNames">List of affinity group names for which cloud service to be filtered out</param> /// <param name="services">Hosted service response to filter services by SourceDC name</param> /// <returns>List of production cloud services</returns> private List<CloudService> ExportCloudServices(List<string> dcAffinityGroupNames, HostedServiceListResponse services) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; Logger.Info(methodName, ProgressResources.ExecutionStarted, ResourceType.CloudService.ToString()); List<CloudService> cloudServices = new List<CloudService>(); using (var client = new ComputeManagementClient(exportParameters.SourceSubscriptionSettings.Credentials, exportParameters.SourceSubscriptionSettings.ServiceUrl)) { DeploymentGetResponse deployment = null; CloudService cloudService = null; foreach (var service in services) { if ((string.Compare(service.Properties.Location, exportParameters.SourceDCName, StringComparison.CurrentCultureIgnoreCase) == 0) || (service.Properties.Location == null && (dcAffinityGroupNames.Contains(service.Properties.AffinityGroup)))) { Logger.Info(methodName, string.Format(ProgressResources.ExportCloudServiceStarted, service.ServiceName), ResourceType.CloudService.ToString(), service.ServiceName); try { // Get deployment of production slot for specific service. deployment = client.Deployments.GetBySlot(service.ServiceName, DeploymentSlot.Production); } catch (CloudException ex) { deployment = null; if (string.Compare(ex.Error.ToString(), Constants.ResourceNotFound) != 0) { Logger.Error(methodName, ex, ResourceType.CloudService.ToString(), service.ServiceName); throw; } } finally { //// Create CloudService object if cloud service exist in SourceDCName location. ////Or create when service exist in affinity group and affinity group location matches with SourceDName. var deploymentDetails = deployment != null ? ExportDeployment(service.ServiceName, deployment) : null; cloudService = new CloudService { IsImported = false, CloudServiceDetails = service, DeploymentDetails = deploymentDetails, }; cloudServices.Add(cloudService); Logger.Info(methodName, string.Format(ProgressResources.ExportCloudServiceCompleted, service.ServiceName), ResourceType.CloudService.ToString(), service.ServiceName); } } } } Logger.Info(methodName, ProgressResources.ExecutionCompleted, ResourceType.CloudService.ToString()); return cloudServices; }
/// <summary> /// Rename the destination cloud service name and validate it. /// </summary> /// <param name="cloudServices">List of Cloud Services from the metadata</param> /// <param name="cloudserviceResponse">List of Cloud Service Response for destination subscription</param> private void RenameAndValidateDestCloudServiceNames(List<CloudService> cloudServices, HostedServiceListResponse cloudserviceResponse) { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; Logger.Info(methodName, ProgressResources.ExecutionStarted, ResourceType.CloudService.ToString()); if (cloudServices.Select(cs => cs.DeploymentDetails).Where(d => d != null).Where(IP => IP.ReservedIPName != null).Select(resip => resip.ReservedIPName).Distinct().Count() != cloudServices.Select(cs => cs.DeploymentDetails).Where(d => d != null).Where(IP => IP.ReservedIPName != null).Select(resip => resip.ReservedIPName).Count()) { throw new ValidationException(ProgressResources.DuplicateReservedIPName); } foreach (var service in cloudServices) { service.CloudServiceDetails.ServiceName = GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName); if (!service.IsImported) { // Check for service name availability. if (!(CheckServiceNameAvailability(service.CloudServiceDetails.ServiceName, importParameters.DestinationSubscriptionSettings.Credentials, importParameters.DestinationSubscriptionSettings.ServiceUrl))) { throw new ValidationException(string.Format(StringResources.InvalidServiceNameExist, service.CloudServiceDetails.ServiceName)); } } service.CloudServiceDetails.Properties.AffinityGroup = GetDestinationResourceName(ResourceType.AffinityGroup, service.CloudServiceDetails.Properties.AffinityGroup); // Validate Virtual machines parameters. if (service.DeploymentDetails != null) { // Validate for ReservedIPName if (service.DeploymentDetails.ReservedIPName != null) { CheckReservedIPNameAvailability(importParameters.DestinationSubscriptionSettings.Credentials, importParameters.DestinationSubscriptionSettings.ServiceUrl, service.DeploymentDetails.ReservedIPName, service.CloudServiceDetails.ServiceName); } service.DeploymentDetails.VirtualNetworkName = GetDestinationResourceName(ResourceType.VirtualNetworkSite, service.DeploymentDetails.VirtualNetworkName); service.DeploymentDetails.Name = GetDestinationResourceName(ResourceType.Deployment, service.DeploymentDetails.Name, ResourceType.CloudService, service.CloudServiceDetails.ServiceName); RenameAndValidateDestVirtualMachineNames(service.DeploymentDetails.VirtualMachines, service.CloudServiceDetails.ServiceName); } } //Check for unique name in cloudServices. // If count doesn't match then cloud service name is duplicate if (cloudServices.Select(cs => cs.CloudServiceDetails.ServiceName).Distinct().Count() != cloudServices.Count()) { throw new ValidationException(StringResources.DuplicateServiceName); } Logger.Info(methodName, ProgressResources.ExecutionCompleted, ResourceType.CloudService.ToString()); }