Ejemplo n.º 1
0
func (routes *Routes) renewInstance(w rest.ResponseWriter, r *rest.Request) {
	iid := r.PathParam(RouteParamInstanceID)
	if iid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance id is required",
		}).Warn("Failed to renew instance")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceIdentifierMissing)
		return
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to renew instance %s", iid)

		return
	}

	if err := catalog.Renew(iid); err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to renew instance %s", iid)

		i18n.Error(r, w, statusCodeFromError(err), i18n.ErrorInstanceHeartbeatFailed)
		return
	}

	w.WriteHeader(http.StatusOK)
}
Ejemplo n.º 2
0
func (routes *Routes) getInstance(w rest.ResponseWriter, r *rest.Request) {
	appid := r.PathParam(RouteParamAppID)
	if appid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "application id is required",
		}).Warn("Failed to query instancee")

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorApplicationIdentifierMissing)
		return
	}

	iid := r.PathParam(RouteParamInstanceID)
	if iid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance id is required",
		}).Warn("Failed to query instance")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceIdentifierMissing)
		return
	}
	uid := buildUniqueInstanceID(appid, iid)

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to renew instance %s", iid)

		return
	}

	si, err := catalog.Instance(uid)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to query instance %s", uid)

		i18n.Error(r, w, http.StatusNotFound, i18n.ErrorInstanceNotFound)
		return
	}

	inst := buildInstanceFromRegistry(si)
	err = w.WriteJson(&InstanceWrapper{inst})
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to encode instance")

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorEncoding)
		return
	}
}
Ejemplo n.º 3
0
func (routes *Routes) registerInstance(w rest.ResponseWriter, r *rest.Request) {
	var err error
	var req *InstanceRegistration
	req, err = routes.parseInstanceRegistrationRequest(w, r)
	if err != nil {
		return // error to client already set
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to register instance %+v", req)

		return
	}

	si := &store.ServiceInstance{
		ServiceName: req.ServiceName,
		Protocol:    protocol.Amalgam8,
		Endpoint:    &store.Endpoint{Type: req.Endpoint.Type, Value: req.Endpoint.Value},
		Status:      strings.ToUpper(req.Status),
		TTL:         time.Duration(req.TTL) * time.Second,
		Metadata:    req.Metadata,
		Tags:        req.Tags}
	var sir *store.ServiceInstance

	if sir, err = catalog.Register(si); err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, statusCodeFromError(err), i18n.ErrorInstanceRegistrationFailed)
		return
	} else if sir == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance is nil",
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorNilObject)
		return
	} else if sir.ID == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance id is empty",
		}).Warnf("Failed to register instance %s", sir)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInstanceIdentifierMissing)
		return
	}

	routes.sendRegistrationResponse(w, r, sir)
}
Ejemplo n.º 4
0
func (routes *Routes) listAppInstances(w rest.ResponseWriter, r *rest.Request) {
	appid := r.PathParam(RouteParamAppID)
	if appid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "application id is required",
		}).Warn("Failed to lookup application")

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorApplicationIdentifierMissing)
		return
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to lookup application %s", appid)

		return
	}

	insts, err := catalog.List(appid, nil)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Errorf("Failed to lookup application %s", appid)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.EurekaErrorApplicationNotFound)
		return
	}

	app := &Application{Name: appid, Instances: make([]*Instance, len(insts))}
	for index, inst := range insts {
		app.Instances[index] = buildInstanceFromRegistry(inst)
	}

	err = w.WriteJson(map[string]*Application{"application": app})
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to encode application")

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorEncoding)
		return
	}

	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("List application instances (%d)", len(app.Instances))
}
Ejemplo n.º 5
0
func (routes *Routes) deregisterInstance(w rest.ResponseWriter, r *rest.Request) {
	appid := r.PathParam(RouteParamAppID)
	if appid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "application id is required",
		}).Warn("Failed to deregister instance")

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorApplicationIdentifierMissing)
		return
	}

	iid := r.PathParam(RouteParamInstanceID)
	if iid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance id is required",
		}).Warn("Failed to deregister instance")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceIdentifierMissing)
		return
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to deregister instance %s", iid)

		return
	}

	uid := buildUniqueInstanceID(appid, iid)
	if err := catalog.Deregister(uid); err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to deregister instance %s", uid)

		i18n.Error(r, w, http.StatusGone, i18n.ErrorInstanceNotFound)
		return
	}

	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("Instance id %s deregistered", uid)

	w.WriteHeader(http.StatusOK)
}
Ejemplo n.º 6
0
func (routes *Routes) sendRegistrationResponse(w rest.ResponseWriter, r *rest.Request, sir *store.ServiceInstance) {
	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("Instance %s registered", sir)

	ttl := uint32(sir.TTL / time.Second)
	linksURL := r.BaseUrl()
	if middleware.IsUsingSecureConnection(r) { // request came in over a secure connection, continue using it
		linksURL.Scheme = "https"
	}

	links := BuildLinks(linksURL.String(), sir.ID)
	instance := &ServiceInstance{
		ID:    sir.ID,
		TTL:   ttl,
		Links: links,
	}

	w.Header().Set("Location", links.Self)
	w.WriteHeader(http.StatusCreated)

	if err := w.WriteJson(instance); err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to write registration response for instance %s", sir)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorEncoding)
	}
}
Ejemplo n.º 7
0
func (routes *Routes) catalog(w rest.ResponseWriter, r *rest.Request) store.Catalog {
	if r.Env[middleware.NamespaceKey] == nil {
		i18n.Error(r, w, http.StatusUnauthorized, i18n.ErrorNamespaceNotFound)
		return nil
	}
	namespace := r.Env[middleware.NamespaceKey].(auth.Namespace)
	if catalog, err := routes.registry.GetCatalog(namespace); err != nil {
		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInternalServer)
		return nil
	} else if catalog == nil {
		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorNamespaceNotFound)
		return nil
	} else {
		return catalog
	}
}
Ejemplo n.º 8
0
func (routes *Routes) listInstances(w rest.ResponseWriter, r *rest.Request) {
	var fields []string

	fields, err := extractFields(r)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to list instances")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorFilterBadFields)
		return
	}

	sc, err := newSelectCriteria(r)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Error("Failed to list instances")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorFilterSelectionCriteria)
		return
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Error("Failed to list instances")

		return
	}

	services := catalog.ListServices(nil)
	if services == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "services list is nil",
		}).Error("Failed to list instances")

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInstanceEnumeration)
		return
	}

	var insts = []*ServiceInstance{}
	for _, svc := range services {
		instances, err := catalog.List(svc.ServiceName, sc.instanceFilter)
		if err != nil {
			routes.logger.WithFields(log.Fields{
				"namespace": r.Env["REMOTE_USER"],
				"error":     err,
			}).Errorf("Failed to list instances for service %s", svc.ServiceName)

			i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInstanceEnumeration)
			return
		}

		for _, si := range instances {
			inst, err := copyInstanceWithFilter(svc.ServiceName, si, fields)
			if err != nil {
				routes.logger.WithFields(log.Fields{
					"namespace": r.Env["REMOTE_USER"],
					"error":     err,
				}).Warn("Failed to list instances")

				i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInstanceEnumeration)
				return
			}
			insts = append(insts, inst)
		}
	}

	if err = w.WriteJson(&InstancesList{Instances: insts}); err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to encode instances list response")

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorEncoding)
		return
	}

	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("Lookup instances (%d)", len(insts))
}
Ejemplo n.º 9
0
func (routes *Routes) parseInstanceRegistrationRequest(w rest.ResponseWriter, r *rest.Request) (*InstanceRegistration, error) {
	var req InstanceRegistration
	var err error

	if err = r.DecodeJsonPayload(&req); err != nil {

		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to register instance")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceRegistrationFailed)
		return nil, err
	}

	if req.ServiceName == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "service name is required",
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorServiceNameMissing)
		return nil, errors.New("Service name is required")
	}

	if req.Endpoint == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "Endpoint is required",
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceEndpointMissing)
		return nil, errors.New("Endpoint is required")
	}

	if req.Endpoint.Type == "" || req.Endpoint.Value == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "Endpoint type or value are missing or mismatched",
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceEndpontMalformed)
		return nil, errors.New("Endpoint type or value are missing or mismatched")
	}

	switch req.Endpoint.Type {
	case EndpointTypeHTTP:
	case EndpointTypeUDP:
	case EndpointTypeTCP:
	case EndpointTypeUser:
	default:
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "Endpoint type is of invalid value",
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceEndpointInvalidType)
		return nil, errors.New("Endpoint type is of invalid value")
	}

	metadataValid := true

	if req.Metadata != nil {
		metadataValid = validateJSON(req.Metadata)
	}

	if !metadataValid {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "Metadata is invalid",
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceMetadataInvalid)
		return nil, errors.New("Metadata is invalid")
	}

	// If the status is not passed in, set it to UP
	if req.Status == "" {
		req.Status = store.Up
	}

	// Validate the status value
	switch strings.ToUpper(req.Status) {
	case store.Up:
	case store.Starting:
	case store.OutOfService:
	default:
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "Status field is not a valid value",
		}).Warnf("Failed to register instance %+v", req)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceStatusInvalid,
			map[string]interface{}{"Status": fmt.Sprintf("%s, %s, %s", store.Up, store.Starting, store.OutOfService)})
		return nil, errors.New("Status field is not a valid value")
	}

	return &req, nil
}
Ejemplo n.º 10
0
func (routes *Routes) registerInstance(w rest.ResponseWriter, r *rest.Request) {
	var err error
	var reg InstanceWrapper

	appid := r.PathParam(RouteParamAppID)
	if appid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "application id is required",
		}).Warn("Failed to register instance")

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorApplicationIdentifierMissing)
		return
	}

	if err = r.DecodeJsonPayload(&reg); err != nil {

		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to register instance")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceRegistrationFailed)
		return
	}

	inst := reg.Inst
	if inst.Application == "" {
		inst.Application = appid
	}

	if inst.HostName == "" || inst.Application == "" || inst.VIPAddr == "" || inst.IPAddr == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "hostname, application, vipaddress and IPaddress are required",
		}).Warnf("Failed to register instance %+v", inst)

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorRequiredFieldsMissing)
		return
	}

	metadataValid := true

	if inst.Metadata != nil {
		metadataValid = validateJSON(inst.Metadata)
	}

	if !metadataValid {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "metadata is not valid",
		}).Warnf("Failed to register instance %+v", inst)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceMetadataInvalid)
		return
	}

	// Get the instance ID
	iid := inst.ID
	if iid == "" {
		iid = inst.HostName
	}

	ttl := defaultDurationInt
	if inst.Lease != nil && inst.Lease.DurationInt > 0 {
		ttl = inst.Lease.DurationInt
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to register instance %+v", inst)

		return
	}

	si := &store.ServiceInstance{
		ID:          buildUniqueInstanceID(appid, iid),
		Protocol:    protocol.Eureka,
		ServiceName: inst.Application,
		Endpoint:    &store.Endpoint{Type: "tcp", Value: fmt.Sprintf("%s:%v", inst.IPAddr, inst.Port.Value)},
		Status:      inst.Status,
		TTL:         time.Duration(ttl) * time.Second,
		Metadata:    inst.Metadata,
		Extension:   buildExtensionFromInstance(inst)}

	var sir *store.ServiceInstance

	if sir, err = catalog.Register(si); err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to register instance %+v", inst)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInstanceRegistrationFailed)
		return
	} else if sir == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance is nil",
		}).Warnf("Failed to register instance %+v", inst)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorNilObject)
		return
	} else if sir.ID == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance id is empty",
		}).Warnf("Failed to register instance %s", sir)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInstanceIdentifierMissing)
		return
	}

	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("Instance %s registered", sir)

	w.WriteHeader(http.StatusNoContent)
}
Ejemplo n.º 11
0
func (routes *Routes) setStatus(w rest.ResponseWriter, r *rest.Request) {
	appid := r.PathParam(RouteParamAppID)
	if appid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "application id is required",
		}).Warn("Failed to set instances status")

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorApplicationIdentifierMissing)
		return
	}

	iid := r.PathParam(RouteParamInstanceID)
	if iid == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "instance id is required",
		}).Warn("Failed to set instances status")

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceIdentifierMissing)
		return
	}
	uid := buildUniqueInstanceID(appid, iid)

	status := r.URL.Query().Get("value")
	if status == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "status value is required",
		}).Warn("Failed to set instances status")

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorStatusMissing)
		return
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to set instance %s status", uid)

		return
	}

	si, err := catalog.Instance(uid)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to set instance %s status", uid)

		i18n.Error(r, w, http.StatusNotFound, i18n.ErrorInstanceNotFound)
		return
	}

	if si.ServiceName != appid {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "Application id does not match",
		}).Warnf("Failed to set instance %s status. service_name: %s", uid, si.ServiceName)

		i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceNotFound)
		return
	}

	err = catalog.SetStatus(uid, status)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warnf("Failed to set instance %s status", uid)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorInstanceNotFound)
		return
	}

	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("Instance %s status was changed. old: %s, new: %s", uid, si.Status, status)

	w.WriteHeader(http.StatusOK)
}
Ejemplo n.º 12
0
func (routes *Routes) listApps(w rest.ResponseWriter, r *rest.Request) {

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Error("Failed to list applications")

		return
	}

	services := catalog.ListServices(nil)
	if services == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "services list is nil",
		}).Error("Failed to list applications")

		i18n.Error(r, w, http.StatusInternalServerError, i18n.EurekaErrorApplicationEnumeration)
		return
	}

	var instsCount int
	apps := &Applications{Application: make([]*Application, len(services))}
	listRes := &ApplicationsList{Applications: apps}

	for index, svc := range services {
		insts, err := catalog.List(svc.ServiceName, nil)
		if err != nil {
			routes.logger.WithFields(log.Fields{
				"namespace": r.Env["REMOTE_USER"],
				"error":     err,
			}).Errorf("Failed to lookup application %s", svc.ServiceName)

			i18n.Error(r, w, http.StatusInternalServerError, i18n.EurekaErrorApplicationEnumeration)
			return
		}

		app := &Application{Name: svc.ServiceName, Instances: make([]*Instance, len(insts))}
		for idx, inst := range insts {
			app.Instances[idx] = buildInstanceFromRegistry(inst)
			instsCount++
		}
		apps.Application[index] = app
	}

	err := w.WriteJson(listRes)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to encode applications list")

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorEncoding)
		return
	}

	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("List applications (%d apps, %d insts)", len(apps.Application), instsCount)
}
Ejemplo n.º 13
0
func (routes *Routes) listVips(w rest.ResponseWriter, r *rest.Request) {

	vip := r.PathParam(RouterParamVip)
	if vip == "" {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "vip is required",
		}).Warn("Failed to list vip")

		i18n.Error(r, w, http.StatusBadRequest, i18n.EurekaErrorVIPRequired)
		return
	}

	catalog := routes.catalog(w, r)
	if catalog == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "catalog is nil",
		}).Errorf("Failed to list vip %s", vip)

		return
	}

	services := catalog.ListServices(nil)
	if services == nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     "services list is nil",
		}).Errorf("Failed to list vip %s", vip)

		i18n.Error(r, w, http.StatusInternalServerError, i18n.EurekaErrorVIPEnumeration)
		return
	}

	var instsCount int
	apps := &Applications{Application: make([]*Application, 0, len(services))}
	listRes := &ApplicationsList{Applications: apps}

	for _, svc := range services {
		insts, err := catalog.List(svc.ServiceName, nil)
		if err != nil {
			routes.logger.WithFields(log.Fields{
				"namespace": r.Env["REMOTE_USER"],
				"error":     err,
			}).Errorf("Failed to list vips %s", vip)

			i18n.Error(r, w, http.StatusInternalServerError, i18n.EurekaErrorVIPEnumeration)
			return
		}

		app := &Application{Name: svc.ServiceName, Instances: make([]*Instance, 0, len(insts))}
		for _, inst := range insts {
			if vipaddr, ok := inst.Extension["VipAddress"]; ok {
				if vipaddr == vip {
					app.Instances = append(app.Instances, buildInstanceFromRegistry(inst))
					instsCount++
				}
			}
		}
		if len(app.Instances) > 0 {
			apps.Application = append(apps.Application, app)
		}
	}

	err := w.WriteJson(listRes)
	if err != nil {
		routes.logger.WithFields(log.Fields{
			"namespace": r.Env["REMOTE_USER"],
			"error":     err,
		}).Warn("Failed to encode vips list")

		i18n.Error(r, w, http.StatusInternalServerError, i18n.ErrorEncoding)
		return
	}

	routes.logger.WithFields(log.Fields{
		"namespace": r.Env["REMOTE_USER"],
	}).Infof("List vips (%d apps, %d insts)", len(apps.Application), instsCount)
}