Skip to content

Commit

Permalink
vpcagent: models: include guest, host
Browse files Browse the repository at this point in the history
  • Loading branch information
yousong committed Apr 24, 2020
1 parent ff26866 commit e4ad3f4
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
23 changes: 23 additions & 0 deletions pkg/vpcagent/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (el *Network) Copy() *Network {
type Guestnetwork struct {
compute_models.SGuestnetwork

Guest *Guest `json:"-"`
Network *Network `json:"-"`
}

Expand All @@ -57,3 +58,25 @@ func (el *Guestnetwork) Copy() *Guestnetwork {
SGuestnetwork: el.SGuestnetwork,
}
}

type Guest struct {
compute_models.SGuest

Host *Host `json:"-"`
}

func (el *Guest) Copy() *Guest {
return &Guest{
SGuest: el.SGuest,
}
}

type Host struct {
compute_models.SHost
}

func (el *Host) Copy() *Host {
return &Host{
SHost: el.SHost,
}
}
79 changes: 79 additions & 0 deletions pkg/vpcagent/models/modelset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

type Vpcs map[string]*Vpc
type Networks map[string]*Network
type Guests map[string]*Guest
type Hosts map[string]*Host // host-vpc as key
type Guestnetworks map[string]*Guestnetwork // guestId as key

func (set Vpcs) ModelManager() mcclient_modulebase.IBaseManager {
Expand Down Expand Up @@ -80,6 +82,67 @@ func (ms Vpcs) joinNetworks(subEntries Networks) bool {
return correct
}

func (set Guests) ModelManager() mcclient_modulebase.IBaseManager {
return &mcclient_modules.Servers
}

func (set Guests) NewModel() db.IModel {
return &Guest{}
}

func (set Guests) AddModel(i db.IModel) {
m := i.(*Guest)
set[m.Id] = m
}

func (set Guests) Copy() apihelper.IModelSet {
setCopy := Guests{}
for id, el := range set {
setCopy[id] = el.Copy()
}
return setCopy
}

func (set Guests) joinHosts(subEntries Hosts) bool {
correct := true
for gId, g := range set {
hId := g.HostId
if hId == "" {
continue
}
h, ok := subEntries[hId]
if !ok {
log.Warningf("guest %s(%s): host id %s not found",
gId, g.Name, hId)
correct = false
continue
}
g.Host = h
}
return correct
}

func (set Hosts) ModelManager() mcclient_modulebase.IBaseManager {
return &mcclient_modules.Hosts
}

func (set Hosts) NewModel() db.IModel {
return &Host{}
}

func (set Hosts) AddModel(i db.IModel) {
m := i.(*Host)
set[m.Id] = m
}

func (set Hosts) Copy() apihelper.IModelSet {
setCopy := Hosts{}
for id, el := range set {
setCopy[id] = el.Copy()
}
return setCopy
}

func (set Networks) ModelManager() mcclient_modulebase.IBaseManager {
return &mcclient_modules.Networks
}
Expand Down Expand Up @@ -146,3 +209,19 @@ func (set Guestnetworks) Copy() apihelper.IModelSet {
}
return setCopy
}

func (set Guestnetworks) joinGuests(subEntries Guests) bool {
correct := true
for _, gn := range set {
gId := gn.GuestId
g, ok := subEntries[gId]
if !ok {
log.Warningf("guestnetwork %d(%s,%s) guest id %s not found",
gn.Index, gn.NetworkId, gn.IpAddr, gId)
correct = false
continue
}
gn.Guest = g
}
return correct
}
26 changes: 23 additions & 3 deletions pkg/vpcagent/models/modelsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,35 @@ func init() {
type ModelSetsMaxUpdatedAt struct {
Vpcs time.Time
Networks time.Time
Guests time.Time
Hosts time.Time
Guestnetworks time.Time
}

func NewModelSetsMaxUpdatedAt() *ModelSetsMaxUpdatedAt {
return &ModelSetsMaxUpdatedAt{
Vpcs: apihelper.PseudoZeroTime,
Networks: apihelper.PseudoZeroTime,
Guests: apihelper.PseudoZeroTime,
Hosts: apihelper.PseudoZeroTime,
Guestnetworks: apihelper.PseudoZeroTime,
}
}

type ModelSets struct {
Vpcs Vpcs
Networks Networks
Guests Guests
Hosts Hosts
Guestnetworks Guestnetworks
}

func NewModelSets() *ModelSets {
return &ModelSets{
Vpcs: Vpcs{},
Networks: Networks{},
Guests: Guests{},
Hosts: Hosts{},
Guestnetworks: Guestnetworks{},
}
}
Expand All @@ -70,6 +78,8 @@ func (mss *ModelSets) ModelSetList() []apihelper.IModelSet {
return []apihelper.IModelSet{
mss.Vpcs,
mss.Networks,
mss.Guests,
mss.Hosts,
mss.Guestnetworks,
}
}
Expand All @@ -82,6 +92,8 @@ func (mss *ModelSets) Copy() apihelper.IModelSets {
mssCopy := &ModelSets{
Vpcs: mss.Vpcs.Copy().(Vpcs),
Networks: mss.Networks.Copy().(Networks),
Guests: mss.Guests.Copy().(Guests),
Hosts: mss.Hosts.Copy().(Hosts),
Guestnetworks: mss.Guestnetworks.Copy().(Guestnetworks),
}
mssCopy.join()
Expand Down Expand Up @@ -109,7 +121,15 @@ func (mss *ModelSets) ApplyUpdates(mssNews apihelper.IModelSets) apihelper.Model
}

func (mss *ModelSets) join() bool {
correct0 := mss.Vpcs.joinNetworks(mss.Networks)
correct1 := mss.Networks.joinGuestnetworks(mss.Guestnetworks)
return correct0 && correct1
var p []bool
p = append(p, mss.Vpcs.joinNetworks(mss.Networks))
p = append(p, mss.Networks.joinGuestnetworks(mss.Guestnetworks))
p = append(p, mss.Guests.joinHosts(mss.Hosts))
p = append(p, mss.Guestnetworks.joinGuests(mss.Guests))
for _, b := range p {
if !b {
return false
}
}
return true
}

0 comments on commit e4ad3f4

Please sign in to comment.