blob: baa4cb51c2f655485d83c2a5a74fce6fc3ea4818 [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"
feisky66803f02015-08-28 22:06:34 +08006 "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 Wilsona87ee062014-09-03 11:26:06 -040010 tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
Ash Wilson4dee1b82014-08-29 14:56:45 -040011 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilson8ba82242014-08-28 15:38:55 -040012)
13
Ash Wilson4dee1b82014-08-29 14:56:45 -040014const (
15 v20 = "v2.0"
16 v30 = "v3.0"
17)
Ash Wilson8ba82242014-08-28 15:38:55 -040018
Ash Wilsona87ee062014-09-03 11:26:06 -040019// NewClient prepares an unauthenticated ProviderClient instance.
20// Most users will probably prefer using the AuthenticatedClient function instead.
21// This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly,
22// for example.
23func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
Ash Wilson09694b92014-09-09 14:08:27 -040024 u, err := url.Parse(endpoint)
25 if err != nil {
26 return nil, err
27 }
28 hadPath := u.Path != ""
29 u.Path, u.RawQuery, u.Fragment = "", "", ""
30 base := u.String()
31
Ash Wilsona8440642014-10-07 09:55:58 -040032 endpoint = gophercloud.NormalizeURL(endpoint)
33 base = gophercloud.NormalizeURL(base)
Ash Wilsone7da01c2014-09-09 12:31:06 -040034
Ash Wilson09694b92014-09-09 14:08:27 -040035 if hadPath {
36 return &gophercloud.ProviderClient{
37 IdentityBase: base,
38 IdentityEndpoint: endpoint,
39 }, nil
40 }
41
42 return &gophercloud.ProviderClient{
43 IdentityBase: base,
44 IdentityEndpoint: "",
45 }, nil
Ash Wilsona87ee062014-09-03 11:26:06 -040046}
47
48// 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 -040049// returns a Client instance that's ready to operate.
Ash Wilson8ba82242014-08-28 15:38:55 -040050// It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses
51// the most recent identity service available to proceed.
Ash Wilsona87ee062014-09-03 11:26:06 -040052func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) {
53 client, err := NewClient(options.IdentityEndpoint)
54 if err != nil {
55 return nil, err
56 }
57
58 err = Authenticate(client, options)
Ash Wilsonccd020b2014-09-02 10:40:54 -040059 if err != nil {
60 return nil, err
61 }
62 return client, nil
63}
64
Ash Wilsonccd020b2014-09-02 10:40:54 -040065// Authenticate or re-authenticate against the most recent identity service supported at the provided endpoint.
Ash Wilsona87ee062014-09-03 11:26:06 -040066func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
Ash Wilson4dee1b82014-08-29 14:56:45 -040067 versions := []*utils.Version{
feisky66803f02015-08-28 22:06:34 +080068 {ID: v20, Priority: 20, Suffix: "/v2.0/"},
69 {ID: v30, Priority: 30, Suffix: "/v3/"},
Ash Wilson4dee1b82014-08-29 14:56:45 -040070 }
71
Ash Wilson2491b4c2015-02-12 16:13:39 -050072 chosen, endpoint, err := utils.ChooseVersion(client, versions)
Ash Wilson4dee1b82014-08-29 14:56:45 -040073 if err != nil {
Ash Wilsonccd020b2014-09-02 10:40:54 -040074 return err
Ash Wilson4dee1b82014-08-29 14:56:45 -040075 }
76
77 switch chosen.ID {
78 case v20:
Ash Wilson09694b92014-09-09 14:08:27 -040079 return v2auth(client, endpoint, options)
Ash Wilson4dee1b82014-08-29 14:56:45 -040080 case v30:
Ash Wilson09694b92014-09-09 14:08:27 -040081 return v3auth(client, endpoint, options)
Ash Wilson4dee1b82014-08-29 14:56:45 -040082 default:
Ash Wilsonccd020b2014-09-02 10:40:54 -040083 // The switch statement must be out of date from the versions list.
Ash Wilsona87ee062014-09-03 11:26:06 -040084 return fmt.Errorf("Unrecognized identity version: %s", chosen.ID)
Ash Wilsonccd020b2014-09-02 10:40:54 -040085 }
86}
87
Ash Wilson09694b92014-09-09 14:08:27 -040088// AuthenticateV2 explicitly authenticates against the identity v2 endpoint.
89func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
90 return v2auth(client, "", options)
91}
92
93func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
94 v2Client := NewIdentityV2(client)
95 if endpoint != "" {
96 v2Client.Endpoint = endpoint
97 }
98
Ash Wilson40095f02014-10-07 15:46:40 -040099 result := tokens2.Create(v2Client, tokens2.AuthOptions{AuthOptions: options})
Ash Wilson52fbd182014-10-03 13:48:06 -0400100
101 token, err := result.ExtractToken()
Ash Wilson09694b92014-09-09 14:08:27 -0400102 if err != nil {
103 return err
104 }
105
Ash Wilson52fbd182014-10-03 13:48:06 -0400106 catalog, err := result.ExtractServiceCatalog()
Ash Wilson09694b92014-09-09 14:08:27 -0400107 if err != nil {
108 return err
109 }
110
Jon Perrittf4052c62015-02-14 09:48:18 -0700111 if options.AllowReauth {
Jon Perritt6fe7c402015-02-17 12:24:53 -0700112 client.ReauthFunc = func() error {
Masahiro Sano1b2bafe2015-03-06 23:26:54 +0900113 client.TokenID = ""
alexcern5ef9a232016-02-10 11:41:00 +0100114 return v2auth(client, endpoint, options)
Jon Perritt6fe7c402015-02-17 12:24:53 -0700115 }
Jon Perrittf4052c62015-02-14 09:48:18 -0700116 }
Ash Wilson09694b92014-09-09 14:08:27 -0400117 client.TokenID = token.ID
Ash Wilson130a6e22014-10-07 10:48:17 -0400118 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
119 return V2EndpointURL(catalog, opts)
120 }
Ash Wilson09694b92014-09-09 14:08:27 -0400121
122 return nil
123}
124
Ash Wilson09694b92014-09-09 14:08:27 -0400125// AuthenticateV3 explicitly authenticates against the identity v3 service.
126func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error {
127 return v3auth(client, "", options)
128}
129
130func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error {
131 // Override the generated service endpoint with the one returned by the version endpoint.
132 v3Client := NewIdentityV3(client)
133 if endpoint != "" {
134 v3Client.Endpoint = endpoint
135 }
136
Guillaume Giamarchi08b33d52015-04-01 01:34:17 +0200137 var scope *tokens3.Scope
138 if options.TenantID != "" {
139 scope = &tokens3.Scope{
140 ProjectID: options.TenantID,
141 }
142 options.TenantID = ""
143 options.TenantName = ""
144 } else {
145 if options.TenantName != "" {
146 scope = &tokens3.Scope{
147 ProjectName: options.TenantName,
148 DomainID: options.DomainID,
149 DomainName: options.DomainName,
150 }
151 options.TenantName = ""
152 }
153 }
154
155 result := tokens3.Create(v3Client, options, scope)
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200156
157 token, err := result.ExtractToken()
Ash Wilson09694b92014-09-09 14:08:27 -0400158 if err != nil {
159 return err
160 }
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200161
162 catalog, err := result.ExtractServiceCatalog()
163 if err != nil {
164 return err
165 }
166
Ash Wilson63b2a292014-10-02 09:29:06 -0400167 client.TokenID = token.ID
Ash Wilson09694b92014-09-09 14:08:27 -0400168
Jon Perrittf4052c62015-02-14 09:48:18 -0700169 if options.AllowReauth {
Jon Perritt6fe7c402015-02-17 12:24:53 -0700170 client.ReauthFunc = func() error {
hzlouchaof6e29262015-10-27 12:51:08 +0800171 client.TokenID = ""
alexcern5ef9a232016-02-10 11:41:00 +0100172 return v3auth(client, endpoint, options)
Jon Perritt6fe7c402015-02-17 12:24:53 -0700173 }
Jon Perrittf4052c62015-02-14 09:48:18 -0700174 }
Ash Wilson09694b92014-09-09 14:08:27 -0400175 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
Guillaume Giamarchib2663b22015-04-01 01:23:29 +0200176 return V3EndpointURL(catalog, opts)
Ash Wilson09694b92014-09-09 14:08:27 -0400177 }
178
179 return nil
180}
181
Ash Wilsona87ee062014-09-03 11:26:06 -0400182// NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service.
183func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400184 v2Endpoint := client.IdentityBase + "v2.0/"
Ash Wilsonccd020b2014-09-02 10:40:54 -0400185
Ash Wilsona87ee062014-09-03 11:26:06 -0400186 return &gophercloud.ServiceClient{
Ash Wilson92c380c2014-10-22 09:14:53 -0400187 ProviderClient: client,
188 Endpoint: v2Endpoint,
Ash Wilson4dee1b82014-08-29 14:56:45 -0400189 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400190}
191
Ash Wilsona87ee062014-09-03 11:26:06 -0400192// NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service.
193func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient {
Ash Wilson09694b92014-09-09 14:08:27 -0400194 v3Endpoint := client.IdentityBase + "v3/"
Ash Wilsona87ee062014-09-03 11:26:06 -0400195
196 return &gophercloud.ServiceClient{
Ash Wilson92c380c2014-10-22 09:14:53 -0400197 ProviderClient: client,
198 Endpoint: v3Endpoint,
Ash Wilsona87ee062014-09-03 11:26:06 -0400199 }
Ash Wilson8ba82242014-08-28 15:38:55 -0400200}
Ash Wilson1cd3e692014-09-09 11:01:47 -0400201
feisky66803f02015-08-28 22:06:34 +0800202func NewIdentityAdminV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
203 eo.ApplyDefaults("identity")
204 eo.Availability = gophercloud.AvailabilityAdmin
205
206 url, err := client.EndpointLocator(eo)
207 if err != nil {
208 return nil, err
209 }
210
211 // Force using v2 API
212 if strings.Contains(url, "/v3") {
213 url = strings.Replace(url, "/v3", "/v2.0", -1)
214 }
215
216 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
217}
218
219func NewIdentityAdminV3(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
220 eo.ApplyDefaults("identity")
221 eo.Availability = gophercloud.AvailabilityAdmin
222
223 url, err := client.EndpointLocator(eo)
224 if err != nil {
225 return nil, err
226 }
227
228 // Force using v3 API
229 if strings.Contains(url, "/v2.0") {
230 url = strings.Replace(url, "/v2.0", "/v3", -1)
231 }
232
233 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
234}
235
Jon Perrittbb5e9812014-10-15 17:53:44 -0500236// NewObjectStorageV1 creates a ServiceClient that may be used with the v1 object storage package.
237func NewObjectStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
Jon Perritt509fbb62014-09-10 13:29:56 -0500238 eo.ApplyDefaults("object-store")
239 url, err := client.EndpointLocator(eo)
Ash Wilson1cd3e692014-09-09 11:01:47 -0400240 if err != nil {
241 return nil, err
242 }
Ash Wilson92c380c2014-10-22 09:14:53 -0400243 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
Ash Wilson1cd3e692014-09-09 11:01:47 -0400244}
Ash Wilson5e57c1b2014-09-17 09:24:46 -0400245
246// NewComputeV2 creates a ServiceClient that may be used with the v2 compute package.
247func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
248 eo.ApplyDefaults("compute")
249 url, err := client.EndpointLocator(eo)
250 if err != nil {
251 return nil, err
252 }
Ash Wilson92c380c2014-10-22 09:14:53 -0400253 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
Ash Wilson5e57c1b2014-09-17 09:24:46 -0400254}
Ash Wilsonebc3d122014-09-24 13:44:05 -0400255
256// NewNetworkV2 creates a ServiceClient that may be used with the v2 network package.
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200257func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
258 eo.ApplyDefaults("network")
259 url, err := client.EndpointLocator(eo)
260 if err != nil {
261 return nil, err
262 }
Ash Wilson99541ab2014-10-06 17:32:39 -0400263 return &gophercloud.ServiceClient{
Ash Wilson92c380c2014-10-22 09:14:53 -0400264 ProviderClient: client,
265 Endpoint: url,
266 ResourceBase: url + "v2.0/",
Ash Wilson99541ab2014-10-06 17:32:39 -0400267 }, nil
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200268}
Jon Perrittc5ee85e2014-09-17 00:53:19 -0500269
270// NewBlockStorageV1 creates a ServiceClient that may be used to access the v1 block storage service.
271func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
272 eo.ApplyDefaults("volume")
273 url, err := client.EndpointLocator(eo)
274 if err != nil {
275 return nil, err
276 }
Ash Wilson92c380c2014-10-22 09:14:53 -0400277 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
Jon Perrittc5ee85e2014-09-17 00:53:19 -0500278}
Jon Perrittebd18ec2015-01-16 09:13:31 -0700279
280// NewCDNV1 creates a ServiceClient that may be used to access the OpenStack v1
281// CDN service.
282func NewCDNV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
283 eo.ApplyDefaults("cdn")
284 url, err := client.EndpointLocator(eo)
285 if err != nil {
286 return nil, err
287 }
288 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
289}
Jon Perritt35e27e42014-12-05 11:10:46 -0700290
291// NewOrchestrationV1 creates a ServiceClient that may be used to access the v1 orchestration service.
292func NewOrchestrationV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
293 eo.ApplyDefaults("orchestration")
294 url, err := client.EndpointLocator(eo)
295 if err != nil {
296 return nil, err
297 }
298 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
299}
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100300
Jamie Hannaford75e8cc42015-11-16 14:09:25 +0100301// NewDBV1 creates a ServiceClient that may be used to access the v1 DB service.
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100302func NewDBV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
303 eo.ApplyDefaults("database")
304 url, err := client.EndpointLocator(eo)
305 if err != nil {
306 return nil, err
307 }
308 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
309}