github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/utils/base_endpoint.go (about)

     1  package utils
     2  
     3  import (
     4  	"net/url"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  // BaseEndpoint will return a URL without the /vX.Y
    10  // portion of the URL.
    11  func BaseEndpoint(endpoint string) (string, error) {
    12  	u, err := url.Parse(endpoint)
    13  	if err != nil {
    14  		return "", err
    15  	}
    16  
    17  	u.RawQuery, u.Fragment = "", ""
    18  
    19  	path := u.Path
    20  	versionRe := regexp.MustCompile("v[0-9.]+/?")
    21  
    22  	if version := versionRe.FindString(path); version != "" {
    23  		versionIndex := strings.Index(path, version)
    24  		u.Path = path[:versionIndex]
    25  	}
    26  
    27  	return u.String(), nil
    28  }