diff --git a/selvpcclient/clients/resell.go b/selvpcclient/clients/resell.go index 90cda82..917e29f 100644 --- a/selvpcclient/clients/resell.go +++ b/selvpcclient/clients/resell.go @@ -15,26 +15,29 @@ const ( type ResellClient struct { Requests *clientservices.RequestService catalog *clientservices.CatalogService + region string } func NewResellClient( requestService *clientservices.RequestService, catalogService *clientservices.CatalogService, + region string, ) *ResellClient { return &ResellClient{ Requests: requestService, catalog: catalogService, + region: region, } } // GetEndpoint - returns service url. func (c *ResellClient) GetEndpoint() (string, error) { - endpoints, err := c.catalog.GetEndpoints(ResellServiceType) + endpoint, err := c.catalog.GetEndpoint(ResellServiceType, c.region) 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..10a252e 100644 --- a/selvpcclient/clients/services/serviceclient.go +++ b/selvpcclient/clients/services/serviceclient.go @@ -24,9 +24,12 @@ 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 + // Field for setting location for endpoints like ResellAPI or Keystone. + AuthRegion string + // Optional field. HTTPClient *http.Client @@ -34,13 +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. @@ -71,6 +68,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..346d975 100644 --- a/selvpcclient/selvpc.go +++ b/selvpcclient/selvpc.go @@ -14,8 +14,10 @@ import ( ) const ( - AppName = "go-selvpcclient" - AppVersion = "3.0.0" + AppName = "go-selvpcclient" + AppVersion = "3.0.0" + DefaultAuthRegion = "ru-1" + DefaultAuthURL = "https://cloud.api.selcloud.ru/identity/v3/" ) type Client struct { @@ -49,6 +51,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 +61,14 @@ type ClientOptions struct { } func NewClient(options *ClientOptions) (*Client, error) { + if options.AuthRegion == "" { + options.AuthRegion = DefaultAuthRegion + } + + if options.AuthURL == "" { + options.AuthURL = DefaultAuthURL + } + serviceClientOptions := clientservices.ServiceClientOptions{ DomainName: options.DomainName, Username: options.Username, @@ -63,6 +76,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 +95,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, 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), } }