Skip to content

Commit

Permalink
fix(region): sanitize eip charge type on all callsites
Browse files Browse the repository at this point in the history
  • Loading branch information
yousong committed Dec 9, 2020
1 parent f3fee11 commit 475842e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
21 changes: 15 additions & 6 deletions pkg/compute/models/elasticips.go
Expand Up @@ -800,14 +800,17 @@ func (manager *SElasticipManager) ValidateCreateData(ctx context.Context, userCr
}
return input, httperrors.NewResourceNotFoundError2("cloudregion", input.CloudregionId)
}
region := obj.(*SCloudregion)
var (
region = obj.(*SCloudregion)
regionDriver = region.GetDriver()
)
input.CloudregionId = region.GetId()

// publicIp cannot be created standalone
input.Mode = api.EIP_MODE_STANDALONE_EIP

if len(input.ChargeType) == 0 {
input.ChargeType = api.EIP_CHARGE_TYPE_DEFAULT
if input.ChargeType == "" {
input.ChargeType = regionDriver.GetEipDefaultChargeType()
}

if !utils.IsInStringArray(input.ChargeType, []string{api.EIP_CHARGE_TYPE_BY_BANDWIDTH, api.EIP_CHARGE_TYPE_BY_TRAFFIC}) {
Expand All @@ -819,7 +822,7 @@ func (manager *SElasticipManager) ValidateCreateData(ctx context.Context, userCr
return input, err
}

err = region.GetDriver().ValidateCreateEipData(ctx, userCred, &input)
err = regionDriver.ValidateCreateEipData(ctx, userCred, &input)
if err != nil {
return input, err
}
Expand Down Expand Up @@ -1219,9 +1222,13 @@ func (self *SElasticip) getMoreDetails(out api.ElasticipDetails) api.ElasticipDe
func (manager *SElasticipManager) NewEipForVMOnHost(ctx context.Context, userCred mcclient.TokenCredential, vm *SGuest,
host *SHost, bw int, chargeType string, autoDellocate bool, pendingUsage quotas.IQuota) (*SElasticip, error) {
region := host.GetRegion()
regionDriver := region.GetDriver()

if len(chargeType) == 0 {
chargeType = api.EIP_CHARGE_TYPE_BY_TRAFFIC
if chargeType == "" {
chargeType = regionDriver.GetEipDefaultChargeType()
}
if err := regionDriver.ValidateEipChargeType(chargeType); err != nil {
return nil, err
}

eip := &SElasticip{}
Expand All @@ -1239,7 +1246,9 @@ func (manager *SElasticipManager) NewEipForVMOnHost(ctx context.Context, userCre
eip.ManagerId = host.ManagerId
eip.CloudregionId = region.Id
eip.Name = fmt.Sprintf("eip-for-%s", vm.GetName())

if host.ManagerId == "" {

hostq := HostManager.Query().SubQuery()
wireq := WireManager.Query().SubQuery()
hostwireq := HostwireManager.Query().SubQuery()
Expand Down
35 changes: 13 additions & 22 deletions pkg/compute/models/guest_actions.go
Expand Up @@ -2998,10 +2998,18 @@ func (self *SGuest) AllowPerformCreateEip(ctx context.Context, userCred mcclient
}

func (self *SGuest) PerformCreateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
var bw int64
chargeType, _ := data.GetString("charge_type")
if len(chargeType) == 0 {
chargeType = api.EIP_CHARGE_TYPE_DEFAULT
var (
host = self.GetHost()
region = host.GetRegion()
regionDriver = region.GetDriver()

bw int64
chargeType string
autoDellocate bool
)
chargeType, _ = data.GetString("charge_type")
if chargeType == "" {
chargeType = regionDriver.GetEipDefaultChargeType()
}

bw, _ = data.Int("bandwidth")
Expand All @@ -3010,24 +3018,7 @@ func (self *SGuest) PerformCreateEip(ctx context.Context, userCred mcclient.Toke
return nil, httperrors.NewMissingParameterError("bandwidth")
}
}
autoDellocate, _ := data.Bool("auto_dellocate")

host := self.GetHost()
if host == nil {
return nil, httperrors.NewInvalidStatusError("No host???")
}
{
if self.ExternalId != "" {
_, err := host.GetDriver()
if err != nil {
return nil, httperrors.NewInvalidStatusError("No valid cloud provider")
}
}
region := host.GetRegion()
if region == nil {
return nil, httperrors.NewInvalidStatusError("No cloudregion???")
}
}
autoDellocate, _ = data.Bool("auto_dellocate")

err := self.GetDriver().ValidateCreateEip(ctx, userCred, data)
if err != nil {
Expand Down

0 comments on commit 475842e

Please sign in to comment.