diff --git a/pkg/vpcagent/models/models.go b/pkg/vpcagent/models/models.go index a9dd7218b6..bda505fdd8 100644 --- a/pkg/vpcagent/models/models.go +++ b/pkg/vpcagent/models/models.go @@ -21,6 +21,7 @@ import ( type Vpc struct { compute_models.SVpc + Wire *Wire `json:"-"` Networks Networks `json:"-"` } @@ -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, } } diff --git a/pkg/vpcagent/models/modelset.go b/pkg/vpcagent/models/modelset.go index ccb6cdc821..5e55c5c200 100644 --- a/pkg/vpcagent/models/modelset.go +++ b/pkg/vpcagent/models/modelset.go @@ -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" @@ -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 @@ -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 } @@ -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 @@ -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 } diff --git a/pkg/vpcagent/models/modelsets.go b/pkg/vpcagent/models/modelsets.go index 4dc3b2c327..fd37ed22d6 100644 --- a/pkg/vpcagent/models/modelsets.go +++ b/pkg/vpcagent/models/modelsets.go @@ -39,6 +39,7 @@ func init() { type ModelSetsMaxUpdatedAt struct { Vpcs time.Time + Wires time.Time Networks time.Time Guests time.Time Hosts time.Time @@ -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, @@ -63,6 +65,7 @@ func NewModelSetsMaxUpdatedAt() *ModelSetsMaxUpdatedAt { type ModelSets struct { Vpcs Vpcs + Wires Wires Networks Networks Guests Guests Hosts Hosts @@ -75,6 +78,7 @@ type ModelSets struct { func NewModelSets() *ModelSets { return &ModelSets{ Vpcs: Vpcs{}, + Wires: Wires{}, Networks: Networks{}, Guests: Guests{}, Hosts: Hosts{}, @@ -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, @@ -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), @@ -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))