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 }
// ListSuperGroupResults supergroup availabilities according to the http request func ListSuperGroupResults(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)) // Parse the request into the input urlValues := r.URL.Query() vars := mux.Vars(r) // Grab Tenant DB configuration from context tenantDbConfig := context.Get(r, "tenant_conf").(config.MongoConfig) session, err := mongo.OpenSession(tenantDbConfig) defer mongo.CloseSession(session) if err != nil { code = http.StatusInternalServerError return code, h, output, err } report := reports.MongoInterface{} err = mongo.FindOne(session, tenantDbConfig.Db, "reports", bson.M{"info.name": vars["report_name"]}, &report) if err != nil { code = http.StatusInternalServerError return code, h, output, err } input := endpointGroupResultQuery{ basicQuery{ Name: vars["group_name"], Granularity: urlValues.Get("granularity"), Format: contentType, StartTime: urlValues.Get("start_time"), EndTime: urlValues.Get("end_time"), Report: report, Vars: vars, }, "", } tenantDB := session.DB(tenantDbConfig.Db) errs := input.Validate(tenantDB) if len(errs) > 0 { out := respond.BadRequestSimple out.Errors = errs output = out.MarshalTo(contentType) code = 400 return code, h, output, err } results := []SuperGroupInterface{} if err != nil { code = http.StatusInternalServerError return code, h, output, err } // Construct the query to mongodb based on the input filter := bson.M{ "date": bson.M{"$gte": input.StartTimeInt, "$lte": input.EndTimeInt}, "report": report.ID, } if input.Name != "" { filter["supergroup"] = input.Name } // Select the granularity of the search daily/monthly if input.Granularity == "daily" { customForm[0] = "20060102" customForm[1] = "2006-01-02" query := DailySuperGroup(filter) err = mongo.Pipe(session, tenantDbConfig.Db, "endpoint_group_ar", query, &results) } else if input.Granularity == "monthly" { customForm[0] = "200601" customForm[1] = "2006-01" query := MonthlySuperGroup(filter) err = mongo.Pipe(session, tenantDbConfig.Db, "endpoint_group_ar", query, &results) } // mongo.Find(session, tenantDbConfig.Db, "endpoint_group_ar", bson.M{}, "_id", &results) if err != nil { code = http.StatusInternalServerError return code, h, output, err } if len(results) == 0 { code = http.StatusNotFound message := "No results found for given query" output, err = createErrorMessage(message, code, contentType) return code, h, output, err } output, err = createSuperGroupView(results, report, input.Format) if err != nil { code = http.StatusInternalServerError return code, h, output, err } return code, h, output, err }
// ListServiceFlavorResults is responsible for handling request to list service flavor results func ListServiceFlavorResults(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 := "application/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 } // Parse the request into the input urlValues := r.URL.Query() vars := mux.Vars(r) tenantDbConfig, err := authentication.AuthenticateTenant(r.Header, cfg) if err != nil { if err.Error() == "Unauthorized" { code = http.StatusUnauthorized out := respond.UnauthorizedMessage output = out.MarshalTo(contentType) return code, h, output, err } code = http.StatusInternalServerError return code, h, output, err } session, err := mongo.OpenSession(tenantDbConfig) defer mongo.CloseSession(session) if err != nil { code = http.StatusInternalServerError return code, h, output, err } report := reports.MongoInterface{} err = mongo.FindOne(session, tenantDbConfig.Db, "reports", bson.M{"info.name": vars["report_name"]}, &report) if err != nil { code = http.StatusBadRequest message := "The report with the name " + vars["report_name"] + " does not exist" output, err := createErrorMessage(message, contentType) //Render the response into XML or JSON h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } input := serviceFlavorResultQuery{ basicQuery: basicQuery{ Name: vars["service_type"], Granularity: urlValues.Get("granularity"), Format: contentType, StartTime: urlValues.Get("start_time"), EndTime: urlValues.Get("end_time"), Report: report, Vars: vars, }, EndpointGroup: vars["lgroup_name"], } tenantDB := session.DB(tenantDbConfig.Db) errs := input.Validate(tenantDB) if len(errs) > 0 { out := respond.BadRequestSimple out.Errors = errs output = out.MarshalTo(contentType) code = 400 return code, h, output, err } if vars["lgroup_type"] != report.GetEndpointGroupType() { code = http.StatusBadRequest message := "The report " + vars["report_name"] + " does not define endpoint group type: " + vars["lgroup_type"] + ". Try using " + report.GetEndpointGroupType() + " instead." output, err := createErrorMessage(message, contentType) //Render the response into XML or JSON h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } results := []ServiceFlavorInterface{} if err != nil { code = http.StatusInternalServerError return code, h, output, err } // Construct the query to mongodb based on the input filter := bson.M{ "date": bson.M{"$gte": input.StartTimeInt, "$lte": input.EndTimeInt}, "report": report.ID, } if input.Name != "" { filter["name"] = input.Name } if input.EndpointGroup != "" { filter["supergroup"] = input.EndpointGroup } // Select the granularity of the search daily/monthly if input.Granularity == "daily" { customForm[0] = "20060102" customForm[1] = "2006-01-02" query := DailyServiceFlavor(filter) err = mongo.Pipe(session, tenantDbConfig.Db, "service_ar", query, &results) } else if input.Granularity == "monthly" { customForm[0] = "200601" customForm[1] = "2006-01" query := MonthlyServiceFlavor(filter) err = mongo.Pipe(session, tenantDbConfig.Db, "service_ar", query, &results) } // mongo.Find(session, tenantDbConfig.Db, "endpoint_group_ar", bson.M{}, "_id", &results) if err != nil { code = http.StatusInternalServerError return code, h, output, err } output, err = createServiceFlavorResultView(results, report, input.Format) if err != nil { code = http.StatusInternalServerError return code, h, output, err } 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 }