Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit 4a4d7a4

Browse files
author
FengyunPan
committed
Fix the matching rule of instance ProviderID
Url.Parse() can't parse ProviderID which contains ':///'. This PR use regexp to match ProviderID.
1 parent 2b8855a commit 4a4d7a4

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

pkg/cloudprovider/providers/openstack/openstack_instances.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package openstack
1919
import (
2020
"errors"
2121
"fmt"
22-
"net/url"
22+
"regexp"
2323

2424
"github.com/golang/glog"
2525
"github.com/gophercloud/gophercloud"
@@ -208,14 +208,16 @@ func srvInstanceType(srv *servers.Server) (string, error) {
208208
return "", fmt.Errorf("flavor name/id not found")
209209
}
210210

211+
// instanceIDFromProviderID splits a provider's id and return instanceID.
212+
// A providerID is build out of '${ProviderName}:///${instance-id}'which contains ':///'.
213+
// See cloudprovider.GetInstanceProviderID and Instances.InstanceID.
211214
func instanceIDFromProviderID(providerID string) (instanceID string, err error) {
212-
parsedID, err := url.Parse(providerID)
213-
if err != nil {
214-
return "", err
215-
}
216-
if parsedID.Scheme != ProviderName {
217-
return "", fmt.Errorf("unrecognized provider %q", parsedID.Scheme)
218-
}
215+
// If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too.
216+
var providerIdRegexp = regexp.MustCompile(`^` + ProviderName + `:///([^/]+)$`)
219217

220-
return parsedID.Host, nil
218+
matches := providerIdRegexp.FindStringSubmatch(providerID)
219+
if len(matches) != 2 {
220+
return "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"openstack:///InstanceID\"", providerID)
221+
}
222+
return matches[1], nil
221223
}

pkg/cloudprovider/providers/openstack/openstack_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,22 @@ func TestInstanceIDFromProviderID(t *testing.T) {
557557
fail bool
558558
}{
559559
{
560-
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
560+
providerID: ProviderName + "://" + "/" + "7b9cf879-7146-417c-abfd-cb4272f0c935",
561561
instanceID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
562562
fail: false,
563563
},
564+
{
565+
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
566+
instanceID: "",
567+
fail: true,
568+
},
564569
{
565570
providerID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
566571
instanceID: "",
567572
fail: true,
568573
},
569574
{
570-
providerID: "other-provider://7b9cf879-7146-417c-abfd-cb4272f0c935",
575+
providerID: "other-provider:///7b9cf879-7146-417c-abfd-cb4272f0c935",
571576
instanceID: "",
572577
fail: true,
573578
},

0 commit comments

Comments
 (0)