Exemple #1
0
func (as ApiService) serviceCreate(c *gin.Context) {
	var newService types.Service
	if err := c.BindJSON(&newService); err != nil {
		c.Error(err)
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	//Guarantees that no one tries to create a destination together with a service
	newService.Destinations = []types.Destination{}

	if _, errs := govalidator.ValidateStruct(newService); errs != nil {
		c.Error(errs)
		c.JSON(http.StatusBadRequest, gin.H{"errors": govalidator.ErrorsByField(errs)})
		return
	}

	// If everthing is ok send it to Raft
	err := as.balancer.AddService(&newService)
	if err != nil {
		c.Error(err)
		if err == types.ErrServiceAlreadyExists {
			c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
		} else {
			c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("UpsertService() failed: %v", err)})
		}
		return
	}

	c.Header("Location", fmt.Sprintf("/services/%s", newService.Name))
	c.JSON(http.StatusCreated, newService)
}
Exemple #2
0
func (b *testBalancer) AddDestination(srv *types.Service, dest *types.Destination) error {
	var foundSrv *types.Service
	for i := range b.services {
		curSrv := b.services[i]
		if b.services[i].Name == srv.Name {
			foundSrv = &b.services[i]
		}
		for j := range curSrv.Destinations {
			if curSrv.Destinations[j].Name == dest.Name {
				return types.ErrDestinationAlreadyExists
			}
		}
	}
	if foundSrv == nil {
		return types.ErrServiceNotFound
	}
	foundSrv.Destinations = append(foundSrv.Destinations, *dest)
	return nil
}