From 8f777460661bbbcbe42730979140f525b382110e Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Mon, 19 Feb 2018 20:51:16 +0100 Subject: introduce Client interface --- account.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'account.go') diff --git a/account.go b/account.go index bbca9c5..6abfaf3 100644 --- a/account.go +++ b/account.go @@ -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 } -- cgit v1.2.3