From c803f9803f385d8a2983fec7509bdadaf13d93b2 Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Thu, 5 May 2016 12:57:33 -0500 Subject: [PATCH] Use Session for auth Change-Id: Ie1fae2b7aec29a8020393b32a92d71e87c2b55fe --- examples/00-authentication.go | 2 +- openstack/auth.go | 32 ++------------------------------ openstack/session.go | 7 ++++++- 3 files changed, 9 insertions(+), 32 deletions(-) diff --git a/examples/00-authentication.go b/examples/00-authentication.go index 1e1370d..a2a5ef1 100644 --- a/examples/00-authentication.go +++ b/examples/00-authentication.go @@ -78,7 +78,7 @@ func main() { } // Get the first endpoint - ep, err := auth.GetEndpoint("compute", "") + _, err = auth.GetEndpoint("compute", "") if err != nil { fmt.Println("No compute endpoint found:", err) return diff --git a/openstack/auth.go b/openstack/auth.go index ec2bc80..2e8e739 100644 --- a/openstack/auth.go +++ b/openstack/auth.go @@ -16,12 +16,7 @@ package openstack import ( - "encoding/json" "errors" - "fmt" - "io/ioutil" - "net/http" - "strings" "time" ) @@ -69,42 +64,19 @@ func (s *AuthOpts) GetAuthType() (string, error) { // Basic auth call // These args should be an interface?? func DoAuthRequest(authopts AuthOpts) (AuthRef, error) { - // url string, body []byte) var auth = AuthToken{} + // Assume passwordv2 for now auth_mod, err := NewUserPassV2(authopts) if err != nil { err = errors.New("Failed to get auth options") return nil, err } - path := auth_mod.AuthUrl + "/tokens" - body := auth_mod.JSON() - headers := &http.Header{} - headers.Add("Content-Type", "application/json") - - resp, err := Post(path, nil, headers, &body) + _, err = PostJSON(auth_mod.AuthUrl + "/tokens", nil, nil, &auth_mod, &auth) if err != nil { return nil, err } - contentType := strings.ToLower(resp.Header.Get("Content-Type")) - if strings.Contains(contentType, "json") != true { - return nil, errors.New("err: header Content-Type is not JSON") - } - - rbody, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, errors.New("error reading response body") - } - - if resp.StatusCode != 200 { - return nil, fmt.Errorf("auth error: %s", rbody) - } - - if err = json.Unmarshal(rbody, &auth); err != nil { - return nil, errors.New("error unmarshalling response body") - } - return auth, nil } diff --git a/openstack/session.go b/openstack/session.go index 20e80b4..16f9c41 100644 --- a/openstack/session.go +++ b/openstack/session.go @@ -148,6 +148,11 @@ func (s *Session) RequestJSON( return nil, err } + if headers == nil { + headers = &http.Header{} + headers.Add("Content-Type", "application/json") + } + resp, err = s.Request(method, url, params, headers, &bodyjson) if err != nil { return nil, err @@ -281,7 +286,7 @@ func PostJSON( responseContainer interface{}, ) (resp *http.Response, err error) { s, _ := NewSession(nil, nil, nil) - return s.RequestJSON("POST", url, params, headers, nil, responseContainer) + return s.RequestJSON("POST", url, params, headers, body, responseContainer) } // Put sends a PUT request.