Exemplo n.º 1
0
// pack request and send request to cgw
// in any case, there should be a resp written to response channel
// if cgw returns ok, raw data from cgw is stored in resp0.data
func requestInterface(start_idx, end_idx, app_id int, owner_uin, interface_name string, ch chan<- Resp0) {
	resp := Resp0{
		code:    cvmcode.OK,
		message: "ok",
		data:    "",
	}
	// prepare request data
	post_data, errcode := preparePostData(start_idx, end_idx, app_id, owner_uin)
	if errcode != 0 {
		resp.code = cvmcode.PREPARE_POST_DATA_FAIL
		resp.message = "prepare post data fail"
		ch <- resp
		return
	}
	log.Infof("req %s\n%s\n", interface_name, post_data)
	postBytesReader := bytes.NewReader([]byte(post_data))
	// http client without timeout
	client := &http.Client{}
	//    client := &http.Client{
	//		Timeout: cgw_conf.CgwTimeout}
	request, err := http.NewRequest("POST", interface_name, postBytesReader)
	if err != nil {
		resp.code = cvmcode.NEWREQUEST_FAIL
		resp.message = "http.NewRequest fail"
		ch <- resp
		return
	}
	response, err := client.Do(request)
	if err != nil {
		resp.code = cvmcode.DO_REQUEST_FAIL
		resp.message = "http client: do request to cgw fail"
		ch <- resp
		return
	}
	defer response.Body.Close()
	if response.StatusCode != 200 {
		resp.code = cvmcode.CGW_HTTP_RETRUNCODE_NOT_200
		resp.message = "http client: cgw http return code not 200"
		ch <- resp
		return
	}
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		resp.code = cvmcode.READ_RESPONSE_FAIL
		resp.message = "http: read response from cgw fail"
		ch <- resp
		return
	}
	resp.data = string(body)
	log.Infof("resp from %s\n%s\n", interface_name, resp.data)
	// response ok
	// do not parse response here
	// check detailed response info in collecting procedure
	ch <- resp
}
Exemplo n.º 2
0
func getCvmList(w http.ResponseWriter, req *http.Request) {
	// parse parameters
	app_id, owner_uin, district := parseParams(req)
	var resp_str []byte
	if app_id < 0 {
		// fail
		log.Error("invalid app_id : ", app_id)
		resp_str = packResponse(cvmcode.PARAM_ERR, "param error", nil, nil, nil, nil)
		w.Write(resp_str)
		return
	}
	log.Infof("get req -> app_id : %d owner_uin : %s district : %s\n", app_id, owner_uin, district)
	// expect cvm list
	code_list, message_list, district_list, device_list := processGetCvmList(app_id, owner_uin, district)
	log.Info("final code_list => ", code_list)
	log.Info("final message_list => ", message_list)
	log.Info("final district_list => ", district_list)
	log.Info("final device_list => ", device_list)
	final_return_code := cvmcode.CGW_INTERFACE_ALL_FAIL
	final_return_msg := "all cgw interface failed"
	// return ok if there are any interface returns ok
	for _, rt_code := range code_list {
		if rt_code == cvmcode.OK {
			final_return_code = cvmcode.OK
			final_return_msg = "ok"
			break
		}
	}
	device_num := len(device_list)
	cvms := sjson.New()
	cvms.Set("totalNum", device_num)
	cvms.Set("deviceList", device_list)
	// response
	resp_str = packResponse(final_return_code, final_return_msg, code_list, message_list, district_list, cvms)
	w.Write(resp_str)
}