aboutsummaryrefslogtreecommitdiff
path: root/gopherschwift/package.go
blob: 7c66b1611362b0e6499ba57e753e1d2de1af7026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/******************************************************************************
*
*  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 gopherschwift contains a Gophercloud backend for Schwift.

If your application uses Gophercloud (https://github.com/gophercloud/gophercloud),
you can use the Wrap() function in this package as an entrypoint to Schwift.
A schwift.Account created this way will re-use Gophercloud's authentication code,
so you only need to obtain a client token once using Gophercloud. For example:

	authOptions, err := openstack.AuthOptionsFromEnv() // or build a gophercloud.AuthOptions instance yourself
	provider, err := openstack.AuthenticatedClient(authOptions)
	client, err := openstack.NewObjectStorageV1(provider, gophercloud.EndpointOpts{})

  account, err := gopherschwift.Wrap(client)

Using this schwift.Account instance, you have access to all of schwift's API.

*/
package gopherschwift

import (
	"io"
	"io/ioutil"
	"net/http"

	"github.com/gophercloud/gophercloud"
	"github.com/majewsky/schwift"
)

//Wrap creates a schwift.Account that uses the given service client as its
//backend. The service client must refer to a Swift endpoint, i.e. it should
//have been created by openstack.NewObjectStorageV1().
func Wrap(client *gophercloud.ServiceClient) (*schwift.Account, error) {
	return schwift.InitializeAccount(&backend{client})
}

type backend struct {
	c *gophercloud.ServiceClient
}

func (g *backend) EndpointURL() string {
	return g.c.Endpoint
}

func (g *backend) Clone(newEndpointURL string) schwift.Backend {
	clonedClient := *g.c
	clonedClient.Endpoint = newEndpointURL
	return &backend{&clonedClient}
}

func (g *backend) Do(req *http.Request) (*http.Response, error) {
	return g.do(req, false)
}

func (g *backend) 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 := io.Copy(ioutil.Discard, resp.Body)
		if err != nil {
			return nil, err
		}
		err = resp.Body.Close()
		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
}