Exemple #1
0
func getTeamNames(u *auth.User) ([]string, error) {
	var teams []auth.Team
	if err := db.Session.Teams().Find(bson.M{"users": u.Email}).All(&teams); err != nil {
		return nil, err
	}
	return auth.GetTeamsNames(teams), nil
}
Exemple #2
0
func GetServicesByOwnerTeams(teamKind string, u *auth.User) (services []Service, err error) {
	teams, err := u.Teams()
	teamsNames := auth.GetTeamsNames(teams)
	q := bson.M{teamKind: bson.M{"$in": teamsNames}, "status": bson.M{"$ne": "deleted"}}
	err = db.Session.Services().Find(q).All(&services)
	return
}
Exemple #3
0
func GetServiceInstancesByServicesAndTeams(services []Service, u *auth.User) (sInstances []ServiceInstance, err error) {
	teams, err := u.Teams()
	if err != nil {
		return
	}
	if len(teams) == 0 {
		return
	}
	q, f := genericServiceInstancesFilter(services, auth.GetTeamsNames(teams))
	err = db.Session.ServiceInstances().Find(q).Select(f).All(&sInstances)
	return
}
Exemple #4
0
func GetServicesByTeamKindAndNoRestriction(teamKind string, u *auth.User) (services []Service, err error) {
	teams, err := u.Teams()
	teamsNames := auth.GetTeamsNames(teams)
	q := bson.M{"$or": []bson.M{
		{teamKind: bson.M{"$in": teamsNames}},
		{"is_restricted": false},
	},
		"status": bson.M{"$ne": "deleted"},
	}
	err = db.Session.Services().Find(q).Select(bson.M{"name": 1}).All(&services)
	return
}
Exemple #5
0
func List(u *auth.User) ([]App, error) {
	var apps []App
	if u.IsAdmin() {
		if err := db.Session.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 := db.Session.Apps().Find(bson.M{"teams": bson.M{"$in": teams}}).All(&apps); err != nil {
		return []App{}, err
	}
	return apps, nil
}
Exemple #6
0
func CreateHandler(w http.ResponseWriter, r *http.Request, u *auth.User) error {
	defer r.Body.Close()
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	var sy serviceYaml
	err = goyaml.Unmarshal(body, &sy)
	if err != nil {
		return err
	}
	if _, ok := sy.Endpoint["production"]; !ok {
		return &errors.Http{Code: http.StatusBadRequest, Message: "You must provide a production endpoint in the manifest file."}
	}
	var teams []auth.Team
	db.Session.Teams().Find(bson.M{"users": u.Email}).All(&teams)
	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 := db.Session.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,
		OwnerTeams: auth.GetTeamsNames(teams),
	}
	err = s.Create()
	if err != nil {
		return err
	}
	fmt.Fprint(w, "success")
	return nil
}