optimized: create eip params

This commit is contained in:
Qu Xuan
2020-09-22 14:33:43 +08:00
parent b398456ee6
commit 17cf23b804
5 changed files with 61 additions and 76 deletions
+14 -15
View File
@@ -21,23 +21,22 @@ type SElasticipCreateInput struct {
// 区域名称或Id, 建议使用Id
// 在指定区域内创建弹性公网ip
Cloudregion string `json:"cloudregion"`
CloudregionId string `json:"cloudregion_id"`
// swagger:ignore
Cloudregion string `json:"cloudregion" yunion-deprecated-by:"cloudregion_id"`
// swagger:ignore
Region string `json:"region" yunion-deprecated-by:"cloudregion_id"`
// swagger:ignore
RegionId string `json:"region_id" yunion-deprecated-by:"cloudregion_id"`
// swagger:ignore
Region string
Cloudprovider string `json:"cloudprovider" yunion-deprecated-by:"manager_id"`
// swagger:ignore
RegionId string
// swagger:ignore
CloudregionId string
Manager string `json:"manager" yunion-deprecated-by:"manager_id"`
// 子订阅Id, 建议使用Id
// 使用指定子订阅创建弹性公网ip
// 弹性公网ip和虚拟机在同一区域同一子订阅底下,才可以进行绑定操作
Cloudprovider string `json:"cloudprovider"`
// swagger:ignore
Manager string
// swagger:ignore
ManagerId string
ManagerId string `json:"manager_id"`
// 计费类型: 流量或带宽
//
@@ -63,11 +62,11 @@ type SElasticipCreateInput struct {
Mode string `json:"mode"`
// 子网名称或Id
// 私有云创建此参数必传,例如Openstack, ZStack
Network string `json:"network"`
// swagger:ignore
NetworkId string
Network string `json:"network" yunion-deprecated-by:"network_id"`
// 子网Id
// 私有云创建此参数必传,例如Openstack, ZStack
NetworkId string `json:"network_id"`
}
type ElasticipDetails struct {
+30 -46
View File
@@ -798,51 +798,20 @@ func (manager *SElasticipManager) getEipByExtEip(ctx context.Context, userCred m
return manager.newFromCloudEip(ctx, userCred, extEip, provider, region, syncOwnerId)
}
func (manager *SElasticipManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.SElasticipCreateInput) (*jsonutils.JSONDict, error) {
var (
region *SCloudregion
provider *SCloudprovider
err error
)
for _, cloudregion := range []string{input.Cloudregion, input.Region, input.RegionId} {
if len(cloudregion) > 0 {
input.Cloudregion = cloudregion
break
}
func (manager *SElasticipManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.SElasticipCreateInput) (api.SElasticipCreateInput, error) {
if input.CloudregionId == "" {
input.CloudregionId = api.DEFAULT_REGION_ID
}
if input.Cloudregion == "" {
input.Cloudregion = api.DEFAULT_REGION_ID
}
if obj, err := CloudregionManager.FetchByIdOrName(nil, input.Cloudregion); err != nil {
obj, err := CloudregionManager.FetchByIdOrName(nil, input.CloudregionId)
if err != nil {
if err != sql.ErrNoRows {
return nil, httperrors.NewGeneralError(err)
} else {
return nil, httperrors.NewResourceNotFoundError("Region %s not found", input.Cloudregion)
return input, httperrors.NewGeneralError(err)
}
} else {
region = obj.(*SCloudregion)
return input, httperrors.NewResourceNotFoundError2("cloudregion", input.CloudregionId)
}
region := obj.(*SCloudregion)
input.CloudregionId = region.GetId()
for _, cloudprovider := range []string{input.Cloudprovider, input.Manager, input.ManagerId} {
if len(cloudprovider) > 0 {
input.Cloudprovider = cloudprovider
break
}
}
if input.Cloudprovider != "" {
providerObj, err := CloudproviderManager.FetchByIdOrName(nil, input.Cloudprovider)
if err != nil {
if err != sql.ErrNoRows {
return nil, httperrors.NewGeneralError(err)
} else {
return nil, httperrors.NewResourceNotFoundError("Cloud provider %s not found", input.Cloudprovider)
}
}
provider = providerObj.(*SCloudprovider)
input.ManagerId = provider.Id
}
// publicIp cannot be created standalone
input.Mode = api.EIP_MODE_STANDALONE_EIP
@@ -851,15 +820,30 @@ func (manager *SElasticipManager) ValidateCreateData(ctx context.Context, userCr
}
if !utils.IsInStringArray(input.ChargeType, []string{api.EIP_CHARGE_TYPE_BY_BANDWIDTH, api.EIP_CHARGE_TYPE_BY_TRAFFIC}) {
return nil, httperrors.NewInputParameterError("charge type %s not supported", input.ChargeType)
return input, httperrors.NewInputParameterError("charge type %s not supported", input.ChargeType)
}
if input.VirtualResourceCreateInput, err = manager.SVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.VirtualResourceCreateInput); err != nil {
return nil, err
input.VirtualResourceCreateInput, err = manager.SVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input.VirtualResourceCreateInput)
if err != nil {
return input, err
}
if err = region.GetDriver().ValidateCreateEipData(ctx, userCred, &input); err != nil {
return nil, err
err = region.GetDriver().ValidateCreateEipData(ctx, userCred, &input)
if err != nil {
return input, err
}
var provider *SCloudprovider = nil
if input.ManagerId != "" {
providerObj, err := CloudproviderManager.FetchByIdOrName(nil, input.ManagerId)
if err != nil {
if err != sql.ErrNoRows {
return input, httperrors.NewGeneralError(err)
}
return input, httperrors.NewResourceNotFoundError2("cloudprovider", input.ManagerId)
}
provider = providerObj.(*SCloudprovider)
input.ManagerId = provider.Id
}
//避免参数重名后还有pending.eip残留
@@ -867,10 +851,10 @@ func (manager *SElasticipManager) ValidateCreateData(ctx context.Context, userCr
quotaKeys := fetchRegionalQuotaKeys(rbacutils.ScopeProject, ownerId, region, provider)
eipPendingUsage.SetKeys(quotaKeys)
if err = quotas.CheckSetPendingQuota(ctx, userCred, eipPendingUsage); err != nil {
return nil, err
return input, err
}
return input.JSON(input), nil
return input, nil
}
func (eip *SElasticip) GetQuotaKeys() (quotas.IQuotaKeys, error) {
+5 -5
View File
@@ -898,13 +898,13 @@ func (self *SKVMRegionDriver) RequestDeleteVpc(ctx context.Context, userCred mcc
}
func (self *SKVMRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, input *api.SElasticipCreateInput) error {
if len(input.Network) == 0 {
return httperrors.NewMissingParameterError("network")
if len(input.NetworkId) == 0 {
return httperrors.NewMissingParameterError("network_id")
}
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.Network)
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.NetworkId)
if err != nil {
if err == sql.ErrNoRows {
return httperrors.NewResourceNotFoundError("failed to found network %s", input.Network)
return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
}
return httperrors.NewGeneralError(err)
}
@@ -922,7 +922,7 @@ func (self *SKVMRegionDriver) ValidateCreateEipData(ctx context.Context, userCre
if err != nil {
return err
}
if region.GetDriver().GetProvider() != self.GetProvider() {
if region.Id != input.CloudregionId {
return httperrors.NewUnsupportOperationError("network %s(%s) does not belong to %s", network.Name, network.Id, self.GetProvider())
}
return nil
+6 -5
View File
@@ -258,13 +258,13 @@ func (self *SOpenStackRegionDriver) ValidateCreateLoadbalancerCertificateData(ct
}
func (self *SOpenStackRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, input *api.SElasticipCreateInput) error {
if len(input.Network) == 0 {
return httperrors.NewMissingParameterError("network")
if len(input.NetworkId) == 0 {
return httperrors.NewMissingParameterError("network_id")
}
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.Network)
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.NetworkId)
if err != nil {
if err == sql.ErrNoRows {
return httperrors.NewResourceNotFoundError("failed to found network %s", input.Network)
return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
}
return httperrors.NewGeneralError(err)
}
@@ -275,11 +275,12 @@ func (self *SOpenStackRegionDriver) ValidateCreateEipData(ctx context.Context, u
if vpc == nil {
return httperrors.NewInputParameterError("failed to found vpc for network %s(%s)", network.Name, network.Id)
}
input.ManagerId = vpc.ManagerId
region, err := vpc.GetRegion()
if err != nil {
return err
}
if region.GetDriver().GetProvider() != self.GetProvider() {
if region.Id != input.CloudregionId {
return httperrors.NewUnsupportOperationError("network %s(%s) does not belong to %s", network.Name, network.Id, self.GetProvider())
}
return nil
+6 -5
View File
@@ -78,13 +78,13 @@ func (self *SZStackRegionDriver) ValidateCreateLoadbalancerCertificateData(ctx c
}
func (self *SZStackRegionDriver) ValidateCreateEipData(ctx context.Context, userCred mcclient.TokenCredential, input *api.SElasticipCreateInput) error {
if len(input.Network) == 0 {
return httperrors.NewMissingParameterError("network")
if len(input.NetworkId) == 0 {
return httperrors.NewMissingParameterError("network_id")
}
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.Network)
_network, err := models.NetworkManager.FetchByIdOrName(userCred, input.NetworkId)
if err != nil {
if err == sql.ErrNoRows {
return httperrors.NewResourceNotFoundError("failed to found network %s", input.Network)
return httperrors.NewResourceNotFoundError2("network", input.NetworkId)
}
return httperrors.NewGeneralError(err)
}
@@ -95,11 +95,12 @@ func (self *SZStackRegionDriver) ValidateCreateEipData(ctx context.Context, user
if vpc == nil {
return httperrors.NewInputParameterError("failed to found vpc for network %s(%s)", network.Name, network.Id)
}
input.ManagerId = vpc.ManagerId
region, err := vpc.GetRegion()
if err != nil {
return err
}
if region.GetDriver().GetProvider() != self.GetProvider() {
if region.Id != input.CloudregionId {
return httperrors.NewUnsupportOperationError("network %s(%s) does not belong to %s", network.Name, network.Id, self.GetProvider())
}
return nil