Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 4 | "fmt" |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 5 | "net/url" |
Ash Wilson | ed6a1d8 | 2014-09-03 12:01:00 -0400 | [diff] [blame] | 6 | "strings" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 7 | |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Ash Wilson | 11c9828 | 2014-09-08 16:05:10 -0400 | [diff] [blame] | 9 | identity2 "github.com/rackspace/gophercloud/openstack/identity/v2" |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 10 | endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints" |
| 11 | services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 12 | tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 16 | const ( |
| 17 | v20 = "v2.0" |
| 18 | v30 = "v3.0" |
| 19 | ) |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 20 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 21 | // NewClient prepares an unauthenticated ProviderClient instance. |
| 22 | // Most users will probably prefer using the AuthenticatedClient function instead. |
| 23 | // This is useful if you wish to explicitly control the version of the identity service that's used for authentication explicitly, |
| 24 | // for example. |
| 25 | func NewClient(endpoint string) (*gophercloud.ProviderClient, error) { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 26 | u, err := url.Parse(endpoint) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | hadPath := u.Path != "" |
| 31 | u.Path, u.RawQuery, u.Fragment = "", "", "" |
| 32 | base := u.String() |
| 33 | |
Ash Wilson | aca58d8 | 2014-09-10 17:00:35 -0400 | [diff] [blame^] | 34 | endpoint = normalizeURL(endpoint) |
| 35 | base = normalizeURL(base) |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 36 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 37 | if hadPath { |
| 38 | return &gophercloud.ProviderClient{ |
| 39 | IdentityBase: base, |
| 40 | IdentityEndpoint: endpoint, |
| 41 | }, nil |
| 42 | } |
| 43 | |
| 44 | return &gophercloud.ProviderClient{ |
| 45 | IdentityBase: base, |
| 46 | IdentityEndpoint: "", |
| 47 | }, nil |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // AuthenticatedClient logs in to an OpenStack cloud found at the identity endpoint specified by options, acquires a token, and |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 51 | // returns a Client instance that's ready to operate. |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 52 | // It first queries the root identity endpoint to determine which versions of the identity service are supported, then chooses |
| 53 | // the most recent identity service available to proceed. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 54 | func AuthenticatedClient(options gophercloud.AuthOptions) (*gophercloud.ProviderClient, error) { |
| 55 | client, err := NewClient(options.IdentityEndpoint) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | err = Authenticate(client, options) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | return client, nil |
| 65 | } |
| 66 | |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 67 | // Authenticate or re-authenticate against the most recent identity service supported at the provided endpoint. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 68 | func Authenticate(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 69 | versions := []*utils.Version{ |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 70 | &utils.Version{ID: v20, Priority: 20, Suffix: "/v2.0/"}, |
| 71 | &utils.Version{ID: v30, Priority: 30, Suffix: "/v3/"}, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 74 | chosen, endpoint, err := utils.ChooseVersion(client.IdentityBase, client.IdentityEndpoint, versions) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 75 | if err != nil { |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 76 | return err |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | switch chosen.ID { |
| 80 | case v20: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 81 | return v2auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 82 | case v30: |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 83 | return v3auth(client, endpoint, options) |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 84 | default: |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 85 | // The switch statement must be out of date from the versions list. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 86 | return fmt.Errorf("Unrecognized identity version: %s", chosen.ID) |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 90 | // AuthenticateV2 explicitly authenticates against the identity v2 endpoint. |
| 91 | func AuthenticateV2(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 92 | return v2auth(client, "", options) |
| 93 | } |
| 94 | |
| 95 | func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 96 | v2Client := NewIdentityV2(client) |
| 97 | if endpoint != "" { |
| 98 | v2Client.Endpoint = endpoint |
| 99 | } |
| 100 | |
| 101 | result, err := identity2.Authenticate(v2Client, options) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | token, err := identity2.GetToken(result) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | client.TokenID = token.ID |
| 112 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 113 | return v2endpointLocator(result, opts) |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 119 | func v2endpointLocator(authResults identity2.AuthResults, opts gophercloud.EndpointOpts) (string, error) { |
| 120 | catalog, err := identity2.GetServiceCatalog(authResults) |
| 121 | if err != nil { |
| 122 | return "", err |
| 123 | } |
| 124 | |
| 125 | entries, err := catalog.CatalogEntries() |
| 126 | if err != nil { |
| 127 | return "", err |
| 128 | } |
| 129 | |
| 130 | // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided. |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 131 | var endpoints = make([]identity2.Endpoint, 0, 1) |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 132 | for _, entry := range entries { |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 133 | if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) { |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 134 | for _, endpoint := range entry.Endpoints { |
| 135 | if opts.Region == "" || endpoint.Region == opts.Region { |
| 136 | endpoints = append(endpoints, endpoint) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Report an error if the options were ambiguous. |
| 143 | if len(endpoints) == 0 { |
| 144 | return "", gophercloud.ErrEndpointNotFound |
| 145 | } |
| 146 | if len(endpoints) > 1 { |
| 147 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 148 | } |
| 149 | |
| 150 | // Extract the appropriate URL from the matching Endpoint. |
| 151 | for _, endpoint := range endpoints { |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 152 | switch opts.Availability { |
| 153 | case gophercloud.AvailabilityPublic: |
Ash Wilson | aca58d8 | 2014-09-10 17:00:35 -0400 | [diff] [blame^] | 154 | return normalizeURL(endpoint.PublicURL), nil |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 155 | case gophercloud.AvailabilityInternal: |
Ash Wilson | aca58d8 | 2014-09-10 17:00:35 -0400 | [diff] [blame^] | 156 | return normalizeURL(endpoint.InternalURL), nil |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 157 | default: |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 158 | return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability) |
Ash Wilson | 9d9876b | 2014-09-09 09:28:00 -0400 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 162 | return "", gophercloud.ErrEndpointNotFound |
| 163 | } |
| 164 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 165 | // AuthenticateV3 explicitly authenticates against the identity v3 service. |
| 166 | func AuthenticateV3(client *gophercloud.ProviderClient, options gophercloud.AuthOptions) error { |
| 167 | return v3auth(client, "", options) |
| 168 | } |
| 169 | |
| 170 | func v3auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { |
| 171 | // Override the generated service endpoint with the one returned by the version endpoint. |
| 172 | v3Client := NewIdentityV3(client) |
| 173 | if endpoint != "" { |
| 174 | v3Client.Endpoint = endpoint |
| 175 | } |
| 176 | |
| 177 | result, err := tokens3.Create(v3Client, options, nil) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | |
| 182 | client.TokenID, err = result.TokenID() |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { |
| 187 | return v3endpointLocator(v3Client, opts) |
| 188 | } |
| 189 | |
| 190 | return nil |
| 191 | } |
| 192 | |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 193 | func v3endpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) { |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 194 | // Discover the service we're interested in. |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 195 | serviceResults, err := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type}) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 196 | if err != nil { |
| 197 | return "", err |
| 198 | } |
| 199 | |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 200 | allServiceResults, err := gophercloud.AllPages(serviceResults) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 201 | if err != nil { |
| 202 | return "", err |
| 203 | } |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 204 | allServices := services3.AsServices(allServiceResults) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 205 | |
| 206 | if opts.Name != "" { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 207 | filtered := make([]services3.Service, 0, 1) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 208 | for _, service := range allServices { |
| 209 | if service.Name == opts.Name { |
| 210 | filtered = append(filtered, service) |
| 211 | } |
| 212 | } |
| 213 | allServices = filtered |
| 214 | } |
| 215 | |
| 216 | if len(allServices) == 0 { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 217 | return "", gophercloud.ErrServiceNotFound |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 218 | } |
| 219 | if len(allServices) > 1 { |
| 220 | return "", fmt.Errorf("Discovered %d matching services: %#v", len(allServices), allServices) |
| 221 | } |
| 222 | |
| 223 | service := allServices[0] |
| 224 | |
| 225 | // Enumerate the endpoints available for this service. |
| 226 | endpointResults, err := endpoints3.List(v3Client, endpoints3.ListOpts{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 227 | Availability: opts.Availability, |
| 228 | ServiceID: service.ID, |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 229 | }) |
| 230 | if err != nil { |
| 231 | return "", err |
| 232 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 233 | allEndpoints, err := gophercloud.AllPages(endpointResults) |
| 234 | if err != nil { |
| 235 | return "", err |
| 236 | } |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 237 | endpoints := endpoints3.AsEndpoints(allEndpoints) |
| 238 | |
| 239 | if opts.Name != "" { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 240 | filtered := make([]endpoints3.Endpoint, 0, 1) |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 241 | for _, endpoint := range endpoints { |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 242 | if opts.Region == "" || endpoint.Region == opts.Region { |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 243 | filtered = append(filtered, endpoint) |
| 244 | } |
| 245 | } |
| 246 | endpoints = filtered |
| 247 | } |
| 248 | |
| 249 | if len(endpoints) == 0 { |
| 250 | return "", gophercloud.ErrEndpointNotFound |
| 251 | } |
| 252 | if len(endpoints) > 1 { |
| 253 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 254 | } |
| 255 | |
| 256 | endpoint := endpoints[0] |
| 257 | |
Ash Wilson | aca58d8 | 2014-09-10 17:00:35 -0400 | [diff] [blame^] | 258 | return normalizeURL(endpoint.URL), nil |
| 259 | } |
| 260 | |
| 261 | // normalizeURL ensures that each endpoint URL has a closing `/`, as expected by ServiceClient. |
| 262 | func normalizeURL(url string) string { |
| 263 | if !strings.HasSuffix(url, "/") { |
| 264 | return url + "/" |
| 265 | } |
| 266 | return url |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 267 | } |
| 268 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 269 | // NewIdentityV2 creates a ServiceClient that may be used to interact with the v2 identity service. |
| 270 | func NewIdentityV2(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 271 | v2Endpoint := client.IdentityBase + "v2.0/" |
Ash Wilson | ccd020b | 2014-09-02 10:40:54 -0400 | [diff] [blame] | 272 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 273 | return &gophercloud.ServiceClient{ |
| 274 | Provider: client, |
| 275 | Endpoint: v2Endpoint, |
Ash Wilson | 4dee1b8 | 2014-08-29 14:56:45 -0400 | [diff] [blame] | 276 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 279 | // NewIdentityV3 creates a ServiceClient that may be used to access the v3 identity service. |
| 280 | func NewIdentityV3(client *gophercloud.ProviderClient) *gophercloud.ServiceClient { |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 281 | v3Endpoint := client.IdentityBase + "v3/" |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 282 | |
| 283 | return &gophercloud.ServiceClient{ |
| 284 | Provider: client, |
| 285 | Endpoint: v3Endpoint, |
| 286 | } |
Ash Wilson | 8ba8224 | 2014-08-28 15:38:55 -0400 | [diff] [blame] | 287 | } |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 288 | |
| 289 | // NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package. |
Jon Perritt | 509fbb6 | 2014-09-10 13:29:56 -0500 | [diff] [blame] | 290 | func NewStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) { |
| 291 | eo.ApplyDefaults("object-store") |
| 292 | url, err := client.EndpointLocator(eo) |
Ash Wilson | 1cd3e69 | 2014-09-09 11:01:47 -0400 | [diff] [blame] | 293 | if err != nil { |
| 294 | return nil, err |
| 295 | } |
| 296 | return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil |
| 297 | } |