blob: 6e0de0605ac814b8ae6d576fce850f0b0376a78b [file] [log] [blame]
Ash Wilson8ba82242014-08-28 15:38:55 -04001package openstack
2
3import (
Ash Wilson4dee1b82014-08-29 14:56:45 -04004 "errors"
5
Ash Wilson8ba82242014-08-28 15:38:55 -04006 "github.com/rackspace/gophercloud"
7 identity3 "github.com/rackspace/gophercloud/openstack/identity/v3"
Ash Wilson4dee1b82014-08-29 14:56:45 -04008 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilson8ba82242014-08-28 15:38:55 -04009)
10
11// Client provides access to service clients for this OpenStack cloud.
Ash Wilson4dee1b82014-08-29 14:56:45 -040012type Client struct {
13 gophercloud.ProviderClient
14}
15
16const (
17 v20 = "v2.0"
18 v30 = "v3.0"
19)
Ash Wilson8ba82242014-08-28 15:38:55 -040020
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.
24func NewClient(authOptions gophercloud.AuthOptions) (*Client, error) {
Ash Wilson4dee1b82014-08-29 14:56:45 -040025 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 Wilson8ba82242014-08-28 15:38:55 -040058}
59
60// IdentityV3 explicitly accesses the v3 identity service.
61func (client *Client) IdentityV3() (*identity3.Client, error) {
62 return nil, nil
63}