Beispiel #1
0
func invokeUpdateRestApi(method string, mon string, url string, contentType string, body io.Reader) (*http.Response, error) {
	var loginUrl = fmt.Sprintf(
		"https://%s:%d/%s/v%d/auth/login/",
		mon,
		models.CEPH_API_PORT,
		models.CEPH_API_DEFAULT_PREFIX,
		models.CEPH_API_DEFAULT_VERSION)

	session := client.GetCephApiSession()

	csrf_token := csrfTokenFromSession(session, loginUrl)

	resp, err := doRequest(
		session,
		csrf_token,
		method,
		contentType,
		url,
		body)
	if err != nil {
		return resp, fmt.Errorf("Error executing request: %v", err)
	}

	if resp.StatusCode != http.StatusOK &&
		resp.StatusCode != http.StatusAccepted &&
		resp.StatusCode != http.StatusForbidden {
		return resp, errors.New("Failed" + resp.Status)
	}
	if resp.StatusCode == http.StatusForbidden {
		logger.Get().Warning("Session seems invalidated. Trying to login again.")
		if err := login(session, mon, loginUrl); err != nil {
			return resp, fmt.Errorf("Failed to relogin")
		}
		return doRequest(session, csrf_token, method, contentType, url, body)
	}

	return resp, nil
}
Beispiel #2
0
func HttpGet(mon, url string) (*http.Response, error) {
	session := client.GetCephApiSession()

	var loginUrl = fmt.Sprintf(
		"https://%s:%d/%s/v%d/auth/login/",
		mon,
		models.CEPH_API_PORT,
		models.CEPH_API_DEFAULT_PREFIX,
		models.CEPH_API_DEFAULT_VERSION)

	csrf_token := csrfTokenFromSession(session, loginUrl)

	resp, err := doRequest(
		session,
		csrf_token,
		"GET",
		"application/json",
		url,
		bytes.NewBuffer([]byte{}))
	if err != nil {
		return resp, fmt.Errorf("Error executing request: %v", err)
	}

	if resp.StatusCode != http.StatusOK &&
		resp.StatusCode != http.StatusAccepted &&
		resp.StatusCode != http.StatusForbidden {
		return resp, fmt.Errorf(resp.Status)
	}
	if resp.StatusCode == http.StatusForbidden {
		logger.Get().Warning("Session seems invalidated. Trying to login again.")
		if err := login(session, mon, loginUrl); err != nil {
			return resp, fmt.Errorf("Failed to relogin")
		}
		return doRequest(session, csrf_token, "GET", "application/json", url, bytes.NewBuffer([]byte{}))
	}

	return resp, nil
}