Ejemplo n.º 1
0
func (s *S) TestAddPool(c *check.C) {
	b := bytes.NewBufferString("name=pool1")
	req, err := http.NewRequest("POST", "/pools", b)
	c.Assert(err, check.IsNil)
	req.Header.Set("Authorization", "bearer "+s.token.GetValue())
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	rec := httptest.NewRecorder()
	defer provision.RemovePool("pool1")
	m := RunServer(true)
	m.ServeHTTP(rec, req)
	c.Assert(rec.Code, check.Equals, http.StatusCreated)
	c.Assert(err, check.IsNil)
	pools, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(pools, check.HasLen, 1)
	b = bytes.NewBufferString("name=pool2&public=true")
	req, err = http.NewRequest("POST", "/pools", b)
	c.Assert(err, check.IsNil)
	req.Header.Set("Authorization", "bearer "+s.token.GetValue())
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	rec = httptest.NewRecorder()
	defer provision.RemovePool("pool2")
	m.ServeHTTP(rec, req)
	c.Assert(rec.Code, check.Equals, http.StatusCreated)
	pools, err = provision.ListPools(bson.M{"_id": "pool2"})
	c.Assert(err, check.IsNil)
	c.Assert(pools[0].Public, check.Equals, true)
}
Ejemplo n.º 2
0
func listPoolsToUser(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(u.Email, "pool-list")
	teams, err := u.Teams()
	if err != nil {
		return err
	}
	var poolsByTeam []PoolsByTeam
	for _, t := range teams {
		pools, err := provision.ListPools(bson.M{"teams": t.Name})
		if err != nil {
			return err
		}
		pbt := PoolsByTeam{Team: t.Name, Pools: provision.GetPoolsNames(pools)}
		poolsByTeam = append(poolsByTeam, pbt)
	}
	publicPools, err := provision.ListPools(bson.M{"public": true})
	if err != nil {
		return err
	}
	p := map[string]interface{}{
		"pools_by_team": poolsByTeam,
		"public_pools":  publicPools,
	}
	w.Header().Set("Content-Type", "application/json")
	return json.NewEncoder(w).Encode(p)
}
Ejemplo n.º 3
0
func listPoolsToUser(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(u.Email, "pool-list")
	teams := []string{}
	contexts := permission.ContextsForPermission(t, permission.PermAppCreate)
	for _, c := range contexts {
		if c.CtxType == permission.CtxGlobal {
			teams = nil
			break
		}
		if c.CtxType != permission.CtxTeam {
			continue
		}
		teams = append(teams, c.Value)
	}
	var filter bson.M
	if teams != nil {
		filter = bson.M{"teams": bson.M{"$in": teams}}
	}
	pools, err := provision.ListPools(filter)
	if err != nil {
		return err
	}
	poolsByTeam := []PoolsByTeam{}
	poolsByTeamMap := map[string]*PoolsByTeam{}
	for _, p := range pools {
		for _, t := range p.Teams {
			if poolsByTeamMap[t] == nil {
				poolsByTeam = append(poolsByTeam, PoolsByTeam{Team: t})
				poolsByTeamMap[t] = &poolsByTeam[len(poolsByTeam)-1]
			}
			poolsByTeamMap[t].Pools = append(poolsByTeamMap[t].Pools, p.Name)
		}
	}
	publicPools, err := provision.ListPools(bson.M{"public": true})
	if err != nil {
		return err
	}
	allowedDefault := permission.Check(t, permission.PermPoolUpdate)
	defaultPool := []provision.Pool{}
	if allowedDefault {
		defaultPool, err = provision.ListPools(bson.M{"default": true})
		if err != nil {
			return err
		}
	}
	p := map[string]interface{}{
		"pools_by_team": poolsByTeam,
		"public_pools":  publicPools,
		"default_pool":  defaultPool,
	}
	w.Header().Set("Content-Type", "application/json")
	return json.NewEncoder(w).Encode(p)
}
Ejemplo n.º 4
0
func (s *S) TestPoolListHandler(c *check.C) {
	team := auth.Team{Name: "angra"}
	err := s.conn.Teams().Insert(team)
	c.Assert(err, check.IsNil)
	token := userWithPermission(c, permission.Permission{
		Scheme:  permission.PermAppCreate,
		Context: permission.Context(permission.CtxTeam, "angra"),
	})
	pool := provision.Pool{Name: "pool1", Teams: []string{"angra"}}
	opts := provision.AddPoolOptions{Name: pool.Name}
	err = provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	err = provision.AddTeamsToPool(pool.Name, pool.Teams)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool(pool.Name)
	opts = provision.AddPoolOptions{Name: "nopool"}
	err = provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool("nopool")
	defaultPools, err := provision.ListPools(bson.M{"default": true})
	c.Assert(err, check.IsNil)
	expected := []provision.Pool{
		defaultPools[0],
		{Name: "pool1", Teams: []string{"angra"}},
	}
	req, err := http.NewRequest("GET", "/pools", nil)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = poolList(rec, req, token)
	c.Assert(err, check.IsNil)
	var pools []provision.Pool
	err = json.NewDecoder(rec.Body).Decode(&pools)
	c.Assert(err, check.IsNil)
	c.Assert(pools, check.DeepEquals, expected)
}
Ejemplo n.º 5
0
func listPoolHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	pools, err := provision.ListPools(nil)
	if err != nil {
		return err
	}
	return json.NewEncoder(w).Encode(pools)
}
Ejemplo n.º 6
0
func (app *App) GetPoolForApp(poolName string) (string, error) {
	var query bson.M
	var poolTeam bool
	if poolName != "" {
		query = bson.M{"_id": poolName}
	} else {
		query = bson.M{"teams": app.TeamOwner}
	}
	pools, err := provision.ListPools(query)
	if err != nil {
		return "", err
	}
	if len(pools) > 1 {
		return "", stderr.New("you have access to more than one pool, please choose one in app creation")
	}
	if len(pools) == 0 {
		if poolName == "" {
			return "", nil
		}
		return "", stderr.New("pool not found")
	}
	for _, team := range pools[0].Teams {
		if team == app.TeamOwner {
			poolTeam = true
			break
		}
	}
	if !pools[0].Public && !poolTeam {
		return "", stderr.New(fmt.Sprintf("You don't have access to pool %s", poolName))
	}
	return pools[0].Name, nil
}
Ejemplo n.º 7
0
func (s *S) TestPoolListPublicPool(c *check.C) {
	pool := provision.Pool{Name: "pool1", Public: true}
	opts := provision.AddPoolOptions{Name: pool.Name, Public: pool.Public}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool(pool.Name)
	defaultPools, err := provision.ListPools(bson.M{"default": true})
	c.Assert(err, check.IsNil)
	expected := []provision.Pool{
		defaultPools[0],
		{Name: "pool1", Public: true, Teams: []string{}},
	}
	token := userWithPermission(c, permission.Permission{
		Scheme:  permission.PermTeamCreate,
		Context: permission.Context(permission.CtxGlobal, ""),
	})
	req, err := http.NewRequest("GET", "/pools", nil)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = poolList(rec, req, token)
	c.Assert(err, check.IsNil)
	var pools []provision.Pool
	err = json.NewDecoder(rec.Body).Decode(&pools)
	c.Assert(err, check.IsNil)
	c.Assert(pools, check.DeepEquals, expected)
}
Ejemplo n.º 8
0
func (app *App) GetDefaultPool() (string, error) {
	pools, err := provision.ListPools(bson.M{"default": true})
	if err != nil {
		return "", err
	}
	if len(pools) == 0 {
		return "", stderr.New("No default pool.")
	}
	return pools[0].Name, nil
}
Ejemplo n.º 9
0
func (app *App) GetFallbackPool() (string, error) {
	query := bson.M{"$or": []bson.M{{"teams": bson.M{"$exists": false}}, {"teams": bson.M{"$size": 0}}}}
	pools, err := provision.ListPools(query)
	if err != nil {
		return "", err
	}
	if len(pools) == 0 {
		return "", stderr.New("No fallback pool.")
	}
	return pools[0].Name, nil
}
Ejemplo n.º 10
0
func (s *S) TestAddPoolHandler(c *check.C) {
	b := bytes.NewBufferString(`{"name": "pool1"}`)
	req, err := http.NewRequest("POST", "/pool", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	defer provision.RemovePool("pool1")
	err = addPoolHandler(rec, req, nil)
	c.Assert(err, check.IsNil)
	pools, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(len(pools), check.Equals, 1)
}
Ejemplo n.º 11
0
func (p *dockerProvisioner) Nodes(app provision.App) ([]cluster.Node, error) {
	pool := app.GetPool()
	var (
		pools []provision.Pool
		err   error
	)
	if pool == "" {
		pools, err = provision.ListPools(bson.M{"$or": []bson.M{{"teams": app.GetTeamOwner()}, {"teams": bson.M{"$in": app.GetTeamsName()}}}})
	} else {
		pools, err = provision.ListPools(bson.M{"_id": pool})
	}
	if err != nil {
		return nil, err
	}
	if len(pools) == 0 {
		query := bson.M{"default": true}
		pools, err = provision.ListPools(query)
		if err != nil {
			return nil, err
		}
	}
	if len(pools) == 0 {
		return nil, errNoDefaultPool
	}
	for _, pool := range pools {
		nodes, err := p.Cluster().NodesForMetadata(map[string]string{"pool": pool.Name})
		if err != nil {
			return nil, errNoDefaultPool
		}
		if len(nodes) > 0 {
			return nodes, nil
		}
	}
	var nameList []string
	for _, pool := range pools {
		nameList = append(nameList, pool.Name)
	}
	poolsStr := strings.Join(nameList, ", pool=")
	return nil, fmt.Errorf("No nodes found with one of the following metadata: pool=%s", poolsStr)
}
Ejemplo n.º 12
0
func (s *S) TestAddPoolHandler(c *check.C) {
	b := bytes.NewBufferString(`{"name": "pool1"}`)
	req, err := http.NewRequest("POST", "/pool", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	defer provision.RemovePool("pool1")
	err = addPoolHandler(rec, req, s.token)
	c.Assert(err, check.IsNil)
	pools, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(pools, check.HasLen, 1)
	b = bytes.NewBufferString(`{"name": "pool2", "public": true}`)
	req, err = http.NewRequest("POST", "/pool", b)
	c.Assert(err, check.IsNil)
	rec = httptest.NewRecorder()
	defer provision.RemovePool("pool2")
	err = addPoolHandler(rec, req, s.token)
	c.Assert(err, check.IsNil)
	pools, err = provision.ListPools(bson.M{"_id": "pool2"})
	c.Assert(err, check.IsNil)
	c.Assert(pools[0].Public, check.Equals, true)
}
Ejemplo n.º 13
0
func (s *S) TestRemovePoolHandler(c *check.C) {
	err := provision.AddPool("pool1", false)
	c.Assert(err, check.IsNil)
	b := bytes.NewBufferString(`{"pool": "pool1"}`)
	req, err := http.NewRequest("DELETE", "/pool", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = removePoolHandler(rec, req, nil)
	c.Assert(err, check.IsNil)
	p, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(len(p), check.Equals, 0)
}
Ejemplo n.º 14
0
func (s *S) TestPoolUpdateToPublicHandler(c *check.C) {
	opts := provision.AddPoolOptions{Name: "pool1"}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool("pool1")
	b := bytes.NewBufferString(`{"public": true}`)
	req, err := http.NewRequest("POST", "/pool/pool1?:name=pool1", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = poolUpdateHandler(rec, req, nil)
	c.Assert(err, check.IsNil)
	p, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(p[0].Public, check.Equals, true)
}
Ejemplo n.º 15
0
func (s *S) TestAddTeamsToPoolHandler(c *check.C) {
	pool := provision.Pool{Name: "pool1"}
	err := provision.AddPool(pool.Name, false)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool(pool.Name)
	b := bytes.NewBufferString(`{"pool": "pool1", "teams": ["test"]}`)
	req, err := http.NewRequest("POST", "/pool/team", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = addTeamToPoolHandler(rec, req, nil)
	c.Assert(err, check.IsNil)
	p, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(p[0].Teams, check.DeepEquals, []string{"test"})
}
Ejemplo n.º 16
0
func (s *S) TestRemovePoolHandler(c *check.C) {
	opts := provision.AddPoolOptions{
		Name: "pool1",
	}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	b := bytes.NewBufferString(`{"pool": "pool1"}`)
	req, err := http.NewRequest("DELETE", "/pool", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = removePoolHandler(rec, req, s.token)
	c.Assert(err, check.IsNil)
	p, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(p, check.HasLen, 0)
}
Ejemplo n.º 17
0
func (s *S) TestRemovePoolHandler(c *check.C) {
	opts := provision.AddPoolOptions{
		Name: "pool1",
	}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	req, err := http.NewRequest("DELETE", "/pools/pool1", nil)
	c.Assert(err, check.IsNil)
	req.Header.Set("Authorization", "bearer "+s.token.GetValue())
	rec := httptest.NewRecorder()
	m := RunServer(true)
	m.ServeHTTP(rec, req)
	c.Assert(rec.Code, check.Equals, http.StatusOK)
	p, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(p, check.HasLen, 0)
}
Ejemplo n.º 18
0
func (s *S) TestRemoveTeamsToPoolHandler(c *check.C) {
	pool := provision.Pool{Name: "pool1", Teams: []string{"test"}}
	opts := provision.AddPoolOptions{Name: pool.Name}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	err = provision.AddTeamsToPool(pool.Name, pool.Teams)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool(pool.Name)
	b := bytes.NewBufferString(`{"pool": "pool1", "teams": ["test"]}`)
	req, err := http.NewRequest("DELETE", "/pool/pool1/team?:name=pool1", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = removeTeamToPoolHandler(rec, req, nil)
	c.Assert(err, check.IsNil)
	p, err := provision.ListPools(nil)
	c.Assert(err, check.IsNil)
	c.Assert(p[0].Teams, check.DeepEquals, []string{})
}
Ejemplo n.º 19
0
func (s *S) TestPoolUpdateToPublicHandler(c *check.C) {
	opts := provision.AddPoolOptions{Name: "pool1"}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool("pool1")
	b := bytes.NewBufferString("public=true")
	req, err := http.NewRequest("PUT", "/pools/pool1", b)
	c.Assert(err, check.IsNil)
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Authorization", "bearer "+s.token.GetValue())
	rec := httptest.NewRecorder()
	m := RunServer(true)
	m.ServeHTTP(rec, req)
	c.Assert(rec.Code, check.Equals, http.StatusOK)
	c.Assert(err, check.IsNil)
	p, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(p[0].Public, check.Equals, true)
}
Ejemplo n.º 20
0
func (s *S) TestAddTeamsToPool(c *check.C) {
	pool := provision.Pool{Name: "pool1"}
	opts := provision.AddPoolOptions{Name: pool.Name}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool(pool.Name)
	b := strings.NewReader("team=test")
	req, err := http.NewRequest("POST", "/pools/pool1/team", b)
	c.Assert(err, check.IsNil)
	req.Header.Set("Authorization", "bearer "+s.token.GetValue())
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	rec := httptest.NewRecorder()
	m := RunServer(true)
	m.ServeHTTP(rec, req)
	c.Assert(rec.Code, check.Equals, http.StatusOK)
	p, err := provision.ListPools(bson.M{"_id": "pool1"})
	c.Assert(err, check.IsNil)
	c.Assert(p[0].Teams, check.DeepEquals, []string{"test"})
}
Ejemplo n.º 21
0
func (s *S) TestPoolUpdateOverwriteDefaultPoolHandler(c *check.C) {
	provision.RemovePool("test1")
	opts := provision.AddPoolOptions{Name: "pool1", Default: true}
	err := provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool("pool1")
	opts = provision.AddPoolOptions{Name: "pool2"}
	err = provision.AddPool(opts)
	c.Assert(err, check.IsNil)
	defer provision.RemovePool("pool2")
	b := bytes.NewBufferString(`{"default": true}`)
	req, err := http.NewRequest("POST", "/pool/pool1?:name=pool2&force=true", b)
	c.Assert(err, check.IsNil)
	rec := httptest.NewRecorder()
	err = poolUpdateHandler(rec, req, s.token)
	c.Assert(err, check.IsNil)
	p, err := provision.ListPools(bson.M{"_id": "pool2"})
	c.Assert(err, check.IsNil)
	c.Assert(p[0].Default, check.Equals, true)
}
Ejemplo n.º 22
0
func (s *HandlersSuite) SetUpSuite(c *check.C) {
	config.Set("database:name", "docker_provision_handlers_tests_s")
	config.Set("docker:collection", "docker_handler_suite")
	config.Set("docker:run-cmd:port", 8888)
	config.Set("docker:router", "fake")
	config.Set("docker:cluster:mongo-url", "127.0.0.1:27017")
	config.Set("docker:cluster:mongo-database", "docker_provision_handlers_tests_cluster_stor")
	config.Set("docker:repository-namespace", "tsuru")
	config.Set("queue:mongo-url", "127.0.0.1:27017")
	config.Set("queue:mongo-database", "queue_provision_docker_tests_handlers")
	config.Set("iaas:default", "test-iaas")
	config.Set("iaas:node-protocol", "http")
	config.Set("iaas:node-port", 1234)
	config.Set("admin-team", "admin")
	config.Set("routers:fake:type", "fake")
	var err error
	s.conn, err = db.Conn()
	c.Assert(err, check.IsNil)
	clusterDbUrl, _ := config.GetString("docker:cluster:mongo-url")
	s.clusterSess, err = mgo.Dial(clusterDbUrl)
	c.Assert(err, check.IsNil)
	pools, err := provision.ListPools(nil)
	c.Assert(err, check.IsNil)
	for _, pool := range pools {
		err = provision.RemovePool(pool.Name)
		c.Assert(err, check.IsNil)
	}
	s.user = &auth.User{Email: "*****@*****.**", Password: "******", Quota: quota.Unlimited}
	nativeScheme := auth.ManagedScheme(native.NativeScheme{})
	app.AuthScheme = nativeScheme
	_, err = nativeScheme.Create(s.user)
	c.Assert(err, check.IsNil)
	s.team = &auth.Team{Name: "admin", Users: []string{s.user.Email}}
	err = s.conn.Teams().Insert(s.team)
	c.Assert(err, check.IsNil)
	s.token, err = nativeScheme.Login(map[string]string{"email": s.user.Email, "password": "******"})
	c.Assert(err, check.IsNil)
}
Ejemplo n.º 23
0
func (app *App) GetPoolForApp(poolName string) (string, error) {
	var query bson.M
	if poolName != "" {
		byTeamOwner := bson.M{"$and": []bson.M{{"_id": poolName}, {"teams": app.TeamOwner}}}
		publicPool := bson.M{"$and": []bson.M{{"_id": poolName}, {"public": true}}}
		query = bson.M{"$or": []bson.M{byTeamOwner, publicPool}}
	} else {
		query = bson.M{"teams": app.TeamOwner}
	}
	pools, err := provision.ListPools(query)
	if err != nil {
		return "", err
	}
	if len(pools) > 1 {
		return "", ManyPoolsError
	}
	if len(pools) == 0 && poolName != "" {
		return "", stderr.New(fmt.Sprintf("You don't have access to pool %s", poolName))
	}
	if len(pools) == 0 {
		return "", nil
	}
	return pools[0].Name, nil
}