Skip to content

Commit

Permalink
vpcagent: models: include wires
Browse files Browse the repository at this point in the history
  • Loading branch information
yousong committed May 21, 2020
1 parent 5fdd534 commit 3a51985
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
17 changes: 14 additions & 3 deletions pkg/vpcagent/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type Vpc struct {
compute_models.SVpc

Wire *Wire `json:"-"`
Networks Networks `json:"-"`
}

Expand All @@ -30,19 +31,29 @@ func (el *Vpc) Copy() *Vpc {
}
}

type Wire struct {
compute_models.SWire

Vpc *Vpc
}

func (el *Wire) Copy() *Wire {
return &Wire{
SWire: el.SWire,
}
}

type Network struct {
compute_models.SNetwork
// returned as extra column
VpcId string

Vpc *Vpc `json:"-"`
Wire *Wire `json:"-"`
Guestnetworks Guestnetworks `json:"-"`
}

func (el *Network) Copy() *Network {
return &Network{
SNetwork: el.SNetwork,
VpcId: el.VpcId,
}
}

Expand Down
72 changes: 66 additions & 6 deletions pkg/vpcagent/models/modelset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package models
import (
"yunion.io/x/log"

"yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
mcclient_modulebase "yunion.io/x/onecloud/pkg/mcclient/modulebase"
mcclient_modules "yunion.io/x/onecloud/pkg/mcclient/modules"
Expand All @@ -26,6 +25,7 @@ import (

type (
Vpcs map[string]*Vpc
Wires map[string]*Wire
Networks map[string]*Network
Guests map[string]*Guest
Hosts map[string]*Host
Expand All @@ -46,9 +46,6 @@ func (set Vpcs) NewModel() db.IModel {

func (set Vpcs) AddModel(i db.IModel) {
m := i.(*Vpc)
if m.Id == compute.DEFAULT_VPC_ID {
return
}
set[m.Id] = m
}

Expand All @@ -68,16 +65,36 @@ func (set Vpcs) IncludeEmulated() bool {
return false
}

func (ms Vpcs) joinWires(subEntries Wires) bool {
correct := true
for _, subEntry := range subEntries {
vpcId := subEntry.VpcId
m, ok := ms[vpcId]
if !ok {
log.Warningf("vpc_id %s of wire %s(%s) is not present", vpcId, subEntry.Name, subEntry.Id)
correct = false
continue
}
subEntry.Vpc = m
m.Wire = subEntry
}
return correct
}

func (ms Vpcs) joinNetworks(subEntries Networks) bool {
for _, m := range ms {
m.Networks = Networks{}
}
correct := true
for subId, subEntry := range subEntries {
id := subEntry.VpcId
if id == compute.DEFAULT_VPC_ID {
wire := subEntry.Wire
if wire == nil {
// ensured by vpcs.joinWires
log.Warningf("network %s(%s) has no wire", subEntry.Name, subEntry.Id)
correct = false
continue
}
id := wire.VpcId
m, ok := ms[id]
if !ok {
// let it go. By the time the subnet has externalId or
Expand All @@ -99,6 +116,49 @@ func (ms Vpcs) joinNetworks(subEntries Networks) bool {
return correct
}

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

func (set Wires) NewModel() db.IModel {
return &Wire{}
}

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

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

func (set Wires) IncludeDetails() bool {
return false
}

func (set Wires) IncludeEmulated() bool {
return true
}

func (ms Wires) joinNetworks(subEntries Networks) bool {
correct := true
for _, subEntry := range subEntries {
wireId := subEntry.WireId
m, ok := ms[wireId]
if !ok {
correct = false
continue
}
subEntry.Wire = m
}
return correct
}

func (set Guests) ModelManager() mcclient_modulebase.IBaseManager {
return &mcclient_modules.Servers
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/vpcagent/models/modelsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {

type ModelSetsMaxUpdatedAt struct {
Vpcs time.Time
Wires time.Time
Networks time.Time
Guests time.Time
Hosts time.Time
Expand All @@ -51,6 +52,7 @@ type ModelSetsMaxUpdatedAt struct {
func NewModelSetsMaxUpdatedAt() *ModelSetsMaxUpdatedAt {
return &ModelSetsMaxUpdatedAt{
Vpcs: apihelper.PseudoZeroTime,
Wires: apihelper.PseudoZeroTime,
Networks: apihelper.PseudoZeroTime,
Guests: apihelper.PseudoZeroTime,
Hosts: apihelper.PseudoZeroTime,
Expand All @@ -63,6 +65,7 @@ func NewModelSetsMaxUpdatedAt() *ModelSetsMaxUpdatedAt {

type ModelSets struct {
Vpcs Vpcs
Wires Wires
Networks Networks
Guests Guests
Hosts Hosts
Expand All @@ -75,6 +78,7 @@ type ModelSets struct {
func NewModelSets() *ModelSets {
return &ModelSets{
Vpcs: Vpcs{},
Wires: Wires{},
Networks: Networks{},
Guests: Guests{},
Hosts: Hosts{},
Expand All @@ -89,6 +93,7 @@ func (mss *ModelSets) ModelSetList() []apihelper.IModelSet {
// it's ordered this way to favour creation, not deletion
return []apihelper.IModelSet{
mss.Vpcs,
mss.Wires,
mss.Networks,
mss.Guests,
mss.Hosts,
Expand All @@ -106,6 +111,7 @@ func (mss *ModelSets) NewEmpty() apihelper.IModelSets {
func (mss *ModelSets) Copy() apihelper.IModelSets {
mssCopy := &ModelSets{
Vpcs: mss.Vpcs.Copy().(Vpcs),
Wires: mss.Wires.Copy().(Wires),
Networks: mss.Networks.Copy().(Networks),
Guests: mss.Guests.Copy().(Guests),
Hosts: mss.Hosts.Copy().(Hosts),
Expand Down Expand Up @@ -141,6 +147,8 @@ func (mss *ModelSets) ApplyUpdates(mssNews apihelper.IModelSets) apihelper.Model
func (mss *ModelSets) join() bool {
mss.Guests.initJoin()
var p []bool
p = append(p, mss.Vpcs.joinWires(mss.Wires))
p = append(p, mss.Wires.joinNetworks(mss.Networks))
p = append(p, mss.Vpcs.joinNetworks(mss.Networks))
p = append(p, mss.Networks.joinGuestnetworks(mss.Guestnetworks))
p = append(p, mss.Guests.joinHosts(mss.Hosts))
Expand Down

0 comments on commit 3a51985

Please sign in to comment.