Use Session for auth

Change-Id: Ie1fae2b7aec29a8020393b32a92d71e87c2b55fe
This commit is contained in:
Dean Troyer 2016-05-05 12:57:33 -05:00
parent 58c3787968
commit c803f9803f
3 changed files with 9 additions and 32 deletions

View File

@ -78,7 +78,7 @@ func main() {
} }
// Get the first endpoint // Get the first endpoint
ep, err := auth.GetEndpoint("compute", "") _, err = auth.GetEndpoint("compute", "")
if err != nil { if err != nil {
fmt.Println("No compute endpoint found:", err) fmt.Println("No compute endpoint found:", err)
return return

View File

@ -16,12 +16,7 @@
package openstack package openstack
import ( import (
"encoding/json"
"errors" "errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time" "time"
) )
@ -69,42 +64,19 @@ func (s *AuthOpts) GetAuthType() (string, error) {
// Basic auth call // Basic auth call
// These args should be an interface?? // These args should be an interface??
func DoAuthRequest(authopts AuthOpts) (AuthRef, error) { func DoAuthRequest(authopts AuthOpts) (AuthRef, error) {
// url string, body []byte)
var auth = AuthToken{} var auth = AuthToken{}
// Assume passwordv2 for now
auth_mod, err := NewUserPassV2(authopts) auth_mod, err := NewUserPassV2(authopts)
if err != nil { if err != nil {
err = errors.New("Failed to get auth options") err = errors.New("Failed to get auth options")
return nil, err return nil, err
} }
path := auth_mod.AuthUrl + "/tokens" _, err = PostJSON(auth_mod.AuthUrl + "/tokens", nil, nil, &auth_mod, &auth)
body := auth_mod.JSON()
headers := &http.Header{}
headers.Add("Content-Type", "application/json")
resp, err := Post(path, nil, headers, &body)
if err != nil { if err != nil {
return nil, err 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 return auth, nil
} }

View File

@ -148,6 +148,11 @@ func (s *Session) RequestJSON(
return nil, err return nil, err
} }
if headers == nil {
headers = &http.Header{}
headers.Add("Content-Type", "application/json")
}
resp, err = s.Request(method, url, params, headers, &bodyjson) resp, err = s.Request(method, url, params, headers, &bodyjson)
if err != nil { if err != nil {
return nil, err return nil, err
@ -281,7 +286,7 @@ func PostJSON(
responseContainer interface{}, responseContainer interface{},
) (resp *http.Response, err error) { ) (resp *http.Response, err error) {
s, _ := NewSession(nil, nil, nil) 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. // Put sends a PUT request.