// POST: /clientgroup/{name} func (cr *ClientGroupController) CreateWithId(name string, cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided and ANON_CG_WRITE is true, use the public user. // Otherwise if no auth was provided or user is not an admin, and ANON_CG_WRITE is false, throw an error. // Otherwise, proceed with creation of the clientgroup with the user. if u == nil && conf.ANON_CG_WRITE == true { u = &user.User{Uuid: "public"} } else if u == nil || !u.Admin { if conf.ANON_CG_WRITE == false { cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized) return } } cg, err := core.CreateClientGroup(name, u) if err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } cx.RespondWithData(cg) return }
// GET: /client/{id} func (cr *ClientController) Read(id string, cx *goweb.Context) { // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("heartbeat") { //handle heartbeat cg, err := request.AuthenticateClientGroup(cx.Request) if err != nil { if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth { if conf.CLIENT_AUTH_REQ == true { cx.RespondWithError(http.StatusUnauthorized) return } } else { logger.Error("Err@AuthenticateClientGroup: " + err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } hbmsg, err := core.QMgr.ClientHeartBeat(id, cg) if err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) } else { cx.RespondWithData(hbmsg) } return } LogRequest(cx.Request) //skip heartbeat in access log // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided, and anonymous read is allowed, use the public user if u == nil { if conf.ANON_READ == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } } client, err := core.QMgr.GetClientByUser(id, u) if err != nil { if err.Error() == e.ClientNotFound { cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest) } else { logger.Error("Error in GET client:" + err.Error()) cx.RespondWithError(http.StatusBadRequest) } return } cx.RespondWithData(client) return }
// GET: /client/{id} func (cr *ClientController) Read(id string, cx *goweb.Context) { // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("heartbeat") { //handle heartbeat hbmsg, err := core.QMgr.ClientHeartBeat(id) if err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) } else { cx.RespondWithData(hbmsg) } return } LogRequest(cx.Request) //skip heartbeat in access log client, err := core.QMgr.GetClient(id) if err != nil { if err.Error() == e.ClientNotFound { cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest) } else { logger.Error("Error in GET client:" + err.Error()) cx.RespondWithError(http.StatusBadRequest) } return } cx.RespondWithData(client) }
// PUT: /client func (cr *ClientController) UpdateMany(cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided, and anonymous read is allowed, use the public user if u == nil { if conf.ANON_WRITE == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } } // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("resumeall") { //resume the suspended client num := core.QMgr.ResumeSuspendedClientsByUser(u) cx.RespondWithData(fmt.Sprintf("%d suspended clients resumed", num)) return } if query.Has("suspendall") { //resume the suspended client num := core.QMgr.SuspendAllClientsByUser(u) cx.RespondWithData(fmt.Sprintf("%d clients suspended", num)) return } cx.RespondWithError(http.StatusNotImplemented) return }
// DELETE: /client/{id} func (cr *ClientController) Delete(id string, cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided, and anonymous read is allowed, use the public user if u == nil { if conf.ANON_DELETE == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } } if err := core.QMgr.DeleteClientByUser(id, u); err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) } else { cx.RespondWithData("client deleted") } return }
// GET: /user func (cr *UserController) ReadMany(cx *goweb.Context) { // Log Request and check for Auth LogRequest(cx.Request) u, err := request.Authenticate(cx.Request) if err != nil { if err.Error() == e.NoAuth || err.Error() == e.UnAuth { cx.RespondWithError(http.StatusUnauthorized) return } else { logger.Error("Err@user_Read: " + err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } if u.Admin { users := user.Users{} user.AdminGet(&users) if len(users) > 0 { cx.RespondWithData(users) return } else { cx.RespondWithNotFound() return } } else { cx.RespondWithError(http.StatusUnauthorized) return } }
// DELETE: /job/{id} func (cr *JobController) Delete(id string, cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) } // If no auth was provided, and anonymous delete is allowed, use the public user if u == nil { if conf.ANON_DELETE == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } } if err = core.QMgr.DeleteJobByUser(id, u); err != nil { if err == mgo.ErrNotFound { cx.RespondWithNotFound() return } else if err.Error() == e.UnAuth { cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized) return } else { cx.RespondWithErrorMessage("fail to delete job: "+id, http.StatusBadRequest) return } } cx.RespondWithData("job deleted: " + id) return }
// DELETE: /job?suspend, /job?zombie func (cr *JobController) DeleteMany(cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided, and anonymous delete is allowed, use the public user if u == nil { if conf.ANON_DELETE == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } } // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("suspend") { num := core.QMgr.DeleteSuspendedJobsByUser(u) cx.RespondWithData(fmt.Sprintf("deleted %d suspended jobs", num)) } else if query.Has("zombie") { num := core.QMgr.DeleteZombieJobsByUser(u) cx.RespondWithData(fmt.Sprintf("deleted %d zombie jobs", num)) } else { cx.RespondWithError(http.StatusNotImplemented) } return }
// GET: /work/{id} // get a workunit by id, read-only func (cr *WorkController) Read(id string, cx *goweb.Context) { LogRequest(cx.Request) // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("datatoken") && query.Has("client") { //a client is requesting data token for this job //***insert code to authenticate and check ACL*** clientid := query.Value("client") token, err := core.QMgr.FetchDataToken(id, clientid) if err != nil { cx.RespondWithErrorMessage("error in getting token for job "+id, http.StatusBadRequest) } cx.RespondWithData(token) return } // Load workunit by id workunit, err := core.QMgr.GetWorkById(id) if err != nil { if err.Error() != e.QueueEmpty { logger.Error("Err@work_Read:core.QMgr.GetWorkById(): " + err.Error()) } cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } // Base case respond with workunit in json cx.RespondWithData(workunit) return }
// GET: /client func (cr *ClientController) ReadMany(cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided, and anonymous read is allowed, use the public user if u == nil { if conf.ANON_READ == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } } clients := core.QMgr.GetAllClientsByUser(u) query := &Query{Li: cx.Request.URL.Query()} filtered := []*core.Client{} if query.Has("busy") { for _, client := range clients { if client.Current_work_length() > 0 { filtered = append(filtered, client) } } } else if query.Has("group") { for _, client := range clients { if client.Group == query.Value("group") { filtered = append(filtered, client) } } } else if query.Has("status") { for _, client := range clients { status := client.Get_Status() stat := strings.Split(status, "-") if status == query.Value("status") { filtered = append(filtered, client) } else if (len(stat) == 2) && (stat[1] == query.Value("status")) { filtered = append(filtered, client) } } } else if query.Has("app") { for _, client := range clients { for _, app := range client.Apps { if app == query.Value("app") { filtered = append(filtered, client) } } } } else { filtered = clients } cx.RespondWithData(filtered) return }
// PUT: /work/{id} -> status update func (cr *WorkController) Update(id string, cx *goweb.Context) { // Log Request and check for Auth LogRequest(cx.Request) // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("status") && query.Has("client") { //notify execution result: "done" or "fail" notice := core.Notice{WorkId: id, Status: query.Value("status"), ClientId: query.Value("client"), Notes: ""} if query.Has("report") { // if "report" is specified in query, parse performance statistics or errlog if _, files, err := ParseMultipartForm(cx.Request); err == nil { if _, ok := files["perf"]; ok { core.QMgr.FinalizeWorkPerf(id, files["perf"].Path) } if _, ok := files["notes"]; ok { if notes, err := ioutil.ReadFile(files["notes"].Path); err == nil { notice.Notes = string(notes) } } } } core.QMgr.NotifyWorkStatus(notice) } cx.RespondWithData("ok") return }
// PUT: /queue func (cr *QueueController) UpdateMany(cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // must be admin user if u == nil || u.Admin == false { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("resume") { core.QMgr.ResumeQueue() logger.Event(event.QUEUE_RESUME, "user="******"work queue resumed") return } if query.Has("suspend") { core.QMgr.SuspendQueue() logger.Event(event.QUEUE_SUSPEND, "user="******"work queue suspended") return } cx.RespondWithErrorMessage("requested queue operation not supported", http.StatusBadRequest) return }
// PUT: /logger func (cr *LoggerController) UpdateMany(cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // must be admin user if u == nil || u.Admin == false { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } // Gather query params query := &Query{Li: cx.Request.URL.Query()} // currently can only reset debug level if query.Has("debug") { levelStr := query.Value("debug") levelInt, err := strconv.Atoi(levelStr) if err != nil { cx.RespondWithErrorMessage("invalid debug level: "+err.Error(), http.StatusBadRequest) } conf.DEBUG_LEVEL = levelInt logger.Event(event.DEBUG_LEVEL, "level="+levelStr+";user="******"debuglevel": conf.DEBUG_LEVEL}) return } cx.RespondWithError(http.StatusNotImplemented) return }
// DELETE: /job/{id} func (cr *JobController) Delete(id string, cx *goweb.Context) { LogRequest(cx.Request) if err := core.QMgr.DeleteJob(id); err != nil { cx.RespondWithErrorMessage("fail to delete job: "+id, http.StatusBadRequest) return } cx.RespondWithData("job deleted: " + id) return }
// POST: /user // To create a new user make a empty POST to /user with user:password // Basic Auth encoded in the header. Return new user object. func (cr *UserController) Create(cx *goweb.Context) { // Log Request LogRequest(cx.Request) if _, ok := cx.Request.Header["Authorization"]; !ok { cx.RespondWithError(http.StatusUnauthorized) return } header := cx.Request.Header.Get("Authorization") tmpAuthArray := strings.Split(header, " ") authValues, err := base64.URLEncoding.DecodeString(tmpAuthArray[1]) if err != nil { err = errors.New("Failed to decode encoded auth settings in http request.") cx.RespondWithError(http.StatusBadRequest) return } authValuesArray := strings.Split(string(authValues), ":") if conf.ANON_CREATEUSER == false && len(authValuesArray) != 4 { if len(authValuesArray) == 2 { cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized) return } else { cx.RespondWithError(http.StatusBadRequest) return } } name := authValuesArray[0] passwd := authValuesArray[1] admin := false if len(authValuesArray) == 4 { if authValuesArray[2] != fmt.Sprint(conf.SECRET_KEY) { cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized) return } else if authValuesArray[3] == "true" { admin = true } } u, err := user.New(name, passwd, admin) if err != nil { // Duplicate key check if e.MongoDupKeyRegex.MatchString(err.Error()) { logger.Error("Err@user_Create: duplicate key error") cx.RespondWithErrorMessage("Username not available", http.StatusBadRequest) return } else { logger.Error("Err@user_Create: " + err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } cx.RespondWithData(u) return }
// POST: /job func (cr *JobController) Create(cx *goweb.Context) { // Log Request and check for Auth LogRequest(cx.Request) // Parse uploaded form params, files, err := ParseMultipartForm(cx.Request) if err != nil { if err.Error() == "request Content-Type isn't multipart/form-data" { cx.RespondWithErrorMessage("No job file is submitted", http.StatusBadRequest) } else { // Some error other than request encoding. Theoretically // could be a lost db connection between user lookup and parsing. // Blame the user, Its probaby their fault anyway. logger.Error("Error parsing form: " + err.Error()) cx.RespondWithError(http.StatusBadRequest) } return } _, has_upload := files["upload"] _, has_awf := files["awf"] if !has_upload && !has_awf { cx.RespondWithErrorMessage("No job script or awf is submitted", http.StatusBadRequest) return } //send job submission request and get back an assigned job number (jid) var jid string jid, err = core.QMgr.JobRegister() if err != nil { logger.Error("Err@job_Create:GetNextJobNum: " + err.Error()) cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } var job *core.Job job, err = core.CreateJobUpload(params, files, jid) if err != nil { logger.Error("Err@job_Create:CreateJobUpload: " + err.Error()) cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } if token, err := request.RetrieveToken(cx.Request); err == nil { job.SetDataToken(token) } core.QMgr.EnqueueTasksByJobId(job.Id, job.TaskList()) //log event about job submission (JB) logger.Event(event.JOB_SUBMISSION, "jobid="+job.Id+";jid="+job.Jid+";name="+job.Info.Name+";project="+job.Info.Project) cx.RespondWithData(job) return }
// GET: /queue // get status from queue manager func (cr *QueueController) ReadMany(cx *goweb.Context) { LogRequest(cx.Request) // Gather query params // query := &Query{list: cx.Request.URL.Query()} msg := core.QMgr.ShowStatus() cx.RespondWithData(msg) return }
// PUT: /job func (cr *JobController) UpdateMany(cx *goweb.Context) { LogRequest(cx.Request) // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("resumeall") { //resume the suspended job num := core.QMgr.ResumeSuspendedJobs() cx.RespondWithData(fmt.Sprintf("%d suspended jobs resumed", num)) return } cx.RespondWithError(http.StatusNotImplemented) }
// GET: /awf/{name} // get a workflow by name, read-only func (cr *AwfController) Read(id string, cx *goweb.Context) { LogRequest(cx.Request) // Load workunit by id workflow, err := core.AwfMgr.GetWorkflow(id) if err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } // Base case respond with workunit in json cx.RespondWithData(workflow) return }
// GET: /queue // get status from queue manager func (cr *QueueController) ReadMany(cx *goweb.Context) { LogRequest(cx.Request) // Gather query params // query := &Query{list: cx.Request.URL.Query()} msg, err := json.Marshal(core.QMgr.ShowStatus()) if err == nil { cx.RespondWithData(string(msg)) } else { cx.RespondWithData("Err") } }
// GET: /clientgroup/{id} func (cr *ClientGroupController) Read(id string, cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided and ANON_CG_READ is true, use the public user. // Otherwise if no auth was provided, throw an error. // Otherwise, proceed with retrieval of the clientgroup using the user. if u == nil { if conf.ANON_CG_READ == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized) return } } // Load clientgroup by id cg, err := core.LoadClientGroup(id) if err != nil { if err == mgo.ErrNotFound { cx.RespondWithNotFound() } else { // In theory the db connection could be lost between // checking user and load but seems unlikely. cx.RespondWithErrorMessage("clientgroup id not found:"+id, http.StatusBadRequest) } return } // User must have read permissions on clientgroup or be clientgroup owner or be an admin or the clientgroup is publicly readable. // The other possibility is that public read of clientgroups is enabled and the clientgroup is publicly readable. rights := cg.Acl.Check(u.Uuid) public_rights := cg.Acl.Check("public") if (u.Uuid != "public" && (cg.Acl.Owner == u.Uuid || rights["read"] == true || u.Admin == true || public_rights["read"] == true)) || (u.Uuid == "public" && conf.ANON_CG_READ == true && public_rights["read"] == true) { cx.RespondWithData(cg) return } cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized) return }
// GET: /work // checkout a workunit with earliest submission time // to-do: to support more options for workunit checkout func (cr *WorkController) ReadMany(cx *goweb.Context) { // Gather query params query := &Query{Li: cx.Request.URL.Query()} if !query.Has("client") { //view workunits var workunits []*core.Workunit if query.Has("state") { workunits = core.QMgr.ShowWorkunits(query.Value("state")) } else { workunits = core.QMgr.ShowWorkunits("") } cx.RespondWithData(workunits) return } if core.Service == "proxy" { //drive proxy workStealer to checkout work from server core.ProxyWorkChan <- true } //checkout a workunit in FCFS order clientid := query.Value("client") workunits, err := core.QMgr.CheckoutWorkunits("FCFS", clientid, 1) if err != nil { if err.Error() != e.QueueEmpty && err.Error() != e.NoEligibleWorkunitFound && err.Error() != e.ClientNotFound { logger.Error("Err@work_ReadMany:core.QMgr.GetWorkByFCFS(): " + err.Error() + ";client=" + clientid) } cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } //log access info only when the queue is not empty, save some log LogRequest(cx.Request) //log event about workunit checkout (WO) workids := []string{} for _, work := range workunits { workids = append(workids, work.Id) } logger.Event(event.WORK_CHECKOUT, "workids="+strings.Join(workids, ","), "clientid="+clientid) // Base case respond with node in json cx.RespondWithData(workunits[0]) return }
// DELETE: /job?suspend func (cr *JobController) DeleteMany(cx *goweb.Context) { LogRequest(cx.Request) // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("suspend") { num := core.QMgr.DeleteSuspendedJobs() cx.RespondWithData(fmt.Sprintf("deleted %d suspended jobs", num)) } else if query.Has("zombie") { num := core.QMgr.DeleteZombieJobs() cx.RespondWithData(fmt.Sprintf("deleted %d zombie jobs", num)) } else { cx.RespondWithError(http.StatusNotImplemented) } return }
// GET: /logger func (cr *LoggerController) ReadMany(cx *goweb.Context) { LogRequest(cx.Request) // Gather query params query := &Query{Li: cx.Request.URL.Query()} // public logging info if query.Has("event") { cx.RespondWithData(event.EventDiscription) return } if query.Has("debug") { cx.RespondWithData(map[string]int{"debuglevel": conf.DEBUG_LEVEL}) return } cx.RespondWithError(http.StatusNotImplemented) return }
// POST: /client - register a new client func (cr *ClientController) Create(cx *goweb.Context) { // Log Request and check for Auth LogRequest(cx.Request) cg, err := request.AuthenticateClientGroup(cx.Request) if err != nil { if err.Error() == e.NoAuth || err.Error() == e.UnAuth || err.Error() == e.InvalidAuth { if conf.CLIENT_AUTH_REQ == true { cx.RespondWithError(http.StatusUnauthorized) return } } else { logger.Error("Err@AuthenticateClientGroup: " + err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } // Parse uploaded form _, files, err := ParseMultipartForm(cx.Request) if err != nil { if err.Error() != "request Content-Type isn't multipart/form-data" { logger.Error("Error parsing form: " + err.Error()) cx.RespondWithError(http.StatusBadRequest) return } } client, err := core.QMgr.RegisterNewClient(files, cg) if err != nil { msg := "Error in registering new client:" + err.Error() logger.Error(msg) cx.RespondWithErrorMessage(msg, http.StatusBadRequest) return } //log event about client registration (CR) logger.Event(event.CLIENT_REGISTRATION, "clientid="+client.Id+";name="+client.Name+";host="+client.Host+";group="+client.Group+";instance_id="+client.InstanceId+";instance_type="+client.InstanceType+";domain="+client.Domain) cx.RespondWithData(client) return }
// GET: /client func (cr *ClientController) ReadMany(cx *goweb.Context) { LogRequest(cx.Request) clients := core.QMgr.GetAllClients() if len(clients) == 0 { cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest) return } query := &Query{Li: cx.Request.URL.Query()} filtered := []*core.Client{} if query.Has("busy") { for _, client := range clients { if len(client.Current_work) > 0 { filtered = append(filtered, client) } } } else { filtered = clients } cx.RespondWithData(filtered) }
// GET: /user/{id} func (cr *UserController) Read(id string, cx *goweb.Context) { // Log Request and check for Auth LogRequest(cx.Request) u, err := request.Authenticate(cx.Request) if err != nil { if err.Error() == e.NoAuth || err.Error() == e.UnAuth { cx.RespondWithError(http.StatusUnauthorized) return } else { logger.Error("Err@user_Read: " + err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } // Any user can access their own user info. Only admins can // access other's info if u.Uuid == id { cx.RespondWithData(u) return } else if u.Admin { nu, err := user.FindByUuid(id) if err != nil { if err.Error() == e.MongoDocNotFound { cx.RespondWithNotFound() return } else { logger.Error("Err@user_Read:Admin: " + err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } cx.RespondWithData(nu) return } else { // Not sure how we could end up here but its probably the // user's fault cx.RespondWithError(http.StatusBadRequest) return } }
// PUT: /client/{id} -> status update func (cr *ClientController) Update(id string, cx *goweb.Context) { LogRequest(cx.Request) // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("subclients") { //update the number of subclients for a proxy if count, err := strconv.Atoi(query.Value("subclients")); err != nil { cx.RespondWithError(http.StatusNotImplemented) } else { core.QMgr.UpdateSubClients(id, count) cx.RespondWithData("ok") } return } if query.Has("suspend") { //resume the suspended client if err := core.QMgr.SuspendClient(id); err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) } else { cx.RespondWithData("client suspended") } return } if query.Has("resume") { //resume the suspended client if err := core.QMgr.ResumeClient(id); err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) } else { cx.RespondWithData("client resumed") } return } cx.RespondWithError(http.StatusNotImplemented) }
// GET: /job/{id} func (cr *JobController) Read(id string, cx *goweb.Context) { LogRequest(cx.Request) // Load job by id job, err := core.LoadJob(id) if err != nil { if err.Error() == e.MongoDocNotFound { cx.RespondWithNotFound() return } else { // In theory the db connection could be lost between // checking user and load but seems unlikely. logger.Error("Err@job_Read:LoadJob: " + id + ":" + err.Error()) cx.RespondWithErrorMessage("job not found:"+id, http.StatusBadRequest) return } } // Gather query params query := &Query{Li: cx.Request.URL.Query()} if query.Has("export") { target := query.Value("export") if target == "" { cx.RespondWithErrorMessage("lacking stage id from which the recompute starts", http.StatusBadRequest) } else if target == "taverna" { wfrun, err := taverna.ExportWorkflowRun(job) if err != nil { cx.RespondWithErrorMessage("failed to export job to taverna workflowrun:"+id, http.StatusBadRequest) } cx.RespondWithData(wfrun) return } } // Base case respond with job in json cx.RespondWithData(job) return }
// GET: /client func (cr *ClientController) ReadMany(cx *goweb.Context) { LogRequest(cx.Request) // Try to authenticate user. u, err := request.Authenticate(cx.Request) if err != nil && err.Error() != e.NoAuth { cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized) return } // If no auth was provided, and anonymous read is allowed, use the public user if u == nil { if conf.ANON_READ == true { u = &user.User{Uuid: "public"} } else { cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized) return } } clients := core.QMgr.GetAllClientsByUser(u) query := &Query{Li: cx.Request.URL.Query()} filtered := []*core.Client{} if query.Has("busy") { for _, client := range clients { if len(client.Current_work) > 0 { filtered = append(filtered, client) } } } else { filtered = clients } cx.RespondWithData(filtered) return }