diff options
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | account.go | 32 | ||||
| -rw-r--r-- | client.go | 94 | ||||
| -rw-r--r-- | doc.go | 2 | ||||
| -rw-r--r-- | request.go | 38 | ||||
| -rw-r--r-- | shared_test.go | 26 |
6 files changed, 124 insertions, 72 deletions
@@ -29,7 +29,7 @@ authOptions, err := openstack.AuthOptionsFromEnv() provider, err := openstack.AuthenticatedClient(authOptions) client, err := openstack.NewObjectStorageV1(provider, gophercloud.EndpointOpts{}) -account, err := schwift.AccountFromClient(client) +account, err := schwift.AccountFromGophercloud(client) ``` To connect to Swift using Swift's built-in authentication: @@ -47,7 +47,7 @@ client, err := swauth.NewObjectStorageV1(provider, swauth.AuthOpts { Key: "password", }) -account, err := schwift.AccountFromClient(client) +account, err := schwift.AccountFromGophercloud(client) ``` From this point, follow the [API documentation](https://godoc.org/github.com/majewsky/schwift) for what you can do with @@ -27,7 +27,7 @@ import ( //Account represents a Swift account. type Account struct { - client *gophercloud.ServiceClient + client Client //URL parts baseURL string name string @@ -37,13 +37,13 @@ type Account struct { var endpointURLRegexp = regexp.MustCompile(`^(.*/)v1/(.*)/$`) -//AccountFromClient takes a gophercloud.ServiceClient which wraps a Swift -//endpoint, and returns the Account instance corresponding to the account or -//project that this client is connected to. -func AccountFromClient(client *gophercloud.ServiceClient) (*Account, error) { - match := endpointURLRegexp.FindStringSubmatch(client.Endpoint) +//AccountFromClient takes something that implements the Client interface, and +//returns the Account instance corresponding to the account/project that this +//client is connected to. +func AccountFromClient(client Client) (*Account, error) { + match := endpointURLRegexp.FindStringSubmatch(client.EndpointURL()) if match == nil { - return nil, fmt.Errorf(`schwift.AccountFromClient(): invalid Swift endpoint URL: cannot find "/v1/" in %q`, client.Endpoint) + return nil, fmt.Errorf(`schwift.AccountFromClient(): invalid Swift endpoint URL: cannot find "/v1/" in %q`, client.EndpointURL()) } return &Account{ client: client, @@ -52,6 +52,13 @@ func AccountFromClient(client *gophercloud.ServiceClient) (*Account, error) { }, nil } +//AccountFromGophercloud takes a gophercloud.ServiceClient which wraps a Swift +//endpoint, and returns the Account instance corresponding to the account or +//project that this client is connected to. +func AccountFromGophercloud(client *gophercloud.ServiceClient) (*Account, error) { + return AccountFromClient(&gophercloudClient{client}) +} + //SwitchAccount returns a handle on a different account on the same server. Note //that you need reseller permissions to access accounts other than that where //you originally authenticated. This method does not check whether the account @@ -60,10 +67,9 @@ func AccountFromClient(client *gophercloud.ServiceClient) (*Account, error) { //The account name is usually the project name with an additional "AUTH_" //prefix. func (a *Account) SwitchAccount(accountName string) *Account { - clonedClient := *a.client - clonedClient.Endpoint = a.baseURL + "v1/" + accountName + "/" + newEndpointURL := a.baseURL + "v1/" + accountName + "/" return &Account{ - client: &clonedClient, + client: a.client.Clone(newEndpointURL), baseURL: a.baseURL, name: accountName, } @@ -75,9 +81,9 @@ func (a *Account) Name() string { return a.name } -//Client returns the gophercloud.ServiceClient which is used to make requests -//against this account. -func (a *Account) Client() *gophercloud.ServiceClient { +//Client returns the Client which is used to make requests against this +//account. +func (a *Account) Client() Client { return a.client } diff --git a/client.go b/client.go new file mode 100644 index 0000000..5f1192b --- /dev/null +++ b/client.go @@ -0,0 +1,94 @@ +/****************************************************************************** +* +* Copyright 2018 Stefan Majewsky <majewsky@gmx.net> +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +******************************************************************************/ + +package schwift + +import ( + "net/http" + + "github.com/gophercloud/gophercloud" +) + +//Client is the interface between Schwift and the libraries providing +//authentication for it. Schwift can wrap gophercloud.ServiceClient to provide +//this interface, so if you have a gophercloud.ServiceClient, use the +//AccountFromGophercloud method to obtain the corresponding schwift.Account +//instance. +type Client interface { + //EndpointURL returns the endpoint URL from the Keystone catalog for the + //Swift account that this client operates on. It should look like + //`http://domain.tld/v1/AUTH_projectid/`. + EndpointURL() string + //Clone returns a deep clone of this client with the endpoint URL changed to + //the given URL. + Clone(newEndpointURL string) Client + //Do executes the given request after adding to it the X-Auth-Token header + //containing the client's current Keystone (or Swift auth) token. It may + //also set other headers, such as User-Agent. If the status code returned is + //401, it shall attempt to acquire a new auth token and restart the request + //with the new token. + Do(req *http.Request) (*http.Response, error) +} + +type gophercloudClient struct { + c *gophercloud.ServiceClient +} + +func (g *gophercloudClient) EndpointURL() string { + return g.c.Endpoint +} + +func (g *gophercloudClient) Clone(newEndpointURL string) Client { + clonedClient := *g.c + clonedClient.Endpoint = newEndpointURL + return &gophercloudClient{&clonedClient} +} + +func (g *gophercloudClient) Do(req *http.Request) (*http.Response, error) { + return g.do(req, false) +} + +func (g *gophercloudClient) do(req *http.Request, afterReauth bool) (*http.Response, error) { + provider := g.c.ProviderClient + + req.Header.Set("User-Agent", provider.UserAgent.Join()) + for key, value := range provider.AuthenticatedHeaders() { + req.Header.Set(key, value) + } + + resp, err := provider.HTTPClient.Do(req) + if err != nil { + return nil, err + } + + //detect expired token + if resp.StatusCode == http.StatusUnauthorized && !afterReauth { + err := drainResponseBody(resp) + if err != nil { + return nil, err + } + err = provider.Reauthenticate(resp.Request.Header.Get("X-Auth-Token")) + if err != nil { + return nil, err + } + //restart request with new token + return g.do(req, true) + } + + return resp, nil +} @@ -37,7 +37,7 @@ Or, if you use Swift's built-in authentication instead of Keystone: Key: "password", }) -Then, in both cases, you use schwift.AccountFromClient() to obtain a +Then, in both cases, you use schwift.AccountFromGophercloud() to obtain a schwift.Account instance, from which point you have access to all of schwift's API. @@ -24,8 +24,6 @@ import ( "net/http" "net/url" "strings" - - "github.com/gophercloud/gophercloud" ) //RequestOptions contains additional headers and values for a request. @@ -62,8 +60,8 @@ type Request struct { } //URL returns the full URL for this request. -func (r Request) URL(client *gophercloud.ServiceClient, values url.Values) (string, error) { - uri, err := url.Parse(client.Endpoint) +func (r Request) URL(client Client, values url.Values) (string, error) { + uri, err := url.Parse(client.EndpointURL()) if err != nil { return "", err } @@ -86,14 +84,8 @@ func (r Request) URL(client *gophercloud.ServiceClient, values url.Values) (stri return uri.String(), nil } -//Do executes this request on the given service client. -func (r Request) Do(client *gophercloud.ServiceClient) (*http.Response, error) { - return r.do(client, false) -} - -func (r Request) do(client *gophercloud.ServiceClient, afterReauth bool) (*http.Response, error) { - provider := client.ProviderClient - +//Do executes this request on the given Client. +func (r Request) Do(client Client) (*http.Response, error) { //build URL var values url.Values if r.Options != nil { @@ -113,15 +105,11 @@ func (r Request) do(client *gophercloud.ServiceClient, afterReauth bool) (*http. for k, v := range r.Headers { req.Header[k] = v } - req.Header.Set("User-Agent", provider.UserAgent.Join()) - for key, value := range provider.AuthenticatedHeaders() { - req.Header.Set(key, value) - } if r.Body != nil { req.Header.Set("Expect", "100-continue") } - resp, err := provider.HTTPClient.Do(req) + resp, err := client.Do(req) if err != nil { return nil, err } @@ -141,21 +129,7 @@ func (r Request) do(client *gophercloud.ServiceClient, afterReauth bool) (*http. } } - //detect expired token - if resp.StatusCode == http.StatusUnauthorized && !afterReauth { - err := drainResponseBody(resp) - if err != nil { - return nil, err - } - err = provider.Reauthenticate(resp.Request.Header.Get("X-Auth-Token")) - if err != nil { - return nil, err - } - //restart request with new token - return r.do(client, true) - } - - //other unexpected status code -> generate UnexpectedStatusCodeError + //unexpected status code -> generate error buf, err := collectResponseBody(resp) if err != nil { return nil, err diff --git a/shared_test.go b/shared_test.go index 2c1d204..16113e2 100644 --- a/shared_test.go +++ b/shared_test.go @@ -23,8 +23,6 @@ import ( "crypto/rand" "encoding/hex" "math" - "net/http" - "net/url" "os" "testing" @@ -33,26 +31,6 @@ import ( "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/swauth" ) -//This function can be used during debugging to redirect the HTTP requests for -//a specific unit test through a mitmproxy. Put this at the beginning of your -//testcase like so: -// -// testWithAccount(t, func(a *Account) { -// withProxy(a, "http://localhost:8888", func() { -// ... -// -// testWithContainer(t, func(c *Container) { -// withProxy(c.Account(), "http://localhost:8888", func() { -// ... -func withProxy(a *Account, proxyURL string, action func()) { - t := http.DefaultTransport.(*http.Transport) - proxyOrig := t.Proxy - t.Proxy = func(*http.Request) (*url.URL, error) { return url.Parse(proxyURL) } - a.client.ProviderClient.HTTPClient.Transport = t - action() - t.Proxy = proxyOrig -} - func testWithAccount(t *testing.T, testCode func(a *Account)) { stAuth := os.Getenv("ST_AUTH") stUser := os.Getenv("ST_USER") @@ -91,9 +69,9 @@ func testWithAccount(t *testing.T, testCode func(a *Account)) { } } - account, err := AccountFromClient(client) + account, err := AccountFromGophercloud(client) if err != nil { - t.Errorf("schwift.AccountFromClient returned: " + err.Error()) + t.Errorf("schwift.AccountFromGophercloud returned: " + err.Error()) return } testCode(account) |
