From 4f5e5bac442a6be342628c5e1b26938a3b67f46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Sat, 8 Dec 2018 17:24:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=AA=8C=E8=AF=81eip=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E5=B8=A6=E5=AE=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cloudprovider/cloudprovider.go | 10 ++++++++++ pkg/compute/models/cloudproviders.go | 4 ++++ pkg/compute/models/elasticips.go | 17 +++++++++++++++++ pkg/util/aliyun/provider/provider.go | 4 ++++ pkg/util/azure/provider/provider.go | 6 ++++++ pkg/util/esxi/provider/provider.go | 5 +++++ 6 files changed, 46 insertions(+) diff --git a/pkg/cloudprovider/cloudprovider.go b/pkg/cloudprovider/cloudprovider.go index 96d2705c62..6c50eb6cb3 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,6 +41,15 @@ func RegisterFactory(factory ICloudProviderFactory) { providerTable[factory.GetId()] = factory } +func GetProviderDriver(provider string) (ICloudProviderFactory, error) { + factory, ok := providerTable[provider] + if ok { + 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) { factory, ok := providerTable[provider] if 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..939ea8cab6 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("Not support change bandwidth for %s", 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..58cca865a5 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("Not support change bandwidth for %s", esxi.CLOUD_PROVIDER_VMWARE) +} + func parseHostPort(host string, defPort int) (string, int, error) { colonPos := strings.IndexByte(host, ':') if colonPos > 0 { From c7166bb713fc5580ea45e6053901c55ff1a83304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Sat, 8 Dec 2018 17:24:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=AA=8C=E8=AF=81eip=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E5=B8=A6=E5=AE=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cloudprovider/cloudprovider.go | 13 +++++++++++-- pkg/compute/models/cloudproviders.go | 4 ++++ pkg/compute/models/elasticips.go | 17 +++++++++++++++++ pkg/util/aliyun/provider/provider.go | 4 ++++ pkg/util/azure/provider/provider.go | 6 ++++++ pkg/util/esxi/provider/provider.go | 5 +++++ 6 files changed, 47 insertions(+), 2 deletions(-) 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 {