From 8433fc5ffbb1cb613af700a7110b0bf312331fa0 Mon Sep 17 00:00:00 2001 From: ioito Date: Mon, 27 Dec 2021 10:48:39 +0800 Subject: [PATCH] fix(region): add vpc host topology --- pkg/apis/compute/vpc.go | 14 ++++++++++++++ pkg/compute/models/vpcs.go | 22 ++++++++++++++++++++++ pkg/compute/models/wires.go | 13 +++++++++++++ 3 files changed, 49 insertions(+) diff --git a/pkg/apis/compute/vpc.go b/pkg/apis/compute/vpc.go index 0e8b14fbaa..351b1599c5 100644 --- a/pkg/apis/compute/vpc.go +++ b/pkg/apis/compute/vpc.go @@ -132,12 +132,26 @@ type NetworkTopologyOutput struct { Address []SNetworkUsedAddress `json:"address"` } +type HostnetworkTopologyOutput struct { + IpAddr string `json:"ip_addr"` + MacAddr string `json:"mac_addr"` +} + +type HostTopologyOutput struct { + Name string `json:"name"` + Status string `json:"status"` + HostStatus string `json:"host_status"` + HostType string `json:"host_type"` + Networks []HostnetworkTopologyOutput `json:"networks"` +} + type WireTopologyOutput struct { Name string `json:"name"` Status string `json:"status"` Bandwidth int `json:"bandwidth"` Zone string `json:"zone"` Networks []NetworkTopologyOutput `json:"networks"` + Hosts []HostTopologyOutput `json:"hosts"` } type VpcTopologyOutput struct { diff --git a/pkg/compute/models/vpcs.go b/pkg/compute/models/vpcs.go index f55a475fed..bae53092d6 100644 --- a/pkg/compute/models/vpcs.go +++ b/pkg/compute/models/vpcs.go @@ -1738,6 +1738,7 @@ func (self *SVpc) GetDetailsTopology(ctx context.Context, userCred mcclient.Toke Status: wires[i].Status, Bandwidth: wires[i].Bandwidth, Networks: []api.NetworkTopologyOutput{}, + Hosts: []api.HostTopologyOutput{}, } if len(wires[i].ZoneId) > 0 { zone, _ := wires[i].GetZone() @@ -1745,6 +1746,27 @@ func (self *SVpc) GetDetailsTopology(ctx context.Context, userCred mcclient.Toke wire.Zone = zone.Name } } + hosts, err := wires[i].GetHosts() + if err != nil { + return nil, errors.Wrapf(err, "GetHosts for wire %s", wires[i].Id) + } + for i := range hosts { + hns := hosts[i].GetBaremetalnetworks() + host := api.HostTopologyOutput{ + Name: hosts[i].Name, + Status: hosts[i].Status, + HostStatus: hosts[i].HostStatus, + HostType: hosts[i].HostType, + Networks: []api.HostnetworkTopologyOutput{}, + } + for j := range hns { + host.Networks = append(host.Networks, api.HostnetworkTopologyOutput{ + IpAddr: hns[j].IpAddr, + MacAddr: hns[j].MacAddr, + }) + } + wire.Hosts = append(wire.Hosts, host) + } networks, err := wires[i].GetNetworks(nil, rbacutils.ScopeSystem) if err != nil { return nil, errors.Wrapf(err, "GetNetworks") diff --git a/pkg/compute/models/wires.go b/pkg/compute/models/wires.go index 5777f21e63..3c2813bab9 100644 --- a/pkg/compute/models/wires.go +++ b/pkg/compute/models/wires.go @@ -251,6 +251,19 @@ func (wire *SWire) GetHostwires() ([]SHostwire, error) { return hostwires, nil } +func (self *SWire) GetHosts() ([]SHost, error) { + networks := NetworkManager.Query().SubQuery() + hns := HostnetworkManager.Query("baremetal_id") + sq := hns.Join(networks, sqlchemy.Equals(hns.Field("network_id"), networks.Field("id"))).SubQuery() + q := HostManager.Query().In("id", sq) + hosts := []SHost{} + err := db.FetchModelObjects(HostManager, q, &hosts) + if err != nil { + return nil, errors.Wrapf(err, "db.FetchModelObjects") + } + return hosts, nil +} + func (wire *SWire) NetworkCount() (int, error) { q := NetworkManager.Query().Equals("wire_id", wire.Id) return q.CountWithError()