@@ -3,7 +3,7 @@ package openstack
3
3
import (
4
4
"fmt"
5
5
"net/url"
6
- "regexp "
6
+ "strconv "
7
7
"strings"
8
8
9
9
"github.com/rackspace/gophercloud"
@@ -17,8 +17,6 @@ const (
17
17
v30 = "v3.0"
18
18
)
19
19
20
- var VERSION_PATTERN = regexp .MustCompile ("/v[1-9]{1}[0-9\\ .]{0,2}/" )
21
-
22
20
// NewClient prepares an unauthenticated ProviderClient instance.
23
21
// Most users will probably prefer using the AuthenticatedClient function instead.
24
22
// This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly,
@@ -34,23 +32,33 @@ func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
34
32
// Base is url with path
35
33
endpoint = gophercloud .NormalizeURL (endpoint )
36
34
base := gophercloud .NormalizeURL (u .String ())
37
- var location = VERSION_PATTERN .FindStringIndex (base )
38
-
39
- if location != nil {
40
- var version = base [location [0 ]+ 1 :location [1 ]- 1 ]
41
- switch version {
42
- case "v2.0" , "v3" :
43
- // post version suffixes in path are not supported
44
- if len (base ) > location [1 ] {
45
- return nil , fmt .Errorf ("Path suffixes (after version) are not supported." )
35
+
36
+ path := u .Path
37
+ if ! strings .HasSuffix (path , "/" ) {
38
+ path = path + "/"
39
+ }
40
+
41
+ parts := strings .Split (path [0 :len (path )- 1 ], "/" )
42
+ for index ,version := range (parts ) {
43
+ if 2 <= len (version ) && len (version ) <= 4 && strings .HasPrefix (version , "v" ) {
44
+ _ , err := strconv .ParseFloat (version [1 :], 64 )
45
+ if err == nil {
46
+ // post version suffixes in path are not supported
47
+ // version must be on the last index
48
+ if index < len (parts ) - 1 {
49
+ return nil , fmt .Errorf ("Path suffixes (after version) are not supported." )
50
+ }
51
+ switch version {
52
+ case "v2.0" , "v3" :
53
+ // valid version found, strip from base
54
+ return & gophercloud.ProviderClient {
55
+ IdentityBase : base [0 :len (base )- len (version )- 1 ],
56
+ IdentityEndpoint : endpoint ,
57
+ }, nil
58
+ default :
59
+ return nil , fmt .Errorf ("Invalid identity endpoint version %v. Supported versions: v2.0, v3" , version )
60
+ }
46
61
}
47
- // valid version found, strip from base
48
- return & gophercloud.ProviderClient {
49
- IdentityBase : base [0 :location [0 ]+ 1 ],
50
- IdentityEndpoint : endpoint ,
51
- }, nil
52
- default :
53
- return nil , fmt .Errorf ("Invalid identity endpoint version %v. Supported versions: v2.0, v3" , version )
54
62
}
55
63
}
56
64
0 commit comments