Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bae4c6f

Browse files
committed
Add EndpointOpts.Version field
We will use this to do proper discovery across multiple versions. Signed-off-by: Stephen Finucane <[email protected]>
1 parent 4329495 commit bae4c6f

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

endpoint_search.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ type EndpointOpts struct {
7979
// Required only for services that span multiple regions.
8080
Region string
8181

82+
// Version [optional] is the major version of the service required. It it not
83+
// a microversion. Use this to ensure the correct endpoint is selected when
84+
// multiple API versions are available.
85+
Version int
86+
8287
// Availability [optional] is the visibility of the endpoint to be returned.
8388
// Valid types include the constants AvailabilityPublic, AvailabilityInternal,
8489
// or AvailabilityAdmin from this package.

openstack/client.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openstack
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"reflect"
78
"strings"
@@ -345,13 +346,20 @@ func NewIdentityV3(client *gophercloud.ProviderClient, eo gophercloud.EndpointOp
345346
}
346347

347348
// TODO(stephenfin): Allow passing aliases to all New${SERVICE}V${VERSION} methods in v3
348-
func initClientOpts(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts, clientType string) (*gophercloud.ServiceClient, error) {
349+
func initClientOpts(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts, clientType string, version int) (*gophercloud.ServiceClient, error) {
349350
sc := new(gophercloud.ServiceClient)
351+
350352
eo.ApplyDefaults(clientType)
353+
if eo.Version != 0 && eo.Version != version {
354+
return sc, errors.New("Conflict between requested service major version and manually set version")
355+
}
356+
eo.Version = version
357+
351358
url, err := client.EndpointLocator(eo)
352359
if err != nil {
353360
return sc, err
354361
}
362+
355363
sc.ProviderClient = client
356364
sc.Endpoint = url
357365
sc.Type = clientType
@@ -361,7 +369,7 @@ func initClientOpts(client *gophercloud.ProviderClient, eo gophercloud.EndpointO
361369
// NewBareMetalV1 creates a ServiceClient that may be used with the v1
362370
// bare metal package.
363371
func NewBareMetalV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
364-
sc, err := initClientOpts(client, eo, "baremetal")
372+
sc, err := initClientOpts(client, eo, "baremetal", 1)
365373
if !strings.HasSuffix(strings.TrimSuffix(sc.Endpoint, "/"), "v1") {
366374
sc.ResourceBase = sc.Endpoint + "v1/"
367375
}
@@ -371,25 +379,25 @@ func NewBareMetalV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointO
371379
// NewBareMetalIntrospectionV1 creates a ServiceClient that may be used with the v1
372380
// bare metal introspection package.
373381
func NewBareMetalIntrospectionV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
374-
return initClientOpts(client, eo, "baremetal-introspection")
382+
return initClientOpts(client, eo, "baremetal-introspection", 1)
375383
}
376384

377385
// NewObjectStorageV1 creates a ServiceClient that may be used with the v1
378386
// object storage package.
379387
func NewObjectStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
380-
return initClientOpts(client, eo, "object-store")
388+
return initClientOpts(client, eo, "object-store", 1)
381389
}
382390

383391
// NewComputeV2 creates a ServiceClient that may be used with the v2 compute
384392
// package.
385393
func NewComputeV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
386-
return initClientOpts(client, eo, "compute")
394+
return initClientOpts(client, eo, "compute", 2)
387395
}
388396

389397
// NewNetworkV2 creates a ServiceClient that may be used with the v2 network
390398
// package.
391399
func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
392-
sc, err := initClientOpts(client, eo, "network")
400+
sc, err := initClientOpts(client, eo, "network", 2)
393401
sc.ResourceBase = sc.Endpoint + "v2.0/"
394402
return sc, err
395403
}
@@ -398,56 +406,56 @@ func NewNetworkV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpt
398406
// NewBlockStorageV1 creates a ServiceClient that may be used to access the v1
399407
// block storage service.
400408
func NewBlockStorageV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
401-
return initClientOpts(client, eo, "volume")
409+
return initClientOpts(client, eo, "volume", 1)
402410
}
403411

404412
// NewBlockStorageV2 creates a ServiceClient that may be used to access the v2
405413
// block storage service.
406414
func NewBlockStorageV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
407-
return initClientOpts(client, eo, "block-storage")
415+
return initClientOpts(client, eo, "block-storage", 2)
408416
}
409417

410418
// NewBlockStorageV3 creates a ServiceClient that may be used to access the v3 block storage service.
411419
func NewBlockStorageV3(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
412-
return initClientOpts(client, eo, "block-storage")
420+
return initClientOpts(client, eo, "block-storage", 3)
413421
}
414422

415423
// NewSharedFileSystemV2 creates a ServiceClient that may be used to access the v2 shared file system service.
416424
func NewSharedFileSystemV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
417-
return initClientOpts(client, eo, "shared-file-system")
425+
return initClientOpts(client, eo, "shared-file-system", 2)
418426
}
419427

420428
// NewOrchestrationV1 creates a ServiceClient that may be used to access the v1
421429
// orchestration service.
422430
func NewOrchestrationV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
423-
return initClientOpts(client, eo, "orchestration")
431+
return initClientOpts(client, eo, "orchestration", 1)
424432
}
425433

426434
// NewDBV1 creates a ServiceClient that may be used to access the v1 DB service.
427435
func NewDBV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
428-
return initClientOpts(client, eo, "database")
436+
return initClientOpts(client, eo, "database", 1)
429437
}
430438

431439
// NewDNSV2 creates a ServiceClient that may be used to access the v2 DNS
432440
// service.
433441
func NewDNSV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
434-
sc, err := initClientOpts(client, eo, "dns")
442+
sc, err := initClientOpts(client, eo, "dns", 2)
435443
sc.ResourceBase = sc.Endpoint + "v2/"
436444
return sc, err
437445
}
438446

439447
// NewImageV2 creates a ServiceClient that may be used to access the v2 image
440448
// service.
441449
func NewImageV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
442-
sc, err := initClientOpts(client, eo, "image")
450+
sc, err := initClientOpts(client, eo, "image", 2)
443451
sc.ResourceBase = sc.Endpoint + "v2/"
444452
return sc, err
445453
}
446454

447455
// NewLoadBalancerV2 creates a ServiceClient that may be used to access the v2
448456
// load balancer service.
449457
func NewLoadBalancerV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
450-
sc, err := initClientOpts(client, eo, "load-balancer")
458+
sc, err := initClientOpts(client, eo, "load-balancer", 2)
451459

452460
// Fixes edge case having an OpenStack lb endpoint with trailing version number.
453461
endpoint := strings.Replace(sc.Endpoint, "v2.0/", "", -1)
@@ -459,36 +467,36 @@ func NewLoadBalancerV2(client *gophercloud.ProviderClient, eo gophercloud.Endpoi
459467
// NewMessagingV2 creates a ServiceClient that may be used with the v2 messaging
460468
// service.
461469
func NewMessagingV2(client *gophercloud.ProviderClient, clientID string, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
462-
sc, err := initClientOpts(client, eo, "message")
470+
sc, err := initClientOpts(client, eo, "message", 2)
463471
sc.MoreHeaders = map[string]string{"Client-ID": clientID}
464472
return sc, err
465473
}
466474

467475
// NewContainerV1 creates a ServiceClient that may be used with v1 container package
468476
func NewContainerV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
469-
return initClientOpts(client, eo, "application-container")
477+
return initClientOpts(client, eo, "application-container", 1)
470478
}
471479

472480
// NewKeyManagerV1 creates a ServiceClient that may be used with the v1 key
473481
// manager service.
474482
func NewKeyManagerV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
475-
sc, err := initClientOpts(client, eo, "key-manager")
483+
sc, err := initClientOpts(client, eo, "key-manager", 1)
476484
sc.ResourceBase = sc.Endpoint + "v1/"
477485
return sc, err
478486
}
479487

480488
// NewContainerInfraV1 creates a ServiceClient that may be used with the v1 container infra management
481489
// package.
482490
func NewContainerInfraV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
483-
return initClientOpts(client, eo, "container-infrastructure-management")
491+
return initClientOpts(client, eo, "container-infrastructure-management", 1)
484492
}
485493

486494
// NewWorkflowV2 creates a ServiceClient that may be used with the v2 workflow management package.
487495
func NewWorkflowV2(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
488-
return initClientOpts(client, eo, "workflow")
496+
return initClientOpts(client, eo, "workflow", 2)
489497
}
490498

491499
// NewPlacementV1 creates a ServiceClient that may be used with the placement package.
492500
func NewPlacementV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
493-
return initClientOpts(client, eo, "placement")
501+
return initClientOpts(client, eo, "placement", 1)
494502
}

0 commit comments

Comments
 (0)