예제 #1
0
        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_MODULE)
                                {
                                    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       = false;
                                    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", string.Format("Module dependency {0} was not found, could not install API to discovery", d));
                                            return(View("New", new Models.newModelOrProcess()
                                            {
                                                url = url, version = version
                                            }));
                                        }
                                        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 process, not a module");
                                    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", "Module"));
            }

            return(View("New", new Models.newModelOrProcess()
            {
                url = url, version = version
            }));
        }