Fix the matching rule of instance ProviderID

Url.Parse() can't parse ProviderID which contains ':///'.
This PR use regexp to match ProviderID.
This commit is contained in:
FengyunPan 2017-07-28 17:38:50 +08:00
parent 270206e9c3
commit c6489da70d
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"
@ -208,14 +208,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,
},