mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(region): lb eip api fix
This commit is contained in:
@@ -228,7 +228,6 @@ func (man *SLoadbalancerManager) ListItemFilter(
|
||||
q.Join(nq, sqlchemy.Equals(nq.Field("id"), gnq.Field("network_id")))
|
||||
q.Join(wq, sqlchemy.Equals(wq.Field("id"), nq.Field("wire_id")))
|
||||
q.Join(vq, sqlchemy.Equals(vq.Field("id"), wq.Field("vpc_id")))
|
||||
q.Filter(sqlchemy.IsNullOrEmpty(gnq.Field("eip_id")))
|
||||
q.Filter(sqlchemy.NotEquals(vq.Field("id"), api.DEFAULT_VPC_ID))
|
||||
// vpc provider thing will be handled ok below
|
||||
}
|
||||
@@ -1563,7 +1562,11 @@ func (lb *SLoadbalancer) IsEipAssociable() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 绑定弹性公网IP, 仅支持kvm
|
||||
func (lb *SLoadbalancer) PerformAssociateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.LoadbalancerAssociateEipInput) (jsonutils.JSONObject, error) {
|
||||
if lb.IsManaged() {
|
||||
return nil, httperrors.NewUnsupportOperationError("not support managed lb")
|
||||
}
|
||||
err := lb.IsEipAssociable()
|
||||
if err != nil {
|
||||
return nil, httperrors.NewGeneralError(err)
|
||||
@@ -1630,10 +1633,24 @@ func (lb *SLoadbalancer) PerformAssociateEip(ctx context.Context, userCred mccli
|
||||
return nil, errors.Wrap(err, "AssociateLoadbalancer")
|
||||
}
|
||||
|
||||
_, err = db.Update(lb, func() error {
|
||||
lb.Address = eip.IpAddr
|
||||
lb.AddressType = api.LB_ADDR_TYPE_INTERNET
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "set loadbalancer address")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 解绑弹性公网IP,仅支持kvm
|
||||
func (lb *SLoadbalancer) PerformDissociateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.LoadbalancerDissociateEipInput) (jsonutils.JSONObject, error) {
|
||||
if lb.IsManaged() {
|
||||
return nil, httperrors.NewUnsupportOperationError("not support managed lb")
|
||||
}
|
||||
|
||||
eip, err := lb.GetEip()
|
||||
if err != nil {
|
||||
log.Errorf("Fail to get Eip %s", err)
|
||||
@@ -1648,6 +1665,18 @@ func (lb *SLoadbalancer) PerformDissociateEip(ctx context.Context, userCred mccl
|
||||
return nil, errors.Wrap(err, "eip is not accessible")
|
||||
}
|
||||
|
||||
lbnet, err := LoadbalancernetworkManager.FetchFirstByLbId(ctx, lb.Id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "LoadbalancernetworkManager.FetchFirstByLbId(%s)", lb.Id)
|
||||
}
|
||||
if _, err := db.Update(lb, func() error {
|
||||
lb.Address = lbnet.IpAddr
|
||||
lb.AddressType = api.LB_ADDR_TYPE_INTRANET
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, errors.Wrapf(err, "db.Update")
|
||||
}
|
||||
|
||||
autoDelete := (input.AudoDelete != nil && *input.AudoDelete)
|
||||
err = lb.DeleteEip(ctx, userCred, autoDelete)
|
||||
if err != nil {
|
||||
|
||||
@@ -797,6 +797,7 @@ func (self *SKVMRegionDriver) RequestCreateLoadbalancer(ctx context.Context, use
|
||||
return nil, errors.Wrapf(err, "db.Update")
|
||||
}
|
||||
// bind eip
|
||||
eipAddr := ""
|
||||
eipId, _ := task.GetParams().GetString("eip_id")
|
||||
eipBw, _ := task.GetParams().Int("eip_bw")
|
||||
if eipBw > 0 && len(eipId) == 0 {
|
||||
@@ -820,6 +821,7 @@ func (self *SKVMRegionDriver) RequestCreateLoadbalancer(ctx context.Context, use
|
||||
log.Errorf("NewEipForVMOnHost fail %s", err)
|
||||
quotas.CancelPendingUsage(ctx, userCred, eipPendingUsage, eipPendingUsage, false)
|
||||
} else {
|
||||
eipAddr = eip.IpAddr
|
||||
opts := api.ElasticipAssociateInput{
|
||||
InstanceId: lb.Id,
|
||||
InstanceExternalId: lb.ExternalId,
|
||||
@@ -841,8 +843,19 @@ func (self *SKVMRegionDriver) RequestCreateLoadbalancer(ctx context.Context, use
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "eip.AssociateLoadbalancer")
|
||||
}
|
||||
eipAddr = eip.IpAddr
|
||||
}
|
||||
|
||||
if len(eipAddr) > 0 {
|
||||
_, err = db.Update(lb, func() error {
|
||||
lb.Address = eipAddr
|
||||
lb.AddressType = api.LB_ADDR_TYPE_INTERNET
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "set loadbalancer address")
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user