Move helpers of LUInstanceCreate and LUInstanceSetParams

This is preparatory for moving both of them into their own files.

Signed-off-by: Aaron Karper <akarper@google.com>
Reviewed-by: Michele Tartara <mtartara@google.com>
Move LUInstanceSetParams to own file

The class has grown to 1700 lines and deserves its own file.

Signed-off-by: Aaron Karper <akarper@google.com>
Reviewed-by: Michele Tartara <mtartara@google.com>
Moving cmdlib.instance.LUInstanceCreate to separate module

The class is 1500 lines long and deserves it's own file. This includes
moving some utilities to the instance_utils module and mocking them out
in the unittests separately.

Signed-off-by: Aaron Karper <akarper@google.com>
Reviewed-by: Michele Tartara <mtartara@google.com>
Fixing handling of diskless instances

There were several checks that `all(...)` disks were of a certain type.
This leads to (often) wrong behaviour: `all([])` is true, so diskless
instances are all kinds of types. This fixes the issue by introducing
the helper functions AllDisksOfType and AnyDisksOfType.

Signed-off-by: Aaron Karper <akarper@google.com>
Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Michele Tartara <mtartara@google.com>
Implement GetAllDisksInfo in config

Implement 'GetAllDisksInfo' function which returns the configuration of
all disks.

Signed-off-by: Ilias Tsitsimpis <iliastsi@grnet.gr>
AllocateDRBDMinor per disk, not per instance

AllocateDRBDMinor used to allocate some DRBD minors for a given instance
using ComputeDRBDMap. ComputeDRBDMap didn't take into account, the DRBD
minors for disks that are detached (don't belong to any instance). This
patch fixes the above problem by modifying AllocateDRBDMinor and
ReleaseDRBDMinors in order to accept the disk for which we want to
allocate the minors (and not the instance). In addition, ComputeDRBDMap
now examines all the disks (even the detached ones) in order to compute
the minors.

Signed-off-by: Ilias Tsitsimpis <iliastsi@grnet.gr>
config: Use disk name to get its info

Add GetDiskInfoByName function that accepts a disk name as a parameter
and returns the associated disk object. The search algorithm follows
below:

* Iterate all disks in config:
  - Compare their name with the provided name.
  - If there is a match, increment a counter and store the disk.
* Once all disks have been checked, if the counter is 1, return the
  stored disk.
* Else, if there are more than one disks with the same name, raise a
  proper exception.
config: Fix docstring of _UnlockedGetInstanceNodes

The list for non-DRBD instances is never empty. It always contains the
primary node of the instance.
cmdlib: Turn index code to functions

Convert the index operations that are used in "add" to functions, so
that they can be used by "attach" later on.
cmdlib: Return RPC result in AssembleInstanceDisks

The AssembleInstanceDisks function uses internally the
call_blockdev_assemble RPC call. It does two passes with this function,
the first in the secondary and the last in the primary node. The results
from the latter operation can be useful to the caller for hotplugging
reasons, so we return them in array format.
config: Create wrappers for attach/detach ops

The _UnlockedAttachInstanceDisk and _UnlockedDetachInstanceDisk
functions are already used in the add and remove operations. Create
wrappers that can be used by attach and detach operations in order to
call only these functions.
constants: Add attach/detach and UUID constant

Modify Haskell code to accept the attach/detach action and the "UUID"
disk parameter. Also, modify the _ConvertNicDiskModifications function
to accept the new DDM_ATTACH/DDM_DETACH values. Finally, do minor
changes in Python code and tests in order to make the tests pass.
cmdlib: Add checks for attach/detach

Add checks for:
* number of arguments,
* instance's compatibility with disk template/nodes

and update existing ones.
cmdlib: Add core attach/detach logic

Extend _ApplyContainerMods to accept an attach and detach function.
These functions operate in the same way as the add and remove functions,
minus the disk creation and disk removal parts.

Also, add two functions that should raise an exception if one attempts
to attach/detach a NIC.

Finally, update the tests in order to work with the new
_ApplyContainerMods function.
config: Remove checks for detached disks

It is not an error sign for a configuration to have detached disks, since they
can be handled from now on by the attach/detach operations.
burnin: Test if attach/detach works properly

The attach/detach test is merged with the add/remove test. The test is
the following: an extra disk is added in each instance and is given a
random name. Once it has been added successfully, we detach it and check
if the instance operates normally. Then, we pick instances randomly and
attach a detached disk to them, using an algorithm that matches a disk
to an instance based on its disk nodes. Then, we remove the disk.

Also, move the start/stop at the top of the tests, since the start/stop
functionality is erroneously used first by other functions (e.g. rename)
and then tested on its own.
tests.config: Add test for GetDiskInfoByName

Check if the GetDiskInfo and GetDiskInfoByName functions operate as
expected.
tests.cmdlib: Test index operations

Test if the GetIndexFromIdentifier and InsertItemToIndex functions work
properly.
tests.opcodes/client: Add test for constants

Also, test if the ConvertNicDiskModifications function operates as
expected.
tests.config: Test attach/detach wrappers

Test if the AttachInstanceDisk and DetachInstanceDisk wrappers update
the configuration as expected.