github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/endpoint_location.go (about)

     1  package openstack
     2  
     3  import (
     4  	golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
     5  	tokens3 "github.com/opentelekomcloud/gophertelekomcloud/openstack/identity/v3/tokens"
     6  )
     7  
     8  /*
     9  V3EndpointURL discovers the endpoint URL for a specific service from a Catalog
    10  acquired during the v3 identity service.
    11  
    12  The specified EndpointOpts are used to identify a unique, unambiguous endpoint
    13  to return. It's an error both when multiple endpoints match the provided
    14  criteria and when none do. The minimum that can be specified is a Type, but you
    15  will also often need to specify a Name and/or a Region depending on what's
    16  available on your OpenStack deployment.
    17  */
    18  func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts golangsdk.EndpointOpts) (string, error) {
    19  	// Extract Endpoints from the catalog entries that match the requested Type, Interface,
    20  	// Name if provided, and Region if provided.
    21  	var endpoints = make([]tokens3.Endpoint, 0, 1)
    22  	for _, entry := range catalog.Entries {
    23  		if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
    24  			for _, endpoint := range entry.Endpoints {
    25  				if opts.Availability != golangsdk.AvailabilityAdmin &&
    26  					opts.Availability != golangsdk.AvailabilityPublic &&
    27  					opts.Availability != golangsdk.AvailabilityInternal {
    28  					err := &ErrInvalidAvailabilityProvided{}
    29  					err.Argument = "Availability"
    30  					err.Value = opts.Availability
    31  					return "", err
    32  				}
    33  				if opts.Availability == golangsdk.Availability(endpoint.Interface) &&
    34  					(opts.Region == "" || endpoint.Region == opts.Region) {
    35  					endpoints = append(endpoints, endpoint)
    36  				}
    37  			}
    38  		}
    39  	}
    40  
    41  	// If multiple endpoints were found, use the first result
    42  	// and disregard the other endpoints.
    43  	//
    44  	// This behavior matches the Python library. See GH-1764.
    45  	if len(endpoints) > 1 {
    46  		endpoints = endpoints[0:1]
    47  	}
    48  
    49  	// Extract the URL from the matching Endpoint.
    50  	for _, endpoint := range endpoints {
    51  		return golangsdk.NormalizeURL(endpoint.URL), nil
    52  	}
    53  
    54  	// Report an error if there were no matching endpoints.
    55  	err := &golangsdk.ErrEndpointNotFound{}
    56  	return "", err
    57  }