Commit c7d249d0 authored by Iustin Pop's avatar Iustin Pop
Browse files

Convert a few existing opcode parameters to safer types



This does a partial conversion of existing opcodes to the new
non-empty string type, which is needed so that we don't add even more
opcodes without this safety net (and to have consistent "target_node",
etc. in opcodes).

By partial I mean that only the opcode are changed, and not the entire
htools types/workflow, so we use error to abort if, when submitting
the opcodes, we detect an empty node name. FIXME added.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent d6979f35
......@@ -40,9 +40,12 @@ import qualified Text.JSON as J
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Test.Ganeti.Types ()
import qualified Ganeti.Constants as C
import qualified Ganeti.OpCodes as OpCodes
import Ganeti.Types
import Ganeti.OpParams
{-# ANN module "HLint: ignore Use camelCase" #-}
......@@ -66,22 +69,38 @@ instance Arbitrary OpCodes.OpCode where
case op_id of
"OP_TEST_DELAY" ->
OpCodes.OpTestDelay <$> arbitrary <*> arbitrary
<*> resize maxNodes (listOf getFQDN)
<*> genNodeNames
"OP_INSTANCE_REPLACE_DISKS" ->
OpCodes.OpInstanceReplaceDisks <$> getFQDN <*> getMaybe getFQDN <*>
arbitrary <*> resize C.maxDisks arbitrary <*> getMaybe getName
OpCodes.OpInstanceReplaceDisks <$> getFQDN <*>
getMaybe genNodeNameNE <*> arbitrary <*> genDiskIndices <*> arbitrary
"OP_INSTANCE_FAILOVER" ->
OpCodes.OpInstanceFailover <$> getFQDN <*> arbitrary <*>
getMaybe getFQDN
getMaybe genNodeNameNE
"OP_INSTANCE_MIGRATE" ->
OpCodes.OpInstanceMigrate <$> getFQDN <*> arbitrary <*>
arbitrary <*> arbitrary <*> getMaybe getFQDN
arbitrary <*> arbitrary <*> getMaybe genNodeNameNE
"OP_TAGS_SET" ->
OpCodes.OpTagsSet <$> arbitrary <*> genTags
"OP_TAGS_DEL" ->
OpCodes.OpTagsSet <$> arbitrary <*> genTags
_ -> fail "Wrong opcode"
-- * Helper functions
-- | Generates list of disk indices.
genDiskIndices :: Gen [DiskIndex]
genDiskIndices = do
cnt <- choose (0, C.maxDisks)
genUniquesList cnt
-- | Generates a list of node names.
genNodeNames :: Gen [String]
genNodeNames = resize maxNodes (listOf getFQDN)
-- | Gets a node name in non-empty type.
genNodeNameNE :: Gen NonEmptyString
genNodeNameNE = getFQDN >>= mkNonEmpty
-- * Test cases
-- | Check that opcode serialization is idempotent.
......
......@@ -90,6 +90,7 @@ import Ganeti.HTools.Types
import Ganeti.Compat
import qualified Ganeti.OpCodes as OpCodes
import Ganeti.Utils
import Ganeti.Types (mkNonEmpty)
-- * Types
......@@ -1447,7 +1448,11 @@ iMoveToJob :: Node.List -- ^ The node list; only used for node
iMoveToJob nl il idx move =
let inst = Container.find idx il
iname = Instance.name inst
lookNode = Just . Container.nameOf nl
lookNode n = case mkNonEmpty (Container.nameOf nl n) of
-- FIXME: convert htools codebase to non-empty strings
Bad msg -> error $ "Empty node name for idx " ++
show n ++ ": " ++ msg ++ "??"
Ok ne -> Just ne
opF = OpCodes.OpInstanceMigrate iname True False True Nothing
opFA n = OpCodes.OpInstanceMigrate iname True False True (lookNode n)
opR n = OpCodes.OpInstanceReplaceDisks iname (lookNode n)
......
......@@ -57,22 +57,22 @@ $(genOpCode "OpCode"
])
, ("OpInstanceReplaceDisks",
[ pInstanceName
, optionalField $ simpleField "remote_node" [t| String |]
, pRemoteNode
, simpleField "mode" [t| ReplaceDisksMode |]
, simpleField "disks" [t| [DiskIndex] |]
, optionalField $ simpleField "iallocator" [t| String |]
, pIallocator
])
, ("OpInstanceFailover",
[ pInstanceName
, simpleField "ignore_consistency" [t| Bool |]
, optionalField $ simpleField "target_node" [t| String |]
, pMigrationTargetNode
])
, ("OpInstanceMigrate",
[ pInstanceName
, simpleField "live" [t| Bool |]
, simpleField "cleanup" [t| Bool |]
, defaultField [| False |] $ simpleField "allow_failover" [t| Bool |]
, optionalField $ simpleField "target_node" [t| String |]
, pMigrationTargetNode
])
, ("OpTagsSet",
[ pTagsObject
......
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