Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame^] | 4 | "errors" |
| 5 | |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | identity3 "github.com/rackspace/gophercloud/openstack/identity/v3" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame^] | 8 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // Client provides access to service clients for this OpenStack cloud. |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame^] | 12 | type Client struct { |
| 13 | gophercloud.ProviderClient |
| 14 | } |
| 15 | |
| 16 | const ( |
| 17 | v20 = "v2.0" |
| 18 | v30 = "v3.0" |
| 19 | ) |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 20 | |
| 21 | // NewClient authenticates to an OpenStack cloud with the provided credentials. |
| 22 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 23 | // the most recent identity service available to proceed. |
| 24 | func NewClient(authOptions gophercloud.AuthOptions) (*Client, error) { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame^] | 25 | versions := []*utils.Version{ |
| 26 | &utils.Version{ID: v20, Priority: 20}, |
| 27 | &utils.Version{ID: v30, Priority: 30}, |
| 28 | } |
| 29 | |
| 30 | chosen, endpoint, err := utils.ChooseVersion(authOptions.IdentityEndpoint, versions) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | |
| 35 | client := Client{ |
| 36 | ProviderClient: gophercloud.ProviderClient{ |
| 37 | IdentityEndpoint: endpoint, |
| 38 | Options: authOptions, |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | switch chosen.ID { |
| 43 | case v20: |
| 44 | case v30: |
| 45 | identityClient := identity3.NewClient(&client.ProviderClient) |
| 46 | token, err := identityClient.Authenticate(authOptions) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | client.ProviderClient.TokenID = token.ID |
| 52 | default: |
| 53 | // The switch must be out of sync with "versions". |
| 54 | return nil, errors.New("Wat") |
| 55 | } |
| 56 | |
| 57 | return &client, nil |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // IdentityV3 explicitly accesses the v3 identity service. |
| 61 | func (client *Client) IdentityV3() (*identity3.Client, error) { |
| 62 | return nil, nil |
| 63 | } |