Ejemplo n.º 1
0
func (s *S) TestSetCName(c *check.C) {
	err := router.Store("myapp", "myapp", routerName)
	c.Assert(err, check.IsNil)
	data := galebData{
		Name:       "myapp",
		RootRuleId: "myrootrule",
	}
	err = data.save()
	c.Assert(err, check.IsNil)
	s.handler.ConditionalContent = map[string]interface{}{
		"/api/virtualhost/": `{"_links":{"self":"vhX"}}`,
	}
	s.handler.RspCode = http.StatusCreated
	gRouter, err := createRouter("routers:galeb")
	c.Assert(err, check.IsNil)
	err = gRouter.SetCName("my.new.cname", "myapp")
	c.Assert(err, check.IsNil)
	c.Assert(s.handler.Url, check.DeepEquals, []string{"/api/virtualhost/"})
	dbData, err := getGalebData("myapp")
	c.Assert(err, check.IsNil)
	c.Assert(dbData.CNames, check.DeepEquals, []galebCNameData{
		{CName: "my.new.cname", VirtualHostId: "vhX"},
	})
	result := map[string]interface{}{}
	err = json.Unmarshal(s.handler.Body[0], &result)
	c.Assert(err, check.IsNil)
	c.Assert(result, check.DeepEquals, map[string]interface{}{
		"name": "my.new.cname", "farmtype": "", "plan": "", "environment": "", "project": "", "rule_default": "myrootrule",
	})
}
Ejemplo n.º 2
0
func (s *S) TestAddRouteParsesURL(c *check.C) {
	err := router.Store("myapp", "myapp", routerName)
	c.Assert(err, check.IsNil)
	data := galebData{
		Name:          "myapp",
		BackendPoolId: "mybackendpoolid",
	}
	err = data.save()
	c.Assert(err, check.IsNil)
	s.handler.ConditionalContent = map[string]interface{}{
		"/api/backend/": `{"_links":{"self":"backend1"}}`,
	}
	s.handler.RspCode = http.StatusCreated
	gRouter, err := createRouter("routers:galeb")
	c.Assert(err, check.IsNil)
	addr, _ := url.Parse("http://10.9.9.9:11001/")
	err = gRouter.AddRoute("myapp", addr)
	c.Assert(err, check.IsNil)
	c.Assert(s.handler.Url, check.DeepEquals, []string{"/api/backend/"})
	dbData, err := getGalebData("myapp")
	c.Assert(err, check.IsNil)
	c.Assert(dbData.Reals, check.DeepEquals, []galebRealData{
		{Real: "10.9.9.9:11001", BackendId: "backend1"},
	})
	result := map[string]interface{}{}
	err = json.Unmarshal(s.handler.Body[0], &result)
	c.Assert(err, check.IsNil)
	c.Assert(result, check.DeepEquals, map[string]interface{}{
		"ip": "10.9.9.9", "port": float64(11001), "backendpool": "mybackendpoolid",
	})
}
Ejemplo n.º 3
0
func (s *S) TestRemoveBackend(c *check.C) {
	s.handler.RspCode = http.StatusNoContent
	err := router.Store("myapp", "myapp", routerName)
	c.Assert(err, check.IsNil)
	data := galebData{
		Name:          "myapp",
		BackendPoolId: s.server.URL + "/api/backend1",
		RootRuleId:    s.server.URL + "/api/rule1",
		VirtualHostId: s.server.URL + "/api/vh1",
		CNames: []galebCNameData{
			{CName: "my.1.cname", VirtualHostId: s.server.URL + "/api/vh2"},
			{CName: "my.2.cname", VirtualHostId: s.server.URL + "/api/vh3"},
		},
	}
	err = data.save()
	c.Assert(err, check.IsNil)
	gRouter, err := createRouter("routers:galeb")
	c.Assert(err, check.IsNil)
	err = gRouter.RemoveBackend("myapp")
	c.Assert(err, check.IsNil)
	c.Assert(s.handler.Url, check.DeepEquals, []string{
		"/api/vh1", "/api/vh2", "/api/vh3", "/api/rule1", "/api/backend1",
	})
	_, err = router.Retrieve("myapp")
	c.Assert(err, check.Equals, router.ErrBackendNotFound)
	_, err = getGalebData("myapp")
	c.Assert(err, check.ErrorMatches, "not found")
}
Ejemplo n.º 4
0
func (r elbRouter) AddBackend(name string) error {
	var err error
	options := elb.CreateLoadBalancer{
		Name: name,
		Listeners: []elb.Listener{
			{
				InstancePort:     80,
				InstanceProtocol: "HTTP",
				LoadBalancerPort: 80,
				Protocol:         "HTTP",
			},
		},
	}
	vpc, _ := config.GetBool("juju:elb-use-vpc")
	if vpc {
		options.Subnets, err = config.GetList("juju:elb-vpc-subnets")
		if err != nil {
			return err
		}
		options.SecurityGroups, err = config.GetList("juju:elb-vpc-secgroups")
		if err != nil {
			return err
		}
		options.Scheme = "internal"
	} else {
		options.AvailZones, err = config.GetList("juju:elb-avail-zones")
		if err != nil {
			return err
		}
	}
	_, err = r.elb().CreateLoadBalancer(&options)
	return router.Store(name, name)
}
Ejemplo n.º 5
0
func (r *fakeRouter) AddBackend(name string) error {
	if r.HasBackend(name) {
		return errors.New("Backend already exists")
	}
	r.mutex.Lock()
	defer r.mutex.Unlock()
	r.backends[name] = nil
	return router.Store(name, name, "fake")
}
Ejemplo n.º 6
0
func (r *fakeRouter) AddBackend(name string) error {
	if r.HasBackend(name) {
		return router.ErrBackendExists
	}
	r.mutex.Lock()
	defer r.mutex.Unlock()
	r.backends[name] = nil
	return router.Store(name, name, "fake")
}
Ejemplo n.º 7
0
func (s *S) TestAddr(c *check.C) {
	err := router.Store("myapp", "myapp", routerName)
	c.Assert(err, check.IsNil)
	data := galebData{
		Name: "myapp",
	}
	err = data.save()
	c.Assert(err, check.IsNil)
	gRouter, err := createRouter("routers:galeb")
	c.Assert(err, check.IsNil)
	addr, err := gRouter.Addr("myapp")
	c.Assert(addr, check.Equals, "myapp.galeb.com")
}
Ejemplo n.º 8
0
func (hipacheRouter) AddBackend(name string) error {
	domain, err := config.GetString("hipache:domain")
	if err != nil {
		return &routeError{"add", err}
	}
	frontend := "frontend:" + name + "." + domain
	conn := connect()
	defer conn.Close()
	_, err = conn.Do("RPUSH", frontend, name)
	if err != nil {
		return &routeError{"add", err}
	}
	return router.Store(name, name)
}
Ejemplo n.º 9
0
Archivo: fusis.go Proyecto: tsuru/tsuru
func (r *fusisRouter) addBackend(name string, proto string, port uint16) error {
	srv := fusisTypes.Service{
		Name:      name,
		Port:      port,
		Protocol:  proto,
		Scheduler: r.scheduler,
	}
	_, err := r.client.CreateService(srv)
	if err != nil {
		if err == fusisTypes.ErrServiceAlreadyExists {
			return router.ErrBackendExists
		}
		return err
	}
	return router.Store(name, name, routerType)
}
Ejemplo n.º 10
0
func (s *S) TestRoutes(c *check.C) {
	err := router.Store("myapp", "myapp", routerName)
	c.Assert(err, check.IsNil)
	data := galebData{
		Name: "myapp",
		Reals: []galebRealData{
			{Real: "10.1.1.10", BackendId: s.server.URL + "/api/backend1"},
			{Real: "10.1.1.11", BackendId: s.server.URL + "/api/backend2"},
		},
	}
	err = data.save()
	c.Assert(err, check.IsNil)
	gRouter, err := createRouter("routers:galeb")
	c.Assert(err, check.IsNil)
	routes, err := gRouter.Routes("myapp")
	c.Assert(err, check.IsNil)
	route1, _ := url.Parse("http://10.1.1.10")
	route2, _ := url.Parse("http://10.1.1.11")
	c.Assert(routes, check.DeepEquals, []*url.URL{route1, route2})
}
Ejemplo n.º 11
0
func (r *hipacheRouter) AddBackend(name string) error {
	domain, err := config.GetString(r.prefix + ":domain")
	if err != nil {
		return &router.RouterError{Op: "add", Err: err}
	}
	frontend := "frontend:" + name + "." + domain
	conn := r.connect()
	defer conn.Close()
	exists, err := redis.Bool(conn.Do("EXISTS", frontend))
	if err != nil {
		return &router.RouterError{Op: "add", Err: err}
	}
	if exists {
		return router.ErrBackendExists
	}
	_, err = conn.Do("RPUSH", frontend, name)
	if err != nil {
		return &router.RouterError{Op: "add", Err: err}
	}
	return router.Store(name, name, routerName)
}
Ejemplo n.º 12
0
func (s *S) TestUnsetCName(c *check.C) {
	err := router.Store("myapp", "myapp", routerName)
	c.Assert(err, check.IsNil)
	data := galebData{
		Name: "myapp",
		CNames: []galebCNameData{
			{CName: "my.new.cname", VirtualHostId: s.server.URL + "/api/vh999"},
		},
	}
	err = data.save()
	c.Assert(err, check.IsNil)
	s.handler.RspCode = http.StatusNoContent
	gRouter, err := createRouter("routers:galeb")
	c.Assert(err, check.IsNil)
	err = gRouter.UnsetCName("my.new.cname", "myapp")
	c.Assert(err, check.IsNil)
	c.Assert(s.handler.Url, check.DeepEquals, []string{"/api/vh999"})
	dbData, err := getGalebData("myapp")
	c.Assert(err, check.IsNil)
	c.Assert(dbData.CNames, check.DeepEquals, []galebCNameData{})
}
Ejemplo n.º 13
0
Archivo: router.go Proyecto: 4eek/tsuru
func (r *galebRouter) AddBackend(name string) error {
	poolParams := galebClient.BackendPoolParams{
		Name: poolName(name),
	}
	_, err := getGalebData(name)
	if err == nil {
		return router.ErrBackendExists
	}
	data := galebData{Name: name}
	client, err := r.getClient()
	if err != nil {
		return err
	}
	data.BackendPoolId, err = client.AddBackendPool(&poolParams)
	if err != nil {
		return err
	}
	ruleParams := galebClient.RuleParams{
		Name:        rootRuleName(name),
		Match:       "/",
		BackendPool: data.BackendPoolId,
	}
	data.RootRuleId, err = client.AddRule(&ruleParams)
	if err != nil {
		return err
	}
	virtualHostParams := galebClient.VirtualHostParams{
		Name:        r.virtualHostName(name),
		RuleDefault: data.RootRuleId,
	}
	data.VirtualHostId, err = client.AddVirtualHost(&virtualHostParams)
	if err != nil {
		return err
	}
	err = data.save()
	if err != nil {
		return err
	}
	return router.Store(name, name, routerName)
}
Ejemplo n.º 14
0
func (s *S) TestRemoveRouteParsesURL(c *check.C) {
	err := router.Store("myapp", "myapp", routerName)
	c.Assert(err, check.IsNil)
	data := galebData{
		Name: "myapp",
		Reals: []galebRealData{
			{Real: "10.1.1.10:1010", BackendId: s.server.URL + "/api/backend1"},
		},
	}
	err = data.save()
	c.Assert(err, check.IsNil)
	s.handler.RspCode = http.StatusNoContent
	gRouter, err := createRouter("routers:galeb")
	c.Assert(err, check.IsNil)
	addr, _ := url.Parse("https://10.1.1.10:1010/")
	err = gRouter.RemoveRoute("myapp", addr)
	c.Assert(err, check.IsNil)
	c.Assert(s.handler.Url, check.DeepEquals, []string{"/api/backend1"})
	dbData, err := getGalebData("myapp")
	c.Assert(err, check.IsNil)
	c.Assert(dbData.Reals, check.DeepEquals, []galebRealData{})
}
Ejemplo n.º 15
0
func (r *galebRouter) AddBackend(name string) error {
	backendPoolId, err := r.client.AddBackendPool(poolName(name))
	if err == galebClient.ErrItemAlreadyExists {
		return router.ErrBackendExists
	}
	if err != nil {
		return err
	}
	virtualHostId, err := r.client.AddVirtualHost(r.virtualHostName(name))
	if err != nil {
		return err
	}
	ruleId, err := r.client.AddRuleToID(ruleName(name), backendPoolId)
	if err != nil {
		return err
	}
	err = r.client.SetRuleVirtualHostIDs(ruleId, virtualHostId)
	if err != nil {
		return err
	}
	return router.Store(name, name, routerName)
}
Ejemplo n.º 16
0
func (r *vulcandRouter) AddBackend(name string) error {
	backendName := r.backendName(name)
	frontendName := r.frontendName(r.frontendHostname(name))
	backendKey := engine.BackendKey{Id: backendName}
	frontendKey := engine.FrontendKey{Id: frontendName}
	if found, _ := r.client.GetBackend(backendKey); found != nil {
		return router.ErrBackendExists
	}
	if found, _ := r.client.GetFrontend(frontendKey); found != nil {
		return router.ErrBackendExists
	}
	backend, err := engine.NewHTTPBackend(
		backendName,
		engine.HTTPBackendSettings{},
	)
	if err != nil {
		return &router.RouterError{Err: err, Op: "add-backend"}
	}
	err = r.client.UpsertBackend(*backend)
	if err != nil {
		return err
	}
	frontend, err := engine.NewHTTPFrontend(
		route.NewMux(),
		frontendName,
		backend.Id,
		fmt.Sprintf(`Host(%q)`, r.frontendHostname(name)),
		engine.HTTPFrontendSettings{},
	)
	if err != nil {
		return &router.RouterError{Err: err, Op: "add-backend"}
	}
	err = r.client.UpsertFrontend(*frontend, engine.NoTTL)
	if err != nil {
		r.client.DeleteBackend(backendKey)
		return &router.RouterError{Err: err, Op: "add-backend"}
	}
	return router.Store(name, name, routerName)
}
Ejemplo n.º 17
0
func (r *hipacheRouter) AddBackend(name string) error {
	domain, err := config.GetString(r.prefix + ":domain")
	if err != nil {
		return &router.RouterError{Op: "add", Err: err}
	}
	frontend := "frontend:" + name + "." + domain
	conn, err := r.connect()
	if err != nil {
		return &router.RouterError{Op: "add", Err: err}
	}
	exists, err := conn.Exists(frontend).Result()
	if err != nil {
		return &router.RouterError{Op: "add", Err: err}
	}
	if exists {
		return router.ErrBackendExists
	}
	err = conn.RPush(frontend, name).Err()
	if err != nil {
		return &router.RouterError{Op: "add", Err: err}
	}
	return router.Store(name, name, routerType)
}