// Add validations to params by the type of action private static DefinedParamsModel AddValidationType(DefinedParamsModel options, string typeAction) { foreach (PropertyInfo property in options.GetType().GetProperties()) { string name = string.Concat(typeAction, "_", property.Name); string[] validation = SettingsHelper.GetSettingArray(name); // Get object BaseParamModel properties = (BaseParamModel)property.GetValue(options, null); properties.AddValidation(validation); // TODO: Refactor to use reflection // property.SetValue(options, properties, null); switch (property.Name) { case "username": options.username = properties; break; case "password": options.password = properties; break; case "urlSite": options.urlSite = properties; break; case "templateName": options.templateName = properties; break; case "dirPath": options.dirPath = properties; break; } } return(options); }
public void RunAction(DefinedParamsModel options, SecureString passwordSecore) { // Validate params LogHelper.writeConsole("Writting info of type actions..."); LogHelper.writeConsole(""); LogHelper.writeConsole(""); LogHelper.writeConsole("Use this action to review help about the different type of actions:"); LogHelper.writeConsole("--action help"); LogHelper.writeConsole(""); LogHelper.writeConsole("Use this action to generate a Template to Upload with the following commands:"); LogHelper.writeConsole("--action export --username [email protected] --password *** --urlSite https://tenant.sharepoint.com/"); LogHelper.writeConsole(""); LogHelper.writeConsole("Use this action to apply a template to site with the following commands:"); LogHelper.writeConsole("--action import --username [email protected] --password *** --urlSite https://tenant.sharepoint.com/ --templateName manifest"); LogHelper.writeConsole(""); LogHelper.writeConsole(""); LogHelper.writeConsole("The params that can be use, be the following:"); LogHelper.writeConsole(" - action: action to be do it"); LogHelper.writeConsole(" - modernAuth: if modernAuth is required to used"); LogHelper.writeConsole(" - username: login email of the admin user"); LogHelper.writeConsole(" - password: password of the admin user"); LogHelper.writeConsole(" - urlSite: url of the site where is to be logged and import/export the template"); LogHelper.writeConsole(" - templateName (No required): name of the file, without extension, it must be a XML file and a valid template"); LogHelper.writeConsole(" - dirPath (No required): Path of directory when the file can be found"); LogHelper.writeConsole(""); LogHelper.writeConsole(""); }
// Set necesary params from app public static ParamsModel ParseParams(string[] args) { ParamsModel _params = new ParamsModel(true); DefinedParamsModel definedParams = _params.basicParams; Dictionary <string, BaseParamModel> dicParams = ParseParamsFromCMD(args); definedParams.action = dicParams.ContainsKey("action") ? dicParams["action"] : new BaseParamModel(); definedParams.modernAuth = dicParams.ContainsKey("modernAuth") ? dicParams["modernAuth"] : new BaseParamModel(); definedParams.username = dicParams.ContainsKey("username") ? dicParams["username"] : new BaseParamModel(); definedParams.password = dicParams.ContainsKey("password") ? dicParams["password"] : new BaseParamModel(); definedParams.urlSite = dicParams.ContainsKey("urlSite") ? dicParams["urlSite"] : new BaseParamModel(); definedParams.templateName = dicParams.ContainsKey("templateName") ? dicParams["templateName"] : new BaseParamModel(); definedParams.dirPath = dicParams.ContainsKey("dirPath") ? dicParams["dirPath"] : new BaseParamModel(); _params.basicParams = definedParams; PropertyInfo[] properties = definedParams.GetType().GetProperties(); foreach (KeyValuePair <string, BaseParamModel> dicParam in dicParams) { PropertyInfo finded = properties.FirstOrDefault(x => { return(x.Name == dicParam.Key); }); if (finded == null) { _params.others.Add(dicParam.Key, dicParam.Value); } } return(_params); }
public void RunAction(DefinedParamsModel options, SecureString passwordSecore) { // Validate params LogHelper.writeBasic("Validate params..."); ValidationParamsModel resultValidation = ParamsHelper.ValidateParams(options, "import"); if (resultValidation.valid) { LogHelper.writeSuccess("The paramaters introduced are valid"); // Load template from file LogHelper.writeBasic("Loading template from file..."); string directory = ""; ProvisioningTemplate template = FileHelper.ReadTemplateFromUrl(options.templateName.value, out directory, options.dirPath.value); if (template != null) { LogHelper.writeSuccess("The template is loaded sucessfully"); // Connect with SP LogHelper.writeBasic("Connecting with SharePoint Online..."); bool modernAuth = false; bool.TryParse(options.modernAuth.value, out modernAuth); ClientContext context = SPOHelper.Connect(options.urlSite.value, options.username.value, passwordSecore, modernAuth); if (context != null) { LogHelper.writeSuccess("The connection with SPO is established sucessfully"); // Import template to SP LogHelper.writeBasic("Import template to SharePoint Online..."); bool resultImport = SPOHelper.ImportTemplate(context, template, directory); if (resultImport) { LogHelper.writeSuccess("The proccess of template has finished correctly."); } else { LogHelper.writeError("The template cant be imported to SharePoint, please review the log.."); } } else { LogHelper.writeError("There are not connection with SharePoint."); } } else { LogHelper.writeError("There are not template to import to SharePoint."); } } else { LogHelper.writeError(string.Concat("The paramaters introduced are not valid", string.Join(", ", resultValidation.errors))); } }
public static void Main(string[] args) { LogHelper.writeBasic("Init provisioning..."); ParamsModel allParams = ParamsHelper.ParseParams(args); DefinedParamsModel options = allParams.basicParams; // Select type try { IService action; switch (options.action.value) { case "import": action = new ImportTemplate(); break; case "export": action = new ExportTemplate(); break; case "help": default: action = new HelpTemplate(); break; } // Generate Secore Password SecureString passwordSecore = new SecureString(); foreach (char c in options.password.value.ToCharArray()) { passwordSecore.AppendChar(c); } // Do the action if (action != null) { LogHelper.writeBasic("Action found and start to run the program..."); action.RunAction(options, passwordSecore); LogHelper.writeBasic("Action finished."); } else { LogHelper.writeWarning(string.Concat("Action not defined in code, please review readme.md.")); } // Code problem } catch (Exception ex) { LogHelper.writeError(string.Concat("An error not recognise happend, please review the code. ", ex.Message)); } LogHelper.writeBasic("Finished process provisioning."); Console.ReadLine(); }
public void RunAction(DefinedParamsModel options, SecureString passwordSecore) { // Validate params LogHelper.writeBasic("Validate params..."); ValidationParamsModel resultValidation = ParamsHelper.ValidateParams(options, "export"); if (resultValidation.valid) { LogHelper.writeSuccess("The paramaters introduced are valid"); // Connect with SP LogHelper.writeBasic("Connecting with SharePoint Online..."); bool modernAuth = false; bool.TryParse(options.modernAuth.value, out modernAuth); ClientContext context = SPOHelper.Connect(options.urlSite.value, options.username.value, passwordSecore, modernAuth); if (context != null) { LogHelper.writeSuccess("The connection with SPO is established sucessfully"); // Generate directory string directory = FileHelper.GeneratePathDirectory(options.dirPath.value); // Import template to SP LogHelper.writeBasic("Export template from SharePoint Online..."); ProvisioningTemplate templateXML = SPOHelper.ExportTemplate(context, directory); if (templateXML != null) { LogHelper.writeSuccess("The proccess of template has finished correctly."); // Save export data LogHelper.writeBasic("Export template to file system started..."); FileHelper.SaveTemplateFromUrl(templateXML, directory, options.templateName.value); LogHelper.writeSuccess("The export of template has finished correctly."); } else { LogHelper.writeError("The template cant be exported to SharePoint, please review the log.."); } } else { LogHelper.writeError("There are not connection with SharePoint."); } } else { LogHelper.writeError(string.Concat("The paramaters introduced are not valid", string.Join(", ", resultValidation.errors))); } }
// Validate necesary params from app public static ValidationParamsModel ValidateParams(DefinedParamsModel options, string typeAction) { DefinedParamsModel validationParams = AddValidationType(options, typeAction); ValidationParamsModel validation = new ValidationParamsModel(true, new String[] { }); // Go over all defined properties in ParamsModel foreach (PropertyInfo property in validationParams.GetType().GetProperties()) { string name = property.Name; // Get object BaseParamModel properties = (BaseParamModel)property.GetValue(options, null); // Check if have validations if (properties.validations?.Length > 0) { string errMsg = ValidateHelper.ValidateField(name, properties.value, properties.validations); if (!string.IsNullOrEmpty(errMsg)) { validation.valid = false; validation.errors.Append(errMsg); } } } return(validation); }