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