func Delete(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) {

	//STANDARD DECLARATIONS START

	code := http.StatusOK
	h := http.Header{}
	output := []byte("")
	err := error(nil)
	contentType := "text/xml"
	charset := "utf-8"

	//STANDARD DECLARATIONS END

	message := ""

	//Authentication procedure
	if authentication.Authenticate(r.Header, cfg) {

		//Extracting record id from url
		urlValues := r.URL.Path
		id := strings.Split(urlValues, "/")[4]
		session, err := mongo.OpenSession(cfg)

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		//We remove the record bassed on its unique id
		err = mongo.IdRemove(session, "AR", "aps", id)

		mongo.CloseSession(session)

		if err != nil {

			message = "No profile matching the requested id" //If not found we inform the user
			output, err := messageXML(message)

			if err != nil {
				code = http.StatusInternalServerError
				return code, h, output, err
			}

			code = http.StatusBadRequest
			h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
			return code, h, output, err
		} else {
			code = http.StatusNoContent
			h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
			return code, h, output, err
		}
	} else {
		code = http.StatusUnauthorized
		output = []byte(http.StatusText(http.StatusUnauthorized)) //If wrong api key is passed we return UNAUTHORIZED http status
		return code, h, output, err
	}

}
func List(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) {

	//STANDARD DECLARATIONS START

	code := http.StatusOK
	h := http.Header{}
	output := []byte("")
	err := error(nil)
	contentType := "text/xml"
	charset := "utf-8"

	//STANDARD DECLARATIONS END

	//Read the search values
	urlValues := r.URL.Query()

	//Searchig is based on name and namespace
	input := AvailabilityProfileSearch{
		urlValues["name"],
		urlValues["namespace"],
	}

	results := []AvailabilityProfileOutput{}
	session, err := mongo.OpenSession(cfg)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	query := readOne(input)

	if len(input.Name) == 0 {
		query = nil //If no name and namespace is provided then we have to retrieve all profiles thus we send nil into db query
	}

	err = mongo.Find(session, "AR", "aps", query, "_id", &results)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	mongo.CloseSession(session)

	output, err = createView(results) //Render the results into XML format

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
	return code, h, output, err
}
Exemple #3
0
func Authenticate(h http.Header, cfg config.Config) bool {

	session, err := mongo.OpenSession(cfg)

	query := bson.M{
		"apiKey": h.Get("x-api-key"),
	}

	results := []Auth{}
	err = mongo.Find(session, "AR", "authentication", query, "apiKey", &results)

	if err != nil {
		return false
	}

	if len(results) > 0 {
		return true
	}
	return false
}
func List(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) {

	//STANDARD DECLARATIONS START

	code := http.StatusOK
	h := http.Header{}
	output := []byte("")
	err := error(nil)
	contentType := "text/xml"
	charset := "utf-8"

	//STANDARD DECLARATIONS END

	session, err := mongo.OpenSession(cfg)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	results := []FactorsOutput{}
	err = mongo.Find(session, "AR", "hepspec", nil, "p", &results)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	output, err = createView(results) //Render the results into XML format

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	mongo.CloseSession(session)
	h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
	return code, h, output, err
}
func List(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) {

	//STANDARD DECLARATIONS START

	code := http.StatusOK
	h := http.Header{}
	output := []byte("")
	err := error(nil)
	contentType := "text/xml"
	charset := "utf-8"

	//STANDARD DECLARATIONS END

	// This is the input we will receive from the API
	urlValues := r.URL.Query()

	input := ApiNgiAvailabilityInProfileInput{
		urlValues.Get("start_time"),
		urlValues.Get("end_time"),
		urlValues.Get("availability_profile"),
		urlValues.Get("granularity"),
		urlValues.Get("infrastructure"),
		urlValues.Get("production"),
		urlValues.Get("monitored"),
		urlValues.Get("certification"),
		urlValues.Get("format"),
		urlValues["group_name"],
	}

	if len(input.Infrastructure) == 0 {
		input.Infrastructure = "Production"
	}

	if len(input.Production) == 0 || input.Production == "true" {
		input.Production = "Y"
	} else {
		input.Production = "N"
	}

	if len(input.Monitored) == 0 || input.Monitored == "true" {
		input.Monitored = "Y"
	} else {
		input.Monitored = "N"
	}

	if len(input.Certification) == 0 {
		input.Certification = "Certified"
	}

	if strings.ToLower(input.format) == "json" {
		contentType = "application/json"
	}

	found, output := caches.HitCache("ngis", input, cfg)
	h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))

	if found {
		return code, h, output, err
	}

	session, err := mongo.OpenSession(cfg)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	results := []ApiNgiAvailabilityInProfileOutput{}

	// Select the granularity of the search daily/monthly
	if len(input.Granularity) == 0 || strings.ToLower(input.Granularity) == "daily" {
		CustomForm[0] = "20060102"
		CustomForm[1] = "2006-01-02"
		query := Daily(input)
		err = mongo.Pipe(session, "AR", "sites", query, &results)

	} else if strings.ToLower(input.Granularity) == "monthly" {
		CustomForm[0] = "200601"
		CustomForm[1] = "2006-01"
		query := Monthly(input)
		err = mongo.Pipe(session, "AR", "sites", query, &results)
	}

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	output, err = createView(results, input.format)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	if len(results) > 0 {
		caches.WriteCache("ngis", input, output, cfg)
	}

	mongo.CloseSession(session)

	return code, h, output, err
}
func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) {

	//STANDARD DECLARATIONS START

	code := http.StatusOK
	h := http.Header{}
	output := []byte("")
	err := error(nil)
	contentType := "text/xml"
	charset := "utf-8"

	//STANDARD DECLARATIONS END

	message := ""

	//Authentication procedure
	if authentication.Authenticate(r.Header, cfg) {

		session, err := mongo.OpenSession(cfg)

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		name := []string{}
		namespace := []string{}

		//Reading the json input
		reqBody, err := ioutil.ReadAll(r.Body)

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		input := AvailabilityProfileInput{}
		results := []AvailabilityProfileOutput{}
		//Unmarshalling the json input into byte form
		err = json.Unmarshal(reqBody, &input)
		//Making sure that no profile with the requested name and namespace combination already exists in the DB
		name = append(name, input.Name)
		namespace = append(namespace, input.Namespace)

		search := AvailabilityProfileSearch{
			name,
			namespace,
		}

		query := readOne(search)
		err = mongo.Find(session, "AR", "aps", query, "name", &results)

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		if len(results) <= 0 {
			//If name-namespace combination is unique we insert the new record into mongo
			query := createOne(input)
			err = mongo.Insert(session, "AR", "aps", query)

			if err != nil {
				code = http.StatusInternalServerError
				return code, h, output, err
			}

			mongo.CloseSession(session)

			//Providing with the appropriate user response
			message = "Availability Profile record successfully created"
			output, err := messageXML(message) //Render the response into XML

			if err != nil {
				code = http.StatusInternalServerError
				return code, h, output, err
			}

			h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
			return code, h, output, err

		} else {
			message = "An availability profile with that name already exists"
			output, err := messageXML(message) //Render the response into XML

			if err != nil {
				code = http.StatusInternalServerError
				return code, h, output, err
			}

			code = http.StatusBadRequest
			h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
			return code, h, output, err
		}

	} else {
		output = []byte(http.StatusText(http.StatusUnauthorized))
		code = http.StatusUnauthorized //If wrong api key is passed we return UNAUTHORIZED http status
		h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
		return code, h, output, err
	}

}
func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) {

	//STANDARD DECLARATIONS START

	code := http.StatusOK
	h := http.Header{}
	output := []byte("")
	err := error(nil)
	contentType := "text/xml"
	charset := "utf-8"

	//STANDARD DECLARATIONS END

	message := ""

	//only authenticated requests triger the handling code
	if authentication.Authenticate(r.Header, cfg) {

		session, err := mongo.OpenSession(cfg)

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		err = r.ParseForm()

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		urlValues := r.Form
		now := time.Now()

		input := RecomputationsInputOutput{
			urlValues.Get("start_time"),
			urlValues.Get("end_time"),
			urlValues.Get("reason"),
			urlValues.Get("ngi_name"),
			urlValues["exclude_site"],
			"pending",
			now.Format("2006-01-02 15:04:05"),
			//urlValues["exclude_sf"],
			//urlValues["exclude_end_point"],
		}

		query := insertQuery(input)
		err = mongo.Insert(session, "AR", "recalculations", query)

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		mongo.CloseSession(session)
		message = "A recalculation request has been filed"
		output, err := messageXML(message) //Render the response into XML

		if err != nil {
			code = http.StatusInternalServerError
			return code, h, output, err
		}

		h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
		return code, h, output, err

	} else {
		output = []byte(http.StatusText(http.StatusUnauthorized))
		code = http.StatusUnauthorized //If wrong api key is passed we return UNAUTHORIZED http status
		h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
		return code, h, output, err
	}
}
func List(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) {

	//STANDARD DECLARATIONS START

	code := http.StatusOK
	h := http.Header{}
	output := []byte("")
	err := error(nil)
	contentType := "text/xml"
	charset := "utf-8"

	//STANDARD DECLARATIONS END

	// This is the input we will receive from the API
	urlValues := r.URL.Query()

	input := ApiSFAvailabilityInProfileInput{
		urlValues.Get("start_time"),
		urlValues.Get("end_time"),
		urlValues.Get("profile"),
		urlValues.Get("granularity"),
		urlValues.Get("format"),
		urlValues["flavor"],
		urlValues["site"],
	}

	if strings.ToLower(input.format) == "json" {
		contentType = "application/json"
	}

	h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset))
	found, output := caches.HitCache("sf", input, cfg)

	if found {
		return code, h, output, err
	}

	session, err := mongo.OpenSession(cfg)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	results := []ApiSFAvailabilityInProfileOutput{}

	if len(input.granularity) == 0 || strings.ToLower(input.granularity) == "daily" {
		customForm[0] = "20060102"
		customForm[1] = "2006-01-02"
		query := Daily(input)
		err = mongo.Pipe(session, "AR", "sfreports", query, &results)

	} else if strings.ToLower(input.granularity) == "monthly" {
		customForm[0] = "200601"
		customForm[1] = "2006-01"
		query := Monthly(input)
		err = mongo.Pipe(session, "AR", "sfreports", query, &results)
	}

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	output, err = createView(results, input.format)

	if err != nil {
		code = http.StatusInternalServerError
		return code, h, output, err
	}

	if len(results) > 0 {
		caches.WriteCache("sf", input, output, cfg)
	}

	mongo.CloseSession(session)

	return code, h, output, err
}