diff --git a/cmd/openstackcli/main.go b/cmd/openstackcli/main.go index 6975a98923..0b87dffc00 100644 --- a/cmd/openstackcli/main.go +++ b/cmd/openstackcli/main.go @@ -93,7 +93,18 @@ func newClient(options *BaseOptions) (*openstack.SRegion, error) { return nil, fmt.Errorf("Missing Password") } - cli, err := openstack.NewOpenStackClient("", "", options.AuthURL, options.Username, options.Password, options.Project, options.EndpointType, options.DomainName, options.ProjectDomain, options.Debug) + cli, err := openstack.NewOpenStackClient( + openstack.NewOpenstackClientConfig( + options.AuthURL, + options.Username, + options.Password, + options.Project, + options.ProjectDomain, + ). + EndpointType(options.EndpointType). + DomainName(options.DomainName). + Debug(options.Debug), + ) if err != nil { return nil, err } diff --git a/pkg/multicloud/openstack/novastorage.go b/pkg/multicloud/openstack/novastorage.go index 956858d43e..8430758003 100644 --- a/pkg/multicloud/openstack/novastorage.go +++ b/pkg/multicloud/openstack/novastorage.go @@ -32,7 +32,7 @@ func (storage *SNovaStorage) GetMetadata() *jsonutils.JSONDict { } func (storage *SNovaStorage) GetId() string { - return fmt.Sprintf("%s-%s-%s", storage.zone.region.client.providerID, storage.zone.GetGlobalId(), storage.GetName()) + return fmt.Sprintf("%s-%s-%s", storage.zone.region.client.cpcfg.Id, storage.zone.GetGlobalId(), storage.GetName()) } func (storage *SNovaStorage) GetName() string { diff --git a/pkg/multicloud/openstack/openstack.go b/pkg/multicloud/openstack/openstack.go index 7af8110f8a..4011b483e8 100644 --- a/pkg/multicloud/openstack/openstack.go +++ b/pkg/multicloud/openstack/openstack.go @@ -37,47 +37,75 @@ const ( OPENSTACK_DEFAULT_REGION = "RegionOne" ) -type SOpenStackClient struct { - providerID string - providerName string - authURL string - username string - password string - project string - projectDomain string - endpointType string - domainName string - client *mcclient.Client - tokenCredential mcclient.TokenCredential - iregions []cloudprovider.ICloudRegion +type OpenstackClientConfig struct { + cpcfg cloudprovider.ProviderConfig - Debug bool + authURL string + username string + password string + project string + projectDomain string + + domainName string + endpointType string + + debug bool } -func NewOpenStackClient(providerID string, providerName string, authURL string, username string, password string, project string, endpointType string, domainName string, projectDomainName string, isDebug bool) (*SOpenStackClient, error) { - cli := &SOpenStackClient{ - providerID: providerID, - providerName: providerName, - authURL: strings.TrimRight(authURL, "/"), +func NewOpenstackClientConfig(authURL, username, password, project, projectDomain string) *OpenstackClientConfig { + cfg := &OpenstackClientConfig{ + authURL: authURL, username: username, password: password, project: project, - projectDomain: projectDomainName, - endpointType: endpointType, - domainName: domainName, - Debug: isDebug, + projectDomain: projectDomain, + } + return cfg +} + +func (cfg *OpenstackClientConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *OpenstackClientConfig { + cfg.cpcfg = cpcfg + return cfg +} + +func (cfg *OpenstackClientConfig) DomainName(domainName string) *OpenstackClientConfig { + cfg.domainName = domainName + return cfg +} + +func (cfg *OpenstackClientConfig) EndpointType(endpointType string) *OpenstackClientConfig { + cfg.endpointType = endpointType + return cfg +} + +func (cfg *OpenstackClientConfig) Debug(debug bool) *OpenstackClientConfig { + cfg.debug = debug + return cfg +} + +type SOpenStackClient struct { + *OpenstackClientConfig + + client *mcclient.Client + tokenCredential mcclient.TokenCredential + iregions []cloudprovider.ICloudRegion +} + +func NewOpenStackClient(cfg *OpenstackClientConfig) (*SOpenStackClient, error) { + cli := &SOpenStackClient{ + OpenstackClientConfig: cfg, } return cli, cli.fetchRegions() } func (cli *SOpenStackClient) GetCloudRegionExternalIdPrefix() string { - return fmt.Sprintf("%s/%s/", CLOUD_PROVIDER_OPENSTACK, cli.providerID) + return fmt.Sprintf("%s/%s/", CLOUD_PROVIDER_OPENSTACK, cli.cpcfg.Id) } func (cli *SOpenStackClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) { subAccount := cloudprovider.SSubAccount{ Account: fmt.Sprintf("%s/%s", cli.project, cli.username), - Name: cli.providerName, + Name: cli.cpcfg.Name, } if len(cli.domainName) > 0 { subAccount.Account = fmt.Sprintf("%s/%s", subAccount.Account, cli.domainName) @@ -194,7 +222,7 @@ func (cli *SOpenStackClient) getVersion(region string, service string) (string, } func (cli *SOpenStackClient) connect() error { - cli.client = mcclient.NewClient(cli.authURL, 5, cli.Debug, false, "", "") + cli.client = mcclient.NewClient(cli.authURL, 5, cli.debug, false, "", "") tokenCredential, err := cli.client.Authenticate(cli.username, cli.password, cli.domainName, cli.project, cli.projectDomain) if err != nil { return err diff --git a/pkg/multicloud/openstack/provider/provider.go b/pkg/multicloud/openstack/provider/provider.go index fd7c335d3a..68e46cffde 100644 --- a/pkg/multicloud/openstack/provider/provider.go +++ b/pkg/multicloud/openstack/provider/provider.go @@ -111,7 +111,18 @@ func (self *SOpenStackProviderFactory) GetProvider(cfg cloudprovider.ProviderCon if len(accountInfo) == 3 { domainName, projectDomainName = accountInfo[2], accountInfo[2] } - client, err := openstack.NewOpenStackClient(cfg.Id, cfg.Name, cfg.URL, username, cfg.Secret, project, endpointType, domainName, projectDomainName, false) + client, err := openstack.NewOpenStackClient( + openstack.NewOpenstackClientConfig( + cfg.URL, + username, + cfg.Secret, + project, + projectDomainName, + ). + DomainName(domainName). + EndpointType(endpointType). + CloudproviderConfig(cfg), + ) if err != nil { return nil, err } diff --git a/pkg/multicloud/openstack/region.go b/pkg/multicloud/openstack/region.go index 8201827f5d..650ee38bf6 100644 --- a/pkg/multicloud/openstack/region.go +++ b/pkg/multicloud/openstack/region.go @@ -59,11 +59,11 @@ func (region *SRegion) GetId() string { } func (region *SRegion) GetName() string { - return fmt.Sprintf("%s-%s", region.client.providerName, region.Name) + return fmt.Sprintf("%s-%s", region.client.cpcfg.Name, region.Name) } func (region *SRegion) GetGlobalId() string { - return fmt.Sprintf("%s/%s/%s", CLOUD_PROVIDER_OPENSTACK, region.client.providerID, region.Name) + return fmt.Sprintf("%s/%s/%s", CLOUD_PROVIDER_OPENSTACK, region.client.cpcfg.Id, region.Name) } func (region *SRegion) IsEmulated() bool { diff --git a/pkg/multicloud/openstack/storagecache.go b/pkg/multicloud/openstack/storagecache.go index 4579603474..9cf3ec1d9c 100644 --- a/pkg/multicloud/openstack/storagecache.go +++ b/pkg/multicloud/openstack/storagecache.go @@ -42,11 +42,11 @@ func (cache *SStoragecache) GetMetadata() *jsonutils.JSONDict { } func (cache *SStoragecache) GetId() string { - return fmt.Sprintf("%s-%s", cache.region.client.providerID, cache.region.GetId()) + return fmt.Sprintf("%s-%s", cache.region.client.cpcfg.Id, cache.region.GetId()) } func (cache *SStoragecache) GetName() string { - return fmt.Sprintf("%s-%s", cache.region.client.providerName, cache.region.GetId()) + return fmt.Sprintf("%s-%s", cache.region.client.cpcfg.Name, cache.region.GetId()) } func (cache *SStoragecache) GetStatus() string { @@ -58,7 +58,7 @@ func (cache *SStoragecache) Refresh() error { } func (cache *SStoragecache) GetGlobalId() string { - return fmt.Sprintf("%s-%s", cache.region.client.providerID, cache.region.GetGlobalId()) + return fmt.Sprintf("%s-%s", cache.region.client.cpcfg.Id, cache.region.GetGlobalId()) } func (cache *SStoragecache) IsEmulated() bool {