Skip to content

Commit

Permalink
feat(region): Add 'EipAutoDellocate' option when creating vm
Browse files Browse the repository at this point in the history
This option specify whether the eip created with vm will be released when deleting vm.
  • Loading branch information
rainzm committed Apr 20, 2020
1 parent a0f1bd0 commit 7271544
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pkg/apis/compute/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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所在的区域创建
Expand Down
4 changes: 3 additions & 1 deletion pkg/compute/models/elasticips.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/tasks/guest_batch_create_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7271544

Please sign in to comment.