Revision ef99e3e8 lib/cli.py

b/lib/cli.py
680 680
  return retval
681 681

  
682 682

  
683
def check_list_ident_key_val(_, opt, value):
684
  """Custom parser for "ident:key=val,key=val/ident:key=val" options.
683
def check_multilist_ident_key_val(_, opt, value):
684
  """Custom parser for "ident:key=val,key=val/ident:key=val//ident:.." options.
685 685

  
686 686
  @rtype: list of dictionary
687
  @return: {ident: {key: val, key: val}, ident: {key: val}}
687
  @return: [{ident: {key: val, key: val}, ident: {key: val}}, {ident:..}]
688 688

  
689 689
  """
690
  return _SplitListKeyVal(opt, value)
690
  retval = []
691
  for line in value.split("//"):
692
    retval.append(_SplitListKeyVal(opt, line))
693
  return retval
691 694

  
692 695

  
693 696
def check_bool(option, opt, value): # pylint: disable=W0613
......
762 765
    "completion_suggest",
763 766
    ]
764 767
  TYPES = Option.TYPES + (
765
    "listidentkeyval",
768
    "multilistidentkeyval",
766 769
    "identkeyval",
767 770
    "keyval",
768 771
    "unit",
......
771 774
    "maybefloat",
772 775
    )
773 776
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
774
  TYPE_CHECKER["listidentkeyval"] = check_list_ident_key_val
777
  TYPE_CHECKER["multilistidentkeyval"] = check_multilist_ident_key_val
775 778
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
776 779
  TYPE_CHECKER["keyval"] = check_key_val
777 780
  TYPE_CHECKER["unit"] = check_unit
......
964 967
IPOLICY_BOUNDS_SPECS_STR = "--ipolicy-bounds-specs"
965 968
IPOLICY_BOUNDS_SPECS_OPT = cli_option(IPOLICY_BOUNDS_SPECS_STR,
966 969
                                      dest="ipolicy_bounds_specs",
967
                                      type="listidentkeyval", default=None,
970
                                      type="multilistidentkeyval", default=None,
968 971
                                      help="Complete instance specs limits")
969 972

  
970 973
IPOLICY_STD_SPECS_STR = "--ipolicy-std-specs"
......
3796 3799
    if stdspecs:
3797 3800
      buf.write(" %s " % IPOLICY_STD_SPECS_STR)
3798 3801
      _PrintSpecsParameters(buf, stdspecs)
3799
  minmax = ipolicy.get("minmax")
3800
  if minmax:
3801
    minspecs = minmax[0].get("min")
3802
    maxspecs = minmax[0].get("max")
3802
  minmaxes = ipolicy.get("minmax", [])
3803
  first = True
3804
  for minmax in minmaxes:
3805
    minspecs = minmax.get("min")
3806
    maxspecs = minmax.get("max")
3803 3807
    if minspecs and maxspecs:
3804
      buf.write(" %s " % IPOLICY_BOUNDS_SPECS_STR)
3808
      if first:
3809
        buf.write(" %s " % IPOLICY_BOUNDS_SPECS_STR)
3810
        first = False
3811
      else:
3812
        buf.write("//")
3805 3813
      buf.write("min:")
3806 3814
      _PrintSpecsParameters(buf, minspecs)
3807 3815
      buf.write("/max:")
......
3945 3953

  
3946 3954
def _GetISpecsInAllowedValues(minmax_ispecs, allowed_values):
3947 3955
  ret = None
3948
  if minmax_ispecs and allowed_values and len(minmax_ispecs) == 1:
3949
    for (key, spec) in minmax_ispecs.items():
3956
  if (minmax_ispecs and allowed_values and len(minmax_ispecs) == 1 and
3957
      len(minmax_ispecs[0]) == 1):
3958
    for (key, spec) in minmax_ispecs[0].items():
3950 3959
      # This loop is executed exactly once
3951 3960
      if key in allowed_values and not spec:
3952 3961
        ret = key
......
3959 3968
  if found_allowed is not None:
3960 3969
    ipolicy_out[constants.ISPECS_MINMAX] = found_allowed
3961 3970
  elif minmax_ispecs is not None:
3962
    minmax_out = {}
3963
    for (key, spec) in minmax_ispecs.items():
3964
      if key not in constants.ISPECS_MINMAX_KEYS:
3965
        msg = "Invalid key in bounds instance specifications: %s" % key
3966
        raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
3967
      minmax_out[key] = _ParseISpec(spec, key, True)
3968
    ipolicy_out[constants.ISPECS_MINMAX] = [minmax_out]
3971
    minmax_out = []
3972
    for mmpair in minmax_ispecs:
3973
      mmpair_out = {}
3974
      for (key, spec) in mmpair.items():
3975
        if key not in constants.ISPECS_MINMAX_KEYS:
3976
          msg = "Invalid key in bounds instance specifications: %s" % key
3977
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
3978
        mmpair_out[key] = _ParseISpec(spec, key, True)
3979
      minmax_out.append(mmpair_out)
3980
    ipolicy_out[constants.ISPECS_MINMAX] = minmax_out
3969 3981
  if std_ispecs is not None:
3970 3982
    assert not group_ipolicy # This is not an option for gnt-group
3971 3983
    ipolicy_out[constants.ISPECS_STD] = _ParseISpec(std_ispecs, "std", False)

Also available in: Unified diff