diff --git a/pkg/cloudprovider/cloudprovider.go b/pkg/cloudprovider/cloudprovider.go index eebed60b4a..814c678e27 100644 --- a/pkg/cloudprovider/cloudprovider.go +++ b/pkg/cloudprovider/cloudprovider.go @@ -4,6 +4,7 @@ import ( "fmt" "errors" + "yunion.io/x/jsonutils" "yunion.io/x/log" ) @@ -15,6 +16,7 @@ var ( type ICloudProviderFactory interface { GetProvider(providerId, providerName, url, account, secret string) (ICloudProvider, error) GetId() string + ValidateChangeBandwidth(instanceId string, bandwidth int64) error } type ICloudProvider interface { @@ -49,10 +51,10 @@ 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) @@ -67,6 +69,14 @@ func GetRegistedProviderIds() []string { return providers } +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 a595ffd276..049cab8bd3 100644 --- a/pkg/compute/models/cloudproviders.go +++ b/pkg/compute/models/cloudproviders.go @@ -404,6 +404,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 99a9f8ddcb..c87dbdc0b6 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -746,6 +746,13 @@ func (self *SElasticip) AllowPerformChangeBandwidth(ctx context.Context, userCre return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "change-bandwidth") } +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) @@ -755,6 +762,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 6b3d2efa99..2d5de59229 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 a4925b2395..a8a0784694 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 f294f864f5..25eb84f786 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" @@ -19,6 +20,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 {