blob: 45199a40326fde91377ef02c655cd64e7ef4f539 [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
Ash Wilsonbab89ef2014-10-09 11:00:38 -040076// AuthenticateV2 explicitly authenticates with v2 of the identity service.
77func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
78 return v2auth(client, "", options)
79}
80
Ash Wilson9e172e82014-10-07 16:42:39 -040081func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
82 v2Client := NewIdentityV2(client)
83 if endpoint != "" {
84 v2Client.Endpoint = endpoint
85 }
86
87 result := tokens2.Create(v2Client, tokens2.WrapOptions(options))
88
89 token, err := result.ExtractToken()
90 if err != nil {
91 return err
92 }
93
94 catalog, err := result.ExtractServiceCatalog()
95 if err != nil {
96 return err
97 }
98
99 client.TokenID = token.ID
100 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
101 return os.V2EndpointURL(catalog, opts)
102 }
103
104 return nil
105}
106
107// NewIdentityV2 creates a ServiceClient that may be used to access the v2 identity service.
108func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
109 v2Endpoint := client.IdentityBase + "v2.0/"
110
111 return &gophercloud.ServiceClient{
Ash Wilson13e7dc22014-10-22 09:26:40 -0400112 ProviderClient: client,
113 Endpoint: v2Endpoint,
Ash Wilson9e172e82014-10-07 16:42:39 -0400114 }
Ash Wilson54b03822014-10-07 14:18:41 -0400115}
Jon Perrittccc2e942014-10-15 18:01:21 -0500116
Ash Wilson2bc96b72014-10-20 15:16:10 -0400117// NewComputeV2 creates a ServiceClient that may be used to access the v2 compute service.
118func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
119 eo.ApplyDefaults("compute")
120 url, err := client.EndpointLocator(eo)
121 if err != nil {
122 return nil, err
123 }
Ash Wilsonbb3a3fd2014-10-22 08:16:31 -0400124
Ash Wilson2bc96b72014-10-20 15:16:10 -0400125 return &gophercloud.ServiceClient{
Ash Wilsona93ac3f2014-10-22 14:33:25 -0400126 ProviderClient: client,
127 Endpoint: url,
Ash Wilson2bc96b72014-10-20 15:16:10 -0400128 }, nil
129}
Ash Wilsonbb3a3fd2014-10-22 08:16:31 -0400130
Jon Perrittccc2e942014-10-15 18:01:21 -0500131// NewObjectCDNV1 creates a ServiceClient that may be used with the Rackspace v1 CDN.
132func NewObjectCDNV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
133 eo.ApplyDefaults("rax:object-cdn")
134 url, err := client.EndpointLocator(eo)
135 if err != nil {
136 return nil, err
137 }
Ash Wilson13e7dc22014-10-22 09:26:40 -0400138 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
Jon Perrittccc2e942014-10-15 18:01:21 -0500139}
140
141// NewObjectStorageV1 creates a ServiceClient that may be used with the Rackspace v1 object storage package.
142func NewObjectStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
143 return os.NewObjectStorageV1(client, eo)
144}
Jamie Hannafordf8ef9862014-10-22 12:37:38 +0200145
146// NewBlockStorageV1 creates a ServiceClient that can be used to access the
147// Rackspace Cloud Block Storage v1 API.
148func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
Jamie Hannaford390ff522014-10-23 14:46:48 +0200149 eo.ApplyDefaults("volume")
Jamie Hannafordf8ef9862014-10-22 12:37:38 +0200150 url, err := client.EndpointLocator(eo)
151 if err != nil {
152 return nil, err
153 }
154
155 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
156}
Jamie Hannaford1ede18f2014-11-05 12:37:52 +0100157
158// NewLBV1 creates a ServiceClient that can be used to access the Rackspace
159// Cloud Load Balancer v1 API.
160func NewLBV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
161 eo.ApplyDefaults("rax:load-balancer")
162 url, err := client.EndpointLocator(eo)
163 if err != nil {
164 return nil, err
165 }
166 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
167}
Jon Perritt53c8a3a2014-11-24 07:46:35 -0700168
169// NewNetworkV2 creates a ServiceClient that can be used to access the Rackspace
170// Networking v2 API.
171func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
172 eo.ApplyDefaults("network")
173 url, err := client.EndpointLocator(eo)
174 if err != nil {
175 return nil, err
176 }
177 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
178}
Jon Perrittf36970b2015-01-16 09:13:45 -0700179
180// NewCDNV1 creates a ServiceClient that may be used to access the Rackspace v1
181// CDN service.
182func NewCDNV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
183 eo.ApplyDefaults("rax:cdn")
184 url, err := client.EndpointLocator(eo)
185 if err != nil {
186 return nil, err
187 }
188 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
189}