Skip to content

Commit 4329495

Browse files
committed
trivial: Simplify endpoint filtering
We do not need to check the validity of the provided opts more than once so don't. We can also simplify our handling of multiple endpoints (though not as much as we'd like in the v2 case, due to forthcoming patches). Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 0d4b2d4 commit 4329495

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

openstack/endpoint_location.go

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,33 @@ available on your OpenStack deployment.
2020
*/
2121
func V2EndpointURL(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
2222
// Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided.
23-
var endpoints = make([]tokens2.Endpoint, 0, 1)
23+
//
24+
// If multiple endpoints are found, we return the first result and disregard the rest.
25+
// This behavior matches the Python library. See GH-1764.
2426
for _, entry := range catalog.Entries {
2527
if (slices.Contains(opts.Types(), entry.Type)) && (opts.Name == "" || entry.Name == opts.Name) {
2628
for _, endpoint := range entry.Endpoints {
27-
if opts.Region == "" || endpoint.Region == opts.Region {
28-
endpoints = append(endpoints, endpoint)
29+
if opts.Region != "" && endpoint.Region != opts.Region {
30+
continue
2931
}
30-
}
31-
}
32-
}
3332

34-
// If multiple endpoints were found, use the first result
35-
// and disregard the other endpoints.
36-
//
37-
// This behavior matches the Python library. See GH-1764.
38-
if len(endpoints) > 1 {
39-
endpoints = endpoints[0:1]
40-
}
33+
var endpointURL string
34+
switch opts.Availability {
35+
case gophercloud.AvailabilityPublic:
36+
endpointURL = gophercloud.NormalizeURL(endpoint.PublicURL)
37+
case gophercloud.AvailabilityInternal:
38+
endpointURL = gophercloud.NormalizeURL(endpoint.InternalURL)
39+
case gophercloud.AvailabilityAdmin:
40+
endpointURL = gophercloud.NormalizeURL(endpoint.AdminURL)
41+
default:
42+
err := &ErrInvalidAvailabilityProvided{}
43+
err.Argument = "Availability"
44+
err.Value = opts.Availability
45+
return "", err
46+
}
4147

42-
// Extract the appropriate URL from the matching Endpoint.
43-
for _, endpoint := range endpoints {
44-
switch opts.Availability {
45-
case gophercloud.AvailabilityPublic:
46-
return gophercloud.NormalizeURL(endpoint.PublicURL), nil
47-
case gophercloud.AvailabilityInternal:
48-
return gophercloud.NormalizeURL(endpoint.InternalURL), nil
49-
case gophercloud.AvailabilityAdmin:
50-
return gophercloud.NormalizeURL(endpoint.AdminURL), nil
51-
default:
52-
err := &ErrInvalidAvailabilityProvided{}
53-
err.Argument = "Availability"
54-
err.Value = opts.Availability
55-
return "", err
48+
return endpointURL, nil
49+
}
5650
}
5751
}
5852

@@ -72,41 +66,35 @@ will also often need to specify a Name and/or a Region depending on what's
7266
available on your OpenStack deployment.
7367
*/
7468
func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
69+
if opts.Availability != gophercloud.AvailabilityAdmin &&
70+
opts.Availability != gophercloud.AvailabilityPublic &&
71+
opts.Availability != gophercloud.AvailabilityInternal {
72+
err := &ErrInvalidAvailabilityProvided{}
73+
err.Argument = "Availability"
74+
err.Value = opts.Availability
75+
return "", err
76+
}
77+
7578
// Extract Endpoints from the catalog entries that match the requested Type, Interface,
7679
// Name if provided, and Region if provided.
77-
var endpoints = make([]tokens3.Endpoint, 0, 1)
80+
//
81+
// If multiple endpoints are found, we return the first result and disregard the rest.
82+
// This behavior matches the Python library. See GH-1764.
7883
for _, entry := range catalog.Entries {
7984
if (slices.Contains(opts.Types(), entry.Type)) && (opts.Name == "" || entry.Name == opts.Name) {
8085
for _, endpoint := range entry.Endpoints {
81-
if opts.Availability != gophercloud.AvailabilityAdmin &&
82-
opts.Availability != gophercloud.AvailabilityPublic &&
83-
opts.Availability != gophercloud.AvailabilityInternal {
84-
err := &ErrInvalidAvailabilityProvided{}
85-
err.Argument = "Availability"
86-
err.Value = opts.Availability
87-
return "", err
86+
if opts.Availability != gophercloud.Availability(endpoint.Interface) {
87+
continue
8888
}
89-
if (opts.Availability == gophercloud.Availability(endpoint.Interface)) &&
90-
(opts.Region == "" || endpoint.Region == opts.Region || endpoint.RegionID == opts.Region) {
91-
endpoints = append(endpoints, endpoint)
89+
if opts.Region != "" && endpoint.Region != opts.Region && endpoint.RegionID != opts.Region {
90+
continue
9291
}
92+
93+
return gophercloud.NormalizeURL(endpoint.URL), nil
9394
}
9495
}
9596
}
9697

97-
// If multiple endpoints were found, use the first result
98-
// and disregard the other endpoints.
99-
//
100-
// This behavior matches the Python library. See GH-1764.
101-
if len(endpoints) > 1 {
102-
endpoints = endpoints[0:1]
103-
}
104-
105-
// Extract the URL from the matching Endpoint.
106-
for _, endpoint := range endpoints {
107-
return gophercloud.NormalizeURL(endpoint.URL), nil
108-
}
109-
11098
// Report an error if there were no matching endpoints.
11199
err := &gophercloud.ErrEndpointNotFound{}
112100
return "", err

0 commit comments

Comments
 (0)