Beispiel #1
0
//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

	}

}
Beispiel #2
0
//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
	}
}
//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

}
Beispiel #4
0
//TODO: Check if project is enabled
func checkProject(request router.Request) (int, []byte) {

	//Get The project
	projectId := request.GetPathParams()["project_id"]
	projectObj, getProjectStatus, getProjectError := getProject(projectId, request.GetContext())
	//Get Alerts
	alerts, getAlertStatus, getAlertsError := getAlerts(projectId, request.GetContext())
	//Get Subscriptions
	subscriptions, getSubsStatus, getSubsError := getSubscriptions(projectId, request.GetContext())

	if getProjectError == nil && getAlertsError == nil && getSubsError == nil {
		//Check Alerts
		return processAlerts(projectObj, alerts, subscriptions, request.GetContext())
	} else {
		return processError(getProjectStatus, getAlertStatus, getSubsStatus,
			getProjectError, getAlertsError, getSubsError)
	}

}
//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

}
Beispiel #7
0
//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())
		}
	}
}
Beispiel #8
0
//Create/Update an alert for the given project
func postAlert(request router.Request) (int, []byte) {

	var alert Alert
	err := json.Unmarshal(request.GetContent(), &alert)
	if err != nil {
		log.Infof(request.GetContext(), "error: %v", err)
		return http.StatusBadRequest, []byte(err.Error())
	}

	//TODO Check Project Exists
	alert.Project = request.GetPathParams()["project_id"]
	err = SaveAlertToGAE(alert, request.GetContext())
	if err != nil {
		log.Infof(request.GetContext(), "error: %v", err)
		return http.StatusInternalServerError, []byte(err.Error())
	}

	return http.StatusOK, nil

}