예제 #1
0
        /// <summary>
        /// Validates and converts input paramters into <see cref="ExportParameters"/> class.
        /// </summary>
        /// <param name="parameters">Collection of input parameters stored in key value pairs
        /// <example> Operation "Export" SourceSubscriptionID "5d14d4a2-8c5a-4fc5-8d7d-86efb48b3a07" SourceDCName "East Asia"
        /// ExportMetadataFolderPath "D:\\DataCenterMigration" SourcePublishSettingsFilePath  "D:\\PublishSettings.PublishSettings"
        /// QuietMode "True" </example> </param>
        /// <returns>Parameters required for export functionality</returns>
        internal static ExportParameters ValidateAndConvertExportParameters(IDictionary <string, string> parameters)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted);
            Logger.Info(methodName, ProgressResources.ParameterValidationStarted);

            #region Check for missing required parameters
            // PublishSettingsFilePath / CertificateThumbprint
            if (!parameters.Keys.Contains(Constants.Parameters.SourcePublishSettingsFilePath) && !parameters.Keys.Contains(
                    Constants.Parameters.SourceCertificateThumbprint))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source,
                                                            Constants.AppConfigArguments));
            }

            // ExportMetadataFolderPath
            if (!parameters.Keys.Contains(Constants.Parameters.ExportMetadataFolderPath))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.ExportMetadataFolderPath, Constants.AppConfigArguments));
            }
            // SourceSubscriptionID
            if (!parameters.Keys.Contains(Constants.Parameters.SourceSubscriptionID))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.SourceSubscriptionID, Constants.AppConfigArguments));
            }

            // SourceDCName
            if (!parameters.Keys.Contains(Constants.Parameters.SourceDCName))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.SourceDCName, Constants.AppConfigArguments));
            }

            #endregion

            #region Check for null or empty values

            // PublishSettingsFilePath
            if ((parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourcePublishSettingsFilePath])) ||
                (parameters.ContainsKey(Constants.Parameters.SourceCertificateThumbprint) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourceCertificateThumbprint])))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source, Constants.AppConfigArguments));
            }

            // ExportMetadataFolderPath
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.ExportMetadataFolderPath]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.ExportMetadataFolderPath));
            }
            // SourceSubscriptionID
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.SourceSubscriptionID]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.SourceSubscriptionID));
            }

            // SourceDCName
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.SourceDCName]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.SourceDCName));
            }

            #endregion

            #region Validate parameter's value

            // Validate PublishSettingsFilePath
            if (parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && !File.Exists(parameters[Constants.Parameters.SourcePublishSettingsFilePath]))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue, Constants.Parameters.SourcePublishSettingsFilePath),
                                              new FileNotFoundException(StringResources.PublishSettingsFilePathParamException));
            }

            // Validate ExportMetadataFolderPath
            string folderPath = parameters[Constants.Parameters.ExportMetadataFolderPath];

            if (!Directory.Exists(parameters[Constants.Parameters.ExportMetadataFolderPath]))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue, Constants.Parameters.ExportMetadataFolderPath),
                                              new DirectoryNotFoundException(StringResources.ExportMetadataFolderPathParamException));
            }

            int    retryCount;
            Double minBackOff;
            Double maxBackOff;
            Double deltaBackOff;
            bool   generateMapperXmlValue;

            if (!parameters.Keys.Contains(Constants.Parameters.RetryCount))
            {
                retryCount = Int32.Parse(Constants.RetryCountDefault);
            }
            else
            {
                Int32.TryParse(parameters[Constants.Parameters.RetryCount], out retryCount);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MinBackoff))
            {
                minBackOff = Double.Parse(Constants.MinBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MinBackoff], out minBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MaxBackoff))
            {
                maxBackOff = Double.Parse(Constants.MaxBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MaxBackoff], out maxBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.DeltaBackoff))
            {
                deltaBackOff = Double.Parse(Constants.DeltaBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.DeltaBackoff], out deltaBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.GenerateMapperXml))
            {
                generateMapperXmlValue = false;
            }
            else
            {
                bool.TryParse(parameters[Constants.Parameters.GenerateMapperXml], out generateMapperXmlValue);
            }

            // Validate SourceSubscriptionID
            PublishSettings publishSettingsFile = null;
            PublishSetting  subscription        = null;
            if (parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath))
            {
                try
                {
                    var fileContents = File.ReadAllText(parameters[Constants.Parameters.SourcePublishSettingsFilePath]);
                    publishSettingsFile = new PublishSettings(fileContents);
                }
                catch (Exception xmlEx)
                {
                    throw new ValidationException(string.Format(StringResources.XMLParsingException + " =>" + xmlEx.Message,
                                                                parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }

                // Check whether subscriptionId exists in publish settings file
                subscription = publishSettingsFile.Subscriptions.FirstOrDefault(a =>
                                                                                string.Compare(a.Id, parameters[Constants.Parameters.SourceSubscriptionID], StringComparison.InvariantCultureIgnoreCase) == 0);
                if (subscription == null)
                {
                    throw new ValidationException(string.Format(StringResources.SubscriptionIdParamException,
                                                                parameters[Constants.Parameters.SourceSubscriptionID], parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }
            }
            else
            {
                string           thumbprint  = parameters[Constants.Parameters.SourceCertificateThumbprint];
                X509Certificate2 certificate = GetStoreCertificate(thumbprint);
                if (!certificate.HasPrivateKey)
                {
                    throw new ValidationException(string.Format(
                                                      StringResources.MissingPrivateKeyInCertificate, StringResources.Source, thumbprint));
                }
                var sourceCredentials = new CertificateCloudCredentials(parameters[Constants.Parameters.SourceSubscriptionID],
                                                                        certificate);
                string sourceSubscriptionName = null;
                using (var client = new ManagementClient(sourceCredentials))
                {
                    BaseParameters baseParams = new BaseParameters()
                    {
                        RetryCount   = retryCount,
                        MinBackOff   = TimeSpan.FromSeconds(minBackOff),
                        MaxBackOff   = TimeSpan.FromSeconds(maxBackOff),
                        DeltaBackOff = TimeSpan.FromSeconds(deltaBackOff)
                    };
                    SubscriptionGetResponse subscriptionResponse = Retry.RetryOperation(() => client.Subscriptions.Get(),
                                                                                        baseParams, ResourceType.None);
                    sourceSubscriptionName = subscriptionResponse.SubscriptionName;
                }
                subscription = new PublishSetting()
                {
                    Id          = parameters[Constants.Parameters.SourceSubscriptionID],
                    Name        = sourceSubscriptionName,
                    ServiceUrl  = new Uri(Constants.ServiceManagementUrlValue),
                    Credentials = sourceCredentials
                };
            }


            List <string> locations = ExportDataCenterLocations(subscription.Credentials, retryCount, minBackOff, maxBackOff, deltaBackOff);
            // Check whether SourceDc name exists in subscription
            bool sourceLocationNamePresent = locations.Any((l => string.Compare(l, parameters[Constants.Parameters.SourceDCName],
                                                                                StringComparison.CurrentCultureIgnoreCase) == 0));

            if (!sourceLocationNamePresent)
            {
                throw new ValidationException(string.Format(StringResources.DCParamException, parameters[Constants.Parameters.SourceDCName]));
            }

            Logger.Info(methodName, ProgressResources.ParametersValidationCompleted);

            #endregion

            // Stores validated parameters in class.
            ExportParameters exportParams = new ExportParameters()
            {
                SourceDCName             = parameters[Constants.Parameters.SourceDCName],
                ExportMetadataFolderPath = Path.Combine(parameters[Constants.Parameters.ExportMetadataFolderPath],
                                                        string.Format(Constants.MetadataFileName, parameters[Constants.Parameters.SourceDCName],
                                                                      DateTime.Now.ToString(Constants.ExportMetadataFileNameFormat))),
                SourceSubscriptionSettings = subscription,
                RetryCount            = retryCount,
                MinBackOff            = TimeSpan.FromSeconds(minBackOff),
                MaxBackOff            = TimeSpan.FromSeconds(maxBackOff),
                DeltaBackOff          = TimeSpan.FromSeconds(deltaBackOff),
                GenerateMapperXml     = generateMapperXmlValue,
                DestinationPrefixName = parameters.Keys.Contains(Constants.Parameters.DestinationPrefixName) ?
                                        parameters[Constants.Parameters.DestinationPrefixName].ToLower() : Constants.DestinationPrefixValue,
            };
            return(exportParams);
        }
예제 #2
0
        /// <summary>
        /// Validates and converts input paramters into <see cref="ExportParameters"/> class.
        /// </summary>
        /// <param name="parameters">Collection of input parameters stored in key value pairs 
        /// <example> Operation "Export" SourceSubscriptionID "5d14d4a2-8c5a-4fc5-8d7d-86efb48b3a07" SourceDCName "East Asia" 
        /// ExportMetadataFolderPath "D:\\DataCenterMigration" SourcePublishSettingsFilePath  "D:\\PublishSettings.PublishSettings"
        /// QuietMode "True" </example> </param>
        /// <returns>Parameters required for export functionality</returns>
        internal static ExportParameters ValidateAndConvertExportParameters(IDictionary<string, string> parameters)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted);
            Logger.Info(methodName, ProgressResources.ParameterValidationStarted);

            #region Check for missing required parameters
            // PublishSettingsFilePath / CertificateThumbprint
            if (!parameters.Keys.Contains(Constants.Parameters.SourcePublishSettingsFilePath) && !parameters.Keys.Contains(
                Constants.Parameters.SourceCertificateThumbprint))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source,
                    Constants.AppConfigArguments));
            }

            // ExportMetadataFolderPath
            if (!parameters.Keys.Contains(Constants.Parameters.ExportMetadataFolderPath))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                    Constants.Parameters.ExportMetadataFolderPath, Constants.AppConfigArguments));
            }
            // SourceSubscriptionID
            if (!parameters.Keys.Contains(Constants.Parameters.SourceSubscriptionID))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                    Constants.Parameters.SourceSubscriptionID, Constants.AppConfigArguments));
            }

            // SourceDCName
            if (!parameters.Keys.Contains(Constants.Parameters.SourceDCName))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                    Constants.Parameters.SourceDCName, Constants.AppConfigArguments));
            }

            #endregion

            #region Check for null or empty values

            // PublishSettingsFilePath
            if ((parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourcePublishSettingsFilePath]))
                   || (parameters.ContainsKey(Constants.Parameters.SourceCertificateThumbprint) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourceCertificateThumbprint])))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source, Constants.AppConfigArguments));
            }

            // ExportMetadataFolderPath
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.ExportMetadataFolderPath]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.ExportMetadataFolderPath));
            }
            // SourceSubscriptionID
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.SourceSubscriptionID]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.SourceSubscriptionID));
            }

            // SourceDCName
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.SourceDCName]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.SourceDCName));
            }

            #endregion

            #region Validate parameter's value

            // Validate PublishSettingsFilePath
            if (parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && !File.Exists(parameters[Constants.Parameters.SourcePublishSettingsFilePath]))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue, Constants.Parameters.SourcePublishSettingsFilePath),
                    new FileNotFoundException(StringResources.PublishSettingsFilePathParamException));
            }

            // Validate ExportMetadataFolderPath
            string folderPath = parameters[Constants.Parameters.ExportMetadataFolderPath];

            if (!Directory.Exists(parameters[Constants.Parameters.ExportMetadataFolderPath]))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue, Constants.Parameters.ExportMetadataFolderPath),
                    new DirectoryNotFoundException(StringResources.ExportMetadataFolderPathParamException));
            }

            int retryCount;
            Double minBackOff;
            Double maxBackOff;
            Double deltaBackOff;
            bool generateMapperXmlValue;

            if (!parameters.Keys.Contains(Constants.Parameters.RetryCount))
            {
                retryCount = Int32.Parse(Constants.RetryCountDefault);
            }
            else
            {
                Int32.TryParse(parameters[Constants.Parameters.RetryCount], out retryCount);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MinBackoff))
            {
                minBackOff = Double.Parse(Constants.MinBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MinBackoff], out minBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MaxBackoff))
            {
                maxBackOff = Double.Parse(Constants.MaxBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MaxBackoff], out maxBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.DeltaBackoff))
            {
                deltaBackOff = Double.Parse(Constants.DeltaBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.DeltaBackoff], out deltaBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.GenerateMapperXml))
            {
                generateMapperXmlValue = false;
            }
            else
            {
                bool.TryParse(parameters[Constants.Parameters.GenerateMapperXml], out generateMapperXmlValue);
            }

            // Validate SourceSubscriptionID
            PublishSettings publishSettingsFile = null;
            PublishSetting subscription = null;
            if (parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath))
            {
                try
                {
                    var fileContents = File.ReadAllText(parameters[Constants.Parameters.SourcePublishSettingsFilePath]);
                    publishSettingsFile = new PublishSettings(fileContents);
                }
                catch (Exception xmlEx)
                {
                    throw new ValidationException(string.Format(StringResources.XMLParsingException + " =>" + xmlEx.Message,
                        parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }

                // Check whether subscriptionId exists in publish settings file
                subscription = publishSettingsFile.Subscriptions.FirstOrDefault(a =>
                    string.Compare(a.Id, parameters[Constants.Parameters.SourceSubscriptionID], StringComparison.InvariantCultureIgnoreCase) == 0);
                if (subscription == null)
                {
                    throw new ValidationException(string.Format(StringResources.SubscriptionIdParamException,
                        parameters[Constants.Parameters.SourceSubscriptionID], parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }
            }
            else
            {
                string thumbprint = parameters[Constants.Parameters.SourceCertificateThumbprint];
                X509Certificate2 certificate = GetStoreCertificate(thumbprint);
                if (!certificate.HasPrivateKey)
                {
                    throw new ValidationException(string.Format(
                        StringResources.MissingPrivateKeyInCertificate, StringResources.Source, thumbprint));
                }
                var sourceCredentials = new CertificateCloudCredentials(parameters[Constants.Parameters.SourceSubscriptionID],
                      certificate);
                string sourceSubscriptionName = null;
                using (var client = new ManagementClient(sourceCredentials))
                {
                    BaseParameters baseParams = new BaseParameters()
                    {
                        RetryCount = retryCount,
                        MinBackOff = TimeSpan.FromSeconds(minBackOff),
                        MaxBackOff = TimeSpan.FromSeconds(maxBackOff),
                        DeltaBackOff = TimeSpan.FromSeconds(deltaBackOff)
                    };
                    SubscriptionGetResponse subscriptionResponse = Retry.RetryOperation(() => client.Subscriptions.Get(),
                       baseParams, ResourceType.None);
                    sourceSubscriptionName = subscriptionResponse.SubscriptionName;
                }
                subscription = new PublishSetting()
                {
                    Id = parameters[Constants.Parameters.SourceSubscriptionID],
                    Name = sourceSubscriptionName,
                    ServiceUrl = new Uri(Constants.ServiceManagementUrlValue),
                    Credentials = sourceCredentials
                };
            }

            List<string> locations = ExportDataCenterLocations(subscription.Credentials, retryCount, minBackOff, maxBackOff, deltaBackOff);
            // Check whether SourceDc name exists in subscription
            bool sourceLocationNamePresent = locations.Any((l => string.Compare(l, parameters[Constants.Parameters.SourceDCName],
                StringComparison.CurrentCultureIgnoreCase) == 0));

            if (!sourceLocationNamePresent)
            {
                throw new ValidationException(string.Format(StringResources.DCParamException, parameters[Constants.Parameters.SourceDCName]));
            }

            Logger.Info(methodName, ProgressResources.ParametersValidationCompleted);

            #endregion

            // Stores validated parameters in class.
            ExportParameters exportParams = new ExportParameters()
            {
                SourceDCName = parameters[Constants.Parameters.SourceDCName],
                ExportMetadataFolderPath = Path.Combine(parameters[Constants.Parameters.ExportMetadataFolderPath],
                    string.Format(Constants.MetadataFileName, parameters[Constants.Parameters.SourceDCName],
                    DateTime.Now.ToString(Constants.ExportMetadataFileNameFormat))),
                SourceSubscriptionSettings = subscription,
                RetryCount = retryCount,
                MinBackOff = TimeSpan.FromSeconds(minBackOff),
                MaxBackOff = TimeSpan.FromSeconds(maxBackOff),
                DeltaBackOff = TimeSpan.FromSeconds(deltaBackOff),
                GenerateMapperXml = generateMapperXmlValue,
                DestinationPrefixName = parameters.Keys.Contains(Constants.Parameters.DestinationPrefixName) ?
                                        parameters[Constants.Parameters.DestinationPrefixName].ToLower() : Constants.DestinationPrefixValue,
            };
            return exportParams;
            }
예제 #3
0
 public ResourceExporter(ExportParameters parameters, DCMigrationManager dcMigration)
 {
     // Set export parameters.
     exportParameters = parameters;
     this.dcMigration = dcMigration;
 }