From 45959c88eed0ced402658b83efa8fbb84dc3d2e3 Mon Sep 17 00:00:00 2001 From: TonyXu Date: Sun, 27 May 2018 13:27:23 +0800 Subject: [PATCH] Add openstack.ContainsProjectId method to detect the endpoint url whether or not contains projectID --- openstack/endpoint_util.go | 11 +++++++++++ testing/endpoint_util_test.go | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 openstack/endpoint_util.go create mode 100644 testing/endpoint_util_test.go diff --git a/openstack/endpoint_util.go b/openstack/endpoint_util.go new file mode 100644 index 000000000..e7d95082f --- /dev/null +++ b/openstack/endpoint_util.go @@ -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)) +} diff --git a/testing/endpoint_util_test.go b/testing/endpoint_util_test.go new file mode 100644 index 000000000..d6ffa2051 --- /dev/null +++ b/testing/endpoint_util_test.go @@ -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)) + } +}