Ejemplo n.º 1
0
func (s *EtcdBackend) readUpstream(upstreamId string) (*Upstream, error) {
	upstreamKey := join(s.etcdKey, "upstreams", upstreamId)

	_, err := s.client.Get(upstreamKey, false, false)
	if err != nil {
		return nil, err
	}
	upstream := &Upstream{
		Name:      suffix(upstreamKey),
		EtcdKey:   upstreamKey,
		Endpoints: []*Endpoint{},
	}

	endpointPairs := s.getVals(join(upstream.EtcdKey, "endpoints"))
	for _, e := range endpointPairs {
		_, err := endpoint.ParseUrl(e.Val)
		if err != nil {
			fmt.Printf("Ignoring endpoint: failed to parse url: %s", e.Val)
			continue
		}
		e := &Endpoint{
			Url:     e.Val,
			EtcdKey: e.Key,
			Name:    suffix(e.Key),
		}
		upstream.Endpoints = append(upstream.Endpoints, e)
	}
	return upstream, nil
}
Ejemplo n.º 2
0
func (s *EtcdBackend) AddEndpoint(upstreamId, id, url string) error {
	if _, err := endpoint.ParseUrl(url); err != nil {
		return fmt.Errorf("Endpoint url '%s' is not valid")
	}
	if id == "" {
		if _, err := s.client.AddChild(s.path("upstreams", upstreamId, "endpoints"), url, 0); err != nil {
			return formatErr(err)
		}
	} else {
		if _, err := s.client.Create(s.path("upstreams", upstreamId, "endpoints", id), url, 0); err != nil {
			return formatErr(err)
		}
	}
	return nil
}