示例#1
0
func TestSetServices(t *testing.T) {
	if skip {
		t.SkipNow()
	}
	services := []core.Service{}
	services = append(services, testService2)

	if err := cluster.SetServices(services); err != nil {
		t.Errorf("Failed to SET services - %v", err)
		t.FailNow()
	}

	if _, err := os.Stat("/tmp/scribbleTest/services/tcp-192_168_0_15-80.json"); !os.IsNotExist(err) {
		t.Errorf("Failed to clear old services on PUT - %v", err)
	}

	service, err := cluster.GetService(testService2.Id)
	if err != nil {
		t.Error(err)
	}

	if service.Host != testService2.Host {
		t.Errorf("Read service differs from written service")
	}
}
示例#2
0
// Replace a service (by replacing all services)
func putService(rw http.ResponseWriter, req *http.Request) {
	// /services/{svcId}
	svcId := req.URL.Query().Get(":svcId")
	// rough sanitization
	if len(strings.Split(svcId, "-")) != 3 {
		writeError(rw, req, NoServiceError, http.StatusBadRequest)
		return
	}

	service, err := parseReqService(req)
	if err != nil {
		writeError(rw, req, err, http.StatusBadRequest)
		return
	}

	services, err := common.GetServices()
	if err != nil {
		writeError(rw, req, err, http.StatusInternalServerError)
		return
	}

	// update service by id
	for i := range services {
		if services[i].Id == svcId {
			services[i] = *service
			break
		}
	}

	// save to cluster
	if err := cluster.SetServices(services); err != nil {
		writeError(rw, req, err, http.StatusInternalServerError)
		return
	}

	writeBody(rw, req, service, http.StatusOK)
}
示例#3
0
// Reset all services
// /services
func putServices(rw http.ResponseWriter, req *http.Request) {
	services := []core.Service{}
	decoder := json.NewDecoder(req.Body)
	if err := decoder.Decode(&services); err != nil {
		writeError(rw, req, BadJson, http.StatusBadRequest)
		return
	}

	for i := range services {
		if services[i].Interface != "" {
			err := services[i].GenHost()
			if err != nil {
				writeError(rw, req, err, http.StatusBadRequest)
				return
			}
		}
		services[i].GenId()
		if services[i].Id == "--0" {
			writeError(rw, req, NoServiceError, http.StatusBadRequest)
			return
		}

		if err := checkPort(services[i]); err != nil {
			writeError(rw, req, err, http.StatusBadRequest)
			return
		}

		for j := range services[i].Servers {
			services[i].Servers[j].GenId()
			if services[i].Servers[j].Id == "-0" {
				writeError(rw, req, NoServerError, http.StatusBadRequest)
				return
			}

			// localhost doesn't work properly, use service.Host
			if services[i].Servers[j].Host == "127.0.0.1" {
				services[i].Servers[j].GenHost(services[i].Id)
			}
		}
	}

	// ensure no duplicate services
	svcs := map[string]core.Service{}
	for i := range services {
		svcs[services[i].Id] = services[i]
	}

	services = []core.Service{}

	for _, v := range svcs {
		services = append(services, v)
	}

	// save to cluster
	err := cluster.SetServices(services)
	if err != nil {
		writeError(rw, req, err, http.StatusInternalServerError)
		return
	}

	writeBody(rw, req, services, http.StatusOK)
}