github.com/gophercloud/gophercloud@v1.11.0/openstack/endpoint_location.go (about) 1 package openstack 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens" 6 tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens" 7 ) 8 9 /* 10 V2EndpointURL discovers the endpoint URL for a specific service from a 11 ServiceCatalog acquired during the v2 identity service. 12 13 The specified EndpointOpts are used to identify a unique, unambiguous endpoint 14 to return. It's an error both when multiple endpoints match the provided 15 criteria and when none do. The minimum that can be specified is a Type, but you 16 will also often need to specify a Name and/or a Region depending on what's 17 available on your OpenStack deployment. 18 */ 19 func V2EndpointURL(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) { 20 // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided. 21 var endpoints = make([]tokens2.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.Region == "" || endpoint.Region == opts.Region { 26 endpoints = append(endpoints, endpoint) 27 } 28 } 29 } 30 } 31 32 // If multiple endpoints were found, use the first result 33 // and disregard the other endpoints. 34 // 35 // This behavior matches the Python library. See GH-1764. 36 if len(endpoints) > 1 { 37 endpoints = endpoints[0:1] 38 } 39 40 // Extract the appropriate URL from the matching Endpoint. 41 for _, endpoint := range endpoints { 42 switch opts.Availability { 43 case gophercloud.AvailabilityPublic: 44 return gophercloud.NormalizeURL(endpoint.PublicURL), nil 45 case gophercloud.AvailabilityInternal: 46 return gophercloud.NormalizeURL(endpoint.InternalURL), nil 47 case gophercloud.AvailabilityAdmin: 48 return gophercloud.NormalizeURL(endpoint.AdminURL), nil 49 default: 50 err := &ErrInvalidAvailabilityProvided{} 51 err.Argument = "Availability" 52 err.Value = opts.Availability 53 return "", err 54 } 55 } 56 57 // Report an error if there were no matching endpoints. 58 err := &gophercloud.ErrEndpointNotFound{} 59 return "", err 60 } 61 62 /* 63 V3EndpointURL discovers the endpoint URL for a specific service from a Catalog 64 acquired during the v3 identity service. 65 66 The specified EndpointOpts are used to identify a unique, unambiguous endpoint 67 to return. It's an error both when multiple endpoints match the provided 68 criteria and when none do. The minimum that can be specified is a Type, but you 69 will also often need to specify a Name and/or a Region depending on what's 70 available on your OpenStack deployment. 71 */ 72 func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) { 73 // Extract Endpoints from the catalog entries that match the requested Type, Interface, 74 // Name if provided, and Region if provided. 75 var endpoints = make([]tokens3.Endpoint, 0, 1) 76 for _, entry := range catalog.Entries { 77 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) { 78 for _, endpoint := range entry.Endpoints { 79 if opts.Availability != gophercloud.AvailabilityAdmin && 80 opts.Availability != gophercloud.AvailabilityPublic && 81 opts.Availability != gophercloud.AvailabilityInternal { 82 err := &ErrInvalidAvailabilityProvided{} 83 err.Argument = "Availability" 84 err.Value = opts.Availability 85 return "", err 86 } 87 if (opts.Availability == gophercloud.Availability(endpoint.Interface)) && 88 (opts.Region == "" || endpoint.Region == opts.Region || endpoint.RegionID == opts.Region) { 89 endpoints = append(endpoints, endpoint) 90 } 91 } 92 } 93 } 94 95 // If multiple endpoints were found, use the first result 96 // and disregard the other endpoints. 97 // 98 // This behavior matches the Python library. See GH-1764. 99 if len(endpoints) > 1 { 100 endpoints = endpoints[0:1] 101 } 102 103 // Extract the URL from the matching Endpoint. 104 for _, endpoint := range endpoints { 105 return gophercloud.NormalizeURL(endpoint.URL), nil 106 } 107 108 // Report an error if there were no matching endpoints. 109 err := &gophercloud.ErrEndpointNotFound{} 110 return "", err 111 }