blob: 6e61944a128c947b2d5facf5f89cff758a71933d [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"
jrperritt93b4a3c2016-07-20 20:29:30 -05006 "reflect"
Ash Wilson4dee1b82014-08-29 14:56:45 -04007
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
10 tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
11 "github.com/gophercloud/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:
Jon Perritta33da232016-03-02 04:43:08 -060079 return v2auth(client, endpoint, options, gophercloud.EndpointOpts{})
Ash Wilson4dee1b82014-08-29 14:56:45 -040080 case v30:
jrperritt0bc55782016-07-27 13:50:14 -050081 return v3auth(client, endpoint, &options, gophercloud.EndpointOpts{})
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.
Jon Perritta33da232016-03-02 04:43:08 -060089func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions, eo gophercloud.EndpointOpts) error {
90 return v2auth(client, "", options, eo)
Ash Wilson09694b92014-09-09 14:08:27 -040091}
92
Jon Perritta33da232016-03-02 04:43:08 -060093func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions, eo gophercloud.EndpointOpts) error {
Jon Perritt376dfce2016-02-28 23:39:09 -060094 v2Client, err := NewIdentityV2(client, eo)
95 if err != nil {
96 return err
97 }
98
Ash Wilson09694b92014-09-09 14:08:27 -040099 if endpoint != "" {
100 v2Client.Endpoint = endpoint
101 }
102
jrperritt64d0ef02016-04-13 13:10:04 -0500103 v2Opts := tokens2.AuthOptions{
104 IdentityEndpoint: options.IdentityEndpoint,
105 Username: options.Username,
106 Password: options.Password,
107 TenantID: options.TenantID,
108 TenantName: options.TenantName,
109 AllowReauth: options.AllowReauth,
110 TokenID: options.TokenID,
111 }
112
113 result := tokens2.Create(v2Client, v2Opts)
Ash Wilson52fbd182014-10-03 13:48:06 -0400114
115 token, err := result.ExtractToken()
Ash Wilson09694b92014-09-09 14:08:27 -0400116 if err != nil {
117 return err
118 }
119
Ash Wilson52fbd182014-10-03 13:48:06 -0400120 catalog, err := result.ExtractServiceCatalog()
Ash Wilson09694b92014-09-09 14:08:27 -0400121 if err != nil {
122 return err
123 }
124
Jon Perrittf4052c62015-02-14 09:48:18 -0700125 if options.AllowReauth {
Jon Perritt6fe7c402015-02-17 12:24:53 -0700126 client.ReauthFunc = func() error {
Masahiro Sano1b2bafe2015-03-06 23:26:54 +0900127 client.TokenID = ""
Jon Perritta33da232016-03-02 04:43:08 -0600128 return v2auth(client, endpoint, options, eo)
Jon Perritt6fe7c402015-02-17 12:24:53 -0700129 }
Jon Perrittf4052c62015-02-14 09:48:18 -0700130 }
Ash Wilson09694b92014-09-09 14:08:27 -0400131 client.TokenID = token.ID
Ash Wilson130a6e22014-10-07 10:48:17 -0400132 client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
133 return V2EndpointURL(catalog, opts)
134 }
Ash Wilson09694b92014-09-09 14:08:27 -0400135
136 return nil
137}
138
Ash Wilson09694b92014-09-09 14:08:27 -0400139// AuthenticateV3 explicitly authenticates against the identity v3 service.
jrperritt0bc55782016-07-27 13:50:14 -0500140func AuthenticateV3(client *gophercloud.ProviderClient, options tokens3.AuthOptionsBuilder, eo gophercloud.EndpointOpts) error {
Jon Perritta33da232016-03-02 04:43:08 -0600141 return v3auth(client, "", options, eo)
Ash Wilson09694b92014-09-09 14:08:27 -0400142}
143
jrperritt0bc55782016-07-27 13:50:14 -0500144func v3auth(client *gophercloud.ProviderClient, endpoint string, opts tokens3.AuthOptionsBuilder, eo gophercloud.EndpointOpts) error {
Ash Wilson09694b92014-09-09 14:08:27 -0400145 // Override the generated service endpoint with the one returned by the version endpoint.
Jon Perritt376dfce2016-02-28 23:39:09 -0600146 v3Client, err := NewIdentityV3(client, eo)
147 if err != nil {
148 return err
149 }
150
Ash Wilson09694b92014-09-09 14:08:27 -0400151 if endpoint != "" {
152 v3Client.Endpoint = endpoint
153 }
154
jrperritt0bc55782016-07-27 13:50:14 -0500155 result := tokens3.Create(v3Client, opts)
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
jrperritt0bc55782016-07-27 13:50:14 -0500169 if opts.CanReauth() {
Jon Perritt6fe7c402015-02-17 12:24:53 -0700170 client.ReauthFunc = func() error {
hzlouchaof6e29262015-10-27 12:51:08 +0800171 client.TokenID = ""
jrperritt0bc55782016-07-27 13:50:14 -0500172 return v3auth(client, endpoint, opts, eo)
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.
Jon Perritt376dfce2016-02-28 23:39:09 -0600183func NewIdentityV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
jrperritt93b4a3c2016-07-20 20:29:30 -0500184 endpoint := client.IdentityBase + "v2.0/"
185 var err error
186 if !reflect.DeepEqual(eo, gophercloud.EndpointOpts{}) {
Jon Perritta33da232016-03-02 04:43:08 -0600187 eo.ApplyDefaults("identity")
jrperritt93b4a3c2016-07-20 20:29:30 -0500188 endpoint, err = client.EndpointLocator(eo)
Jon Perritta33da232016-03-02 04:43:08 -0600189 if err != nil {
190 return nil, err
191 }
jrperritt93b4a3c2016-07-20 20:29:30 -0500192 }
Ash Wilsonccd020b2014-09-02 10:40:54 -0400193
Ash Wilsona87ee062014-09-03 11:26:06 -0400194 return &gophercloud.ServiceClient{
Ash Wilson92c380c2014-10-22 09:14:53 -0400195 ProviderClient: client,
jrperritt93b4a3c2016-07-20 20:29:30 -0500196 Endpoint: endpoint,
Jon Perritt376dfce2016-02-28 23:39:09 -0600197 }, nil
Ash Wilson8ba82242014-08-28 15:38:55 -0400198}
199
Ash Wilsona87ee062014-09-03 11:26:06 -0400200// NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service.
Jon Perritt376dfce2016-02-28 23:39:09 -0600201func NewIdentityV3(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
jrperritt93b4a3c2016-07-20 20:29:30 -0500202 endpoint := client.IdentityBase + "v3/"
203 var err error
204 if !reflect.DeepEqual(eo, gophercloud.EndpointOpts{}) {
Jon Perritta33da232016-03-02 04:43:08 -0600205 eo.ApplyDefaults("identity")
jrperritt93b4a3c2016-07-20 20:29:30 -0500206 endpoint, err = client.EndpointLocator(eo)
Jon Perritta33da232016-03-02 04:43:08 -0600207 if err != nil {
208 return nil, err
209 }
jrperritt93b4a3c2016-07-20 20:29:30 -0500210 }
Ash Wilsona87ee062014-09-03 11:26:06 -0400211
212 return &gophercloud.ServiceClient{
Ash Wilson92c380c2014-10-22 09:14:53 -0400213 ProviderClient: client,
jrperritt93b4a3c2016-07-20 20:29:30 -0500214 Endpoint: endpoint,
Jon Perritt376dfce2016-02-28 23:39:09 -0600215 }, nil
feisky66803f02015-08-28 22:06:34 +0800216}
217
Jon Perrittbb5e9812014-10-15 17:53:44 -0500218// NewObjectStorageV1 creates a ServiceClient that may be used with the v1 object storage package.
219func NewObjectStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
Jon Perritt509fbb62014-09-10 13:29:56 -0500220 eo.ApplyDefaults("object-store")
221 url, err := client.EndpointLocator(eo)
Ash Wilson1cd3e692014-09-09 11:01:47 -0400222 if err != nil {
223 return nil, err
224 }
Ash Wilson92c380c2014-10-22 09:14:53 -0400225 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
Ash Wilson1cd3e692014-09-09 11:01:47 -0400226}
Ash Wilson5e57c1b2014-09-17 09:24:46 -0400227
228// NewComputeV2 creates a ServiceClient that may be used with the v2 compute package.
229func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
230 eo.ApplyDefaults("compute")
231 url, err := client.EndpointLocator(eo)
232 if err != nil {
233 return nil, err
234 }
Ash Wilson92c380c2014-10-22 09:14:53 -0400235 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
Ash Wilson5e57c1b2014-09-17 09:24:46 -0400236}
Ash Wilsonebc3d122014-09-24 13:44:05 -0400237
238// NewNetworkV2 creates a ServiceClient that may be used with the v2 network package.
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200239func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
240 eo.ApplyDefaults("network")
241 url, err := client.EndpointLocator(eo)
242 if err != nil {
243 return nil, err
244 }
Ash Wilson99541ab2014-10-06 17:32:39 -0400245 return &gophercloud.ServiceClient{
Ash Wilson92c380c2014-10-22 09:14:53 -0400246 ProviderClient: client,
247 Endpoint: url,
248 ResourceBase: url + "v2.0/",
Ash Wilson99541ab2014-10-06 17:32:39 -0400249 }, nil
Jamie Hannaford7ea29582014-09-11 15:49:46 +0200250}
Jon Perrittc5ee85e2014-09-17 00:53:19 -0500251
252// NewBlockStorageV1 creates a ServiceClient that may be used to access the v1 block storage service.
253func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
254 eo.ApplyDefaults("volume")
255 url, err := client.EndpointLocator(eo)
256 if err != nil {
257 return nil, err
258 }
Ash Wilson92c380c2014-10-22 09:14:53 -0400259 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
Jon Perrittc5ee85e2014-09-17 00:53:19 -0500260}
Jon Perrittebd18ec2015-01-16 09:13:31 -0700261
feiskyda546142015-09-17 12:28:23 +0800262// NewBlockStorageV2 creates a ServiceClient that may be used to access the v2 block storage service.
263func NewBlockStorageV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
Nick Craig-Woodb64fd202016-05-13 15:56:18 +0100264 eo.ApplyDefaults("volumev2")
feiskyda546142015-09-17 12:28:23 +0800265 url, err := client.EndpointLocator(eo)
266 if err != nil {
267 return nil, err
268 }
Nick Craig-Woodb64fd202016-05-13 15:56:18 +0100269 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
feiskyda546142015-09-17 12:28:23 +0800270}
271
ehdou10f1f852016-10-14 20:58:23 +0300272// NewSharedFileSystemV2 creates a ServiceClient that may be used to access the v2 shared file system service.
273func NewSharedFileSystemV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
274 eo.ApplyDefaults("sharev2")
275 url, err := client.EndpointLocator(eo)
276 if err != nil {
277 return nil, err
278 }
279 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
280}
281
Jon Perrittebd18ec2015-01-16 09:13:31 -0700282// NewCDNV1 creates a ServiceClient that may be used to access the OpenStack v1
283// CDN service.
284func NewCDNV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
285 eo.ApplyDefaults("cdn")
286 url, err := client.EndpointLocator(eo)
287 if err != nil {
288 return nil, err
289 }
290 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
291}
Jon Perritt35e27e42014-12-05 11:10:46 -0700292
293// NewOrchestrationV1 creates a ServiceClient that may be used to access the v1 orchestration service.
294func NewOrchestrationV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
295 eo.ApplyDefaults("orchestration")
296 url, err := client.EndpointLocator(eo)
297 if err != nil {
298 return nil, err
299 }
300 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
301}
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100302
Jamie Hannaford75e8cc42015-11-16 14:09:25 +0100303// NewDBV1 creates a ServiceClient that may be used to access the v1 DB service.
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100304func NewDBV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
305 eo.ApplyDefaults("database")
306 url, err := client.EndpointLocator(eo)
307 if err != nil {
308 return nil, err
309 }
310 return &gophercloud.ServiceClient{ProviderClient: client, Endpoint: url}, nil
311}
jrperrittc5c590a2016-11-04 14:41:15 -0500312
313// NewImageServiceV2 creates a ServiceClient that may be used to access the v2 image service.
314func NewImageServiceV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
315 eo.ApplyDefaults("image")
316 url, err := client.EndpointLocator(eo)
317 if err != nil {
318 return nil, err
319 }
320 return &gophercloud.ServiceClient{ProviderClient: client,
321 Endpoint: url,
322 ResourceBase: url + "v2/"}, nil
323}