Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit e481b2e

Browse files
author
Markus Suonto
committed
Reimplemented without regex
1 parent 04ac06f commit e481b2e

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

openstack/client.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package openstack
33
import (
44
"fmt"
55
"net/url"
6-
"regexp"
6+
"strconv"
77
"strings"
88

99
"github.com/rackspace/gophercloud"
@@ -17,8 +17,6 @@ const (
1717
v30 = "v3.0"
1818
)
1919

20-
var VERSION_PATTERN = regexp.MustCompile("/v[1-9]{1}[0-9\\.]{0,2}/")
21-
2220
// NewClient prepares an unauthenticated ProviderClient instance.
2321
// Most users will probably prefer using the AuthenticatedClient function instead.
2422
// 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) {
3432
// Base is url with path
3533
endpoint = gophercloud.NormalizeURL(endpoint)
3634
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+
}
4661
}
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)
5462
}
5563
}
5664

0 commit comments

Comments
 (0)