From bbcc1825706c82d6b98c50b610c50d808c6e06c9 Mon Sep 17 00:00:00 2001 From: Anton Shnayder Date: Fri, 8 Dec 2023 21:10:07 +0600 Subject: [PATCH 1/4] Add default region for Keystone and Resell API --- selvpcclient/clients/resell.go | 15 +++++++++------ selvpcclient/clients/services/serviceclient.go | 8 +++++++- selvpcclient/selvpc.go | 15 ++++++++++++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/selvpcclient/clients/resell.go b/selvpcclient/clients/resell.go index 90cda82..bc121e4 100644 --- a/selvpcclient/clients/resell.go +++ b/selvpcclient/clients/resell.go @@ -13,28 +13,31 @@ const ( // ResellClient resell client with X-Auth-Token authorization. type ResellClient struct { - Requests *clientservices.RequestService - catalog *clientservices.CatalogService + Requests *clientservices.RequestService + catalog *clientservices.CatalogService + authRegion string } func NewResellClient( requestService *clientservices.RequestService, catalogService *clientservices.CatalogService, + authRegion string, ) *ResellClient { return &ResellClient{ - Requests: requestService, - catalog: catalogService, + Requests: requestService, + catalog: catalogService, + authRegion: authRegion, } } // GetEndpoint - returns service url. func (c *ResellClient) GetEndpoint() (string, error) { - endpoints, err := c.catalog.GetEndpoints(ResellServiceType) + endpoint, err := c.catalog.GetEndpoint(ResellServiceType, c.authRegion) if err != nil { return "", fmt.Errorf("failed to resolve endpoint for %s, err: %w", ResellServiceType, err) } - url := fmt.Sprintf("%s/%s", endpoints[0].URL, ResellAPIVersion) + url := fmt.Sprintf("%s/%s", endpoint.URL, ResellAPIVersion) return url, nil } diff --git a/selvpcclient/clients/services/serviceclient.go b/selvpcclient/clients/services/serviceclient.go index 94a798d..03a2795 100644 --- a/selvpcclient/clients/services/serviceclient.go +++ b/selvpcclient/clients/services/serviceclient.go @@ -27,6 +27,9 @@ type ServiceClientOptions struct { // Optional field for setting a non-default Identity endpoint. AuthURL string + // Optional field for setting a non-default location for endpoints like ResellAPI or Keystone. + AuthRegion string + // Optional field. HTTPClient *http.Client @@ -34,7 +37,9 @@ type ServiceClientOptions struct { UserAgent string } -const AuthURL = "https://cloud.api.selcloud.ru/identity/v3/" +const ( + AuthURL = "https://cloud.api.selcloud.ru/identity/v3/" +) func NewServiceClient(options *ServiceClientOptions) (*gophercloud.ServiceClient, error) { if options.AuthURL == "" { @@ -71,6 +76,7 @@ func NewServiceClient(options *ServiceClientOptions) (*gophercloud.ServiceClient serviceClient, err := openstack.NewIdentityV3(authProvider, gophercloud.EndpointOpts{ Availability: gophercloud.AvailabilityPublic, + Region: options.AuthRegion, }) if err != nil { return nil, fmt.Errorf("failed to create service client, err: %w", err) diff --git a/selvpcclient/selvpc.go b/selvpcclient/selvpc.go index c359437..1a37500 100644 --- a/selvpcclient/selvpc.go +++ b/selvpcclient/selvpc.go @@ -14,8 +14,9 @@ import ( ) const ( - AppName = "go-selvpcclient" - AppVersion = "3.0.0" + AppName = "go-selvpcclient" + AppVersion = "3.0.0" + defaultAuthRegion = "ru-1" ) type Client struct { @@ -49,6 +50,9 @@ type ClientOptions struct { // Optional field to specify a non-default Identity endpoint. AuthURL string + // Optional field for setting a non-default location for endpoints like ResellAPI or Keystone. + AuthRegion string + // Optional field to specify the domain name where the user is located. // Used in private clouds to issue a token not from owned domain. // If this field is not set, then it will be equal to the value of DomainName. @@ -56,6 +60,10 @@ type ClientOptions struct { } func NewClient(options *ClientOptions) (*Client, error) { + if options.AuthRegion == "" { + options.AuthRegion = defaultAuthRegion + } + serviceClientOptions := clientservices.ServiceClientOptions{ DomainName: options.DomainName, Username: options.Username, @@ -63,6 +71,7 @@ func NewClient(options *ClientOptions) (*Client, error) { ProjectID: options.ProjectID, UserDomainName: options.UserDomainName, AuthURL: options.AuthURL, + AuthRegion: options.AuthRegion, UserAgent: fmt.Sprintf("%s/%s", AppName, AppVersion), } @@ -81,7 +90,7 @@ func NewClient(options *ClientOptions) (*Client, error) { requestService := clientservices.NewRequestService(serviceClient) client := Client{ - Resell: clients.NewResellClient(requestService, catalogService), + Resell: clients.NewResellClient(requestService, catalogService, options.AuthRegion), QuotaManager: clients.NewQuotaManagerClient(requestService, catalogService), Catalog: catalogService, serviceClient: serviceClient, From 428bd89c8eaf9df6e61760ea216795c08529698c Mon Sep 17 00:00:00 2001 From: Anton Shnayder Date: Fri, 8 Dec 2023 21:16:31 +0600 Subject: [PATCH 2/4] Fix tests --- selvpcclient/selvpc.go | 4 ++-- selvpcclient/testutils/clients.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/selvpcclient/selvpc.go b/selvpcclient/selvpc.go index 1a37500..2057873 100644 --- a/selvpcclient/selvpc.go +++ b/selvpcclient/selvpc.go @@ -16,7 +16,7 @@ import ( const ( AppName = "go-selvpcclient" AppVersion = "3.0.0" - defaultAuthRegion = "ru-1" + DefaultAuthRegion = "ru-1" ) type Client struct { @@ -61,7 +61,7 @@ type ClientOptions struct { func NewClient(options *ClientOptions) (*Client, error) { if options.AuthRegion == "" { - options.AuthRegion = defaultAuthRegion + options.AuthRegion = DefaultAuthRegion } serviceClientOptions := clientservices.ServiceClientOptions{ diff --git a/selvpcclient/testutils/clients.go b/selvpcclient/testutils/clients.go index a7c959c..f6943db 100644 --- a/selvpcclient/testutils/clients.go +++ b/selvpcclient/testutils/clients.go @@ -26,7 +26,7 @@ func (testEnv *TestEnv) NewSelVPCClient() { requestService := clientservices.NewRequestService(serviceClient) testEnv.Client = &selvpcclient.Client{ - Resell: clients.NewResellClient(requestService, catalogService), + Resell: clients.NewResellClient(requestService, catalogService, selvpcclient.DefaultAuthRegion), QuotaManager: clients.NewQuotaManagerClient(requestService, catalogService), } } From 6bd5ec426ba8fccf07a55c66ac59162712e2853f Mon Sep 17 00:00:00 2001 From: Anton Shnayder Date: Mon, 11 Dec 2023 16:24:07 +0600 Subject: [PATCH 3/4] Review fixes --- selvpcclient/clients/resell.go | 16 ++++++++-------- selvpcclient/clients/services/serviceclient.go | 8 -------- selvpcclient/selvpc.go | 5 +++++ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/selvpcclient/clients/resell.go b/selvpcclient/clients/resell.go index bc121e4..917e29f 100644 --- a/selvpcclient/clients/resell.go +++ b/selvpcclient/clients/resell.go @@ -13,26 +13,26 @@ const ( // ResellClient resell client with X-Auth-Token authorization. type ResellClient struct { - Requests *clientservices.RequestService - catalog *clientservices.CatalogService - authRegion string + Requests *clientservices.RequestService + catalog *clientservices.CatalogService + region string } func NewResellClient( requestService *clientservices.RequestService, catalogService *clientservices.CatalogService, - authRegion string, + region string, ) *ResellClient { return &ResellClient{ - Requests: requestService, - catalog: catalogService, - authRegion: authRegion, + Requests: requestService, + catalog: catalogService, + region: region, } } // GetEndpoint - returns service url. func (c *ResellClient) GetEndpoint() (string, error) { - endpoint, err := c.catalog.GetEndpoint(ResellServiceType, c.authRegion) + endpoint, err := c.catalog.GetEndpoint(ResellServiceType, c.region) if err != nil { return "", fmt.Errorf("failed to resolve endpoint for %s, err: %w", ResellServiceType, err) } diff --git a/selvpcclient/clients/services/serviceclient.go b/selvpcclient/clients/services/serviceclient.go index 03a2795..8615970 100644 --- a/selvpcclient/clients/services/serviceclient.go +++ b/selvpcclient/clients/services/serviceclient.go @@ -37,15 +37,7 @@ type ServiceClientOptions struct { UserAgent string } -const ( - AuthURL = "https://cloud.api.selcloud.ru/identity/v3/" -) - func NewServiceClient(options *ServiceClientOptions) (*gophercloud.ServiceClient, error) { - if options.AuthURL == "" { - options.AuthURL = AuthURL - } - // UserDomainName field to specify the domain name where the user is located. // If this field is not specified, then we will think that the token will be // issued in the same domain where the user is located. diff --git a/selvpcclient/selvpc.go b/selvpcclient/selvpc.go index 2057873..346d975 100644 --- a/selvpcclient/selvpc.go +++ b/selvpcclient/selvpc.go @@ -17,6 +17,7 @@ const ( AppName = "go-selvpcclient" AppVersion = "3.0.0" DefaultAuthRegion = "ru-1" + DefaultAuthURL = "https://cloud.api.selcloud.ru/identity/v3/" ) type Client struct { @@ -64,6 +65,10 @@ func NewClient(options *ClientOptions) (*Client, error) { options.AuthRegion = DefaultAuthRegion } + if options.AuthURL == "" { + options.AuthURL = DefaultAuthURL + } + serviceClientOptions := clientservices.ServiceClientOptions{ DomainName: options.DomainName, Username: options.Username, From c414c4ec05214b97162ff0f51f153059f1d8e689 Mon Sep 17 00:00:00 2001 From: Anton Shnayder Date: Tue, 12 Dec 2023 18:39:35 +0600 Subject: [PATCH 4/4] Remove optional from description --- selvpcclient/clients/services/serviceclient.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/selvpcclient/clients/services/serviceclient.go b/selvpcclient/clients/services/serviceclient.go index 8615970..10a252e 100644 --- a/selvpcclient/clients/services/serviceclient.go +++ b/selvpcclient/clients/services/serviceclient.go @@ -24,10 +24,10 @@ type ServiceClientOptions struct { // Optional field. The name of the domain where the user resides (Identity v3). UserDomainName string - // Optional field for setting a non-default Identity endpoint. + // Field for setting Identity endpoint. AuthURL string - // Optional field for setting a non-default location for endpoints like ResellAPI or Keystone. + // Field for setting location for endpoints like ResellAPI or Keystone. AuthRegion string // Optional field.