blob: a2a9e917bed5645f571dda46a6604aeba5cd4a49 [file] [log] [blame]
Ash Wilson8ba82242014-08-28 15:38:55 -04001package openstack
2
3import (
Ash Wilsona87ee062014-09-03 11:26:06 -04004 "fmt"
Ash Wilson09694b92014-09-09 14:08:27 -04005 "net/url"
Ash Wilson4dee1b82014-08-29 14:56:45 -04006
Ash Wilson8ba82242014-08-28 15:38:55 -04007 "github.com/rackspace/gophercloud"
Ash Wilson52fbd182014-10-03 13:48:06 -04008 tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
Ash Wilsonb8401a72014-09-08 17:07:49 -04009 endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints"
10 services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services"
Ash Wilsona87ee062014-09-03 11:26:06 -040011 tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
Ash Wilson4dee1b82014-08-29 14:56:45 -040012 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilson3c8cc772014-09-16 11:40:49 -040013 "github.com/rackspace/gophercloud/pagination"
Ash Wilson8ba82242014-08-28 15:38:55 -040014)
15
Ash Wilson4dee1b82014-08-29 14:56:45 -040016const (
17 v20 = "v2.0"
18 v30 = "v3.0"
19)
Ash Wilson8ba82242014-08-28 15:38:55 -040020
Ash Wilsona87ee062014-09-03 11:26:06 -040021// NewClient prepares an unauthenticated ProviderClient instance.
22// Most users will probably prefer using the AuthenticatedClient function instead.
23// This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly,
24// for example.
25func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
Ash Wilson09694b92014-09-09 14:08:27 -040026 u, err := url.Parse(endpoint)
27 if err != nil {
28 return nil, err
29 }
30 hadPath := u.Path != ""
31 u.Path, u.RawQuery, u.Fragment = "", "", ""
32 base := u.String()
33
Ash Wilsona8440642014-10-07 09:55:58 -040034 endpoint = gophercloud.NormalizeURL(endpoint)
35 base = gophercloud.NormalizeURL(base)
Ash Wilsone7da01c2014-09-09 12:31:06 -040036
Ash Wilson09694b92014-09-09 14:08:27 -040037 if hadPath {
38 return &gophercloud.ProviderClient{
39 IdentityBase: base,
40 IdentityEndpoint: endpoint,
41 }, nil
42 }
43
44 return &gophercloud.ProviderClient{
45 IdentityBase: base,
46 IdentityEndpoint: "",
47 }, nil
Ash Wilsona87ee062014-09-03 11:26:06 -040048}
49
50// AuthenticatedClient logs in to an OpenStack cloud found at the identity endpoint specified by options, acquires a token, and
Ash Wilsonccd020b2014-09-02 10:40:54 -040051// returns a Client instance that's ready to operate.
Ash Wilson8ba82242014-08-28 15:38:55 -040052// It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses
53// the most recent identity service available to proceed.
Ash Wilsona87ee062014-09-03 11:26:06 -040054func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
55 client, err := NewClient(options.IdentityEndpoint)
56 if err != nil {
57 return nil, err
58 }
59
60 err = Authenticate(client, options)
Ash Wilsonccd020b2014-09-02 10:40:54 -040061 if err != nil {
62 return nil, err
63 }
64 return client, nil
65}
66
Ash Wilsonccd020b2014-09-02 10:40:54 -040067// Authenticate or re-authenticate against the most recent identity service supported at the provided endpoint.
Ash Wilsona87ee062014-09-03 11:26:06 -040068func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
Ash Wilson4dee1b82014-08-29 14:56:45 -040069 versions := []*utils.Version{
Ash Wilson09694b92014-09-09 14:08:27 -040070 &utils.Version{ID: v20, Priority: 20, Suffix: "/v2.0/"},
71 &utils.Version{ID: v30, Priority: 30, Suffix: "/v3/"},
Ash Wilson4dee1b82014-08-29 14:56:45 -040072 }
73
Ash Wilson09694b92014-09-09 14:08:27 -040074 chosen, endpoint, err := utils.ChooseVersion(client.IdentityBase, client.IdentityEndpoint, versions)
Ash Wilson4dee1b82014-08-29 14:56:45 -040075 if err != nil {
Ash Wilsonccd020b2014-09-02 10:40:54 -040076 return err
Ash Wilson4dee1b82014-08-29 14:56:45 -040077 }
78
79 switch chosen.ID {
80 case v20:
Ash Wilson09694b92014-09-09 14:08:27 -040081 return v2auth(client, endpoint, options)
Ash Wilson4dee1b82014-08-29 14:56:45 -040082 case v30:
Ash Wilson09694b92014-09-09 14:08:27 -040083 return v3auth(client, endpoint, options)
Ash Wilson4dee1b82014-08-29 14:56:45 -040084 default:
Ash Wilsonccd020b2014-09-02 10:40:54 -040085 // The switch statement must be out of date from the versions list.
Ash Wilsona87ee062014-09-03 11:26:06 -040086 return fmt.Errorf("Unrecognized identity version: %s", chosen.ID)
Ash Wilsonccd020b2014-09-02 10:40:54 -040087 }
88}
89
Ash Wilson09694b92014-09-09 14:08:27 -040090// AuthenticateV2 explicitly authenticates against the identity v2 endpoint.
91func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
92 return v2auth(client, "", options)
93}
94
95func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
96 v2Client := NewIdentityV2(client)
97 if endpoint != "" {
98 v2Client.Endpoint = endpoint
99 }
100
Ash Wilson6cc00702014-10-03 14:00:26 -0400101 result := tokens2.Create(v2Client, options)
Ash Wilson52fbd182014-10-03 13:48:06 -0400102
103 token, err := result.ExtractToken()
Ash Wilson09694b92014-09-09 14:08:27 -0400104 if err != nil {
105 return err
106 }
107
Ash Wilson52fbd182014-10-03 13:48:06 -0400108 catalog, err := result.ExtractServiceCatalog()
Ash Wilson09694b92014-09-09 14:08:27 -0400109 if err != nil {
110 return err
111 }
112
113 client.TokenID = token.ID
114 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
Ash Wilson52fbd182014-10-03 13:48:06 -0400115 return v2endpointLocator(catalog, opts)
Ash Wilson09694b92014-09-09 14:08:27 -0400116 }
117
118 return nil
119}
120
Ash Wilson6cc00702014-10-03 14:00:26 -0400121func v2endpointLocator(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
Ash Wilson9d9876b2014-09-09 09:28:00 -0400122 // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided.
Ash Wilson6cc00702014-10-03 14:00:26 -0400123 var endpoints = make([]tokens2.Endpoint, 0, 1)
124 for _, entry := range catalog.Entries {
Ash Wilsone7da01c2014-09-09 12:31:06 -0400125 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
Ash Wilson9d9876b2014-09-09 09:28:00 -0400126 for _, endpoint := range entry.Endpoints {
127 if opts.Region == "" || endpoint.Region == opts.Region {
128 endpoints = append(endpoints, endpoint)
129 }
130 }
131 }
132 }
133
134 // Report an error if the options were ambiguous.
135 if len(endpoints) == 0 {
136 return "", gophercloud.ErrEndpointNotFound
137 }
138 if len(endpoints) > 1 {
139 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
140 }
141
142 // Extract the appropriate URL from the matching Endpoint.
143 for _, endpoint := range endpoints {
Ash Wilsonefac18b2014-09-10 14:44:42 -0400144 switch opts.Availability {
145 case gophercloud.AvailabilityPublic:
Ash Wilsonaca58d82014-09-10 17:00:35 -0400146 return normalizeURL(endpoint.PublicURL), nil
Ash Wilsonefac18b2014-09-10 14:44:42 -0400147 case gophercloud.AvailabilityInternal:
Ash Wilsonaca58d82014-09-10 17:00:35 -0400148 return normalizeURL(endpoint.InternalURL), nil
Ash Wilson6cc00702014-10-03 14:00:26 -0400149 case gophercloud.AvailabilityAdmin:
150 return normalizeURL(endpoint.AdminURL), nil
Ash Wilson9d9876b2014-09-09 09:28:00 -0400151 default:
Ash Wilsonefac18b2014-09-10 14:44:42 -0400152 return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability)
Ash Wilson9d9876b2014-09-09 09:28:00 -0400153 }
154 }
155
Ash Wilsonb8401a72014-09-08 17:07:49 -0400156 return "", gophercloud.ErrEndpointNotFound
157}
158
Ash Wilson09694b92014-09-09 14:08:27 -0400159// AuthenticateV3 explicitly authenticates against the identity v3 service.
160func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
161 return v3auth(client, "", options)
162}
163
164func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
165 // Override the generated service endpoint with the one returned by the version endpoint.
166 v3Client := NewIdentityV3(client)
167 if endpoint != "" {
168 v3Client.Endpoint = endpoint
169 }
170
Ash Wilson63b2a292014-10-02 09:29:06 -0400171 token, err := tokens3.Create(v3Client, options, nil).Extract()
Ash Wilson09694b92014-09-09 14:08:27 -0400172 if err != nil {
173 return err
174 }
Ash Wilson63b2a292014-10-02 09:29:06 -0400175 client.TokenID = token.ID
Ash Wilson09694b92014-09-09 14:08:27 -0400176
Ash Wilson09694b92014-09-09 14:08:27 -0400177 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
178 return v3endpointLocator(v3Client, opts)
179 }
180
181 return nil
182}
183
Ash Wilsonb8401a72014-09-08 17:07:49 -0400184func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) {
Ash Wilsonb8401a72014-09-08 17:07:49 -0400185 // Discover the service we're interested in.
Ash Wilson566613e2014-09-12 14:51:46 -0400186 var services = make([]services3.Service, 0, 1)
Ash Wilson6b35e502014-09-12 15:15:23 -0400187 servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
Ash Wilson3c8cc772014-09-16 11:40:49 -0400188 err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson566613e2014-09-12 14:51:46 -0400189 part, err := services3.ExtractServices(page)
190 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400191 return false, err
Ash Wilson566613e2014-09-12 14:51:46 -0400192 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400193
Ash Wilson566613e2014-09-12 14:51:46 -0400194 for _, service := range part {
Ash Wilsonb8401a72014-09-08 17:07:49 -0400195 if service.Name == opts.Name {
Ash Wilson566613e2014-09-12 14:51:46 -0400196 services = append(services, service)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400197 }
198 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400199
Ash Wilson6b35e502014-09-12 15:15:23 -0400200 return true, nil
Ash Wilsonb8401a72014-09-08 17:07:49 -0400201 })
202 if err != nil {
203 return "", err
204 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400205
Ash Wilson566613e2014-09-12 14:51:46 -0400206 if len(services) == 0 {
207 return "", gophercloud.ErrServiceNotFound
208 }
209 if len(services) > 1 {
210 return "", fmt.Errorf("Discovered %d matching services: %#v", len(services), services)
211 }
212 service := services[0]
213
214 // Enumerate the endpoints available for this service.
215 var endpoints []endpoints3.Endpoint
Ash Wilson6b35e502014-09-12 15:15:23 -0400216 endpointPager := endpoints3.List(v3Client, endpoints3.ListOpts{
Ash Wilson566613e2014-09-12 14:51:46 -0400217 Availability: opts.Availability,
218 ServiceID: service.ID,
Ash Wilson6b35e502014-09-12 15:15:23 -0400219 })
Ash Wilson3c8cc772014-09-16 11:40:49 -0400220 err = endpointPager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson566613e2014-09-12 14:51:46 -0400221 part, err := endpoints3.ExtractEndpoints(page)
222 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400223 return false, err
Ash Wilson566613e2014-09-12 14:51:46 -0400224 }
225
226 for _, endpoint := range part {
Ash Wilson1cd3e692014-09-09 11:01:47 -0400227 if opts.Region == "" || endpoint.Region == opts.Region {
Ash Wilson566613e2014-09-12 14:51:46 -0400228 endpoints = append(endpoints, endpoint)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400229 }
230 }
Ash Wilson566613e2014-09-12 14:51:46 -0400231
Ash Wilson6b35e502014-09-12 15:15:23 -0400232 return true, nil
Ash Wilson566613e2014-09-12 14:51:46 -0400233 })
234 if err != nil {
235 return "", err
Ash Wilsonb8401a72014-09-08 17:07:49 -0400236 }
237
238 if len(endpoints) == 0 {
239 return "", gophercloud.ErrEndpointNotFound
240 }
241 if len(endpoints) > 1 {
242 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
243 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400244 endpoint := endpoints[0]
245
Ash Wilsona8440642014-10-07 09:55:58 -0400246 return gophercloud.NormalizeURL(endpoint.URL), nil
Ash Wilsonb8401a72014-09-08 17:07:49 -0400247}
248
Ash Wilsona87ee062014-09-03 11:26:06 -0400249// NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service.
250func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400251 v2Endpoint := client.IdentityBase + "v2.0/"
Ash Wilsonccd020b2014-09-02 10:40:54 -0400252
Ash Wilsona87ee062014-09-03 11:26:06 -0400253 return &gophercloud.ServiceClient{
254 Provider: client,
255 Endpoint: v2Endpoint,
Ash Wilson4dee1b82014-08-29 14:56:45 -0400256 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400257}
258
Ash Wilsona87ee062014-09-03 11:26:06 -0400259// NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service.
260func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400261 v3Endpoint := client.IdentityBase + "v3/"
Ash Wilsona87ee062014-09-03 11:26:06 -0400262
263 return &gophercloud.ServiceClient{
264 Provider: client,
265 Endpoint: v3Endpoint,
266 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400267}
Ash Wilson1cd3e692014-09-09 11:01:47 -0400268
269// NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package.
Jon Perritt509fbb62014-09-10 13:29:56 -0500270func NewStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
271 eo.ApplyDefaults("object-store")
272 url, err := client.EndpointLocator(eo)
Ash Wilson1cd3e692014-09-09 11:01:47 -0400273 if err != nil {
274 return nil, err
275 }
276 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
277}
Ash Wilson5e57c1b2014-09-17 09:24:46 -0400278
279// NewComputeV2 creates a ServiceClient that may be used with the v2 compute package.
280func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
281 eo.ApplyDefaults("compute")
282 url, err := client.EndpointLocator(eo)
283 if err != nil {
284 return nil, err
285 }
286 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
287}
Ash Wilsonebc3d122014-09-24 13:44:05 -0400288
289// NewNetworkV2 creates a ServiceClient that may be used with the v2 network package.
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200290func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
291 eo.ApplyDefaults("network")
292 url, err := client.EndpointLocator(eo)
293 if err != nil {
294 return nil, err
295 }
Ash Wilson99541ab2014-10-06 17:32:39 -0400296 return &gophercloud.ServiceClient{
297 Provider: client,
298 Endpoint: url,
299 ResourceBase: url + "v2.0/",
300 }, nil
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200301}
Jon Perrittc5ee85e2014-09-17 00:53:19 -0500302
303// NewBlockStorageV1 creates a ServiceClient that may be used to access the v1 block storage service.
304func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
305 eo.ApplyDefaults("volume")
306 url, err := client.EndpointLocator(eo)
307 if err != nil {
308 return nil, err
309 }
310 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
311}