blob: c5fd16fbb5c3e7b9e6c707abe6f9680cfdaaff6c [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 Wilsoned6a1d82014-09-03 12:01:00 -04006 "strings"
Ash Wilson4dee1b82014-08-29 14:56:45 -04007
Ash Wilson8ba82242014-08-28 15:38:55 -04008 "github.com/rackspace/gophercloud"
Ash Wilson52fbd182014-10-03 13:48:06 -04009 tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
Ash Wilsonb8401a72014-09-08 17:07:49 -040010 endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints"
11 services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services"
Ash Wilsona87ee062014-09-03 11:26:06 -040012 tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
Ash Wilson4dee1b82014-08-29 14:56:45 -040013 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilson3c8cc772014-09-16 11:40:49 -040014 "github.com/rackspace/gophercloud/pagination"
Ash Wilson8ba82242014-08-28 15:38:55 -040015)
16
Ash Wilson4dee1b82014-08-29 14:56:45 -040017const (
18 v20 = "v2.0"
19 v30 = "v3.0"
20)
Ash Wilson8ba82242014-08-28 15:38:55 -040021
Ash Wilsona87ee062014-09-03 11:26:06 -040022// NewClient prepares an unauthenticated ProviderClient instance.
23// Most users will probably prefer using the AuthenticatedClient function instead.
24// This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly,
25// for example.
26func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
Ash Wilson09694b92014-09-09 14:08:27 -040027 u, err := url.Parse(endpoint)
28 if err != nil {
29 return nil, err
30 }
31 hadPath := u.Path != ""
32 u.Path, u.RawQuery, u.Fragment = "", "", ""
33 base := u.String()
34
Ash Wilsonaca58d82014-09-10 17:00:35 -040035 endpoint = normalizeURL(endpoint)
36 base = normalizeURL(base)
Ash Wilsone7da01c2014-09-09 12:31:06 -040037
Ash Wilson09694b92014-09-09 14:08:27 -040038 if hadPath {
39 return &gophercloud.ProviderClient{
40 IdentityBase: base,
41 IdentityEndpoint: endpoint,
42 }, nil
43 }
44
45 return &gophercloud.ProviderClient{
46 IdentityBase: base,
47 IdentityEndpoint: "",
48 }, nil
Ash Wilsona87ee062014-09-03 11:26:06 -040049}
50
51// 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 -040052// returns a Client instance that's ready to operate.
Ash Wilson8ba82242014-08-28 15:38:55 -040053// It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses
54// the most recent identity service available to proceed.
Ash Wilsona87ee062014-09-03 11:26:06 -040055func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
56 client, err := NewClient(options.IdentityEndpoint)
57 if err != nil {
58 return nil, err
59 }
60
61 err = Authenticate(client, options)
Ash Wilsonccd020b2014-09-02 10:40:54 -040062 if err != nil {
63 return nil, err
64 }
65 return client, nil
66}
67
Ash Wilsonccd020b2014-09-02 10:40:54 -040068// Authenticate or re-authenticate against the most recent identity service supported at the provided endpoint.
Ash Wilsona87ee062014-09-03 11:26:06 -040069func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
Ash Wilson4dee1b82014-08-29 14:56:45 -040070 versions := []*utils.Version{
Ash Wilson09694b92014-09-09 14:08:27 -040071 &utils.Version{ID: v20, Priority: 20, Suffix: "/v2.0/"},
72 &utils.Version{ID: v30, Priority: 30, Suffix: "/v3/"},
Ash Wilson4dee1b82014-08-29 14:56:45 -040073 }
74
Ash Wilson09694b92014-09-09 14:08:27 -040075 chosen, endpoint, err := utils.ChooseVersion(client.IdentityBase, client.IdentityEndpoint, versions)
Ash Wilson4dee1b82014-08-29 14:56:45 -040076 if err != nil {
Ash Wilsonccd020b2014-09-02 10:40:54 -040077 return err
Ash Wilson4dee1b82014-08-29 14:56:45 -040078 }
79
80 switch chosen.ID {
81 case v20:
Ash Wilson09694b92014-09-09 14:08:27 -040082 return v2auth(client, endpoint, options)
Ash Wilson4dee1b82014-08-29 14:56:45 -040083 case v30:
Ash Wilson09694b92014-09-09 14:08:27 -040084 return v3auth(client, endpoint, options)
Ash Wilson4dee1b82014-08-29 14:56:45 -040085 default:
Ash Wilsonccd020b2014-09-02 10:40:54 -040086 // The switch statement must be out of date from the versions list.
Ash Wilsona87ee062014-09-03 11:26:06 -040087 return fmt.Errorf("Unrecognized identity version: %s", chosen.ID)
Ash Wilsonccd020b2014-09-02 10:40:54 -040088 }
89}
90
Ash Wilson09694b92014-09-09 14:08:27 -040091// AuthenticateV2 explicitly authenticates against the identity v2 endpoint.
92func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
93 return v2auth(client, "", options)
94}
95
96func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
97 v2Client := NewIdentityV2(client)
98 if endpoint != "" {
99 v2Client.Endpoint = endpoint
100 }
101
Ash Wilson52fbd182014-10-03 13:48:06 -0400102 result := tokens2.Create(v2client, options)
103
104 token, err := result.ExtractToken()
Ash Wilson09694b92014-09-09 14:08:27 -0400105 if err != nil {
106 return err
107 }
108
Ash Wilson52fbd182014-10-03 13:48:06 -0400109 catalog, err := result.ExtractServiceCatalog()
Ash Wilson09694b92014-09-09 14:08:27 -0400110 if err != nil {
111 return err
112 }
113
114 client.TokenID = token.ID
115 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
Ash Wilson52fbd182014-10-03 13:48:06 -0400116 return v2endpointLocator(catalog, opts)
Ash Wilson09694b92014-09-09 14:08:27 -0400117 }
118
119 return nil
120}
121
Ash Wilson52fbd182014-10-03 13:48:06 -0400122func v2endpointLocator(catalog tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
Ash Wilson9d9876b2014-09-09 09:28:00 -0400123 catalog, err := identity2.GetServiceCatalog(authResults)
124 if err != nil {
125 return "", err
126 }
127
128 entries, err := catalog.CatalogEntries()
129 if err != nil {
130 return "", err
131 }
132
133 // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided.
Ash Wilsone7da01c2014-09-09 12:31:06 -0400134 var endpoints = make([]identity2.Endpoint, 0, 1)
Ash Wilson9d9876b2014-09-09 09:28:00 -0400135 for _, entry := range entries {
Ash Wilsone7da01c2014-09-09 12:31:06 -0400136 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
Ash Wilson9d9876b2014-09-09 09:28:00 -0400137 for _, endpoint := range entry.Endpoints {
138 if opts.Region == "" || endpoint.Region == opts.Region {
139 endpoints = append(endpoints, endpoint)
140 }
141 }
142 }
143 }
144
145 // Report an error if the options were ambiguous.
146 if len(endpoints) == 0 {
147 return "", gophercloud.ErrEndpointNotFound
148 }
149 if len(endpoints) > 1 {
150 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
151 }
152
153 // Extract the appropriate URL from the matching Endpoint.
154 for _, endpoint := range endpoints {
Ash Wilsonefac18b2014-09-10 14:44:42 -0400155 switch opts.Availability {
156 case gophercloud.AvailabilityPublic:
Ash Wilsonaca58d82014-09-10 17:00:35 -0400157 return normalizeURL(endpoint.PublicURL), nil
Ash Wilsonefac18b2014-09-10 14:44:42 -0400158 case gophercloud.AvailabilityInternal:
Ash Wilsonaca58d82014-09-10 17:00:35 -0400159 return normalizeURL(endpoint.InternalURL), nil
Ash Wilson9d9876b2014-09-09 09:28:00 -0400160 default:
Ash Wilsonefac18b2014-09-10 14:44:42 -0400161 return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability)
Ash Wilson9d9876b2014-09-09 09:28:00 -0400162 }
163 }
164
Ash Wilsonb8401a72014-09-08 17:07:49 -0400165 return "", gophercloud.ErrEndpointNotFound
166}
167
Ash Wilson09694b92014-09-09 14:08:27 -0400168// AuthenticateV3 explicitly authenticates against the identity v3 service.
169func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
170 return v3auth(client, "", options)
171}
172
173func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
174 // Override the generated service endpoint with the one returned by the version endpoint.
175 v3Client := NewIdentityV3(client)
176 if endpoint != "" {
177 v3Client.Endpoint = endpoint
178 }
179
180 result, err := tokens3.Create(v3Client, options, nil)
181 if err != nil {
182 return err
183 }
184
185 client.TokenID, err = result.TokenID()
186 if err != nil {
187 return err
188 }
189 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
190 return v3endpointLocator(v3Client, opts)
191 }
192
193 return nil
194}
195
Ash Wilsonb8401a72014-09-08 17:07:49 -0400196func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) {
Ash Wilsonb8401a72014-09-08 17:07:49 -0400197 // Discover the service we're interested in.
Ash Wilson566613e2014-09-12 14:51:46 -0400198 var services = make([]services3.Service, 0, 1)
Ash Wilson6b35e502014-09-12 15:15:23 -0400199 servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
Ash Wilson3c8cc772014-09-16 11:40:49 -0400200 err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson566613e2014-09-12 14:51:46 -0400201 part, err := services3.ExtractServices(page)
202 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400203 return false, err
Ash Wilson566613e2014-09-12 14:51:46 -0400204 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400205
Ash Wilson566613e2014-09-12 14:51:46 -0400206 for _, service := range part {
Ash Wilsonb8401a72014-09-08 17:07:49 -0400207 if service.Name == opts.Name {
Ash Wilson566613e2014-09-12 14:51:46 -0400208 services = append(services, service)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400209 }
210 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400211
Ash Wilson6b35e502014-09-12 15:15:23 -0400212 return true, nil
Ash Wilsonb8401a72014-09-08 17:07:49 -0400213 })
214 if err != nil {
215 return "", err
216 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400217
Ash Wilson566613e2014-09-12 14:51:46 -0400218 if len(services) == 0 {
219 return "", gophercloud.ErrServiceNotFound
220 }
221 if len(services) > 1 {
222 return "", fmt.Errorf("Discovered %d matching services: %#v", len(services), services)
223 }
224 service := services[0]
225
226 // Enumerate the endpoints available for this service.
227 var endpoints []endpoints3.Endpoint
Ash Wilson6b35e502014-09-12 15:15:23 -0400228 endpointPager := endpoints3.List(v3Client, endpoints3.ListOpts{
Ash Wilson566613e2014-09-12 14:51:46 -0400229 Availability: opts.Availability,
230 ServiceID: service.ID,
Ash Wilson6b35e502014-09-12 15:15:23 -0400231 })
Ash Wilson3c8cc772014-09-16 11:40:49 -0400232 err = endpointPager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson566613e2014-09-12 14:51:46 -0400233 part, err := endpoints3.ExtractEndpoints(page)
234 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400235 return false, err
Ash Wilson566613e2014-09-12 14:51:46 -0400236 }
237
238 for _, endpoint := range part {
Ash Wilson1cd3e692014-09-09 11:01:47 -0400239 if opts.Region == "" || endpoint.Region == opts.Region {
Ash Wilson566613e2014-09-12 14:51:46 -0400240 endpoints = append(endpoints, endpoint)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400241 }
242 }
Ash Wilson566613e2014-09-12 14:51:46 -0400243
Ash Wilson6b35e502014-09-12 15:15:23 -0400244 return true, nil
Ash Wilson566613e2014-09-12 14:51:46 -0400245 })
246 if err != nil {
247 return "", err
Ash Wilsonb8401a72014-09-08 17:07:49 -0400248 }
249
250 if len(endpoints) == 0 {
251 return "", gophercloud.ErrEndpointNotFound
252 }
253 if len(endpoints) > 1 {
254 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
255 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400256 endpoint := endpoints[0]
257
Ash Wilsonaca58d82014-09-10 17:00:35 -0400258 return normalizeURL(endpoint.URL), nil
259}
260
261// normalizeURL ensures that each endpoint URL has a closing `/`, as expected by ServiceClient.
262func normalizeURL(url string) string {
263 if !strings.HasSuffix(url, "/") {
264 return url + "/"
265 }
266 return url
Ash Wilsonb8401a72014-09-08 17:07:49 -0400267}
268
Ash Wilsona87ee062014-09-03 11:26:06 -0400269// NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service.
270func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400271 v2Endpoint := client.IdentityBase + "v2.0/"
Ash Wilsonccd020b2014-09-02 10:40:54 -0400272
Ash Wilsona87ee062014-09-03 11:26:06 -0400273 return &gophercloud.ServiceClient{
274 Provider: client,
275 Endpoint: v2Endpoint,
Ash Wilson4dee1b82014-08-29 14:56:45 -0400276 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400277}
278
Ash Wilsona87ee062014-09-03 11:26:06 -0400279// NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service.
280func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400281 v3Endpoint := client.IdentityBase + "v3/"
Ash Wilsona87ee062014-09-03 11:26:06 -0400282
283 return &gophercloud.ServiceClient{
284 Provider: client,
285 Endpoint: v3Endpoint,
286 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400287}
Ash Wilson1cd3e692014-09-09 11:01:47 -0400288
289// NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package.
Jon Perritt509fbb62014-09-10 13:29:56 -0500290func NewStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
291 eo.ApplyDefaults("object-store")
292 url, err := client.EndpointLocator(eo)
Ash Wilson1cd3e692014-09-09 11:01:47 -0400293 if err != nil {
294 return nil, err
295 }
296 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
297}
Ash Wilson5e57c1b2014-09-17 09:24:46 -0400298
299// NewComputeV2 creates a ServiceClient that may be used with the v2 compute package.
300func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
301 eo.ApplyDefaults("compute")
302 url, err := client.EndpointLocator(eo)
303 if err != nil {
304 return nil, err
305 }
306 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
307}
Ash Wilsonebc3d122014-09-24 13:44:05 -0400308
309// NewNetworkV2 creates a ServiceClient that may be used with the v2 network package.
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200310func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
311 eo.ApplyDefaults("network")
312 url, err := client.EndpointLocator(eo)
313 if err != nil {
314 return nil, err
315 }
316 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
317}