blob: c4ce622335c2a93817d652f97986e01773440139 [file] [log] [blame]
Ash Wilson54b03822014-10-07 14:18:41 -04001package rackspace
2
3import (
Ash Wilson9e172e82014-10-07 16:42:39 -04004 "fmt"
Ash Wilson54b03822014-10-07 14:18:41 -04005
6 "github.com/rackspace/gophercloud"
7 os "github.com/rackspace/gophercloud/openstack"
Ash Wilson9e172e82014-10-07 16:42:39 -04008 "github.com/rackspace/gophercloud/openstack/utils"
9 tokens2 "github.com/rackspace/gophercloud/rackspace/identity/v2/tokens"
Ash Wilson54b03822014-10-07 14:18:41 -040010)
11
12const (
13 // RackspaceUSIdentity is an identity endpoint located in the United States.
14 RackspaceUSIdentity = "https://identity.api.rackspacecloud.com/v2.0/"
15
16 // RackspaceUKIdentity is an identity endpoint located in the UK.
17 RackspaceUKIdentity = "https://lon.identity.api.rackspacecloud.com/v2.0/"
18)
19
Ash Wilson9e172e82014-10-07 16:42:39 -040020const (
21 v20 = "v2.0"
22)
23
Ash Wilson54b03822014-10-07 14:18:41 -040024// NewClient creates a client that's prepared to communicate with the Rackspace API, but is not
25// yet authenticated. Most users will probably prefer using the AuthenticatedClient function
26// instead.
27//
28// Provide the base URL of the identity endpoint you wish to authenticate against as "endpoint".
29// Often, this will be either RackspaceUSIdentity or RackspaceUKIdentity.
30func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
Ash Wilson0d86a3e2014-10-09 11:00:21 -040031 if endpoint == "" {
32 return os.NewClient(RackspaceUSIdentity)
33 }
Ash Wilson54b03822014-10-07 14:18:41 -040034 return os.NewClient(endpoint)
35}
36
37// AuthenticatedClient logs in to Rackspace with the provided credentials and constructs a
38// ProviderClient that's ready to operate.
39//
40// If the provided AuthOptions does not specify an explicit IdentityEndpoint, it will default to
41// the canonical, production Rackspace US identity endpoint.
42func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
Ash Wilson9e172e82014-10-07 16:42:39 -040043 client, err := NewClient(options.IdentityEndpoint)
Ash Wilson54b03822014-10-07 14:18:41 -040044 if err != nil {
45 return nil, err
46 }
47
Ash Wilson9e172e82014-10-07 16:42:39 -040048 err = Authenticate(client, options)
49 if err != nil {
50 return nil, err
51 }
52 return client, nil
53}
54
55// Authenticate or re-authenticate against the most recent identity service supported at the
56// provided endpoint.
57func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
58 versions := []*utils.Version{
59 &utils.Version{ID: v20, Priority: 20, Suffix: "/v2.0/"},
60 }
61
62 chosen, endpoint, err := utils.ChooseVersion(client.IdentityBase, client.IdentityEndpoint, versions)
63 if err != nil {
64 return err
65 }
66
67 switch chosen.ID {
68 case v20:
69 return v2auth(client, endpoint, options)
70 default:
71 // The switch statement must be out of date from the versions list.
72 return fmt.Errorf("Unrecognized identity version: %s", chosen.ID)
73 }
74}
75
76func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
77 v2Client := NewIdentityV2(client)
78 if endpoint != "" {
79 v2Client.Endpoint = endpoint
80 }
81
82 result := tokens2.Create(v2Client, tokens2.WrapOptions(options))
83
84 token, err := result.ExtractToken()
85 if err != nil {
86 return err
87 }
88
89 catalog, err := result.ExtractServiceCatalog()
90 if err != nil {
91 return err
92 }
93
94 client.TokenID = token.ID
95 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
96 return os.V2EndpointURL(catalog, opts)
97 }
98
99 return nil
100}
101
102// NewIdentityV2 creates a ServiceClient that may be used to access the v2 identity service.
103func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
104 v2Endpoint := client.IdentityBase + "v2.0/"
105
106 return &gophercloud.ServiceClient{
107 Provider: client,
108 Endpoint: v2Endpoint,
109 }
Ash Wilson54b03822014-10-07 14:18:41 -0400110}