public async Task <int> SubmitConfiguration(IOperation operation, ModuleConfigurationSubmitEntity entity) { var file = await fileStore.Download(operation, entity.FileId); if (file == null) { throw CommonExceptions.FileWasNotFound(operation, entity.FileId); } var configuration = ModuleConfigurationUtils.Parse(operation, file); if (!configuration.IsValid()) { throw CommonExceptions.ModuleConfigurationIsInvalid(operation); } var module = await moduleStore.GetModuleByAlias(operation, configuration.Alias); if (module == null) { if (entity.ModuleId != null) { throw CommonExceptions.FailedToDefineModuleForSubmission(operation); } if (!await userDataStore.IsUserExist(operation, entity.UserId)) { throw CommonExceptions.UserWasNotFound(operation, entity.UserId); } if (!await portalSettingsStore.IsHostExist(operation, entity.HostId)) { throw CommonExceptions.HostWasNotFound(operation, entity.HostId); } // Module is new for the system var moduleIdForCreate = await moduleStore.CreateModule(operation, entity.UserId, entity.HostId, entity.FileId, configuration); await new ProcessDefinition.StartProcessInstanceByKeyRequest(CamundaWorkerKey.ModuleInstallationProcess) { BusinessKey = moduleIdForCreate.ToString(), Variables = new CamundaVariables { { CamundaWorkerKey.ModuleId, moduleIdForCreate }, { CamundaWorkerKey.Action, ActionType.NoChanges } } }.SendRequest(operation, camundaClient, true); return(moduleIdForCreate); } var operatingSystems = configuration.Instructions.Where(item => item.OperatingSystem.HasValue).Select(item => item.OperatingSystem.Value); if (!operatingSystems.Contains(module.HostOperatingSystem)) { // Configuration doesn't have instructions for OS which used on current host throw CommonExceptions.FailedToSubmitModuleConfiguration(operation, ModuleConfigurationVerdict.BrokenChanges); } if (module.Status == ModuleStatus.Run || module.Status == ModuleStatus.Stopped || module.Status == ModuleStatus.FailedToRun || module.Status == ModuleStatus.FailedToStop) { if (!await userDataStore.IsUserExist(operation, entity.UserId)) { throw CommonExceptions.UserWasNotFound(operation, entity.UserId); } // Module in stable mode: run or stopped var moduleIdForUpgrade = ValidateToUpgradeModule(operation, entity.ModuleId, module, configuration); var fullModule = await moduleStore.GetModule(operation, module.Id); var historicalConfiguration = fullModule.Configurations.FirstOrDefault(config => config.Version == configuration.Version); if (historicalConfiguration != null) { await moduleStore.UpgradeModule(operation, module.Id, entity.UserId, historicalConfiguration.Id); } else { await moduleStore.UpgradeModule(operation, moduleIdForUpgrade, entity.UserId, entity.FileId, configuration); } await new ProcessDefinition.StartProcessInstanceByKeyRequest(CamundaWorkerKey.ModuleUpgradeProcess) { BusinessKey = moduleIdForUpgrade.ToString(), Variables = new CamundaVariables { { CamundaWorkerKey.ModuleId, moduleIdForUpgrade }, { CamundaWorkerKey.ComponentsStopRequired, module.Status == ModuleStatus.Run }, { CamundaWorkerKey.Action, ActionType.Upgrade } } }.SendRequest(operation, camundaClient, true); return(moduleIdForUpgrade); } throw CommonExceptions.FailedToSubmitModuleConfiguration(operation, ModuleConfigurationVerdict.Updating); }