guests: include vpc, network info in guest nics list

This commit is contained in:
Yousong Zhou
2020-05-20 22:45:16 +08:00
parent d8d4c6f24e
commit b29c4b3869
2 changed files with 37 additions and 11 deletions
+5
View File
@@ -27,6 +27,7 @@ type GuestnetworkShortDesc struct {
// IP地址
IpAddr string `json:"ip_addr"`
// 是否为外网网卡
// Deprecated
IsExit bool `json:"is_exit"`
// IPv6地址
Ip6Addr string `json:"ip6_addr"`
@@ -34,6 +35,10 @@ type GuestnetworkShortDesc struct {
Mac string `json:"mac"`
// Bonding的配对网卡MAC
TeamWith string `json:"team_with"`
// 所属Vpc
VpcId string `json:"vpc_id"`
// 所属Network
NetworkId string `json:"network_id"`
}
type GuestnetworkListInput struct {
+32 -11
View File
@@ -265,20 +265,41 @@ func fetchGuestIPs(guestIds []string, virtual tristate.TriState) map[string][]st
}
func fetchGuestNICs(ctx context.Context, guestIds []string, virtual tristate.TriState) map[string][]api.GuestnetworkShortDesc {
q := GuestnetworkManager.Query().In("guest_id", guestIds)
nics := make([]SGuestnetwork, 0)
if err := q.All(&nics); err != nil {
netq := NetworkManager.Query().SubQuery()
wirq := WireManager.Query().SubQuery()
gnwq := GuestnetworkManager.Query()
q := gnwq.AppendField(
gnwq.Field("guest_id"),
gnwq.Field("ip_addr"),
gnwq.Field("ip6_addr"),
gnwq.Field("mac_addr").Label("mac"),
gnwq.Field("team_with"),
gnwq.Field("network_id"), // caution: do not alias netq.id as network_id
wirq.Field("vpc_id"),
)
q = q.Join(netq, sqlchemy.Equals(netq.Field("id"), gnwq.Field("network_id")))
q = q.Join(wirq, sqlchemy.Equals(wirq.Field("id"), netq.Field("wire_id")))
q = q.In("guest_id", guestIds)
var descs []struct {
GuestId string `json:"guest_id"`
api.GuestnetworkShortDesc
}
if err := q.All(&descs); err != nil {
if err != sql.ErrNoRows {
log.Errorf("query guest nics info: %v", err)
}
return nil
}
ret := make(map[string][]api.GuestnetworkShortDesc)
for i := range nics {
desc := api.GuestnetworkShortDesc{}
jsonDesc := nics[i].GetShortDesc(ctx)
jsonDesc.Unmarshal(&desc)
if _, ok := ret[nics[i].GuestId]; !ok {
ret[nics[i].GuestId] = []api.GuestnetworkShortDesc{desc}
ret := map[string][]api.GuestnetworkShortDesc{}
for i := range descs {
desc := &descs[i]
guestId := desc.GuestId
if _, ok := ret[guestId]; !ok {
ret[guestId] = []api.GuestnetworkShortDesc{desc.GuestnetworkShortDesc}
} else {
ret[nics[i].GuestId] = append(ret[nics[i].GuestId], desc)
ret[guestId] = append(ret[guestId], desc.GuestnetworkShortDesc)
}
}
return ret