Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default region for Keystone and Resell API #169

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions selvpcclient/clients/resell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 5 additions & 7 deletions selvpcclient/clients/services/serviceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@ 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

// Optional field.
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.
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions selvpcclient/selvpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -49,20 +51,32 @@ 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.
UserDomainName string
}

func NewClient(options *ClientOptions) (*Client, error) {
if options.AuthRegion == "" {
options.AuthRegion = DefaultAuthRegion
}

milkrage marked this conversation as resolved.
Show resolved Hide resolved
if options.AuthURL == "" {
options.AuthURL = DefaultAuthURL
}

serviceClientOptions := clientservices.ServiceClientOptions{
DomainName: options.DomainName,
Username: options.Username,
Password: options.Password,
ProjectID: options.ProjectID,
UserDomainName: options.UserDomainName,
AuthURL: options.AuthURL,
AuthRegion: options.AuthRegion,
UserAgent: fmt.Sprintf("%s/%s", AppName, AppVersion),
}

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion selvpcclient/testutils/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}