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
```
This commit is contained in:
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 ( import (
"errors" "errors"
"fmt" "fmt"
"net/url" "regexp"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud"
@ -180,14 +180,16 @@ func srvInstanceType(srv *servers.Server) (string, error) {
return "", fmt.Errorf("flavor name/id not found") 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) { func instanceIDFromProviderID(providerID string) (instanceID string, err error) {
parsedID, err := url.Parse(providerID) // If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too.
if err != nil { var providerIdRegexp = regexp.MustCompile(`^` + ProviderName + `:///([^/]+)$`)
return "", err
}
if parsedID.Scheme != ProviderName {
return "", fmt.Errorf("unrecognized provider %q", parsedID.Scheme)
}
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 fail bool
}{ }{
{ {
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935", providerID: ProviderName + "://" + "/" + "7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "7b9cf879-7146-417c-abfd-cb4272f0c935", instanceID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
fail: false, fail: false,
}, },
{
providerID: "openstack://7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "",
fail: true,
},
{ {
providerID: "7b9cf879-7146-417c-abfd-cb4272f0c935", providerID: "7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "", instanceID: "",
fail: true, fail: true,
}, },
{ {
providerID: "other-provider://7b9cf879-7146-417c-abfd-cb4272f0c935", providerID: "other-provider:///7b9cf879-7146-417c-abfd-cb4272f0c935",
instanceID: "", instanceID: "",
fail: true, fail: true,
}, },