mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-30 17:13:08 +08:00
cloudprovider: use ProviderConfig
This commit is contained in:
@@ -299,7 +299,16 @@ func (provider *SCloudprovider) GetProvider() (cloudprovider.ICloudProvider, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cloudprovider.GetProvider(provider.Id, provider.Name, accessUrl, provider.Account, passwd, provider.Provider)
|
||||
return cloudprovider.GetProvider(
|
||||
cloudprovider.ProviderConfig{
|
||||
Id: provider.Id,
|
||||
Name: provider.Name,
|
||||
Vendor: provider.Provider,
|
||||
URL: accessUrl,
|
||||
Account: provider.Account,
|
||||
Secret: passwd,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (manager *SCloudproviderManager) InitializeData() error {
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -140,8 +141,22 @@ type SCloudaccount struct {
|
||||
AccessUrl string `json:"access_url"`
|
||||
}
|
||||
|
||||
type ProviderConfig struct {
|
||||
// Id, Name are properties of Cloudprovider object
|
||||
Id string
|
||||
Name string
|
||||
|
||||
// Vendor are names like Aliyun, OpenStack, etc.
|
||||
Vendor string
|
||||
URL string
|
||||
Account string
|
||||
Secret string
|
||||
|
||||
ProxyFunc httputils.TransportProxyFunc
|
||||
}
|
||||
|
||||
type ICloudProviderFactory interface {
|
||||
GetProvider(providerId, providerName, url, account, secret string) (ICloudProvider, error)
|
||||
GetProvider(cfg ProviderConfig) (ICloudProvider, error)
|
||||
|
||||
GetClientRC(url, account, secret string) (map[string]string, error)
|
||||
|
||||
@@ -240,12 +255,12 @@ func GetRegistedProviderIds() []string {
|
||||
return providers
|
||||
}
|
||||
|
||||
func GetProvider(providerId, providerName, accessUrl, account, secret, provider string) (ICloudProvider, error) {
|
||||
driver, err := GetProviderFactory(provider)
|
||||
func GetProvider(cfg ProviderConfig) (ICloudProvider, error) {
|
||||
driver, err := GetProviderFactory(cfg.Vendor)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetProviderFactory")
|
||||
}
|
||||
return driver.GetProvider(providerId, providerName, accessUrl, account, secret)
|
||||
return driver.GetProvider(cfg)
|
||||
}
|
||||
|
||||
func GetClientRC(accessUrl, account, secret, provider string) (map[string]string, error) {
|
||||
@@ -261,10 +276,10 @@ func IsSupported(provider string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsValidCloudAccount(accessUrl, account, secret, provider string) (string, error) {
|
||||
factory, ok := providerTable[provider]
|
||||
func IsValidCloudAccount(cfg ProviderConfig) (string, error) {
|
||||
factory, ok := providerTable[cfg.Vendor]
|
||||
if ok {
|
||||
provider, err := factory.GetProvider("", "", accessUrl, account, secret)
|
||||
provider, err := factory.GetProvider(cfg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -149,6 +149,8 @@ type SCloudaccount struct {
|
||||
IsPublic bool `default:"false" nullable:"false"`
|
||||
// add share_mode field to indicate the share range of this account
|
||||
ShareMode string `width:"32" charset:"ascii" nullable:"true" list:"domain"`
|
||||
|
||||
ProxySettingId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"optional" default:"default"`
|
||||
}
|
||||
|
||||
func (self *SCloudaccount) GetCloudproviders() []SCloudprovider {
|
||||
@@ -343,7 +345,12 @@ func (manager *SCloudaccountManager) ValidateCreateData(ctx context.Context, use
|
||||
return input, httperrors.NewConflictError("The account has been registered")
|
||||
}
|
||||
|
||||
accountId, err := cloudprovider.IsValidCloudAccount(input.AccessUrl, input.Account, input.Secret, input.Provider)
|
||||
accountId, err := cloudprovider.IsValidCloudAccount(cloudprovider.ProviderConfig{
|
||||
Vendor: input.Provider,
|
||||
URL: input.AccessUrl,
|
||||
Account: input.Account,
|
||||
Secret: input.Secret,
|
||||
})
|
||||
if err != nil {
|
||||
if err == cloudprovider.ErrNoSuchProvder {
|
||||
return input, httperrors.NewResourceNotFoundError("no such provider %s", input.Provider)
|
||||
@@ -470,7 +477,12 @@ func (self *SCloudaccount) PerformTestConnectivity(ctx context.Context, userCred
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = cloudprovider.IsValidCloudAccount(self.AccessUrl, account.Account, account.Secret, self.Provider)
|
||||
_, err = cloudprovider.IsValidCloudAccount(cloudprovider.ProviderConfig{
|
||||
URL: self.AccessUrl,
|
||||
Vendor: self.Provider,
|
||||
Account: account.Account,
|
||||
Secret: account.Secret,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, httperrors.NewInputParameterError("invalid cloud account info error: %s", err.Error())
|
||||
}
|
||||
@@ -521,7 +533,12 @@ func (self *SCloudaccount) PerformUpdateCredential(ctx context.Context, userCred
|
||||
|
||||
originSecret, _ := self.getPassword()
|
||||
|
||||
accountId, err := cloudprovider.IsValidCloudAccount(self.AccessUrl, account.Account, account.Secret, self.Provider)
|
||||
accountId, err := cloudprovider.IsValidCloudAccount(cloudprovider.ProviderConfig{
|
||||
Vendor: self.Provider,
|
||||
URL: self.AccessUrl,
|
||||
Account: account.Account,
|
||||
Secret: account.Secret,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, httperrors.NewInputParameterError("invalid cloud account info error: %s", err.Error())
|
||||
}
|
||||
@@ -678,7 +695,14 @@ func (self *SCloudaccount) getProviderInternal() (cloudprovider.ICloudProvider,
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Invalid password %s", err)
|
||||
}
|
||||
return cloudprovider.GetProvider(self.Id, self.Name, self.AccessUrl, self.Account, secret, self.Provider)
|
||||
return cloudprovider.GetProvider(cloudprovider.ProviderConfig{
|
||||
Id: self.Id,
|
||||
Name: self.Name,
|
||||
Vendor: self.Provider,
|
||||
URL: self.AccessUrl,
|
||||
Account: self.Account,
|
||||
Secret: secret,
|
||||
})
|
||||
}
|
||||
|
||||
func (self *SCloudaccount) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
|
||||
|
||||
@@ -754,7 +754,14 @@ func (self *SCloudprovider) GetProvider() (cloudprovider.ICloudProvider, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cloudprovider.GetProvider(self.Id, self.Name, accessUrl, self.Account, passwd, self.Provider)
|
||||
return cloudprovider.GetProvider(cloudprovider.ProviderConfig{
|
||||
Id: self.Id,
|
||||
Name: self.Name,
|
||||
Vendor: self.Provider,
|
||||
URL: accessUrl,
|
||||
Account: self.Account,
|
||||
Secret: passwd,
|
||||
})
|
||||
}
|
||||
|
||||
func (self *SCloudprovider) savePassword(secret string) error {
|
||||
|
||||
@@ -71,8 +71,10 @@ func (self *SAliyunProviderFactory) ValidateUpdateCloudaccountCredential(ctx con
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SAliyunProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := aliyun.NewAliyunClient(providerId, providerName, account, secret, true)
|
||||
func (self *SAliyunProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := aliyun.NewAliyunClient(
|
||||
cfg.Id, cfg.Name, cfg.Account, cfg.Secret, true,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -75,8 +75,8 @@ func (self *SAwsProviderFactory) ValidateUpdateCloudaccountCredential(ctx contex
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SAwsProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := aws.NewAwsClient(providerId, providerName, url, account, secret, false)
|
||||
func (self *SAwsProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := aws.NewAwsClient(cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -111,9 +111,9 @@ func parseAccount(account, secret string) (tenantId string, appId string, appKey
|
||||
return
|
||||
}
|
||||
|
||||
func (self *SAzureProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
tenantId, appId, appKey, subId := parseAccount(account, secret)
|
||||
if client, err := azure.NewAzureClient(providerId, providerName, url, tenantId, appId, appKey, subId, false); err != nil {
|
||||
func (self *SAzureProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
tenantId, appId, appKey, subId := parseAccount(cfg.Account, cfg.Secret)
|
||||
if client, err := azure.NewAzureClient(cfg.Id, cfg.Name, cfg.URL, tenantId, appId, appKey, subId, false); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &SAzureProvider{
|
||||
|
||||
@@ -76,15 +76,16 @@ func (self *SCtyunProviderFactory) ValidateUpdateCloudaccountCredential(ctx cont
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SCtyunProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
segs := strings.Split(account, "/")
|
||||
func (self *SCtyunProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
segs := strings.Split(cfg.Account, "/")
|
||||
projectId := ""
|
||||
account := cfg.Account
|
||||
if len(segs) == 2 {
|
||||
projectId = segs[1]
|
||||
account = segs[0]
|
||||
}
|
||||
|
||||
client, err := ctyun.NewSCtyunClient(providerId, providerName, projectId, account, secret, false)
|
||||
client, err := ctyun.NewSCtyunClient(cfg.Id, cfg.Name, projectId, account, cfg.Secret, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -101,8 +101,8 @@ func parseHostPort(host string, defPort int) (string, int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SESXiProviderFactory) GetProvider(providerId, providerName, urlStr, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
parts, err := url.Parse(urlStr)
|
||||
func (self *SESXiProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
parts, err := url.Parse(cfg.URL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -111,7 +111,7 @@ func (self *SESXiProviderFactory) GetProvider(providerId, providerName, urlStr,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client, err := esxi.NewESXiClient(providerId, providerName, host, port, account, secret)
|
||||
client, err := esxi.NewESXiClient(cfg.Id, cfg.Name, host, port, cfg.Account, cfg.Secret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -101,21 +101,21 @@ func (self *SGoogleProviderFactory) ValidateUpdateCloudaccountCredential(ctx con
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SGoogleProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
func (self *SGoogleProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
privateKeyID, privateKey := "", ""
|
||||
privateKeyInfo := strings.Split(secret, "/")
|
||||
privateKeyInfo := strings.Split(cfg.Secret, "/")
|
||||
if len(privateKeyInfo) < 2 {
|
||||
return nil, fmt.Errorf("Missing privateKeyID or privateKey for google cloud")
|
||||
}
|
||||
privateKeyID = privateKeyInfo[0]
|
||||
privateKey = strings.Join(privateKeyInfo[1:], "/")
|
||||
projectID, clientEmail := "", ""
|
||||
accountInfo := strings.Split(account, "/")
|
||||
accountInfo := strings.Split(cfg.Account, "/")
|
||||
if len(accountInfo) < 2 {
|
||||
return nil, fmt.Errorf("Invalid projectID or client email for google cloud %s", account)
|
||||
return nil, fmt.Errorf("Invalid projectID or client email for google cloud %s", cfg.Account)
|
||||
}
|
||||
projectID, clientEmail = accountInfo[0], accountInfo[1]
|
||||
client, err := google.NewGoogleClient(providerId, providerName, projectID, clientEmail, privateKeyID, privateKey, false)
|
||||
client, err := google.NewGoogleClient(cfg.Id, cfg.Name, projectID, clientEmail, privateKeyID, privateKey, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -99,9 +99,9 @@ func parseAccount(account string) (accessKey string, projectId string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (self *SHuaweiProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
accessKey, projectId := parseAccount(account)
|
||||
client, err := huawei.NewHuaweiClient(providerId, providerName, url, accessKey, secret, projectId, false)
|
||||
func (self *SHuaweiProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
accessKey, projectId := parseAccount(cfg.Account)
|
||||
client, err := huawei.NewHuaweiClient(cfg.Id, cfg.Name, cfg.URL, accessKey, cfg.Secret, projectId, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ func (self *SCephRadosProviderFactory) GetName() string {
|
||||
return api.CLOUD_PROVIDER_CEPH
|
||||
}
|
||||
|
||||
func (self *SCephRadosProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := ceph.NewCephRados(providerId, providerName, url, account, secret, false)
|
||||
func (self *SCephRadosProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := ceph.NewCephRados(cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -71,8 +71,11 @@ func (self *SObjectStoreProviderFactory) ValidateUpdateCloudaccountCredential(ct
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SObjectStoreProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := objectstore.NewObjectStoreClient(providerId, providerName, url, account, secret, false)
|
||||
func (self *SObjectStoreProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
dbg := false
|
||||
client, err := objectstore.NewObjectStoreClient(
|
||||
cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, dbg,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ func (self *SXskyProviderFactory) GetName() string {
|
||||
return api.CLOUD_PROVIDER_XSKY
|
||||
}
|
||||
|
||||
func (self *SXskyProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := xsky.NewXskyClient(providerId, providerName, url, account, secret, false)
|
||||
func (self *SXskyProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := xsky.NewXskyClient(cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -102,16 +102,16 @@ func (self *SOpenStackProviderFactory) ValidateUpdateCloudaccountCredential(ctx
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SOpenStackProviderFactory) GetProvider(providerId, providerName, url, account, password string) (cloudprovider.ICloudProvider, error) {
|
||||
accountInfo := strings.Split(account, "/")
|
||||
func (self *SOpenStackProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
accountInfo := strings.Split(cfg.Account, "/")
|
||||
if len(accountInfo) < 2 {
|
||||
return nil, fmt.Errorf("Missing username or project name %s", account)
|
||||
return nil, fmt.Errorf("Missing username or project name %s", cfg.Account)
|
||||
}
|
||||
project, username, endpointType, domainName, projectDomainName := accountInfo[0], accountInfo[1], "internal", "Default", "Default"
|
||||
if len(accountInfo) == 3 {
|
||||
domainName, projectDomainName = accountInfo[2], accountInfo[2]
|
||||
}
|
||||
client, err := openstack.NewOpenStackClient(providerId, providerName, url, username, password, project, endpointType, domainName, projectDomainName, false)
|
||||
client, err := openstack.NewOpenStackClient(cfg.Id, cfg.Name, cfg.URL, username, cfg.Secret, project, endpointType, domainName, projectDomainName, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -98,14 +98,14 @@ func (self *SQcloudProviderFactory) ValidateUpdateCloudaccountCredential(ctx con
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SQcloudProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
secretId := account
|
||||
func (self *SQcloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
secretId := cfg.Account
|
||||
appId := ""
|
||||
if tmp := strings.Split(account, "/"); len(tmp) == 2 {
|
||||
if tmp := strings.Split(cfg.Account, "/"); len(tmp) == 2 {
|
||||
secretId = tmp[0]
|
||||
appId = tmp[1]
|
||||
}
|
||||
client, err := qcloud.NewQcloudClient(providerId, providerName, secretId, secret, appId, false)
|
||||
client, err := qcloud.NewQcloudClient(cfg.Id, cfg.Name, secretId, cfg.Secret, appId, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -82,9 +82,9 @@ func parseAccount(account string) (accessKey string, projectId string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (self *SUcloudProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) {
|
||||
accessKey, projectId := parseAccount(account)
|
||||
client, err := ucloud.NewUcloudClient(providerId, providerName, accessKey, secret, projectId, false)
|
||||
func (self *SUcloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
accessKey, projectId := parseAccount(cfg.Account)
|
||||
client, err := ucloud.NewUcloudClient(cfg.Id, cfg.Name, accessKey, cfg.Secret, projectId, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -76,8 +76,8 @@ func (self *SZStackProviderFactory) ValidateUpdateCloudaccountCredential(ctx con
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (self *SZStackProviderFactory) GetProvider(providerId, providerName, url, username, password string) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := zstack.NewZStackClient(providerId, providerName, url, username, password, false)
|
||||
func (self *SZStackProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
|
||||
client, err := zstack.NewZStackClient(cfg.Id, cfg.Name, cfg.URL, cfg.Account, cfg.Secret, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -136,5 +136,12 @@ func (provider *SCloudproviderDelegate) GetProvider() (cloudprovider.ICloudProvi
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cloudprovider.GetProvider(provider.Id, provider.Name, accessUrl, provider.Account, passwd, provider.Provider)
|
||||
return cloudprovider.GetProvider(cloudprovider.ProviderConfig{
|
||||
Id: provider.Id,
|
||||
Name: provider.Name,
|
||||
Vendor: provider.Provider,
|
||||
URL: accessUrl,
|
||||
Account: provider.Account,
|
||||
Secret: passwd,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user