diff --git a/pkg/cloudprovider/cloudprovider.go b/pkg/cloudprovider/cloudprovider.go index 96d2705c62..58ed5d20f8 100644 --- a/pkg/cloudprovider/cloudprovider.go +++ b/pkg/cloudprovider/cloudprovider.go @@ -10,6 +10,7 @@ import ( type ICloudProviderFactory interface { GetProvider(providerId, providerName, url, account, secret string) (ICloudProvider, error) GetId() string + ValidateChangeBandwidth(instanceId string, bandwidth int64) error } type ICloudProvider interface { @@ -40,15 +41,23 @@ func RegisterFactory(factory ICloudProviderFactory) { providerTable[factory.GetId()] = factory } -func GetProvider(providerId, providerName, accessUrl, account, secret, provider string) (ICloudProvider, error) { +func GetProviderDriver(provider string) (ICloudProviderFactory, error) { factory, ok := providerTable[provider] if ok { - return factory.GetProvider(providerId, providerName, accessUrl, account, secret) + return factory, nil } log.Errorf("Provider %s not registerd", provider) return nil, fmt.Errorf("No such provider %s", provider) } +func GetProvider(providerId, providerName, accessUrl, account, secret, provider string) (ICloudProvider, error) { + driver, err := GetProviderDriver(provider) + if err != nil { + return nil, err + } + return driver.GetProvider(providerId, providerName, accessUrl, account, secret) +} + func IsSupported(provider string) bool { _, ok := providerTable[provider] return ok diff --git a/pkg/compute/models/cloudproviders.go b/pkg/compute/models/cloudproviders.go index d3e9b0e5bf..a7b93a8ab7 100644 --- a/pkg/compute/models/cloudproviders.go +++ b/pkg/compute/models/cloudproviders.go @@ -364,6 +364,10 @@ func (self *SCloudprovider) MarkStartSync(userCred mcclient.TokenCredential) { self.SetStatus(userCred, CLOUD_PROVIDER_START_SYNC, "") } +func (self *SCloudprovider) GetProviderDriver() (cloudprovider.ICloudProviderFactory, error) { + return cloudprovider.GetProviderDriver(self.Provider) +} + func (self *SCloudprovider) GetDriver() (cloudprovider.ICloudProvider, error) { if !self.Enabled { return nil, fmt.Errorf("Cloud provider is not enabled") diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 43bcc29653..70f402d4c2 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -722,6 +722,13 @@ func (self *SElasticip) AllowPerformChangeBandwidth(ctx context.Context, userCre return self.IsOwner(userCred) } +func (self *SElasticip) GetProviderDriver() (cloudprovider.ICloudProviderFactory, error) { + if provider := self.GetCloudprovider(); provider != nil { + return provider.GetProviderDriver() + } + return nil, fmt.Errorf("failed to find provider for eip %s", self.Name) +} + func (self *SElasticip) PerformChangeBandwidth(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { if self.Status != EIP_STATUS_READY { return nil, httperrors.NewInvalidStatusError("cannot change bandwidth in status %s", self.Status) @@ -731,6 +738,16 @@ func (self *SElasticip) PerformChangeBandwidth(ctx context.Context, userCred mcc if err != nil || bandwidth <= 0 { return nil, httperrors.NewInputParameterError("Invalid bandwidth") } + + dirver, err := self.GetProviderDriver() + if err != nil { + return nil, err + } + + if err := dirver.ValidateChangeBandwidth(self.AssociateId, bandwidth); err != nil { + return nil, httperrors.NewInputParameterError(err.Error()) + } + err = self.StartEipChangeBandwidthTask(ctx, userCred, bandwidth) if err != nil { return nil, httperrors.NewGeneralError(err) diff --git a/pkg/util/aliyun/provider/provider.go b/pkg/util/aliyun/provider/provider.go index 58d7c89a7f..92518732bc 100644 --- a/pkg/util/aliyun/provider/provider.go +++ b/pkg/util/aliyun/provider/provider.go @@ -15,6 +15,10 @@ func (self *SAliyunProviderFactory) GetId() string { return aliyun.CLOUD_PROVIDER_ALIYUN } +func (self *SAliyunProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error { + return nil +} + func (self *SAliyunProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) { /* provider, ok := self.providerTable[providerId] if ok { diff --git a/pkg/util/azure/provider/provider.go b/pkg/util/azure/provider/provider.go index 38299343c1..ecb8fa2506 100644 --- a/pkg/util/azure/provider/provider.go +++ b/pkg/util/azure/provider/provider.go @@ -1,6 +1,8 @@ package provider import ( + "fmt" + "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/util/azure" @@ -14,6 +16,10 @@ func (self *SAzureProviderFactory) GetId() string { return azure.CLOUD_PROVIDER_AZURE } +func (self *SAzureProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error { + return fmt.Errorf("Changing %s bandwidth is not supported", azure.CLOUD_PROVIDER_AZURE) +} + func (self *SAzureProviderFactory) GetProvider(providerId, providerName, url, account, secret string) (cloudprovider.ICloudProvider, error) { if client, err := azure.NewAzureClient(providerId, providerName, account, secret, url); err != nil { return nil, err diff --git a/pkg/util/esxi/provider/provider.go b/pkg/util/esxi/provider/provider.go index 02c5434485..d8ceb43ad2 100644 --- a/pkg/util/esxi/provider/provider.go +++ b/pkg/util/esxi/provider/provider.go @@ -1,6 +1,7 @@ package provider import ( + "fmt" "net/url" "strconv" "strings" @@ -20,6 +21,10 @@ func (self *SESXiProviderFactory) GetId() string { return esxi.CLOUD_PROVIDER_VMWARE } +func (self *SESXiProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error { + return fmt.Errorf("Changing %s bandwidth is not supported", esxi.CLOUD_PROVIDER_VMWARE) +} + func parseHostPort(host string, defPort int) (string, int, error) { colonPos := strings.IndexByte(host, ':') if colonPos > 0 {