コード例 #1
0
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
	}

}
コード例 #2
0
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
	}

}
コード例 #3
0
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
	}
}