diff --git a/pkg/compute/models/loadbalancerbackends.go b/pkg/compute/models/loadbalancerbackends.go index e275466bc9..488280fd6b 100644 --- a/pkg/compute/models/loadbalancerbackends.go +++ b/pkg/compute/models/loadbalancerbackends.go @@ -187,7 +187,8 @@ func (man *SLoadbalancerBackendManager) ValidateBackendVpc(lb *SLoadbalancer, gu return httperrors.NewBadRequestError("%s", err) } if len(lb.VpcId) > 0 { - if vpc.Id != lb.VpcId { + lbVpc := lb.GetVpc() + if lbVpc != nil && !lbVpc.IsEmulated && vpc.Id != lb.VpcId { return httperrors.NewBadRequestError("guest %s(%s) vpc %s(%s) not same as loadbalancer vpc %s", guest.Name, guest.Id, vpc.Name, vpc.Id, lb.VpcId) } return nil diff --git a/pkg/compute/models/loadbalancercachedcertificates.go b/pkg/compute/models/loadbalancercachedcertificates.go index 83dd6835d8..5ca4e5d6da 100644 --- a/pkg/compute/models/loadbalancercachedcertificates.go +++ b/pkg/compute/models/loadbalancercachedcertificates.go @@ -114,6 +114,10 @@ func (self *SCachedLoadbalancerCertificate) ValidateDeleteCondition(ctx context. return nil } +func (self *SCachedLoadbalancerCertificate) ValidatePurgeCondition(ctx context.Context) error { + return nil +} + func (self *SCachedLoadbalancerCertificate) Delete(ctx context.Context, userCred mcclient.TokenCredential) error { return nil } diff --git a/pkg/compute/models/purge.go b/pkg/compute/models/purge.go index 34c3689bd0..0d54e8f565 100644 --- a/pkg/compute/models/purge.go +++ b/pkg/compute/models/purge.go @@ -176,7 +176,7 @@ func (lbcert *SCachedLoadbalancerCertificate) purge(ctx context.Context, userCre lockman.LockObject(ctx, lbcert) defer lockman.ReleaseObject(ctx, lbcert) - err := lbcert.ValidateDeleteCondition(ctx) + err := lbcert.ValidatePurgeCondition(ctx) if err != nil { return err } diff --git a/pkg/compute/regiondrivers/aliyun.go b/pkg/compute/regiondrivers/aliyun.go index faa0641ded..da20fac620 100644 --- a/pkg/compute/regiondrivers/aliyun.go +++ b/pkg/compute/regiondrivers/aliyun.go @@ -1205,6 +1205,7 @@ func (self *SAliyunRegionDriver) ValidateCreateElasticcacheData(ctx context.Cont // validate sku billingType, _ := data.GetString("billing_type") zone := zoneV.Model.(*models.SZone) + network := networkV.Model.(*models.SNetwork) if sku, err := data.GetString("instance_type"); err != nil || len(sku) == 0 { return nil, httperrors.NewMissingParameterError("instance_type") } else { @@ -1214,7 +1215,7 @@ func (self *SAliyunRegionDriver) ValidateCreateElasticcacheData(ctx context.Cont } skuModel := _skuModel.(*models.SElasticcacheSku) - if err := ValidateElasticcacheSku(zone.Id, billingType, skuModel); err != nil { + if err := ValidateElasticcacheSku(zone.Id, billingType, skuModel, network); err != nil { return nil, err } else { data.Set("instance_type", jsonutils.NewString(skuModel.InstanceSpec)) @@ -1238,7 +1239,6 @@ func (self *SAliyunRegionDriver) ValidateCreateElasticcacheData(ctx context.Cont data.Set("billing_cycle", jsonutils.NewString(cycle.String())) } - network := networkV.Model.(*models.SNetwork) vpc := network.GetVpc() if vpc == nil { return nil, httperrors.NewNotFoundError("network %s related vpc not found", network.GetId()) @@ -1370,7 +1370,7 @@ func (self *SAliyunRegionDriver) ValidateCreateElasticcacheAccountData(ctx conte accountPrivilegeV.Value) } - return data, nil + return self.SManagedVirtualizationRegionDriver.ValidateCreateElasticcacheAccountData(ctx, userCred, ownerId, data) } func (self *SAliyunRegionDriver) RequestCreateElasticcacheAccount(ctx context.Context, userCred mcclient.TokenCredential, ea *models.SElasticcacheAccount, task taskman.ITask) error { diff --git a/pkg/compute/regiondrivers/huawei.go b/pkg/compute/regiondrivers/huawei.go index bbf425d35f..733641267c 100644 --- a/pkg/compute/regiondrivers/huawei.go +++ b/pkg/compute/regiondrivers/huawei.go @@ -2340,7 +2340,7 @@ func validatorSlaveZones(ownerId mcclient.IIdentityProvider, data *jsonutils.JSO skuModel := _skuModel.(*models.SElasticcacheSku) for _, zoneId := range zones { - if err := ValidateElasticcacheSku(zoneId, chargeType, skuModel); err != nil { + if err := ValidateElasticcacheSku(zoneId, chargeType, skuModel, nil); err != nil { return err } } @@ -2350,11 +2350,17 @@ func validatorSlaveZones(ownerId mcclient.IIdentityProvider, data *jsonutils.JSO return nil } -func ValidateElasticcacheSku(zoneId string, chargeType string, sku *models.SElasticcacheSku) error { +func ValidateElasticcacheSku(zoneId string, chargeType string, sku *models.SElasticcacheSku, network *models.SNetwork) error { if sku.ZoneId != zoneId { return httperrors.NewResourceNotFoundError("zone mismatch, elastic cache sku zone %s != %s", sku.ZoneId, zoneId) } + if network != nil { + if zone := network.GetZone(); zone != nil && zone.Id != sku.ZoneId { + return httperrors.NewResourceNotFoundError("elastic cache sku zone (%s) and subnet zone (%s) mismatch", sku.ZoneId, zone.Id) + } + } + if chargeType == billing_api.BILLING_TYPE_PREPAID { if sku.PrepaidStatus != api.SkuStatusAvailable { return httperrors.NewOutOfResourceError("sku %s is soldout", sku.Name) @@ -2409,8 +2415,8 @@ func (self *SHuaWeiRegionDriver) ValidateCreateElasticcacheData(ctx context.Cont sku := instanceTypeV.Model.(*models.SElasticcacheSku) zoneId, _ := data.GetString("zone_id") billingType, _ := data.GetString("billing_type") - - if err := ValidateElasticcacheSku(zoneId, billingType, sku); err != nil { + network := networkV.Model.(*models.SNetwork) + if err := ValidateElasticcacheSku(zoneId, billingType, sku, network); err != nil { return nil, err } else { data.Set("instance_type", jsonutils.NewString(sku.InstanceSpec)) @@ -2462,7 +2468,6 @@ func (self *SHuaWeiRegionDriver) ValidateCreateElasticcacheData(ctx context.Cont data.Set("billing_cycle", jsonutils.NewString(cycle.String())) } - network := networkV.Model.(*models.SNetwork) vpc := network.GetVpc() if vpc == nil { return nil, httperrors.NewNotFoundError("network %s related vpc not found", network.GetId()) diff --git a/pkg/compute/regiondrivers/managedvirtual.go b/pkg/compute/regiondrivers/managedvirtual.go index 66b5ca9545..fc76b988f0 100644 --- a/pkg/compute/regiondrivers/managedvirtual.go +++ b/pkg/compute/regiondrivers/managedvirtual.go @@ -2289,7 +2289,11 @@ func (self *SManagedVirtualizationRegionDriver) RequestElasticcacheUpdateBackupP } func (self *SManagedVirtualizationRegionDriver) ValidateCreateElasticcacheAccountData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { - return nil, nil + if name, _ := data.GetString("name"); name == "root" { + return nil, httperrors.NewConflictError("account name 'root' is not allowed") + } + + return data, nil } func (self *SManagedVirtualizationRegionDriver) ValidateCreateElasticcacheAclData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { diff --git a/pkg/compute/regiondrivers/qcloud.go b/pkg/compute/regiondrivers/qcloud.go index 36c7e6ec4b..bb50ce3f2c 100644 --- a/pkg/compute/regiondrivers/qcloud.go +++ b/pkg/compute/regiondrivers/qcloud.go @@ -1486,7 +1486,8 @@ func (self *SQcloudRegionDriver) ValidateCreateElasticcacheData(ctx context.Cont billingType, _ := data.GetString("billing_type") // validate sku sku := instanceTypeV.Model.(*models.SElasticcacheSku) - if err := ValidateElasticcacheSku(zoneId, billingType, sku); err != nil { + network := networkV.Model.(*models.SNetwork) + if err := ValidateElasticcacheSku(zoneId, billingType, sku, network); err != nil { return nil, err } else { data.Set("instance_type", jsonutils.NewString(sku.InstanceSpec)) @@ -1528,7 +1529,6 @@ func (self *SQcloudRegionDriver) ValidateCreateElasticcacheData(ctx context.Cont data.Set("billing_cycle", jsonutils.NewString(cycle.String())) } - network := networkV.Model.(*models.SNetwork) vpc := network.GetVpc() if vpc == nil { return nil, httperrors.NewNotFoundError("network %s related vpc not found", network.GetId()) @@ -1701,7 +1701,7 @@ func (self *SQcloudRegionDriver) ValidateCreateElasticcacheAccountData(ctx conte return nil, httperrors.NewWeakPasswordError() } - return data, nil + return self.SManagedVirtualizationRegionDriver.ValidateCreateElasticcacheAccountData(ctx, userCred, ownerId, data) } func (self *SQcloudRegionDriver) RequestCreateElasticcacheAccount(ctx context.Context, userCred mcclient.TokenCredential, ea *models.SElasticcacheAccount, task taskman.ITask) error { diff --git a/pkg/multicloud/aliyun/elasticcache_instance.go b/pkg/multicloud/aliyun/elasticcache_instance.go index 0b3c425852..7d1af2b347 100644 --- a/pkg/multicloud/aliyun/elasticcache_instance.go +++ b/pkg/multicloud/aliyun/elasticcache_instance.go @@ -882,7 +882,8 @@ func (self *SElasticcache) GetSecurityGroupIds() ([]string, error) { func (self *SElasticcache) GetICloudElasticcacheAccount(accountId string) (cloudprovider.ICloudElasticcacheAccount, error) { segs := strings.Split(accountId, "/") if len(segs) < 2 { - return nil, errors.Wrap(fmt.Errorf("%s", accountId), "elasticcache.GetICloudElasticcacheAccount invalid account id ") + log.Debugf("elasticcache.GetICloudElasticcacheAccount invalid account id %s", accountId) + return nil, errors.Wrap(cloudprovider.ErrNotFound, "invalid account id") } return self.GetICloudElasticcacheAccountByName(segs[1]) diff --git a/pkg/multicloud/aliyun/provider/provider.go b/pkg/multicloud/aliyun/provider/provider.go index e282701ad6..fd58e75dd0 100644 --- a/pkg/multicloud/aliyun/provider/provider.go +++ b/pkg/multicloud/aliyun/provider/provider.go @@ -16,6 +16,7 @@ package provider import ( "context" + "strings" "yunion.io/x/jsonutils" "yunion.io/x/pkg/errors" @@ -174,6 +175,33 @@ func (self *SAliyunProviderFactory) ValidateUpdateCloudaccountCredential(ctx con return output, nil } +func validateClientCloudenv(client *aliyun.SAliyunClient) error { + regions := client.GetIRegions() + if len(regions) == 0 { + return nil + } + + isFinanceAccount := false + for i := range regions { + if strings.Contains(regions[i].GetId(), "-finance") { + isFinanceAccount = true + break + } + } + + if isFinanceAccount { + if regions[0].GetCloudEnv() != "FinanceCloud" { + return errors.Wrap(httperrors.ErrInvalidCredential, "aksk is aliyun finance account") + } + } else { + if regions[0].GetCloudEnv() == "FinanceCloud" { + return errors.Wrap(httperrors.ErrInvalidCredential, "aksk is not aliyun finance account") + } + } + + return nil +} + func (self *SAliyunProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) { client, err := aliyun.NewAliyunClient( aliyun.NewAliyunClientConfig( @@ -185,6 +213,12 @@ func (self *SAliyunProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig if err != nil { return nil, err } + + err = validateClientCloudenv(client) + if err != nil { + return nil, errors.Wrap(err, "validateClientCloudenv") + } + return &SAliyunProvider{ SBaseProvider: cloudprovider.NewBaseProvider(self), client: client,