mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(region): return error for query guest network (#19886)
This commit is contained in:
@@ -88,7 +88,10 @@ func (drv *SManagedVirtualizedGuestDriver) GetJsonDescAtHost(ctx context.Context
|
||||
|
||||
nics, _ := guest.GetNetworks("")
|
||||
if len(nics) > 0 {
|
||||
net := nics[0].GetNetwork()
|
||||
net, err := nics[0].GetNetwork()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
config.ExternalNetworkId = net.ExternalId
|
||||
vpc, err := net.GetVpc()
|
||||
if err == nil {
|
||||
|
||||
@@ -1505,7 +1505,7 @@ func (self *SGuest) fixFakeServerInfo(ctx context.Context, userCred mcclient.Tok
|
||||
if len(networks[i].IpAddr) > 0 {
|
||||
continue
|
||||
}
|
||||
network := networks[i].GetNetwork()
|
||||
network, _ := networks[i].GetNetwork()
|
||||
if network != nil {
|
||||
db.Update(&networks[i], func() error {
|
||||
networks[i].IpAddr, _ = network.GetFreeIP(ctx, userCred, nil, nil, "", api.IPAllocationRandom, false, api.AddressTypeIPv4)
|
||||
@@ -2469,7 +2469,10 @@ func (self *SGuest) PerformChangeIpaddr(
|
||||
taskData.Set("restart_network", jsonutils.JSONTrue)
|
||||
taskData.Set("prev_ip", jsonutils.NewString(gn.IpAddr))
|
||||
taskData.Set("prev_mac", jsonutils.NewString(newMacAddr))
|
||||
net := ngn.GetNetwork()
|
||||
net, err := ngn.GetNetwork()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
taskData.Set("is_vpc_network", jsonutils.NewBool(net.isOneCloudVpcNetwork()))
|
||||
taskData.Set("ip_mask", jsonutils.NewString(ipMask))
|
||||
taskData.Set("gateway", jsonutils.NewString(newGateway))
|
||||
@@ -2588,10 +2591,12 @@ func (guest *SGuest) fixDefaultGateway(ctx context.Context, userCred mcclient.To
|
||||
nicList := netutils2.SNicInfoList{}
|
||||
nics, _ := guest.GetNetworks("")
|
||||
for i := range nics {
|
||||
net := nics[i].GetNetwork()
|
||||
nicList = nicList.Add(nics[i].IpAddr, nics[i].MacAddr, net.GuestGateway)
|
||||
if nics[i].IsDefault {
|
||||
defaultGwCnt++
|
||||
net, _ := nics[i].GetNetwork()
|
||||
if net != nil {
|
||||
nicList = nicList.Add(nics[i].IpAddr, nics[i].MacAddr, net.GuestGateway)
|
||||
if nics[i].IsDefault {
|
||||
defaultGwCnt++
|
||||
}
|
||||
}
|
||||
}
|
||||
if defaultGwCnt != 1 {
|
||||
|
||||
@@ -137,7 +137,7 @@ func (guest *SGuest) sshableTryEach(
|
||||
var gnInfos []gnInfo
|
||||
for i := range gns {
|
||||
gn := &gns[i]
|
||||
network := gn.GetNetwork()
|
||||
network, _ := gn.GetNetwork()
|
||||
if network == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -505,12 +505,12 @@ func (gn *SGuestnetwork) GetGuest() *SGuest {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gn *SGuestnetwork) GetNetwork() *SNetwork {
|
||||
net, _ := NetworkManager.FetchById(gn.NetworkId)
|
||||
if net != nil {
|
||||
return net.(*SNetwork)
|
||||
func (gn *SGuestnetwork) GetNetwork() (*SNetwork, error) {
|
||||
net, err := NetworkManager.FetchById(gn.NetworkId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "FetchById %s", gn.NetworkId)
|
||||
}
|
||||
return nil
|
||||
return net.(*SNetwork), nil
|
||||
}
|
||||
|
||||
func (gn *SGuestnetwork) GetTeamGuestnetwork() (*SGuestnetwork, error) {
|
||||
@@ -521,7 +521,7 @@ func (gn *SGuestnetwork) GetTeamGuestnetwork() (*SGuestnetwork, error) {
|
||||
}
|
||||
|
||||
func (gn *SGuestnetwork) getJsonDescAtBaremetal(host *SHost) *api.GuestnetworkJsonDesc {
|
||||
net := gn.GetNetwork()
|
||||
net, _ := gn.GetNetwork()
|
||||
netif := guestGetHostNetifFromNetwork(host, net)
|
||||
if netif == nil {
|
||||
log.Errorf("fail to find a valid net interface on baremetal %s for network %s", host.String(), net.String())
|
||||
@@ -544,8 +544,8 @@ func guestGetHostNetifFromNetwork(host *SHost, network *SNetwork) *SNetInterface
|
||||
|
||||
func (gn *SGuestnetwork) getJsonDescAtHost(ctx context.Context, host *SHost) *api.GuestnetworkJsonDesc {
|
||||
var (
|
||||
ret *api.GuestnetworkJsonDesc = nil
|
||||
network = gn.GetNetwork()
|
||||
ret *api.GuestnetworkJsonDesc = nil
|
||||
network, _ = gn.GetNetwork()
|
||||
)
|
||||
if network.isOneCloudVpcNetwork() {
|
||||
ret = gn.getJsonDescOneCloudVpc(network)
|
||||
@@ -617,7 +617,7 @@ func (gn *SGuestnetwork) getJsonDescOneCloudVpc(network *SNetwork) *api.Guestnet
|
||||
}
|
||||
|
||||
func (gn *SGuestnetwork) getJsonDesc() *api.GuestnetworkJsonDesc {
|
||||
net := gn.GetNetwork()
|
||||
net, _ := gn.GetNetwork()
|
||||
desc := &api.GuestnetworkJsonDesc{
|
||||
GuestnetworkBaseDesc: api.GuestnetworkBaseDesc{
|
||||
Net: net.Name,
|
||||
@@ -765,7 +765,10 @@ func (manager *SGuestnetworkManager) GetGuestByAddress(address string, projectId
|
||||
}
|
||||
|
||||
func (gn *SGuestnetwork) GetDetailedString() string {
|
||||
network := gn.GetNetwork()
|
||||
network, err := gn.GetNetwork()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
naCount, _ := NetworkAddressManager.fetchAddressCountByGuestnetworkId(gn.RowId)
|
||||
parts := []string{
|
||||
gn.IpAddr, fmt.Sprintf("%d", network.GuestIpMask),
|
||||
@@ -804,7 +807,10 @@ func (gn *SGuestnetwork) ValidateUpdateData(
|
||||
}
|
||||
}
|
||||
if input.IsDefault != nil && *input.IsDefault {
|
||||
net := gn.GetNetwork()
|
||||
net, err := gn.GetNetwork()
|
||||
if err != nil {
|
||||
return input, errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
if len(net.GuestGateway) == 0 {
|
||||
return input, errors.Wrap(httperrors.ErrInvalidStatus, "network of default gateway has no gateway")
|
||||
}
|
||||
@@ -856,7 +862,7 @@ func (manager *SGuestnetworkManager) DeleteGuestNics(ctx context.Context, userCr
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetIsolatedDeviceByNetworkIndex")
|
||||
}
|
||||
net := gn.GetNetwork()
|
||||
net, _ := gn.GetNetwork()
|
||||
if !gotypes.IsNil(net) && (regutils.MatchIP4Addr(gn.IpAddr) || regutils.MatchIP6Addr(gn.Ip6Addr)) {
|
||||
net.updateDnsRecord(&gn, false)
|
||||
if regutils.MatchIP4Addr(gn.IpAddr) {
|
||||
@@ -1011,7 +1017,7 @@ func (gn *SGuestnetwork) IsExit() bool {
|
||||
return netutils.IsExitAddress(addr)
|
||||
}
|
||||
}
|
||||
net := gn.GetNetwork()
|
||||
net, _ := gn.GetNetwork()
|
||||
if net != nil {
|
||||
return net.IsExitNetwork()
|
||||
}
|
||||
@@ -1025,7 +1031,7 @@ func (gn *SGuestnetwork) getBandwidth() int {
|
||||
if gn.BwLimit > 0 && gn.BwLimit <= api.MAX_BANDWIDTH {
|
||||
return gn.BwLimit
|
||||
} else {
|
||||
net := gn.GetNetwork()
|
||||
net, _ := gn.GetNetwork()
|
||||
if net != nil {
|
||||
wire, _ := net.GetWire()
|
||||
if wire != nil {
|
||||
@@ -1069,7 +1075,7 @@ func (gn *SGuestnetwork) IsAllocated() bool {
|
||||
func (gn *SGuestnetwork) GetVirtualIPs() []string {
|
||||
ips := make([]string, 0)
|
||||
guest := gn.GetGuest()
|
||||
net := gn.GetNetwork()
|
||||
net, _ := gn.GetNetwork()
|
||||
for _, guestgroup := range guest.GetGroups() {
|
||||
group := guestgroup.GetGroup()
|
||||
groupnets, err := group.GetNetworks()
|
||||
@@ -1078,7 +1084,7 @@ func (gn *SGuestnetwork) GetVirtualIPs() []string {
|
||||
}
|
||||
for _, groupnetwork := range groupnets {
|
||||
gnet := groupnetwork.GetNetwork()
|
||||
if gnet.WireId == net.WireId {
|
||||
if net != nil && gnet.WireId == net.WireId {
|
||||
ips = append(ips, groupnetwork.IpAddr)
|
||||
}
|
||||
}
|
||||
@@ -1218,8 +1224,8 @@ func (gn *SGuestnetwork) GetShortDesc(ctx context.Context) *jsonutils.JSONDict {
|
||||
}
|
||||
|
||||
func (gn *SGuestnetwork) ToNetworkConfig() *api.NetworkConfig {
|
||||
net := gn.GetNetwork()
|
||||
if net == nil {
|
||||
net, err := gn.GetNetwork()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
wire, _ := net.GetWire()
|
||||
|
||||
@@ -1038,9 +1038,9 @@ func (guest *SGuest) GetVpc() (*SVpc, error) {
|
||||
return nil, errors.Wrapf(err, "failed getting guest network of %s(%s)", guest.Name, guest.Id)
|
||||
}
|
||||
guestnic.SetModelManager(GuestnetworkManager, guestnic)
|
||||
network := guestnic.GetNetwork()
|
||||
if network == nil {
|
||||
return nil, errors.Wrapf(err, "failed getting network for guest %s(%s)", guest.Name, guest.Id)
|
||||
network, err := guestnic.GetNetwork()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetVpc")
|
||||
}
|
||||
vpc, err := network.GetVpc()
|
||||
if err != nil {
|
||||
@@ -1055,7 +1055,7 @@ func (guest *SGuest) IsOneCloudVpcNetwork() (bool, error) {
|
||||
return false, errors.Wrap(err, "GetNetworks")
|
||||
}
|
||||
for _, gn := range gns {
|
||||
n := gn.GetNetwork()
|
||||
n, _ := gn.GetNetwork()
|
||||
if n != nil && n.isOneCloudVpcNetwork() {
|
||||
return true, nil
|
||||
}
|
||||
@@ -3656,7 +3656,7 @@ func (self *SGuest) SyncVMNics(
|
||||
continue
|
||||
}
|
||||
_, err = db.Update(&commondb[i], func() error {
|
||||
network := commondb[i].GetNetwork()
|
||||
network, _ := commondb[i].GetNetwork()
|
||||
ip := commonext[i].GetIP()
|
||||
ip6 := commonext[i].GetIP6()
|
||||
if len(ip) > 0 {
|
||||
@@ -4166,10 +4166,13 @@ func (self *SGuest) allocSriovNicDevice(
|
||||
gn *SGuestnetwork, netConfig *api.NetworkConfig,
|
||||
pendingUsageZone quotas.IQuota,
|
||||
) error {
|
||||
net := gn.GetNetwork()
|
||||
net, err := gn.GetNetwork()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
netConfig.SriovDevice.NetworkIndex = &gn.Index
|
||||
netConfig.SriovDevice.WireId = net.WireId
|
||||
err := self.createIsolatedDeviceOnHost(ctx, userCred, host, netConfig.SriovDevice, pendingUsageZone)
|
||||
err = self.createIsolatedDeviceOnHost(ctx, userCred, host, netConfig.SriovDevice, pendingUsageZone)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "self.createIsolatedDeviceOnHost")
|
||||
}
|
||||
@@ -6329,7 +6332,10 @@ func (self *SGuest) ToNetworksConfig() []*api.NetworkConfig {
|
||||
}
|
||||
for _, guestNetwork := range guestNetworks {
|
||||
netConf := new(api.NetworkConfig)
|
||||
network := guestNetwork.GetNetwork()
|
||||
network, err := guestNetwork.GetNetwork()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
requireTeaming := false
|
||||
if tg, _ := guestNetwork.GetTeamGuestnetwork(); tg != nil {
|
||||
requireTeaming = true
|
||||
|
||||
@@ -187,10 +187,14 @@ func (self *SGuest) doPrepaidRecycleNoLock(ctx context.Context, userCred mcclien
|
||||
}
|
||||
ifname := fmt.Sprintf("eth%d", i)
|
||||
brname := fmt.Sprintf("br%d", i)
|
||||
net, err := guestnics[i].GetNetwork()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
err = fakeHost.addNetif(ctx, userCred,
|
||||
guestnics[i].MacAddr,
|
||||
1,
|
||||
guestnics[i].GetNetwork().WireId,
|
||||
net.WireId,
|
||||
"",
|
||||
1000,
|
||||
nicType,
|
||||
|
||||
@@ -477,7 +477,10 @@ func (manager *SHostManager) ListItemFilter(
|
||||
if len(nets) > 0 {
|
||||
wires := []string{}
|
||||
for i := 0; i < len(nets); i++ {
|
||||
net := nets[i].GetNetwork()
|
||||
net, _ := nets[i].GetNetwork()
|
||||
if net == nil {
|
||||
continue
|
||||
}
|
||||
vpc, _ := net.GetVpc()
|
||||
if vpc.Id != api.DEFAULT_VPC_ID {
|
||||
q = q.IsNotEmpty("ovn_version")
|
||||
|
||||
@@ -450,7 +450,10 @@ func (flow *SNetTapFlow) getMirrorConfig(needTapHostIp bool) (api.SMirrorConfig,
|
||||
}
|
||||
ret.Port = gn.Ifname
|
||||
hostId = guest.HostId
|
||||
net := gn.GetNetwork()
|
||||
net, err := gn.GetNetwork()
|
||||
if err != nil {
|
||||
return ret, errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
if net.IsClassic() {
|
||||
wireId = net.WireId
|
||||
} else {
|
||||
|
||||
@@ -206,10 +206,9 @@ func (man *SNetworkAddressManager) removeGuestnetworkSubIPs(ctx context.Context,
|
||||
}
|
||||
|
||||
func (man *SNetworkAddressManager) addGuestnetworkSubIPs(ctx context.Context, userCred mcclient.TokenCredential, guestnetwork *SGuestnetwork, ipAddrs []string, useReserved bool) error {
|
||||
net := guestnetwork.GetNetwork()
|
||||
if net == nil {
|
||||
return errors.Wrapf(errors.ErrNotFound, "find network %s of guestnetwork %d",
|
||||
guestnetwork.NetworkId, guestnetwork.RowId)
|
||||
net, err := guestnetwork.GetNetwork()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
|
||||
lockman.LockObject(ctx, net)
|
||||
@@ -655,9 +654,9 @@ func (g *SGuest) PerformAddSubIps(ctx context.Context, userCred mcclient.TokenCr
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "getGuestnetworkByIpOrMac ip=%s mac=%s", input.IpAddr, input.Mac)
|
||||
}
|
||||
net := gn.GetNetwork()
|
||||
if net == nil {
|
||||
return nil, httperrors.NewInternalServerError("cannot fetch network of guestnetwork %d", gn.RowId)
|
||||
net, err := gn.GetNetwork()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetNetwork")
|
||||
}
|
||||
|
||||
if input.Count == 0 {
|
||||
|
||||
Reference in New Issue
Block a user