// Update function used to implement update report request. // This is an http PUT request that gets a specific report's name // as a urlvar parameter input and a json structure in the request // body in order to update the datastore document for the specific // report func Update(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) charset := "utf-8" //STANDARD DECLARATIONS END // Set Content-Type response Header value contentType := r.Header.Get("Accept") h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) // Grab Tenant DB configuration from context tenantDbConfig := context.Get(r, "tenant_conf").(config.MongoConfig) //Extracting report name from url id := mux.Vars(r)["id"] //Reading the json input reqBody, err := ioutil.ReadAll(r.Body) input := MongoInterface{} //Unmarshalling the json input into byte form err = json.Unmarshal(reqBody, &input) if err != nil { // User provided malformed json input data output, _ := respond.MarshalContent(respond.MalformedJSONInput, contentType, "", " ") code = http.StatusBadRequest h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } sanitizedInput := bson.M{ "$set": bson.M{ // "info": bson.M{ "info.name": input.Info.Name, "info.description": input.Info.Description, "info.updated": time.Now().Format("2006-01-02 15:04:05"), // }, "profiles": input.Profiles, "filter_tags": input.Tags, "topology_schema": input.Topology, }} // Try to open the mongo session session, err := mongo.OpenSession(tenantDbConfig) defer session.Close() if err != nil { code = http.StatusInternalServerError return code, h, output, err } // Validate profiles given in report validationErrors := input.ValidateProfiles(session.DB(tenantDbConfig.Db)) if len(validationErrors) > 0 { code = 422 out := respond.UnprocessableEntity out.Errors = validationErrors output = out.MarshalTo(contentType) return code, h, output, err } // We search by name and update query := bson.M{"id": id} err = mongo.Update(session, tenantDbConfig.Db, reportsColl, query, sanitizedInput) if err != nil { if err.Error() != "not found" { code = http.StatusInternalServerError return code, h, output, err } //Render the response into XML code = http.StatusNotFound output, err = ReportNotFound(contentType) return code, h, output, err } //Render the response into XML output, err = respond.CreateResponseMessage("Report was successfully updated", "200", contentType) if err != nil { code = http.StatusInternalServerError return code, h, output, err } code = http.StatusOK return code, h, output, err }
// Delete function used to implement remove report request 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) charset := "utf-8" //STANDARD DECLARATIONS END // Set Content-Type response Header value contentType := r.Header.Get("Accept") h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) // Grab Tenant DB configuration from context tenantDbConfig := context.Get(r, "tenant_conf").(config.MongoConfig) //Extracting record id from url id := mux.Vars(r)["id"] // Try to open the mongo session session, err := mongo.OpenSession(tenantDbConfig) defer session.Close() if err != nil { code = http.StatusInternalServerError return code, h, output, err } // We search by name and delete the document in db query := bson.M{"id": id} info, err := mongo.Remove(session, tenantDbConfig.Db, reportsColl, query) if err != nil { if err.Error() != "not found" { code = http.StatusInternalServerError return code, h, output, err } //Render the response into XML code = http.StatusNotFound output, err = ReportNotFound(contentType) return code, h, output, err } // info.Removed > 0 means that many documents have been removed // If deletion took place we notify user accordingly. // Else we notify that no tenant matched the specific name if info.Removed > 0 { code = http.StatusOK output, err = respond.CreateResponseMessage("Report was successfully deleted", "200", contentType) } else { code = http.StatusNotFound output, err = ReportNotFound(contentType) } //Render the response into XML if err != nil { code = http.StatusInternalServerError return code, h, output, err } return code, h, output, err }
// Delete function used to implement remove report request 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 contentType, err = respond.ParseAcceptHeader(r) h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) if err != nil { code = http.StatusNotAcceptable output, _ = respond.MarshalContent(respond.NotAcceptableContentType, contentType, "", " ") return code, h, output, err } tenantDbConfig, err := authentication.AuthenticateTenant(r.Header, cfg) if err != nil { output, _ = respond.MarshalContent(respond.UnauthorizedMessage, contentType, "", " ") code = http.StatusUnauthorized //If wrong api key is passed we return UNAUTHORIZED http status return code, h, output, err } //Extracting record id from url id := mux.Vars(r)["id"] // Try to open the mongo session session, err := mongo.OpenSession(tenantDbConfig) defer session.Close() if err != nil { code = http.StatusInternalServerError return code, h, output, err } // We search by name and delete the document in db query := bson.M{"id": id} info, err := mongo.Remove(session, tenantDbConfig.Db, reportsColl, query) if err != nil { if err.Error() != "not found" { code = http.StatusInternalServerError return code, h, output, err } //Render the response into XML code = http.StatusNotFound output, err = ReportNotFound(contentType) return code, h, output, err } // info.Removed > 0 means that many documents have been removed // If deletion took place we notify user accordingly. // Else we notify that no tenant matched the specific name if info.Removed > 0 { code = http.StatusOK output, err = respond.CreateResponseMessage("Report was successfully deleted", "200", contentType) } else { code = http.StatusNotFound output, err = ReportNotFound(contentType) } //Render the response into XML if err != nil { code = http.StatusInternalServerError return code, h, output, err } return code, h, output, err }