filterValidGroups (ng:ngs) inst =
  let (valid_ngs, msgs) = filterValidGroups ngs inst
      hasNetwork nic = case Nic.network nic of
        Just net -> net `elem` Group.networks (fst ng)
        Nothing -> True
      hasRequiredNetworks = all hasNetwork (Instance.nics inst)
  in if hasRequiredNetworks
      then (ng:valid_ngs, msgs)
      else (valid_ngs,
            ("group " ++ Group.name (fst ng) ++
             " is not connected to a network required by instance " ++
             Instance.name inst):msgs)
filterValidGroups (ng:ngs) inst =
  let (valid_ngs, msgs) = filterValidGroups ngs inst
      hasNetwork nic = case Nic.network nic of
        Just net -> net `elem` Group.networks (fst ng)
        Nothing -> True
      hasRequiredNetworks = all hasNetwork (Instance.nics inst)
  in if hasRequiredNetworks
      then (ng:valid_ngs, msgs)
      else (valid_ngs,
            ("group " ++ Group.name (fst ng) ++
             " is not connected to a network required by instance " ++
             Instance.name inst):msgs)
filterValidGroups (ng:ngs) inst =
  let (valid_ngs, msgs) = filterValidGroups ngs inst
      hasNetwork nic = case Nic.network nic of
        Just net -> net `elem` Group.networks (fst ng)
        Nothing -> True
      hasRequiredNetworks = all hasNetwork (Instance.nics inst)
  in if hasRequiredNetworks
      then (ng:valid_ngs, msgs)
      else (valid_ngs,
            ("group " ++ Group.name (fst ng) ++
             " is not connected to a network required by instance " ++
             Instance.name inst):msgs)