Example #1
0
File: router.go Project: 4eek/tsuru
func (r *galebRouter) SetCName(cname, name string) error {
	backendName, err := router.Retrieve(name)
	if err != nil {
		return err
	}
	domain, err := config.GetString(r.prefix + ":domain")
	if err != nil {
		return err
	}
	if !router.ValidCName(cname, domain) {
		return router.ErrCNameNotAllowed
	}
	data, err := getGalebData(backendName)
	if err != nil {
		return err
	}
	client, err := r.getClient()
	if err != nil {
		return err
	}
	for _, val := range data.CNames {
		if val.CName == cname {
			return router.ErrCNameExists
		}
	}
	virtualHostParams := galebClient.VirtualHostParams{
		Name:        cname,
		RuleDefault: data.RootRuleId,
	}
	virtualHostId, err := client.AddVirtualHost(&virtualHostParams)
	if err != nil {
		return err
	}
	return data.addCName(cname, virtualHostId)
}
Example #2
0
func (r *vulcandRouter) SetCName(cname, name string) error {
	usedName, err := router.Retrieve(name)
	if err != nil {
		return err
	}
	if !router.ValidCName(cname, r.domain) {
		return router.ErrCNameNotAllowed
	}
	frontendName := r.frontendName(cname)
	if found, _ := r.client.GetFrontend(engine.FrontendKey{Id: frontendName}); found != nil {
		return router.ErrCNameExists
	}
	frontend, err := engine.NewHTTPFrontend(
		frontendName,
		r.backendName(usedName),
		fmt.Sprintf(`Host(%q)`, cname),
		engine.HTTPFrontendSettings{},
	)
	if err != nil {
		return &router.RouterError{Err: err, Op: "set-cname"}
	}
	err = r.client.UpsertFrontend(*frontend, engine.NoTTL)
	if err != nil {
		return &router.RouterError{Err: err, Op: "set-cname"}
	}
	return nil
}
Example #3
0
func (r *galebRouter) SetCName(cname, name string) error {
	backendName, err := router.Retrieve(name)
	if err != nil {
		return err
	}
	if !router.ValidCName(cname, r.domain) {
		return router.ErrCNameNotAllowed
	}
	_, err = r.client.AddVirtualHost(r.virtualHostName(cname))
	if err == galebClient.ErrItemAlreadyExists {
		return router.ErrCNameExists
	}
	if err != nil {
		return err
	}
	return r.client.SetRuleVirtualHost(ruleName(backendName), r.virtualHostName(cname))
}
Example #4
0
func (r *fakeRouter) SetCName(cname, name string) error {
	backendName, err := router.Retrieve(name)
	if err != nil {
		return err
	}
	if !r.HasBackend(backendName) {
		return nil
	}
	if !router.ValidCName(cname, "fakerouter.com") {
		return router.ErrCNameNotAllowed
	}
	r.mutex.Lock()
	defer r.mutex.Unlock()
	if _, ok := r.cnames[cname]; ok {
		return router.ErrCNameExists
	}
	r.cnames[cname] = backendName
	return nil
}
Example #5
0
func (r *hipacheRouter) SetCName(cname, name string) error {
	backendName, err := router.Retrieve(name)
	if err != nil {
		return err
	}
	domain, err := config.GetString(r.prefix + ":domain")
	if err != nil {
		return &router.RouterError{Op: "setCName", Err: err}
	}
	if !router.ValidCName(cname, domain) {
		return router.ErrCNameNotAllowed
	}
	conn, err := r.connect()
	if err != nil {
		return &router.RouterError{Op: "set", Err: err}
	}
	cnameExists := false
	currentCnames, err := conn.LRange("cname:"+backendName, 0, -1).Result()
	if err != nil {
		return &router.RouterError{Op: "set", Err: err}
	}
	for _, n := range currentCnames {
		if n == cname {
			cnameExists = true
			break
		}
	}
	if !cnameExists {
		err = conn.RPush("cname:"+backendName, cname).Err()
		if err != nil {
			return &router.RouterError{Op: "set", Err: err}
		}
	}
	frontend := "frontend:" + backendName + "." + domain
	cnameFrontend := "frontend:" + cname
	wantedRoutes, err := conn.LRange(frontend, 1, -1).Result()
	if err != nil {
		return &router.RouterError{Op: "get", Err: err}
	}
	currentRoutes, err := conn.LRange(cnameFrontend, 0, -1).Result()
	if err != nil {
		return &router.RouterError{Op: "get", Err: err}
	}
	// Routes are always added again and duplicates removed this will ensure
	// that after a call to SetCName is made routes will be identical to the
	// original entry.
	if len(currentRoutes) == 0 {
		err = conn.RPush(cnameFrontend, backendName).Err()
		if err != nil {
			return &router.RouterError{Op: "setCName", Err: err}
		}
	} else {
		currentRoutes = currentRoutes[1:]
	}
	for _, r := range wantedRoutes {
		err = conn.RPush(cnameFrontend, r).Err()
		if err != nil {
			return &router.RouterError{Op: "setCName", Err: err}
		}
	}
	for _, r := range currentRoutes {
		err = conn.LRem(cnameFrontend, 1, r).Err()
		if err != nil {
			return &router.RouterError{Op: "setCName", Err: err}
		}
	}
	if cnameExists {
		return router.ErrCNameExists
	}
	return nil
}