Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions openstack/endpoint_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package openstack

import "regexp"

// A regular expression used to verify whether or not contains a project id in an endpoint url
var endpointProjectIdMatcher = regexp.MustCompile(`http[s]?://.+/(?:[V|v]\d+|[V|v]\d+\.\d+)/([a-z|A-Z|0-9]{32})`)

// ContainsProjectId detects whether or not the encpoint url contains a projectid
func ContainsProjectId(endpointUrl string) bool {
return endpointProjectIdMatcher.Match([]byte(endpointUrl))
}
37 changes: 37 additions & 0 deletions testing/endpoint_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package testing

import (
"testing"

"github.com/huaweicloud/golangsdk/openstack"
th "github.com/huaweicloud/golangsdk/testhelper"
)

func TestContainsProjectId(t *testing.T) {
endpointContains := []string{"https://as.eu-de.otc.t-systems.com/autoscaling-api/v1/f9842224f84e44f99c2878eddc7f9ef5",
"https://elb.t-systems.com/rds/v1.0/c9842224f84e44f99c2878eddc7f9ef5/",
"https://elb.eu-de.otc.t-systems.com/v1.1/c9842224f84e44f99c2878eddc7f9ef5",
"https://elb.eu-de.otc.t-systems.com/v2/c9842224f84e44f99c2878eddc7f9ef5",
"https://elb.eu-de.otc.t-systems.com/v2.0/c9842224f84e44f99c2878eddc7f9ef5",
"https://elb.eu-de.otc.t-systems.com/V2.0/c9842224f84e44f99c2878eddc7f9ef5/list",
"https://as.eu-de.otc.t-systems.com/autoscaling-api/v1/c9842224f84e44f99c2878eddc7f9ef5/abc",
"https://as.eu-de.otc.t-systems.com/autoscaling-api/V11/c9842224f84e44f99c2878eddc7f9ef5",
"https://as.eu-de.otc.t-systems.com/autoscaling-api/v2/c9842224f84e44f99c2878eddc7f9ef5",
"https://as.eu-de.otc.t-systems.com/autoscaling-api/V2/c9842224f84e44f99c2878eddc7f9ef5",
"http://as.eu-de.otc.t-systems.com/autoscaling-api/V2/c9842224f84e44f99c2878eddc7f9ef5"}

for _, enpoint := range endpointContains {
th.AssertEquals(t, true, openstack.ContainsProjectId(enpoint))
}
}

func TestNotContainsProjectId(t *testing.T) {
endpointContains := []string{"https://as.eu-de.otc.t-systems.com/autoscaling-api/v1",
"https://as.eu-de.otc.t-systems.com/autoscaling-api/v1/",
"https://as.eu-de.otc.t-systems.com/autoscaling-api/v1/abc",
"https://as.eu-de.otc.t-systems.com/autoscaling-api/V1"}

for _, enpoint := range endpointContains {
th.AssertEquals(t, false, openstack.ContainsProjectId(enpoint))
}
}