public async Task<ActionResult> Add(string url, string version) { var ctx = new APIDiscoveryContext(); if (ModelState.IsValid) { RestHelper helper = new RestHelper(); try { var status = await helper.Ping(url, version); if (status != null && status.status.ToLower() == Constants.ASTE_PINGSTATUS_SUCCESS) { try { var config = await helper.Config(url, version); if (config != null) { if (config.type.ToLower() == Constants.ASTE_MODULETYPE_PROCESS) { Module new_module = new Module(); new_module.name = config.name; new_module.created = DateTime.Now; new_module.guid = config.guid; new_module.description = config.description; new_module.author = config.author; new_module.authorContact = config.authorContact; new_module.isProcess = true; new_module.methods = new List<ModuleMethod>(); new_module.my_dependencies = new List<ModuleDependency>(); new_module.api_url = url; new_module.version = version; if (config.methods != null) { foreach (var m in config.methods) { new_module.methods.Add(new ModuleMethod() { created = new DateTime(), name = m.method, methodInfo = m.@params }); } } var dependency_not_found = false; foreach (var d in config.dependencies) { var db_module = ctx.modules.Where(x => x.guid == d).FirstOrDefault(); if (db_module == null) { ModelState.AddModelError("Config", "Process dependency {0} was not found, could not install API to discovery"); dependency_not_found = true; break; } else { new_module.my_dependencies.Add(new ModuleDependency() { created = DateTime.Now, dependency_id = db_module.id, name = db_module.name }); } } if (!dependency_not_found) { ctx.modules.Add(new_module); ctx.SaveChanges(); } } else { ModelState.AddModelError("Config", "Contacted api is a module, not a process"); return View("New", new Models.newModelOrProcess() { url = url, version = version }); } } else { ModelState.AddModelError("Config", "config did not return any data"); return View("New", new Models.newModelOrProcess() { url = url, version = version }); } } catch (Exception ex) { ModelState.AddModelError("Config", "Url of the API could not be resolved, or the service is not listening - config method"); return View("New", new Models.newModelOrProcess() { url = url, version = version }); } } else { ModelState.AddModelError("Ping", status.message); return View("New", new Models.newModelOrProcess() { url = url, version = version }); } } catch (Exception ex) { ModelState.AddModelError("Ping", "Url of the API could not be resolved, or the service is not listening - ping method"); return View("New", new Models.newModelOrProcess() { url = url, version = version }); } return RedirectToAction("Index", "Process"); } return View("New", new Models.newModelOrProcess() { url = url, version = version }); }
public async Task<ModuleInfo> GetActiveModules (string module, string method, string version, string source) { RestHelper rs = new RestHelper(); Helper helper = new Helper(); var api_key = helper.GetApiKey(Request); await rs.Log("Incoming getActiveModule call from: " + source + " at " + helper.GetClientIp(Request)); if(string.IsNullOrEmpty(module)) { await rs.Log("GetActiveModule failed: Missing module name from " + source + " at " + helper.GetClientIp(Request) ); HttpResponseMessage no_name_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module name cannot be empty")); throw new HttpResponseException(no_name_response); } if (string.IsNullOrEmpty(method)) { await rs.Log("GetActiveModule failed: Missing module method from " + source + " at " + helper.GetClientIp(Request)); HttpResponseMessage no_method_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module method cannot be empty")); throw new HttpResponseException(no_method_response); } if (string.IsNullOrEmpty(version)) { await rs.Log("GetActiveModule failed: Missing module version from " + source + " at " + helper.GetClientIp(Request)); HttpResponseMessage no_version_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module version cannot be empty")); throw new HttpResponseException(no_version_response); } if (string.IsNullOrEmpty(source)) { await rs.Log("GetActiveModule failed: Missing module source at " + helper.GetClientIp(Request)); HttpResponseMessage no_source_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("module source cannot be empty")); throw new HttpResponseException(no_source_response); } if (!string.IsNullOrEmpty(api_key)) { var exists = ctx.clients.Where(x => x.api_key == api_key && !x.isdeleted).FirstOrDefault(); if (exists != null) { var activeModules = ctx.modules.Where(x => !x.isdeleted && x.active && !x.isProcess && x.name == module).ToList(); if(activeModules == null) { var module_not_found_message = string.Format("Module {0} not found from: " + source + " at " + helper.GetClientIp(Request), module); await rs.Log(module_not_found_message); HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Module {0} not found",module)); throw new HttpResponseException(response); } var VersionFound = false; var MethodFound = false; foreach(var m in activeModules) { if(m.version == version) { VersionFound = true; foreach (var moduleMethod in m.methods) { if(moduleMethod.name == method) { MethodFound = true; ModuleInfo info = new ModuleInfo() { api_url = m.api_url, name = m.name, version = m.version }; return info; } } } } if(!VersionFound) { var version_not_found_message = string.Format("Version {0} is not available for module {1} from: " + source + " at " + helper.GetClientIp(Request),version, module); await rs.Log(version_not_found_message); HttpResponseMessage version_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Version {0} is not available for module {1}.",version,module)); throw new HttpResponseException(version_response); } if(!MethodFound) { var method_not_found_message = string.Format("method {0} is not available for module {1} version {2} from: " + source + " at " + helper.GetClientIp(Request), method, module,version); await rs.Log(method_not_found_message); HttpResponseMessage method_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("method {0} is not available for module {1} version {2}.", method, module, version)); throw new HttpResponseException(method_response); } var message = string.Format("Version {0} is not available for module {1} from: " + source + " at " + helper.GetClientIp(Request), version, module); await rs.Log(message); HttpResponseMessage error_response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error while fetching module information"); throw new HttpResponseException(error_response); } else { var message = string.Format("Invalid API key from: " + source + " at " + helper.GetClientIp(Request)); await rs.Log(message); HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid API key."); throw new HttpResponseException(response); } } else { var message = string.Format("API key missing from: " + source + " at " + helper.GetClientIp(Request)); await rs.Log(message); HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "API key missing."); throw new HttpResponseException(response); } }