blob: 253fcdcf5e6b6b25aece2acf2972ab179d1cfeda [file] [log] [blame]
Ash Wilson8ba82242014-08-28 15:38:55 -04001package openstack
2
3import (
Ash Wilsona87ee062014-09-03 11:26:06 -04004 "fmt"
Ash Wilsoned6a1d82014-09-03 12:01:00 -04005 "strings"
Ash Wilson4dee1b82014-08-29 14:56:45 -04006
Ash Wilson8ba82242014-08-28 15:38:55 -04007 "github.com/rackspace/gophercloud"
Ash Wilson11c98282014-09-08 16:05:10 -04008 identity2 "github.com/rackspace/gophercloud/openstack/identity/v2"
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 Wilson8ba82242014-08-28 15:38:55 -040013)
14
Ash Wilson4dee1b82014-08-29 14:56:45 -040015const (
16 v20 = "v2.0"
17 v30 = "v3.0"
18)
Ash Wilson8ba82242014-08-28 15:38:55 -040019
Ash Wilsona87ee062014-09-03 11:26:06 -040020// NewClient prepares an unauthenticated ProviderClient instance.
21// Most users will probably prefer using the AuthenticatedClient function instead.
22// This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly,
23// for example.
24func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
Ash Wilsona0c4c842014-09-09 11:30:58 -040025 return &gophercloud.ProviderClient{IdentityEndpoint: endpoint}, nil
Ash Wilsona87ee062014-09-03 11:26:06 -040026}
27
28// 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 -040029// returns a Client instance that's ready to operate.
Ash Wilson8ba82242014-08-28 15:38:55 -040030// It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses
31// the most recent identity service available to proceed.
Ash Wilsona87ee062014-09-03 11:26:06 -040032func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
33 client, err := NewClient(options.IdentityEndpoint)
34 if err != nil {
35 return nil, err
36 }
37
38 err = Authenticate(client, options)
Ash Wilsonccd020b2014-09-02 10:40:54 -040039 if err != nil {
40 return nil, err
41 }
42 return client, nil
43}
44
Ash Wilsonccd020b2014-09-02 10:40:54 -040045// Authenticate or re-authenticate against the most recent identity service supported at the provided endpoint.
Ash Wilsona87ee062014-09-03 11:26:06 -040046func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
Ash Wilson4dee1b82014-08-29 14:56:45 -040047 versions := []*utils.Version{
48 &utils.Version{ID: v20, Priority: 20},
49 &utils.Version{ID: v30, Priority: 30},
50 }
51
Ash Wilsona87ee062014-09-03 11:26:06 -040052 chosen, endpoint, err := utils.ChooseVersion(client.IdentityEndpoint, versions)
Ash Wilson4dee1b82014-08-29 14:56:45 -040053 if err != nil {
Ash Wilsonccd020b2014-09-02 10:40:54 -040054 return err
Ash Wilson4dee1b82014-08-29 14:56:45 -040055 }
56
Ash Wilsoned6a1d82014-09-03 12:01:00 -040057 if !strings.HasSuffix(endpoint, "/") {
58 endpoint = endpoint + "/"
59 }
60
Ash Wilson4dee1b82014-08-29 14:56:45 -040061 switch chosen.ID {
62 case v20:
Ash Wilson11c98282014-09-08 16:05:10 -040063 v2Client := NewIdentityV2(client)
64 v2Client.Endpoint = endpoint
Ash Wilson11c98282014-09-08 16:05:10 -040065
66 result, err := identity2.Authenticate(v2Client, options)
67 if err != nil {
68 return err
69 }
70
71 token, err := identity2.GetToken(result)
72 if err != nil {
73 return err
74 }
Ash Wilsonb8401a72014-09-08 17:07:49 -040075
Ash Wilson11c98282014-09-08 16:05:10 -040076 client.TokenID = token.ID
Ash Wilsonb8401a72014-09-08 17:07:49 -040077 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
Ash Wilson9d9876b2014-09-09 09:28:00 -040078 return v2endpointLocator(result, opts)
Ash Wilsonb8401a72014-09-08 17:07:49 -040079 }
Ash Wilson11c98282014-09-08 16:05:10 -040080
81 return nil
Ash Wilson4dee1b82014-08-29 14:56:45 -040082 case v30:
Ash Wilsona87ee062014-09-03 11:26:06 -040083 // Override the generated service endpoint with the one returned by the version endpoint.
84 v3Client := NewIdentityV3(client)
85 v3Client.Endpoint = endpoint
86
87 result, err := tokens3.Create(v3Client, options, nil)
88 if err != nil {
89 return err
90 }
91
92 client.TokenID, err = result.TokenID()
93 if err != nil {
94 return err
95 }
Ash Wilsonb8401a72014-09-08 17:07:49 -040096 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
97 return v3endpointLocator(v3Client, opts)
98 }
Ash Wilsona87ee062014-09-03 11:26:06 -040099
100 return nil
Ash Wilson4dee1b82014-08-29 14:56:45 -0400101 default:
Ash Wilsonccd020b2014-09-02 10:40:54 -0400102 // The switch statement must be out of date from the versions list.
Ash Wilsona87ee062014-09-03 11:26:06 -0400103 return fmt.Errorf("Unrecognized identity version: %s", chosen.ID)
Ash Wilsonccd020b2014-09-02 10:40:54 -0400104 }
105}
106
Ash Wilson9d9876b2014-09-09 09:28:00 -0400107func v2endpointLocator(authResults identity2.AuthResults, opts gophercloud.EndpointOpts) (string, error) {
108 catalog, err := identity2.GetServiceCatalog(authResults)
109 if err != nil {
110 return "", err
111 }
112
113 entries, err := catalog.CatalogEntries()
114 if err != nil {
115 return "", err
116 }
117
118 // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided.
119 var endpoints = make([]identity2.Endpoint, 0, 6)
120 for _, entry := range entries {
121 matched := true
122
123 if entry.Type != opts.Type {
124 matched = false
125 }
126
127 if opts.Name != "" && entry.Name != opts.Name {
128 matched = false
129 }
130
131 if matched {
132 for _, endpoint := range entry.Endpoints {
133 if opts.Region == "" || endpoint.Region == opts.Region {
134 endpoints = append(endpoints, endpoint)
135 }
136 }
137 }
138 }
139
140 // Report an error if the options were ambiguous.
141 if len(endpoints) == 0 {
142 return "", gophercloud.ErrEndpointNotFound
143 }
144 if len(endpoints) > 1 {
145 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
146 }
147
148 // Extract the appropriate URL from the matching Endpoint.
149 for _, endpoint := range endpoints {
150 switch opts.URLType {
151 case "public", "":
152 return endpoint.PublicURL, nil
153 case "private":
154 return endpoint.InternalURL, nil
155 default:
156 return "", fmt.Errorf("Unexpected URLType in endpoint query: %s", opts.URLType)
157 }
158 }
159
Ash Wilsonb8401a72014-09-08 17:07:49 -0400160 return "", gophercloud.ErrEndpointNotFound
161}
162
163func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) {
164 // Transform URLType into an Interface.
165 var endpointInterface = endpoints3.InterfacePublic
166 switch opts.URLType {
167 case "", "public":
168 endpointInterface = endpoints3.InterfacePublic
169 case "internal":
170 endpointInterface = endpoints3.InterfaceInternal
171 case "admin":
172 endpointInterface = endpoints3.InterfaceAdmin
173 default:
174 return "", fmt.Errorf("Unrecognized URLType: %s", opts.URLType)
175 }
176
177 // Discover the service we're interested in.
Ash Wilson1cd3e692014-09-09 11:01:47 -0400178 serviceResults, err := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
Ash Wilsonb8401a72014-09-08 17:07:49 -0400179 if err != nil {
180 return "", err
181 }
182
Ash Wilson1cd3e692014-09-09 11:01:47 -0400183 allServiceResults, err := gophercloud.AllPages(serviceResults)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400184 if err != nil {
185 return "", err
186 }
Ash Wilson1cd3e692014-09-09 11:01:47 -0400187 allServices := services3.AsServices(allServiceResults)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400188
189 if opts.Name != "" {
Ash Wilson1cd3e692014-09-09 11:01:47 -0400190 filtered := make([]services3.Service, 0, 1)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400191 for _, service := range allServices {
192 if service.Name == opts.Name {
193 filtered = append(filtered, service)
194 }
195 }
196 allServices = filtered
197 }
198
199 if len(allServices) == 0 {
Ash Wilson1cd3e692014-09-09 11:01:47 -0400200 return "", gophercloud.ErrServiceNotFound
Ash Wilsonb8401a72014-09-08 17:07:49 -0400201 }
202 if len(allServices) > 1 {
203 return "", fmt.Errorf("Discovered %d matching services: %#v", len(allServices), allServices)
204 }
205
206 service := allServices[0]
207
208 // Enumerate the endpoints available for this service.
209 endpointResults, err := endpoints3.List(v3Client, endpoints3.ListOpts{
210 Interface: endpointInterface,
211 ServiceID: service.ID,
212 })
213 if err != nil {
214 return "", err
215 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400216 allEndpoints, err := gophercloud.AllPages(endpointResults)
217 if err != nil {
218 return "", err
219 }
Ash Wilsonb8401a72014-09-08 17:07:49 -0400220 endpoints := endpoints3.AsEndpoints(allEndpoints)
221
222 if opts.Name != "" {
Ash Wilson1cd3e692014-09-09 11:01:47 -0400223 filtered := make([]endpoints3.Endpoint, 0, 1)
Ash Wilsonb8401a72014-09-08 17:07:49 -0400224 for _, endpoint := range endpoints {
Ash Wilson1cd3e692014-09-09 11:01:47 -0400225 if opts.Region == "" || endpoint.Region == opts.Region {
Ash Wilsonb8401a72014-09-08 17:07:49 -0400226 filtered = append(filtered, endpoint)
227 }
228 }
229 endpoints = filtered
230 }
231
232 if len(endpoints) == 0 {
233 return "", gophercloud.ErrEndpointNotFound
234 }
235 if len(endpoints) > 1 {
236 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
237 }
238
239 endpoint := endpoints[0]
240
241 return endpoint.URL, nil
242}
243
Ash Wilsona87ee062014-09-03 11:26:06 -0400244// NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service.
245func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilsoned6a1d82014-09-03 12:01:00 -0400246 v2Endpoint := client.IdentityEndpoint + "/v2.0/"
Ash Wilsonccd020b2014-09-02 10:40:54 -0400247
Ash Wilsona87ee062014-09-03 11:26:06 -0400248 return &gophercloud.ServiceClient{
249 Provider: client,
250 Endpoint: v2Endpoint,
Ash Wilson4dee1b82014-08-29 14:56:45 -0400251 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400252}
253
Ash Wilsona87ee062014-09-03 11:26:06 -0400254// NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service.
255func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilsoned6a1d82014-09-03 12:01:00 -0400256 v3Endpoint := client.IdentityEndpoint + "/v3/"
Ash Wilsona87ee062014-09-03 11:26:06 -0400257
258 return &gophercloud.ServiceClient{
259 Provider: client,
260 Endpoint: v3Endpoint,
261 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400262}
Ash Wilson1cd3e692014-09-09 11:01:47 -0400263
264// NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package.
265func NewStorageV1(client *gophercloud.ProviderClient, region string) (*gophercloud.ServiceClient, error) {
266 url, err := client.EndpointLocator(gophercloud.EndpointOpts{Type: "object-store", Name: "swift"})
267 if err != nil {
268 return nil, err
269 }
270 return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
271}