func serviceInfo(w http.ResponseWriter, r *http.Request, t *auth.Token) error { u, err := t.User() if err != nil { return err } serviceName := r.URL.Query().Get(":name") rec.Log(u.Email, "service-info", serviceName) _, err = getServiceOrError(serviceName, u) if err != nil { return err } instances := []service.ServiceInstance{} teams, err := u.Teams() if err != nil { return err } conn, err := db.Conn() if err != nil { return err } defer conn.Close() teamsNames := auth.GetTeamsNames(teams) q := bson.M{"service_name": serviceName, "teams": bson.M{"$in": teamsNames}} err = conn.ServiceInstances().Find(q).All(&instances) if err != nil { return err } b, err := json.Marshal(instances) if err != nil { return nil } w.Write(b) return nil }
// List returns the list of apps that the given user has access to. // // If the user does not have access to any app, this function returns an empty // list and a nil error. // // The list can be filtered through the filter parameter. func List(u *auth.User, filter *Filter) ([]App, error) { var apps []App conn, err := db.Conn() if err != nil { return nil, err } defer conn.Close() query := filter.Query() if u == nil || u.IsAdmin() { if err = conn.Apps().Find(query).All(&apps); err != nil { return []App{}, err } return apps, nil } ts, err := u.Teams() if err != nil { return []App{}, err } teams := auth.GetTeamsNames(ts) query["teams"] = bson.M{"$in": teams} if err := conn.Apps().Find(query).All(&apps); err != nil { return []App{}, err } return apps, nil }
func GetServiceInstancesByServicesAndTeams(services []Service, u *auth.User, appName string) ([]ServiceInstance, error) { var instances []ServiceInstance teams, err := u.Teams() if err != nil { return nil, err } if len(teams) == 0 { return nil, nil } conn, err := db.Conn() if err != nil { return nil, err } defer conn.Close() var teamNames []string if !u.IsAdmin() { teamNames = auth.GetTeamsNames(teams) } query := genericServiceInstancesFilter(services, teamNames) if appName != "" { query["apps"] = appName } err = conn.ServiceInstances().Find(query).All(&instances) return instances, err }
func serviceCreate(w http.ResponseWriter, r *http.Request, t auth.Token) error { defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { return err } var sy serviceYaml err = yaml.Unmarshal(body, &sy) if err != nil { return err } err = sy.validate() if err != nil { return err } u, err := t.User() if err != nil { return err } rec.Log(u.Email, "create-service", sy.Id, sy.Endpoint) conn, err := db.Conn() if err != nil { return err } defer conn.Close() var teams []auth.Team err = conn.Teams().Find(bson.M{"users": u.Email}).All(&teams) if err != nil { return err } if len(teams) == 0 { msg := "In order to create a service, you should be member of at least one team" return &errors.HTTP{Code: http.StatusForbidden, Message: msg} } n, err := conn.Services().Find(bson.M{"_id": sy.Id}).Count() if err != nil { return &errors.HTTP{Code: http.StatusInternalServerError, Message: err.Error()} } if n != 0 { msg := fmt.Sprintf("Service with name %s already exists.", sy.Id) return &errors.HTTP{Code: http.StatusInternalServerError, Message: msg} } s := service.Service{ Name: sy.Id, Endpoint: sy.Endpoint, Password: sy.Password, OwnerTeams: auth.GetTeamsNames(teams), } err = s.Create() if err != nil { return err } fmt.Fprint(w, "success") return nil }
func listUsers(w http.ResponseWriter, r *http.Request, t auth.Token) error { users, err := auth.ListUsers() if err != nil { return err } apiUsers := make([]apiUser, len(users)) for i, user := range users { var teamsNames []string if teams, err := user.Teams(); err == nil { teamsNames = auth.GetTeamsNames(teams) } apiUsers[i] = apiUser{Email: user.Email, Teams: teamsNames} } return json.NewEncoder(w).Encode(apiUsers) }
func GetServicesByOwnerTeams(teamKind string, u *auth.User) ([]Service, error) { teams, err := u.Teams() if err != nil { return nil, err } conn, err := db.Conn() if err != nil { return nil, err } defer conn.Close() teamsNames := auth.GetTeamsNames(teams) q := bson.M{teamKind: bson.M{"$in": teamsNames}} var services []Service err = conn.Services().Find(q).All(&services) return services, err }
func GetServiceInstancesByServicesAndTeams(services []Service, u *auth.User) ([]ServiceInstance, error) { var instances []ServiceInstance teams, err := u.Teams() if err != nil { return nil, err } if len(teams) == 0 { return nil, nil } conn, err := db.Conn() if err != nil { return nil, err } defer conn.Close() q, f := genericServiceInstancesFilter(services, auth.GetTeamsNames(teams)) err = conn.ServiceInstances().Find(q).Select(f).All(&instances) return instances, err }
func GetServicesByTeamKindAndNoRestriction(teamKind string, u *auth.User) ([]Service, error) { teams, err := u.Teams() if err != nil { return nil, err } conn, err := db.Conn() if err != nil { return nil, err } defer conn.Close() teamsNames := auth.GetTeamsNames(teams) q := bson.M{"$or": []bson.M{ {teamKind: bson.M{"$in": teamsNames}}, {"is_restricted": false}, }} var services []Service err = conn.Services().Find(q).Select(bson.M{"name": 1}).All(&services) return services, err }
// List returns the list of apps that the given user has access to. // // If the user does not have acces to any app, this function returns an empty // list and a nil error. func List(u *auth.User) ([]App, error) { var apps []App conn, err := db.Conn() if err != nil { return nil, err } defer conn.Close() if u.IsAdmin() { if err := conn.Apps().Find(nil).All(&apps); err != nil { return []App{}, err } return apps, nil } ts, err := u.Teams() if err != nil { return []App{}, err } teams := auth.GetTeamsNames(ts) if err := conn.Apps().Find(bson.M{"teams": bson.M{"$in": teams}}).All(&apps); err != nil { return []App{}, err } return apps, nil }