Commit 331663dd authored by Petr Pudlak's avatar Petr Pudlak

Move generateOneMAC to the new Utils.Random module

.. since it's a general, randomized function.
Signed-off-by: default avatarPetr Pudlak <pudlak@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent d5c2dad6
......@@ -25,8 +25,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
module Ganeti.Utils.Random
( generateSecret
, generateOneMAC
) where
import Control.Applicative
import Control.Monad
import Control.Monad.State
import System.Random
......@@ -38,3 +40,12 @@ generateSecret :: (RandomGen g) => Int -> g -> (String, g)
generateSecret n =
runState . liftM (concatMap $ printf "%02x")
$ replicateM n (state $ randomR (0 :: Int, 255))
-- | Given a prefix, randomly generates a full MAC address.
--
-- See 'generateMAC' for discussion about how this function uses
-- the random generator.
generateOneMAC :: (RandomGen g) => String -> g -> (String, g)
generateOneMAC prefix = runState $
let randByte = state (randomR (0, 255 :: Int))
in printf "%s:%02x:%02x:%02x" prefix <$> randByte <*> randByte <*> randByte
......@@ -61,7 +61,6 @@ import qualified Data.Map as M
import Data.Monoid
import qualified Data.Set as S
import System.Random
import Text.Printf
import Ganeti.BasicTypes
import Ganeti.Config
......@@ -242,15 +241,6 @@ dropAllReservations jobId = trsMACsL %~ dropReservationsFor jobId
-- ** MAC addresses
-- | Given a prefix, randomly generates a full MAC address.
--
-- See 'generateMAC' for discussion about how this function uses
-- the random generator.
generateOneMAC :: (RandomGen g) => MAC -> g -> (MAC, g)
generateOneMAC prefix = runState $
let randByte = state (randomR (0, 255 :: Int))
in printf "%s:%02x:%02x:%02x" prefix <$> randByte <*> randByte <*> randByte
-- Randomly generate a MAC for an instance.
-- Checks that the generated MAC isn't used by another instance.
--
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment