Revision 9a8ae794 lib/rapi/client.py

b/lib/rapi/client.py
675 675
      body.update((key, value) for key, value in kwargs.iteritems()
676 676
                  if key != "dry_run")
677 677
    else:
678
      # Old request format (version 0)
679

  
680
      # The following code must make sure that an exception is raised when an
681
      # unsupported setting is requested by the caller. Otherwise this can lead
682
      # to bugs difficult to find. The interface of this function must stay
683
      # exactly the same for version 0 and 1 (e.g. they aren't allowed to
684
      # require different data types).
685

  
686
      # Validate disks
687
      for idx, disk in enumerate(disks):
688
        unsupported = set(disk.keys()) - _INST_CREATE_V0_DISK_PARAMS
689
        if unsupported:
690
          raise GanetiApiError("Server supports request version 0 only, but"
691
                               " disk %s specifies the unsupported parameters"
692
                               " %s, allowed are %s" %
693
                               (idx, unsupported,
694
                                list(_INST_CREATE_V0_DISK_PARAMS)))
695

  
696
      assert (len(_INST_CREATE_V0_DISK_PARAMS) == 1 and
697
              "size" in _INST_CREATE_V0_DISK_PARAMS)
698
      disk_sizes = [disk["size"] for disk in disks]
699

  
700
      # Validate NICs
701
      if not nics:
702
        raise GanetiApiError("Server supports request version 0 only, but"
703
                             " no NIC specified")
704
      elif len(nics) > 1:
705
        raise GanetiApiError("Server supports request version 0 only, but"
706
                             " more than one NIC specified")
707

  
708
      assert len(nics) == 1
709

  
710
      unsupported = set(nics[0].keys()) - _INST_NIC_PARAMS
711
      if unsupported:
712
        raise GanetiApiError("Server supports request version 0 only, but"
713
                             " NIC 0 specifies the unsupported parameters %s,"
714
                             " allowed are %s" %
715
                             (unsupported, list(_INST_NIC_PARAMS)))
716

  
717
      # Validate other parameters
718
      unsupported = (set(kwargs.keys()) - _INST_CREATE_V0_PARAMS -
719
                     _INST_CREATE_V0_DPARAMS)
720
      if unsupported:
721
        allowed = _INST_CREATE_V0_PARAMS.union(_INST_CREATE_V0_DPARAMS)
722
        raise GanetiApiError("Server supports request version 0 only, but"
723
                             " the following unsupported parameters are"
724
                             " specified: %s, allowed are %s" %
725
                             (unsupported, list(allowed)))
726

  
727
      # All required fields for request data version 0
728
      body = {
729
        _REQ_DATA_VERSION_FIELD: 0,
730
        "name": name,
731
        "disk_template": disk_template,
732
        "disks": disk_sizes,
733
        }
734

  
735
      # NIC fields
736
      assert len(nics) == 1
737
      assert not (set(body.keys()) & set(nics[0].keys()))
738
      body.update(nics[0])
739

  
740
      # Copy supported fields
741
      assert not (set(body.keys()) & set(kwargs.keys()))
742
      body.update(dict((key, value) for key, value in kwargs.items()
743
                       if key in _INST_CREATE_V0_PARAMS))
744

  
745
      # Merge dictionaries
746
      for i in (value for key, value in kwargs.items()
747
                if key in _INST_CREATE_V0_DPARAMS):
748
        assert not (set(body.keys()) & set(i.keys()))
749
        body.update(i)
750

  
751
      assert not (set(kwargs.keys()) -
752
                  (_INST_CREATE_V0_PARAMS | _INST_CREATE_V0_DPARAMS))
753
      assert not (set(body.keys()) & _INST_CREATE_V0_DPARAMS)
678
      raise GanetiApiError("Server does not support new-style (version 1)"
679
                           " instance creation requests")
754 680

  
755 681
    return self._SendRequest(HTTP_POST, "/%s/instances" % GANETI_RAPI_VERSION,
756 682
                             query, body)

Also available in: Unified diff