예제 #1
0
func TestGetPool(t *testing.T) {

	pool := testPool()
	poolData := testPoolData()

	if err := zk.SetPool(pool); err != nil {
		t.Fatalf("couldn't set pool for get")
	}
	if err := zk.AddHosts(pool.Name, pool.Hosts); err != nil {
		t.Fatalf("couldn't set pool for get")
	}

	defer func() {
		if err := zk.DeletePool(pool.Name); err != nil {
			t.Fatalf("couldn't clean up")
		}
	}()

	statusCode, data, err := client.BuildAndSendRequest("GET", "/pools/"+pool.Name, "")
	if err != nil {
		t.Fatalf("could not get pool: %s", err)
	}

	if statusCode != 200 {

		t.Fatalf("incorrect status code returned, should be 200")
	}

	if data != poolData {
		t.Fatalf("Value from get not as expected \n %s \b %s", data, poolData)
	}
}
예제 #2
0
func TestGetHosts(t *testing.T) {

	pool := testPool()

	host := testHost()
	hostData := testHostData()

	if err := zk.SetPool(pool); err != nil {
		t.Fatalf("failed to put pool to test add host")
	}

	defer func() {
		if err := zk.DeletePool(pool.Name); err != nil {
			t.Fatalf("could not clean up")
		}
	}()

	if err := zk.AddHosts(pool.Name, host); err != nil {
		t.Fatalf("couldn't add hosts to attempt get")
	}

	statusCode, data, err := client.BuildAndSendRequest("GET", "/pools/"+pool.Name+"/hosts", "")
	if err != nil {
		t.Fatalf("couldn't get hosts")
	}

	if statusCode != 200 {
		t.Fatalf("incorrect status code")
	}

	if data != hostData {
		t.Fatalf("data from get doesn't match the put data")
	}

}
예제 #3
0
func SetPool(w http.ResponseWriter, r *http.Request) {

	err := GetUserSecretAndAuth(r)
	if err != nil {
		WriteResponse(w, NotAuthorizedStatusCode, GetErrorStatusJson(NotAuthenticatedStatus, err))
		return
	}

	if r.Header.Get("Content-Type") != "application/json" {
		WriteResponse(w, BadRequestStatusCode, GetStatusJson(IncorrectContentTypeStatus))
		return
	}

	body, err := GetRequestBody(r)
	if err != nil {
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}

	var pool cfg.Pool
	err = json.Unmarshal(body, &pool)
	if err != nil {
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}

	err = zk.SetPool(pool)

	if err != nil {
		WriteResponse(w, ServerErrorCode, GetErrorStatusJson(CouldNotCompleteOperationStatus, err))
		return
	}

	//If the pool has hosts when sent in, call AddHosts with them
	if len(pool.Hosts) > 0 {
		err = zk.AddHosts(pool.Name, pool.Hosts)
		if err != nil {
			WriteResponse(w, ServerErrorCode, GetErrorStatusJson(CouldNotCompleteOperationStatus, err))
			return
		}
	}

	WriteResponse(w, OkStatusCode, GetStatusJson(RequestSuccesfulStatus))
}
예제 #4
0
func AddHosts(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	err := GetUserSecretAndAuth(r)
	if err != nil {
		WriteResponse(w, NotAuthorizedStatusCode, GetErrorStatusJson(NotAuthenticatedStatus, err))
		return
	}

	if r.Header.Get("Content-Type") != "application/json" {
		WriteResponse(w, BadRequestStatusCode, GetStatusJson(IncorrectContentTypeStatus))
		return
	}

	var hostMap map[string]cfg.Host
	body, err := GetRequestBody(r)
	if err != nil {
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}
	err = json.Unmarshal(body, &hostMap)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		WriteResponse(w, BadRequestStatusCode, GetErrorStatusJson(CouldNotReadRequestDataStatus, err))
		return
	}

	err = zk.AddHosts(vars["PoolName"], hostMap)
	if err != nil {
		WriteResponse(w, ServerErrorCode, GetErrorStatusJson(CouldNotCompleteOperationStatus, err))
		return
	}

	WriteResponse(w, OkStatusCode, GetStatusJson(RequestSuccesfulStatus))

}