From 799ad29420d59216f75eafbc79d07cd6cc897b7f Mon Sep 17 00:00:00 2001 From: ioito Date: Mon, 10 Jun 2019 16:52:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0brand=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/apis/compute/cloudaccount_const.go | 2 ++ pkg/cloudprovider/cloudprovider.go | 5 +++++ pkg/compute/models/cloudaccounts.go | 10 ++++++++++ pkg/mcclient/options/cloudaccounts.go | 2 +- pkg/util/zstack/provider/provider.go | 4 ++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/apis/compute/cloudaccount_const.go b/pkg/apis/compute/cloudaccount_const.go index b45fa2552c..4442a73453 100644 --- a/pkg/apis/compute/cloudaccount_const.go +++ b/pkg/apis/compute/cloudaccount_const.go @@ -44,6 +44,8 @@ const ( CLOUD_PROVIDER_HEALTH_SUSPENDED = "suspended" // 远端处于冻结状态 CLOUD_PROVIDER_HEALTH_ARREARS = "arrears" // 远端处于欠费状态 CLOUD_PROVIDER_HEALTH_UNKNOWN = "unknown" // 未知状态,查询失败 + + ZSTACK_BRAND_DSTACK = "DStack" ) var ( diff --git a/pkg/cloudprovider/cloudprovider.go b/pkg/cloudprovider/cloudprovider.go index c0d7248a5d..ef5d4b57c9 100644 --- a/pkg/cloudprovider/cloudprovider.go +++ b/pkg/cloudprovider/cloudprovider.go @@ -44,6 +44,7 @@ type ICloudProviderFactory interface { ValidateChangeBandwidth(instanceId string, bandwidth int64) error ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) error ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, data jsonutils.JSONObject, cloudaccount string) (*SCloudaccount, error) + GetSupportedBrands() []string IsPublicCloud() bool IsOnPremise() bool @@ -179,6 +180,10 @@ func (factory *baseProviderFactory) ValidateChangeBandwidth(instanceId string, b return nil } +func (factory *baseProviderFactory) GetSupportedBrands() []string { + return []string{} +} + func (factory *baseProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) error { return httperrors.NewNotImplementedError("Not Implemented ValidateCreateCloudaccountData") } diff --git a/pkg/compute/models/cloudaccounts.go b/pkg/compute/models/cloudaccounts.go index b3235b1ca8..8c7b4309fb 100644 --- a/pkg/compute/models/cloudaccounts.go +++ b/pkg/compute/models/cloudaccounts.go @@ -256,6 +256,16 @@ func (manager *SCloudaccountManager) ValidateCreateData(ctx context.Context, use if err := providerDriver.ValidateCreateCloudaccountData(ctx, userCred, data); err != nil { return nil, err } + brand, _ := data.GetString("brand") + if len(brand) > 0 && brand != providerDriver.GetName() { + brands := providerDriver.GetSupportedBrands() + if !utils.IsInStringArray(providerDriver.GetName(), brands) { + brands = append(brands, providerDriver.GetName()) + } + if !utils.IsInStringArray(brand, brands) { + return nil, httperrors.NewUnsupportOperationError("Not support brand %s, only support %s", brand, brands) + } + } data.Set("is_public_cloud", jsonutils.NewBool(providerDriver.IsPublicCloud())) data.Set("is_on_premise", jsonutils.NewBool(providerDriver.IsOnPremise())) // check duplication diff --git a/pkg/mcclient/options/cloudaccounts.go b/pkg/mcclient/options/cloudaccounts.go index d04bfda9d1..8bb723b14f 100644 --- a/pkg/mcclient/options/cloudaccounts.go +++ b/pkg/mcclient/options/cloudaccounts.go @@ -75,7 +75,7 @@ type SCloudAccountCreateBaseOptions struct { Name string `help:"Name of cloud account" positional:"true"` // PROVIDER string `help:"Driver for cloud account" choices:"VMware|Aliyun|Azure|Qcloud|OpenStack|Huawei|Aws"` Desc string `help:"Description" token:"desc" json:"description"` - Brand string `help:"Brand of cloud account"` + Brand string `help:"Brand of cloud account" choices:"DStack"` AutoCreateProject bool `help:"Enable the account with same name project"` EnableAutoSync bool `help:"Enable automatically synchronize resources of this account"` diff --git a/pkg/util/zstack/provider/provider.go b/pkg/util/zstack/provider/provider.go index 3219d08e39..d22f81aeee 100644 --- a/pkg/util/zstack/provider/provider.go +++ b/pkg/util/zstack/provider/provider.go @@ -39,6 +39,10 @@ func (self *SZStackProviderFactory) GetName() string { return zstack.CLOUD_PROVIDER_ZSTACK } +func (self *SZStackProviderFactory) GetSupportedBrands() []string { + return []string{api.ZSTACK_BRAND_DSTACK} +} + func (self *SZStackProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict) error { username, _ := data.GetString("username") if len(username) == 0 {