diff --git a/pkg/apis/compute/api.go b/pkg/apis/compute/api.go index ebab70634a..bcb74c503d 100644 --- a/pkg/apis/compute/api.go +++ b/pkg/apis/compute/api.go @@ -445,9 +445,10 @@ type ServerCreateInput struct { // 指定此参数后会创建新的弹性公网IP并绑定到新建的虚拟机 // 私有云不支持此参数 EipBw int `json:"eip_bw,omitzero"` - // 弹性公网IP计费类型 EipChargeType string `json:"eip_charge_type,omitempty"` + // 是否跟随主机删除而自动释放 + EipAutoDellocate bool `json:"eip_auto_dellocate,omitempty"` // 弹性公网IP名称或ID // 绑定已有弹性公网IP, 此参数会限制虚拟机再谈下公网IP所在的区域创建 diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 38d060429c..1290ac5d04 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -1206,7 +1206,8 @@ func (self *SElasticip) getMoreDetails(out api.ElasticipDetails) api.ElasticipDe return out } -func (manager *SElasticipManager) NewEipForVMOnHost(ctx context.Context, userCred mcclient.TokenCredential, vm *SGuest, host *SHost, bw int, chargeType string, pendingUsage quotas.IQuota) (*SElasticip, error) { +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() if len(chargeType) == 0 { @@ -1221,6 +1222,7 @@ func (manager *SElasticipManager) NewEipForVMOnHost(ctx context.Context, userCre // eip.AutoDellocate = tristate.True eip.Bandwidth = bw eip.ChargeType = chargeType + eip.AutoDellocate = tristate.NewFromBool(autoDellocate) eip.DomainId = vm.DomainId eip.ProjectId = vm.ProjectId eip.ProjectSrc = string(apis.OWNER_SOURCE_LOCAL) diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 41268a8b65..60a0282b10 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -2932,6 +2932,7 @@ func (self *SGuest) PerformCreateEip(ctx context.Context, userCred mcclient.Toke return nil, httperrors.NewMissingParameterError("bandwidth") } } + autoDellocate, _ := data.Bool("auto_dellocate") if len(self.ExternalId) == 0 { return nil, httperrors.NewInvalidStatusError("Not a managed VM") @@ -2967,7 +2968,7 @@ func (self *SGuest) PerformCreateEip(ctx context.Context, userCred mcclient.Toke return nil, httperrors.NewOutOfQuotaError("Out of eip quota: %s", err) } - eip, err := ElasticipManager.NewEipForVMOnHost(ctx, userCred, self, host, int(bw), chargeType, eipPendingUsage) + eip, err := ElasticipManager.NewEipForVMOnHost(ctx, userCred, self, host, int(bw), chargeType, autoDellocate, eipPendingUsage) if err != nil { quotas.CancelPendingUsage(ctx, userCred, eipPendingUsage, eipPendingUsage, false) return nil, httperrors.NewGeneralError(err) diff --git a/pkg/compute/tasks/guest_batch_create_task.go b/pkg/compute/tasks/guest_batch_create_task.go index 62c599a642..a343670adf 100644 --- a/pkg/compute/tasks/guest_batch_create_task.go +++ b/pkg/compute/tasks/guest_batch_create_task.go @@ -178,7 +178,7 @@ func (self *GuestBatchCreateTask) allocateGuestOnHost(ctx context.Context, guest // allocate eips if input.EipBw > 0 { - eip, err := models.ElasticipManager.NewEipForVMOnHost(ctx, self.UserCred, guest, host, input.EipBw, input.EipChargeType, &pendingRegionUsage) + eip, err := models.ElasticipManager.NewEipForVMOnHost(ctx, self.UserCred, guest, host, input.EipBw, input.EipChargeType, input.EipAutoDellocate, &pendingRegionUsage) self.SetPendingUsage(&pendingRegionUsage, 1) if err != nil { log.Errorf("guest.CreateElasticipOnHost failed: %s", err)