openstack: use OpenstackClientConfig

This commit is contained in:
Yousong Zhou
2020-03-22 19:36:45 +08:00
parent a3f9e185ac
commit 0ff96ace44
6 changed files with 84 additions and 34 deletions
+1 -1
View File
@@ -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 {
+54 -26
View File
@@ -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
+12 -1
View File
@@ -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
}
+2 -2
View File
@@ -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 {
+3 -3
View File
@@ -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 {