Skip to content

Commit

Permalink
guests: include vpc, network info in guest nics list
Browse files Browse the repository at this point in the history
  • Loading branch information
yousong committed May 20, 2020
1 parent d8d4c6f commit b29c4b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pkg/apis/compute/guestnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ type GuestnetworkShortDesc struct {
// IP地址
IpAddr string `json:"ip_addr"`
// 是否为外网网卡
// Deprecated
IsExit bool `json:"is_exit"`
// IPv6地址
Ip6Addr string `json:"ip6_addr"`
// Mac地址
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 {
Expand Down
43 changes: 32 additions & 11 deletions pkg/compute/models/guest_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b29c4b3

Please sign in to comment.