func getServiceInstace(instanceName, appName string, u *auth.User) (service.ServiceInstance, app.App, error) { var app app.App conn, err := db.Conn() if err != nil { return service.ServiceInstance{}, app, err } defer conn.Close() instance, err := service.GetInstance(instanceName) if err != nil { err = &errors.Http{Code: http.StatusNotFound, Message: "Instance not found"} return instance, app, err } if !auth.CheckUserAccess(instance.Teams, u) { err = &errors.Http{Code: http.StatusForbidden, Message: "This user does not have access to this instance"} return instance, app, err } err = conn.Apps().Find(bson.M{"name": appName}).One(&app) if err != nil { err = &errors.Http{Code: http.StatusNotFound, Message: fmt.Sprintf("App %s not found.", appName)} return instance, app, err } if !auth.CheckUserAccess(app.Teams, u) { err = &errors.Http{Code: http.StatusForbidden, Message: "This user does not have access to this app"} return instance, app, err } return instance, app, nil }
func getServiceInstanceOrError(name string, u *auth.User) (service.ServiceInstance, error) { si, err := service.GetInstance(name) if err != nil { return si, &errors.Http{Code: http.StatusNotFound, Message: "Service instance not found"} } if !auth.CheckUserAccess(si.Teams, u) { msg := "This user does not have access to this service instance" return si, &errors.Http{Code: http.StatusForbidden, Message: msg} } return si, nil }
func ServiceInstanceStatusHandler(w http.ResponseWriter, r *http.Request, u *auth.User) error { // #TODO (flaviamissi) should check if user has access to service // just call GetServiceInstanceOrError should be enough siName := r.URL.Query().Get(":instance") if siName == "" { return &errors.Http{Code: http.StatusBadRequest, Message: "Service instance name not provided."} } si, err := service.GetInstance(siName) if err != nil { msg := fmt.Sprintf("Service instance does not exists, error: %s", err.Error()) return &errors.Http{Code: http.StatusInternalServerError, Message: msg} } var b string if b, err = si.Status(); err != nil { msg := fmt.Sprintf("Could not retrieve status of service instance, error: %s", err.Error()) return &errors.Http{Code: http.StatusInternalServerError, Message: msg} } b = fmt.Sprintf(`Service instance "%s" is %s`, siName, b) n, err := w.Write([]byte(b)) if n != len(b) { return &errors.Http{Code: http.StatusInternalServerError, Message: "Failed to write response body"} } return nil }