/// <summary>
 /// Returns a new data masking rule model
 /// </summary>
 /// <param name="rules">The database's data masking rules</param>
 /// <returns>A data masking rule object, initialized for the user provided rule identity</returns>
 protected override DatabaseDataMaskingRuleModel GetRule(IEnumerable<DatabaseDataMaskingRuleModel> rules)
 {
     DatabaseDataMaskingRuleModel rule = new DatabaseDataMaskingRuleModel();
     rule.ResourceGroupName = ResourceGroupName;
     rule.ServerName = ServerName;
     rule.DatabaseName = DatabaseName;
     return rule;
 }
 /// <summary>
 /// Updates the data masking rule that this cmdlet operated in the list of rules of this database
 /// </summary>
 /// <param name="rules">The data masking rules already defined for this database</param>
 /// <param name="rule">The rule that this cmdlet operated on</param>
 /// <returns>The updated list of data masking rules</returns>
 protected override IEnumerable<DatabaseDataMaskingRuleModel> UpdateRuleList(IEnumerable<DatabaseDataMaskingRuleModel> rules, DatabaseDataMaskingRuleModel rule)
 {
     return rules;
 }
 /// <summary>
 /// Adds the data masking rule that this cmdlet operated on to the list of rules of this database
 /// </summary>
 /// <param name="rules">The data masking rules already defined for this database</param>
 /// <param name="rule">The rule that this cmdlet operated on</param>
 /// <returns>The updated list of data masking rules</returns>
 protected override IEnumerable<DatabaseDataMaskingRuleModel> UpdateRuleList(IEnumerable<DatabaseDataMaskingRuleModel> rules, DatabaseDataMaskingRuleModel rule)
 {
     List<DatabaseDataMaskingRuleModel> rulesList = rules.ToList();
     rulesList.Add(rule);
     return rulesList;
 }
        /// <summary>
        /// Transforms a data masking rule to its cmdlet model representation
        /// </summary>
        private DatabaseDataMaskingRuleModel ModelizeDatabaseDataMaskingRule(DataMaskingRule rule, string resourceGroup, string serverName, string databaseName)
        {
            DatabaseDataMaskingRuleModel dbRuleModel = new DatabaseDataMaskingRuleModel();
            DataMaskingRuleProperties properties = rule.Properties;
            dbRuleModel.ResourceGroupName = resourceGroup;
            dbRuleModel.ServerName = serverName;
            dbRuleModel.DatabaseName = databaseName;
            dbRuleModel.ColumnName = properties.ColumnName;
            dbRuleModel.TableName = properties.TableName;
            dbRuleModel.SchemaName = properties.SchemaName;
            dbRuleModel.MaskingFunction = ModelizeMaskingFunction(properties.MaskingFunction);
            dbRuleModel.PrefixSize = ModelizeNullableUint(properties.PrefixSize);
            dbRuleModel.ReplacementString = properties.ReplacementString;
            dbRuleModel.SuffixSize = ModelizeNullableUint(properties.SuffixSize);
            dbRuleModel.NumberFrom = ModelizeNullableDouble(properties.NumberFrom);
            dbRuleModel.NumberTo = ModelizeNullableDouble(properties.NumberTo);
            return dbRuleModel;

        }
 /// <summary>
 /// Takes the cmdlets model object and transform it to the policy as expected by the endpoint
 /// </summary>
 /// <param name="model">The data masking Policy model object</param>
 /// <returns>The communication model object</returns>
 private DataMaskingRuleCreateOrUpdateParameters PolicizeDatabaseDataRuleModel(DatabaseDataMaskingRuleModel model)
 {
     DataMaskingRuleCreateOrUpdateParameters updateParameters = new DataMaskingRuleCreateOrUpdateParameters();
     DataMaskingRuleProperties properties = new DataMaskingRuleProperties();
     updateParameters.Properties = properties;
     properties.Id = ExtractDataMaskingRuleId(model);
     properties.TableName = model.TableName;
     properties.SchemaName = model.SchemaName;
     properties.ColumnName = model.ColumnName;
     properties.MaskingFunction = PolicizeMaskingFunction(model.MaskingFunction);
     properties.PrefixSize = (model.PrefixSize == null) ? null : model.PrefixSize.ToString();
     properties.ReplacementString = model.ReplacementString;
     properties.SuffixSize = (model.SuffixSize == null) ? null : model.SuffixSize.ToString();
     properties.NumberFrom = (model.NumberFrom == null) ? null : model.NumberFrom.ToString();
     properties.NumberTo = (model.NumberTo == null) ? null : model.NumberTo.ToString();
     properties.RuleState = SecurityConstants.Enabled;
     return updateParameters;
 }
 private string ExtractDataMaskingRuleId(DatabaseDataMaskingRuleModel model)
 {
     var baseId = string.Format("{0}_{1}_{2}", model.SchemaName, model.TableName, model.ColumnName);
     Regex rgx = new Regex("[/\\\\#+=<>*%&:?.]");
     return rgx.Replace(baseId, "");
 }
 /// <summary>
 /// Removes a data masking rule based on the information provided by the model object
 /// </summary>
 public void RemoveDatabaseDataMaskingRule(DatabaseDataMaskingRuleModel model, String clientId)
 {
     if (!IsRightServerVersionForDataMasking(model.ResourceGroupName, model.ServerName, clientId))
     {
         throw new Exception(Properties.Resources.ServerNotApplicableForDataMasking);
     }
     DataMaskingRuleCreateOrUpdateParameters parameters = PolicizeDatabaseDataRuleModel(model);
     parameters.Properties.RuleState = SecurityConstants.Disabled;
     string ruleId = ExtractDataMaskingRuleId(model);
     Communicator.SetDatabaseDataMaskingRule(model.ResourceGroupName, model.ServerName, model.DatabaseName, ruleId, clientId, parameters);
 }
        /// <summary>
        /// Sets a data masking rule based on the information provided by the model object
        /// </summary>
        public void SetDatabaseDataMaskingRule(DatabaseDataMaskingRuleModel model, String clientId)
        {
             if (!IsRightServerVersionForDataMasking(model.ResourceGroupName, model.ServerName, clientId))
            {
                throw new Exception(Properties.Resources.ServerNotApplicableForDataMasking);
            }

            DatabaseDataMaskingPolicyModel policyModel = GetDatabaseDataMaskingPolicy(model.ResourceGroupName, model.ServerName, model.DatabaseName, clientId);
            if (policyModel.DataMaskingState == DataMaskingStateType.Uninitialized)
            {
                policyModel.DataMaskingState = DataMaskingStateType.Enabled;
                SetDatabaseDataMaskingPolicy(policyModel, clientId);
            }
            DataMaskingRuleCreateOrUpdateParameters parameters = PolicizeDatabaseDataRuleModel(model);
            Communicator.SetDatabaseDataMaskingRule(model.ResourceGroupName, model.ServerName, model.DatabaseName, ExtractDataMaskingRuleId(model), clientId, parameters);
        }
        /// <summary>
        /// Update the rule this cmdlet is operating on based on the values provided by the user
        /// </summary>
        /// <param name="rule">The rule this cmdlet operates on</param>
        /// <returns>An updated rule model</returns>
        protected DatabaseDataMaskingRuleModel UpdateRule(DatabaseDataMaskingRuleModel rule)
        {
            if (!string.IsNullOrEmpty(SchemaName)) // only update if the user provided this value
            {
                rule.SchemaName = SchemaName;
            }

            if (!string.IsNullOrEmpty(TableName)) // only update if the user provided this value
            {
                rule.TableName = TableName;
            }

            if (!string.IsNullOrEmpty(ColumnName)) // only update if the user provided this value
            {
                rule.ColumnName = ColumnName;
            }

            if(!string.IsNullOrEmpty(MaskingFunction)) // only update if the user provided this value
            {
                rule.MaskingFunction = ModelizeMaskingFunction();
            }

            if(rule.MaskingFunction == Model.MaskingFunction.Text)
            {
                if (PrefixSize != null) // only update if the user provided this value
                {
                    rule.PrefixSize = PrefixSize;
                }

                if (!string.IsNullOrEmpty(ReplacementString)) // only update if the user provided this value
                {
                    rule.ReplacementString = ReplacementString;
                }

                if (SuffixSize != null) // only update if the user provided this value
                {
                    rule.SuffixSize = SuffixSize;
                }

                if(rule.PrefixSize == null)
                {
                    rule.PrefixSize = SecurityConstants.PrefixSizeDefaultValue;
                }

                if (string.IsNullOrEmpty(rule.ReplacementString))
                {
                    rule.ReplacementString = SecurityConstants.ReplacementStringDefaultValue;
                }

                if (rule.SuffixSize == null)
                {
                    rule.SuffixSize = SecurityConstants.SuffixSizeDefaultValue;
                }
            }

            if (rule.MaskingFunction == Model.MaskingFunction.Number)
            {
                if (NumberFrom != null) // only update if the user provided this value
                {
                    rule.NumberFrom = NumberFrom;
                }

                if (NumberTo != null) // only update if the user provided this value
                {
                    rule.NumberTo = NumberTo;
                }

                if (rule.NumberFrom == null)
                {
                    rule.NumberFrom = SecurityConstants.NumberFromDefaultValue;
                }

                if (rule.NumberTo == null)
                {
                    rule.NumberTo = SecurityConstants.NumberToDefaultValue;
                }

                if(rule.NumberFrom > rule.NumberTo)
                {
                    throw new Exception(string.Format(CultureInfo.InvariantCulture, Microsoft.Azure.Commands.Sql.Properties.Resources.DataMaskingNumberRuleIntervalDefinitionError));
                }
            }
            return rule;
        }
 /// <summary>
 /// Update the rule that this cmdlet operates on based on the user provided values
 /// </summary>
 /// <param name="rules">The data masking rules of the database</param>
 /// <param name="rule">The rule that this cmdlet operates on</param>
 /// <returns>The update list of the database's data masking rules</returns>
 protected abstract IEnumerable<DatabaseDataMaskingRuleModel> UpdateRuleList(IEnumerable<DatabaseDataMaskingRuleModel> rules, DatabaseDataMaskingRuleModel rule);
 /// <summary>
 /// Sets a data masking rule based on the information provided by the model object
 /// </summary>
 public void SetDatabaseDataMaskingRule(DatabaseDataMaskingRuleModel model, String clientId)
 {
     DatabaseDataMaskingPolicyModel policyModel = GetDatabaseDataMaskingPolicy(model.ResourceGroupName, model.ServerName, model.DatabaseName, clientId);
     if (policyModel.DataMaskingState == DataMaskingStateType.Uninitialized)
     {
         policyModel.DataMaskingState = DataMaskingStateType.Enabled;
         SetDatabaseDataMaskingPolicy(policyModel, clientId);
     }
     DataMaskingRuleCreateOrUpdateParameters parameters = PolicizeDatabaseDataRuleModel(model);
     Communicator.SetDatabaseDataMaskingRule(model.ResourceGroupName, model.ServerName, model.DatabaseName, model.RuleId, clientId, parameters);
 }
 /// <summary>
 /// Removes a data masking rule based on the information provided by the model object
 /// </summary>
 public void RemoveDatabaseDataMaskingRule(DatabaseDataMaskingRuleModel model, String clientId)
 {
     Communicator.DeleteDataMaskingRule(model.ResourceGroupName, model.ServerName, model.DatabaseName, model.RuleId, clientId);
 }
 /// <summary>
 /// Sets a data masking rule based on the information provided by the model object
 /// </summary>
 public void SetDatabaseDataMaskingRule(DatabaseDataMaskingRuleModel model, String clientId)
 {
     DataMaskingRuleCreateOrUpdateParameters parameters = PolicizeDatabaseDataRuleModel(model);
     Communicator.SetDatabaseDataMaskingRule(model.ResourceGroupName, model.ServerName, model.DatabaseName, model.RuleId, clientId, parameters);
 }