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

Commit 4c17040

Browse files
author
Markus Suonto
committed
Fix NewClient to not remove relative paths from IdentityBase
gophercloud.provider_client.go struct ProviderClient describes IdentityBase as follows: "IdentityBase is the base URL used for a particular provider's identity service - it will be used when issuing authenticatation requests. It should point to the root resource of the identity service, not a specific identity version." Currently gophercloud.openstack.client.go func NewClient strips endpoints with a non-empty relative path. For example, the IdentityBase returned for endpoint: http://example.com/foo is: http://example.com/ when it should be: http://example.com/foo/ This change corrects NewClient to correctly handle such cases. Unit tests are provided in openstack.test_client.go [1] gophercloud.openstack.client.go Fixes-Issue: #577
1 parent adc2065 commit 4c17040

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

openstack/client.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package openstack
33
import (
44
"fmt"
55
"net/url"
6+
"regexp"
67
"strings"
78

89
"github.com/rackspace/gophercloud"
@@ -16,6 +17,8 @@ const (
1617
v30 = "v3.0"
1718
)
1819

20+
var VERSION_PATTERN = regexp.MustCompile("/v[1-9]{1}[0-9.]*")
21+
1922
// NewClient prepares an unauthenticated ProviderClient instance.
2023
// Most users will probably prefer using the AuthenticatedClient function instead.
2124
// This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly,
@@ -25,14 +28,18 @@ func NewClient(endpoint string) (*gophercloud.ProviderClient, error) {
2528
if err != nil {
2629
return nil, err
2730
}
28-
hadPath := u.Path != ""
29-
u.Path, u.RawQuery, u.Fragment = "", "", ""
30-
base := u.String()
3131

32+
u.RawQuery, u.Fragment = "", ""
33+
34+
// Base is url with path
3235
endpoint = gophercloud.NormalizeURL(endpoint)
33-
base = gophercloud.NormalizeURL(base)
36+
base := gophercloud.NormalizeURL(u.String())
37+
38+
var location = VERSION_PATTERN.FindStringIndex(base)
3439

35-
if hadPath {
40+
// If version found remove it from base
41+
if location != nil {
42+
base = base[0:location[0]+1]
3643
return &gophercloud.ProviderClient{
3744
IdentityBase: base,
3845
IdentityEndpoint: endpoint,

openstack/client_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,45 @@ func TestAuthenticatedClientV2(t *testing.T) {
159159
th.AssertNoErr(t, err)
160160
th.CheckEquals(t, "01234567890", client.TokenID)
161161
}
162+
163+
func TestNewClient(t *testing.T) {
164+
client, err := NewClient("https://example.com")
165+
th.AssertNoErr(t, err)
166+
th.AssertEquals(t, "", client.IdentityEndpoint)
167+
th.AssertEquals(t, "https://example.com/", client.IdentityBase)
168+
169+
client, err = NewClient("https://example.com/")
170+
th.AssertNoErr(t, err)
171+
th.AssertEquals(t, "", client.IdentityEndpoint)
172+
th.AssertEquals(t, "https://example.com/", client.IdentityBase)
173+
174+
client, err = NewClient("https://example.com/v2.0")
175+
th.AssertNoErr(t, err)
176+
th.AssertEquals(t, "https://example.com/v2.0/", client.IdentityEndpoint)
177+
th.AssertEquals(t, "https://example.com/", client.IdentityBase)
178+
179+
client, err = NewClient("https://example.com/v2.0/")
180+
th.AssertNoErr(t, err)
181+
th.AssertEquals(t, "https://example.com/v2.0/", client.IdentityEndpoint)
182+
th.AssertEquals(t, "https://example.com/", client.IdentityBase)
183+
184+
client, err = NewClient("https://example.com/foo/bar")
185+
th.AssertNoErr(t, err)
186+
th.AssertEquals(t, "", client.IdentityEndpoint)
187+
th.AssertEquals(t, "https://example.com/foo/bar/", client.IdentityBase)
188+
189+
client, err = NewClient("https://example.com/foo/bar/")
190+
th.AssertNoErr(t, err)
191+
th.AssertEquals(t, "", client.IdentityEndpoint)
192+
th.AssertEquals(t, "https://example.com/foo/bar/", client.IdentityBase)
193+
194+
client, err = NewClient("https://example.com/foo/bar/v2.0")
195+
th.AssertNoErr(t, err)
196+
th.AssertEquals(t, "https://example.com/foo/bar/v2.0/", client.IdentityEndpoint)
197+
th.AssertEquals(t, "https://example.com/foo/bar/", client.IdentityBase)
198+
199+
client, err = NewClient("https://example.com/foo/bar/v2.0/")
200+
th.AssertNoErr(t, err)
201+
th.AssertEquals(t, "https://example.com/foo/bar/v2.0/", client.IdentityEndpoint)
202+
th.AssertEquals(t, "https://example.com/foo/bar/", client.IdentityBase)
203+
}

0 commit comments

Comments
 (0)