Joe Topjian | 7a0f3e5 | 2016-08-22 20:26:34 -0600 | [diff] [blame] | 1 | package swauth |
| 2 | |
| 3 | import "github.com/gophercloud/gophercloud" |
| 4 | |
| 5 | // AuthOptsBuilder describes struct types that can be accepted by the Auth call. |
| 6 | // The AuthOpts struct in this package does. |
| 7 | type AuthOptsBuilder interface { |
| 8 | ToAuthOptsMap() (map[string]string, error) |
| 9 | } |
| 10 | |
| 11 | // AuthOpts specifies an authentication request. |
| 12 | type AuthOpts struct { |
| 13 | // User is an Swauth-based username in username:tenant format. |
| 14 | User string `h:"X-Auth-User" required:"true"` |
| 15 | // Key is a secret/password to authenticate the User with. |
| 16 | Key string `h:"X-Auth-Key" required:"true"` |
| 17 | } |
| 18 | |
| 19 | // ToAuthOptsMap formats an AuthOpts structure into a request body. |
| 20 | func (opts AuthOpts) ToAuthOptsMap() (map[string]string, error) { |
| 21 | return gophercloud.BuildHeaders(opts) |
| 22 | } |
| 23 | |
| 24 | // Auth performs an authentication request for a Swauth-based user. |
| 25 | func Auth(c *gophercloud.ProviderClient, opts AuthOptsBuilder) (r GetAuthResult) { |
| 26 | h := make(map[string]string) |
| 27 | |
| 28 | if opts != nil { |
| 29 | headers, err := opts.ToAuthOptsMap() |
| 30 | if err != nil { |
| 31 | r.Err = err |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | for k, v := range headers { |
| 36 | h[k] = v |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | resp, err := c.Request("GET", getURL(c), &gophercloud.RequestOpts{ |
| 41 | MoreHeaders: h, |
| 42 | OkCodes: []int{200}, |
| 43 | }) |
| 44 | |
| 45 | if resp != nil { |
| 46 | r.Header = resp.Header |
| 47 | } |
| 48 | |
| 49 | r.Err = err |
| 50 | |
| 51 | return r |
| 52 | } |
| 53 | |
| 54 | // NewObjectStorageV1 creates a Swauth-authenticated *gophercloud.ServiceClient |
| 55 | // client that can issue ObjectStorage-based API calls. |
| 56 | func NewObjectStorageV1(pc *gophercloud.ProviderClient, authOpts AuthOpts) (*gophercloud.ServiceClient, error) { |
| 57 | auth, err := Auth(pc, authOpts).Extract() |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | swiftClient := &gophercloud.ServiceClient{ |
| 63 | ProviderClient: pc, |
Joe Topjian | 176266d | 2016-11-07 14:45:46 -0700 | [diff] [blame^] | 64 | Endpoint: gophercloud.NormalizeURL(auth.StorageURL), |
Joe Topjian | 7a0f3e5 | 2016-08-22 20:26:34 -0600 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | swiftClient.TokenID = auth.Token |
| 68 | |
| 69 | return swiftClient, nil |
| 70 | } |