blob: e14a48bfb9fe6d16eebb970e84470c65004dd4a5 [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 Wilson11c98282014-09-08 16:05:10 -04009 identity2 "github.com/rackspace/gophercloud/openstack/identity/v2"
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
102 result, err := identity2.Authenticate(v2Client, options)
103 if err != nil {
104 return err
105 }
106
107 token, err := identity2.GetToken(result)
108 if err != nil {
109 return err
110 }
111
112 client.TokenID = token.ID
113 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
114 return v2endpointLocator(result, opts)
115 }
116
117 return nil
118}
119
Ash Wilson9d9876b2014-09-09 09:28:00 -0400120func v2endpointLocator(authResults identity2.AuthResults, opts gophercloud.EndpointOpts) (string, error) {
121 catalog, err := identity2.GetServiceCatalog(authResults)
122 if err != nil {
123 return "", err
124 }
125
126 entries, err := catalog.CatalogEntries()
127 if err != nil {
128 return "", err
129 }
130
131 // 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 -0400132 var endpoints = make([]identity2.Endpoint, 0, 1)
Ash Wilson9d9876b2014-09-09 09:28:00 -0400133 for _, entry := range entries {
Ash Wilsone7da01c2014-09-09 12:31:06 -0400134 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
Ash Wilson9d9876b2014-09-09 09:28:00 -0400135 for _, endpoint := range entry.Endpoints {
136 if opts.Region == "" || endpoint.Region == opts.Region {
137 endpoints = append(endpoints, endpoint)
138 }
139 }
140 }
141 }
142
143 // Report an error if the options were ambiguous.
144 if len(endpoints) == 0 {
145 return "", gophercloud.ErrEndpointNotFound
146 }
147 if len(endpoints) > 1 {
148 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
149 }
150
151 // Extract the appropriate URL from the matching Endpoint.
152 for _, endpoint := range endpoints {
Ash Wilsonefac18b2014-09-10 14:44:42 -0400153 switch opts.Availability {
154 case gophercloud.AvailabilityPublic:
Ash Wilsonaca58d82014-09-10 17:00:35 -0400155 return normalizeURL(endpoint.PublicURL), nil
Ash Wilsonefac18b2014-09-10 14:44:42 -0400156 case gophercloud.AvailabilityInternal:
Ash Wilsonaca58d82014-09-10 17:00:35 -0400157 return normalizeURL(endpoint.InternalURL), nil
Ash Wilson9d9876b2014-09-09 09:28:00 -0400158 default:
Ash Wilsonefac18b2014-09-10 14:44:42 -0400159 return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability)
Ash Wilson9d9876b2014-09-09 09:28:00 -0400160 }
161 }
162
Ash Wilsonb8401a72014-09-08 17:07:49 -0400163 return "", gophercloud.ErrEndpointNotFound
164}
165
Ash Wilson09694b92014-09-09 14:08:27 -0400166// AuthenticateV3 explicitly authenticates against the identity v3 service.
167func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
168 return v3auth(client, "", options)
169}
170
171func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
172 // Override the generated service endpoint with the one returned by the version endpoint.
173 v3Client := NewIdentityV3(client)
174 if endpoint != "" {
175 v3Client.Endpoint = endpoint
176 }
177
Ash Wilson63b2a292014-10-02 09:29:06 -0400178 token, err := tokens3.Create(v3Client, options, nil).Extract()
Ash Wilson09694b92014-09-09 14:08:27 -0400179 if err != nil {
180 return err
181 }
Ash Wilson63b2a292014-10-02 09:29:06 -0400182 client.TokenID = token.ID
Ash Wilson09694b92014-09-09 14:08:27 -0400183
Ash Wilson09694b92014-09-09 14:08:27 -0400184 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
185 return v3endpointLocator(v3Client, opts)
186 }
187
188 return nil
189}
190
Ash Wilsonb8401a72014-09-08 17:07:49 -0400191func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) {
Ash Wilsonb8401a72014-09-08 17:07:49 -0400192 // Discover the service we're interested in.
Ash Wilson566613e2014-09-12 14:51:46 -0400193 var services = make([]services3.Service, 0, 1)
Ash Wilson6b35e502014-09-12 15:15:23 -0400194 servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
Ash Wilson3c8cc772014-09-16 11:40:49 -0400195 err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson566613e2014-09-12 14:51:46 -0400196 part, err := services3.ExtractServices(page)
197 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400198 return false, err
Ash Wilson566613e2014-09-12 14:51:46 -0400199 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400200
Ash Wilson566613e2014-09-12 14:51:46 -0400201 for _, service := range part {
Ash Wilsonb8401a72014-09-08 17:07:49 -0400202 if service.Name == opts.Name {
Ash Wilson566613e2014-09-12 14:51:46 -0400203 services = append(services, service)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400204 }
205 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400206
Ash Wilson6b35e502014-09-12 15:15:23 -0400207 return true, nil
Ash Wilsonb8401a72014-09-08 17:07:49 -0400208 })
209 if err != nil {
210 return "", err
211 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400212
Ash Wilson566613e2014-09-12 14:51:46 -0400213 if len(services) == 0 {
214 return "", gophercloud.ErrServiceNotFound
215 }
216 if len(services) > 1 {
217 return "", fmt.Errorf("Discovered %d matching services: %#v", len(services), services)
218 }
219 service := services[0]
220
221 // Enumerate the endpoints available for this service.
222 var endpoints []endpoints3.Endpoint
Ash Wilson6b35e502014-09-12 15:15:23 -0400223 endpointPager := endpoints3.List(v3Client, endpoints3.ListOpts{
Ash Wilson566613e2014-09-12 14:51:46 -0400224 Availability: opts.Availability,
225 ServiceID: service.ID,
Ash Wilson6b35e502014-09-12 15:15:23 -0400226 })
Ash Wilson3c8cc772014-09-16 11:40:49 -0400227 err = endpointPager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson566613e2014-09-12 14:51:46 -0400228 part, err := endpoints3.ExtractEndpoints(page)
229 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400230 return false, err
Ash Wilson566613e2014-09-12 14:51:46 -0400231 }
232
233 for _, endpoint := range part {
Ash Wilson1cd3e692014-09-09 11:01:47 -0400234 if opts.Region == "" || endpoint.Region == opts.Region {
Ash Wilson566613e2014-09-12 14:51:46 -0400235 endpoints = append(endpoints, endpoint)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400236 }
237 }
Ash Wilson566613e2014-09-12 14:51:46 -0400238
Ash Wilson6b35e502014-09-12 15:15:23 -0400239 return true, nil
Ash Wilson566613e2014-09-12 14:51:46 -0400240 })
241 if err != nil {
242 return "", err
Ash Wilsonb8401a72014-09-08 17:07:49 -0400243 }
244
245 if len(endpoints) == 0 {
246 return "", gophercloud.ErrEndpointNotFound
247 }
248 if len(endpoints) > 1 {
249 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
250 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400251 endpoint := endpoints[0]
252
Ash Wilsonaca58d82014-09-10 17:00:35 -0400253 return normalizeURL(endpoint.URL), nil
254}
255
256// normalizeURL ensures that each endpoint URL has a closing `/`, as expected by ServiceClient.
257func normalizeURL(url string) string {
258 if !strings.HasSuffix(url, "/") {
259 return url + "/"
260 }
261 return url
Ash Wilsonb8401a72014-09-08 17:07:49 -0400262}
263
Ash Wilsona87ee062014-09-03 11:26:06 -0400264// NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service.
265func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400266 v2Endpoint := client.IdentityBase + "v2.0/"
Ash Wilsonccd020b2014-09-02 10:40:54 -0400267
Ash Wilsona87ee062014-09-03 11:26:06 -0400268 return &gophercloud.ServiceClient{
269 Provider: client,
270 Endpoint: v2Endpoint,
Ash Wilson4dee1b82014-08-29 14:56:45 -0400271 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400272}
273
Ash Wilsona87ee062014-09-03 11:26:06 -0400274// NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service.
275func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400276 v3Endpoint := client.IdentityBase + "v3/"
Ash Wilsona87ee062014-09-03 11:26:06 -0400277
278 return &gophercloud.ServiceClient{
279 Provider: client,
280 Endpoint: v3Endpoint,
281 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400282}
Ash Wilson1cd3e692014-09-09 11:01:47 -0400283
284// NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package.
Jon Perritt509fbb62014-09-10 13:29:56 -0500285func NewStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
286 eo.ApplyDefaults("object-store")
287 url, err := client.EndpointLocator(eo)
Ash Wilson1cd3e692014-09-09 11:01:47 -0400288 if err != nil {
289 return nil, err
290 }
291 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
292}
Ash Wilson5e57c1b2014-09-17 09:24:46 -0400293
294// NewComputeV2 creates a ServiceClient that may be used with the v2 compute package.
295func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
296 eo.ApplyDefaults("compute")
297 url, err := client.EndpointLocator(eo)
298 if err != nil {
299 return nil, err
300 }
301 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
302}
Ash Wilsonebc3d122014-09-24 13:44:05 -0400303
304// NewNetworkV2 creates a ServiceClient that may be used with the v2 network package.
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200305func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
306 eo.ApplyDefaults("network")
307 url, err := client.EndpointLocator(eo)
308 if err != nil {
309 return nil, err
310 }
311 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
312}