//Get a specific project func getProject(request router.Request) (int, []byte) { projectDTO, err := GetProjectDTOFromGAE(request.GetPathParams()["project_id"], request.GetContext()) if err != nil && strings.Contains(err.Error(), "no such entity") { log.Errorf(request.GetContext(), "Error retriving Project: %v", err) return http.StatusNotFound, []byte("Project Not Found") } else if err != nil { log.Errorf(request.GetContext(), "Error retriving project: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { project, err := projectDTO.GetProject() if err != nil { log.Infof(request.GetContext(), "Error %v", err) return http.StatusInternalServerError, []byte(err.Error()) } projectJSON, err := json.MarshalIndent(project, "", " ") if err != nil { log.Infof(request.GetContext(), "Error %v", err) return http.StatusInternalServerError, []byte(err.Error()) } return http.StatusOK, projectJSON } }
func registerUser(request router.Request) (int, []byte) { var registration UserResgistration err := json.Unmarshal(request.GetContent(), ®istration) //Check for correct desrialization if err != nil { log.Errorf(request.GetContext(), "error: %v", err) return http.StatusBadRequest, []byte(err.Error()) } //check if username already exists if IsUsernameAvailable(registration.UserName, request.GetContext()) { // generate a random salt with default rounds of complexity salt, _ := bcrypt.Salt() // hash and verify a password with random salt hash, _ := bcrypt.Hash(registration.Password) var user = UserData{registration.UserName, nil, salt, hash} err = SaveUserToGAE(user, request.GetContext()) if err != nil { log.Errorf(request.GetContext(), "error: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { return http.StatusOK, nil } } else { return http.StatusConflict, []byte("Username not avaiable") } }
//Get all projects //TODO: Check for Admin to return all //TODO: If not admin only return current users projects func getAllProjects(request router.Request) (int, []byte) { projectDTOs, err := GetProjectDTOsFromGAE(request.GetContext()) if err != nil { log.Errorf(request.GetContext(), "Errorf retriving project: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { //Convert to Projects var projects []Project for _, projectDTO := range projectDTOs { var project, err = projectDTO.GetProject() if err == nil { projects = append(projects, project) } else { return http.StatusInternalServerError, []byte(err.Error()) } } projectBytes, err := json.MarshalIndent(projects, "", " ") if err != nil { log.Errorf(request.GetContext(), "Errorf retriving Projects: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } return http.StatusOK, projectBytes } }
//Get all alerts for a given project func getAlerts(request router.Request) (int, []byte) { alerts, err := GetAlertsFromGAE(request.GetPathParams()["project_id"], request.GetContext()) if err != nil { log.Errorf(request.GetContext(), "Error retriving alerts: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { alertBytes, err := json.MarshalIndent(alerts, "", " ") if err != nil { log.Errorf(request.GetContext(), "Error retriving Alerts: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } return http.StatusOK, alertBytes } }
func getUsers(request router.Request) (int, []byte) { users, err := GetUsersFromGAE(request.GetContext()) if err != nil { log.Errorf(request.GetContext(), "Error retriving user: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { userBytes, err := json.MarshalIndent(users, "", " ") if err == nil { return http.StatusOK, userBytes } else { log.Errorf(request.GetContext(), "Errror %v", err) return http.StatusInternalServerError, []byte(err.Error()) } } }
func TriggerSubscriptionsIfNeeded(check alert.Check, subscriptions []Subscription, context appengine.Context) { for _, subscription := range subscriptions { if check.Changed && check.CurrentState > check.PreviousState { log.Infof(context, "Firing Subscrption %v", subscription) subject := fmt.Sprintf("Alert %s changed state from %s to %s", check.Alert, alert.GetStateString(check.PreviousState), alert.GetStateString(check.CurrentState)) message := fmt.Sprintf("Alert %s changed state from %s to %s with value %f\n Value measured at %s.\n", check.Alert, alert.GetStateString(check.PreviousState), alert.GetStateString(check.CurrentState), check.Value, time.Now().UTC().String()) msg := &mail.Message{ Sender: "Klaxon <*****@*****.**>", To: []string{subscription.Target}, Subject: subject, Body: message, } if err := mail.Send(context, msg); err != nil { log.Errorf(context, "Couldn't send email: %v", err) } } } }
func getProject(projectId string, context appengine.Context) (project.Project, int, error) { projectDto, err := project.GetProjectDTOFromGAE(projectId, context) if err != nil && strings.Contains(err.Error(), "no such entity") { log.Errorf(context, "Error retriving project: %v", err) return nil, http.StatusNotFound, err } else if err != nil { log.Errorf(context, "Error retriving project: %v", err) return nil, http.StatusInternalServerError, err } else { projectObj, err := projectDto.GetProject() status, err := verifyProject(projectObj, err, context) if err != nil { return nil, status, err } else { return projectObj, 0, nil } } }
func getAlerts(projectId string, context appengine.Context) ([]alert.Alert, int, error) { //Get Alerts for Project alerts, err := alert.GetAlertsFromGAE(projectId, context) if err != nil { log.Errorf(context, "Error retriving alerts: %v", err) return nil, http.StatusInternalServerError, err } else { return alerts, http.StatusOK, nil } }
//Get all subscriptions for a given project func getSubscriptions(request router.Request) (int, []byte) { subscriptions, err := GetSubscriptionsFromGAE(request.GetPathParams()["project_id"], request.GetContext()) if err != nil { log.Errorf(request.GetContext(), "Error retriving Subscriptions: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } subscriptionBytes, err := json.MarshalIndent(subscriptions, "", " ") if err != nil { log.Errorf(request.GetContext(), "Error retriving Subscriptions: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } return http.StatusOK, subscriptionBytes }
func getSubscriptions(projectId string, context appengine.Context) ([]subscription.Subscription, int, error) { //Get Subscriptions for Project subscriptions, err := subscription.GetSubscriptionsFromGAE(projectId, context) if err != nil { log.Errorf(context, "Error retriving subscriptions: %v", err) return nil, http.StatusInternalServerError, err } else { return subscriptions, http.StatusOK, nil } }
//Get a specific subscription for a project func getSubscription(request router.Request) (int, []byte) { subscription, err := GetSubscriptionFromGAE(request.GetPathParams()["project_id"], request.GetPathParams()["subscription_id"], request.GetContext()) if err != nil && strings.Contains(err.Error(), "no such entity") { return http.StatusNotFound, []byte("Subscription not found") } else if err != nil { log.Errorf(request.GetContext(), "Error retriving Subsciption: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { subscriptionBytes, err := json.MarshalIndent(subscription, "", " ") if err == nil { return http.StatusOK, subscriptionBytes } else { log.Errorf(request.GetContext(), "Errror %v", err) return http.StatusInternalServerError, []byte(err.Error()) } } }
//Create/Update an subscription for the given project func postSubscription(request router.Request) (int, []byte) { var subscription Subscription err := json.Unmarshal(request.GetContent(), &subscription) if err != nil { log.Errorf(request.GetContext(), "error: %v", err) return http.StatusBadRequest, []byte(err.Error()) } subscription.Project = request.GetPathParams()["project_id"] err = SaveSubscriptionToGAE(subscription, request.GetContext()) if err != nil { log.Errorf(request.GetContext(), "error: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } return http.StatusOK, nil }
//Get a specific alert for a project func getAlert(request router.Request) (int, []byte) { alert, err := GetAlertFromGAE(request.GetPathParams()["project_id"], request.GetPathParams()["alert_id"], request.GetContext()) if err != nil && strings.Contains(err.Error(), "no such entity") { log.Errorf(request.GetContext(), "Error retriving Alert: %v", err) return http.StatusNotFound, []byte("Alert not found") } else if err != nil { log.Errorf(request.GetContext(), "Error retriving alert: %v", err) return http.StatusBadRequest, []byte(err.Error()) } else { var alertJSON, err = json.MarshalIndent(alert, "", " ") if err == nil { return http.StatusOK, alertJSON } else { log.Errorf(request.GetContext(), "Errror %v", err) return http.StatusBadRequest, []byte(err.Error()) } } }
func getTick(request router.Request) (int, []byte) { query := datastore.NewQuery(project.PROJECT_KEY) projects := make([]project.ProjectDTO, 0) _, err := query.GetAll(request.GetContext(), &projects) if err != nil { log.Errorf(request.GetContext(), "Error retriving projects: %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { for _, project := range projects { task := taskqueue.NewPOSTTask("/rest/internal/check/"+project.Name, nil) if _, err := taskqueue.Add(request.GetContext(), task, "alertCheckQueue"); err != nil { log.Errorf(request.GetContext(), "Error posting to task queue: %v", err) } } return http.StatusOK, nil } }
func processAlert(projectAlert alert.Alert, context appengine.Context, graphiteReader graphite.GraphiteReader, subscriptions []subscription.Subscription, alertChecks []alert.Check) alert.Check { log.Infof(context, "Sending Graphite request too %s", projectAlert.Target) value, err := graphiteReader.ReadValue(projectAlert.Target) if err != nil { log.Errorf(context, "Error processing Alert: %v", err) projectAlert.SaveChangeIfNeeded(projectAlert.PreviousState != alert.UNKNOWN, alert.UNKNOWN, context) return alert.Check{projectAlert.Project, projectAlert.Name, projectAlert.PreviousState, alert.UNKNOWN, projectAlert.PreviousState != alert.UNKNOWN, 0} } else { changed, previous, current := projectAlert.CheckAlertStatusChange(value) projectAlert.SaveChangeIfNeeded(changed, current, context) check := alert.Check{projectAlert.Project, projectAlert.Name, previous, current, changed, value} subscription.TriggerSubscriptionsIfNeeded(check, subscriptions, context) return check } }
func processAlerts(projectObj project.Project, alerts []alert.Alert, subscriptions []subscription.Subscription, context appengine.Context) (int, []byte) { graphiteReader, _ := graphite.MakeGraphiteReader(projectObj.GetConfig()["graphite.baseurl"], projectObj.GetConfig()["graphite.lookback"], context) alertChecks := make([]alert.Check, 0) for _, projectAlert := range alerts { //TODO Make this in to a go routine alertCheck := processAlert(projectAlert, context, graphiteReader, subscriptions, alertChecks) alertChecks = append(alertChecks, alertCheck) } alertBytes, err := json.MarshalIndent(alertChecks, "", " ") if err != nil { log.Errorf(context, "Error marshalling response %v", err) return http.StatusInternalServerError, []byte(err.Error()) } else { return http.StatusOK, alertBytes } }