Move Ipolicy utility function to cmdlib/common.py
[ganeti-local] / lib / cmdlib / common.py
index 8ea6f76..73132c2 100644 (file)
@@ -58,11 +58,11 @@ def _ExpandItemName(expand_fn, name, kind):
   @raise errors.OpPrereqError: if the item is not found
 
   """
-  full_name = expand_fn(name)
-  if full_name is None:
+  (uuid, full_name) = expand_fn(name)
+  if uuid is None or full_name is None:
     raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
                                errors.ECODE_NOENT)
-  return full_name
+  return (uuid, full_name)
 
 
 def ExpandInstanceUuidAndName(cfg, expected_uuid, name):
@@ -1079,7 +1079,11 @@ def CheckDiskTemplateEnabled(cluster, disk_template):
 
   """
   assert disk_template is not None
-  assert disk_template in constants.DISK_TEMPLATES
+  if disk_template not in constants.DISK_TEMPLATES:
+    raise errors.OpPrereqError("'%s' is not a valid disk template."
+                               " Valid disk templates are: %s" %
+                               (disk_template,
+                                ",".join(constants.DISK_TEMPLATES)))
   if not disk_template in cluster.enabled_disk_templates:
     raise errors.OpPrereqError("Disk template '%s' is not enabled in cluster."
                                " Enabled disk templates are: %s" %
@@ -1097,7 +1101,7 @@ def CheckStorageTypeEnabled(cluster, storage_type):
 
   """
   assert storage_type is not None
-  assert storage_type in constants.VALID_STORAGE_TYPES
+  assert storage_type in constants.STORAGE_TYPES
   # special case for lvm-pv, because it cannot be enabled
   # via disk templates
   if storage_type == constants.ST_LVM_PV:
@@ -1112,3 +1116,24 @@ def CheckStorageTypeEnabled(cluster, storage_type):
                                " enabled in this cluster. Enabled disk"
                                " templates are: %s" % (storage_type,
                                ",".join(cluster.enabled_disk_templates)))
+
+
+def CheckIpolicyVsDiskTemplates(ipolicy, enabled_disk_templates):
+  """Checks ipolicy disk templates against enabled disk tempaltes.
+
+  @type ipolicy: dict
+  @param ipolicy: the new ipolicy
+  @type enabled_disk_templates: list of string
+  @param enabled_disk_templates: list of enabled disk templates on the
+    cluster
+  @raises errors.OpPrereqError: if there is at least one allowed disk
+    template that is not also enabled.
+
+  """
+  assert constants.IPOLICY_DTS in ipolicy
+  allowed_disk_templates = ipolicy[constants.IPOLICY_DTS]
+  not_enabled = set(allowed_disk_templates) - set(enabled_disk_templates)
+  if not_enabled:
+    raise errors.OpPrereqError("The following disk template are allowed"
+                               " by the ipolicy, but not enabled on the"
+                               " cluster: %s" % utils.CommaJoin(not_enabled))