Merge pull request #49770 from FengyunPan/fix-GetInstanceIDFromProviderID

Automatic merge from submit-queue (batch tested with PRs 51244, 50559, 49770, 51194, 50901)

Fix the matching rule of instance ProviderID

Url.Parse() can't parse ProviderID which contains ':///'.
This PR use regexp to match ProviderID.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #
Fix #49769

**Release note**:
```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-08-25 04:11:10 -07:00 committed by GitHub
commit d7102a0f36
2 changed files with 18 additions and 11 deletions

View File

@ -19,7 +19,7 @@ package openstack
import (
"errors"
"fmt"
"net/url"
"regexp"
"github.com/golang/glog"
"github.com/gophercloud/gophercloud"
@ -180,14 +180,16 @@ func srvInstanceType(srv *servers.Server) (string, error) {
return "", fmt.Errorf("flavor name/id not found")
}
// instanceIDFromProviderID splits a provider's id and return instanceID.
// A providerID is build out of '${ProviderName}:///${instance-id}'which contains ':///'.
// See cloudprovider.GetInstanceProviderID and Instances.InstanceID.
func instanceIDFromProviderID(providerID string) (instanceID string, err error) {
parsedID, err := url.Parse(providerID)
if err != nil {
return "", err
}
if parsedID.Scheme != ProviderName {
return "", fmt.Errorf("unrecognized provider %q", parsedID.Scheme)
}
// If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too.
var providerIdRegexp = regexp.MustCompile(`^` + ProviderName + `:///([^/]+)$`)
return parsedID.Host, nil
matches := providerIdRegexp.FindStringSubmatch(providerID)
if len(matches) != 2 {
return "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"openstack:///InstanceID\"", providerID)
}
return matches[1], nil
}

View File

@ -557,17 +557,22 @@ func TestInstanceIDFromProviderID(t *testing.T) {
fail bool
}{
{
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
providerID: ProviderName + "://" + "/" + "7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
fail: false,
},
{
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "",
fail: true,
},
{
providerID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "",
fail: true,
},
{
providerID: "other-provider://7b9cf879-7146-417c-abfd-cb4272f0c935",
providerID: "other-provider:///7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "",
fail: true,
},