Skip to content

Commit

Permalink
guests: support server-create-eip for onecloud guests and eip
Browse files Browse the repository at this point in the history
  • Loading branch information
yousong committed May 28, 2020
1 parent df4d80f commit 7e02808
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
3 changes: 3 additions & 0 deletions pkg/compute/guestdrivers/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ func (self *SKVMGuestDriver) IsSupportEip() bool {
return true
}

func (self *SKVMGuestDriver) ValidateCreateEip(ctx context.Context, userCred mcclient.TokenCredential, data jsonutils.JSONObject) error {
return nil
}
func (self *SKVMGuestDriver) RequestAssociateEip(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, eip *models.SElasticip, task taskman.ITask) error {
defer task.ScheduleRun(nil)

Expand Down
26 changes: 26 additions & 0 deletions pkg/compute/models/elasticips.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,32 @@ 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()
q := NetworkManager.Query()
q = q.Join(wireq, sqlchemy.Equals(wireq.Field("id"), q.Field("wire_id")))
q = q.Join(hostwireq, sqlchemy.Equals(hostwireq.Field("wire_id"), wireq.Field("id")))
q = q.Join(hostq, sqlchemy.Equals(hostq.Field("id"), host.Id))
q = q.Equals("server_type", api.NETWORK_TYPE_EIP)
var nets []SNetwork
if err := db.FetchModelObjects(NetworkManager, q, &nets); err != nil {
return nil, errors.Wrapf(err, "fetch eip networks usable in host %s(%s)",
host.Name, host.Id)
}
for i := range nets {
net := &nets[i]
cnt, err := net.GetFreeAddressCount()
if err != nil {
continue
}
if cnt > 0 {
eip.NetworkId = net.Id
break
}
}
}

var err error
eip.Name, err = db.GenerateName(manager, userCred, eip.Name)
Expand Down
25 changes: 12 additions & 13 deletions pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2932,25 +2932,24 @@ func (self *SGuest) PerformCreateEip(ctx context.Context, userCred mcclient.Toke
}
autoDellocate, _ := data.Bool("auto_dellocate")

if len(self.ExternalId) == 0 {
return nil, httperrors.NewInvalidStatusError("Not a managed VM")
}
host := self.GetHost()
if host == nil {
return nil, httperrors.NewInvalidStatusError("No host???")
}

_, 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???")
{
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???")
}
}

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

0 comments on commit 7e02808

Please sign in to comment.