Revision 768747ed
b/lib/rapi/client.py | ||
---|---|---|
356 | 356 |
"User-Agent": self.USER_AGENT, |
357 | 357 |
} |
358 | 358 |
|
359 |
def _SendRequest(self, method, path, query=None, content=None):
|
|
359 |
def _SendRequest(self, method, path, query, content):
|
|
360 | 360 |
"""Sends an HTTP request. |
361 | 361 |
|
362 | 362 |
This constructs a full URL, encodes and decodes HTTP bodies, and |
363 | 363 |
handles invalid responses in a pythonic way. |
364 | 364 |
|
365 |
@type method: str |
|
365 |
@type method: string
|
|
366 | 366 |
@param method: HTTP method to use |
367 |
@type path: str |
|
367 |
@type path: string
|
|
368 | 368 |
@param path: HTTP URL path |
369 | 369 |
@type query: list of two-tuples |
370 | 370 |
@param query: query arguments to pass to urllib.urlencode |
... | ... | |
425 | 425 |
@return: Ganeti Remote API version |
426 | 426 |
|
427 | 427 |
""" |
428 |
return self._SendRequest(HTTP_GET, "/version") |
|
428 |
return self._SendRequest(HTTP_GET, "/version", None, None)
|
|
429 | 429 |
|
430 | 430 |
def GetOperatingSystems(self): |
431 | 431 |
"""Gets the Operating Systems running in the Ganeti cluster. |
... | ... | |
434 | 434 |
@return: operating systems |
435 | 435 |
|
436 | 436 |
""" |
437 |
return self._SendRequest(HTTP_GET, "/2/os") |
|
437 |
return self._SendRequest(HTTP_GET, "/2/os", None, None)
|
|
438 | 438 |
|
439 | 439 |
def GetInfo(self): |
440 | 440 |
"""Gets info about the cluster. |
... | ... | |
443 | 443 |
@return: information about the cluster |
444 | 444 |
|
445 | 445 |
""" |
446 |
return self._SendRequest(HTTP_GET, "/2/info") |
|
446 |
return self._SendRequest(HTTP_GET, "/2/info", None, None)
|
|
447 | 447 |
|
448 | 448 |
def GetClusterTags(self): |
449 | 449 |
"""Gets the cluster tags. |
... | ... | |
452 | 452 |
@return: cluster tags |
453 | 453 |
|
454 | 454 |
""" |
455 |
return self._SendRequest(HTTP_GET, "/2/tags") |
|
455 |
return self._SendRequest(HTTP_GET, "/2/tags", None, None)
|
|
456 | 456 |
|
457 | 457 |
def AddClusterTags(self, tags, dry_run=False): |
458 | 458 |
"""Adds tags to the cluster. |
... | ... | |
470 | 470 |
if dry_run: |
471 | 471 |
query.append(("dry-run", 1)) |
472 | 472 |
|
473 |
return self._SendRequest(HTTP_PUT, "/2/tags", query) |
|
473 |
return self._SendRequest(HTTP_PUT, "/2/tags", query, None)
|
|
474 | 474 |
|
475 | 475 |
def DeleteClusterTags(self, tags, dry_run=False): |
476 | 476 |
"""Deletes tags from the cluster. |
... | ... | |
485 | 485 |
if dry_run: |
486 | 486 |
query.append(("dry-run", 1)) |
487 | 487 |
|
488 |
return self._SendRequest(HTTP_DELETE, "/2/tags", query) |
|
488 |
return self._SendRequest(HTTP_DELETE, "/2/tags", query, None)
|
|
489 | 489 |
|
490 | 490 |
def GetInstances(self, bulk=False): |
491 | 491 |
"""Gets information about instances on the cluster. |
... | ... | |
501 | 501 |
if bulk: |
502 | 502 |
query.append(("bulk", 1)) |
503 | 503 |
|
504 |
instances = self._SendRequest(HTTP_GET, "/2/instances", query) |
|
504 |
instances = self._SendRequest(HTTP_GET, "/2/instances", query, None)
|
|
505 | 505 |
if bulk: |
506 | 506 |
return instances |
507 | 507 |
else: |
... | ... | |
517 | 517 |
@return: info about the instance |
518 | 518 |
|
519 | 519 |
""" |
520 |
return self._SendRequest(HTTP_GET, "/2/instances/%s" % instance) |
|
520 |
return self._SendRequest(HTTP_GET, "/2/instances/%s" % instance, None, None)
|
|
521 | 521 |
|
522 | 522 |
def CreateInstance(self, dry_run=False): |
523 | 523 |
"""Creates a new instance. |
... | ... | |
534 | 534 |
if dry_run: |
535 | 535 |
query.append(("dry-run", 1)) |
536 | 536 |
|
537 |
return self._SendRequest(HTTP_POST, "/2/instances", query) |
|
537 |
return self._SendRequest(HTTP_POST, "/2/instances", query, None)
|
|
538 | 538 |
|
539 | 539 |
def DeleteInstance(self, instance, dry_run=False): |
540 | 540 |
"""Deletes an instance. |
... | ... | |
550 | 550 |
if dry_run: |
551 | 551 |
query.append(("dry-run", 1)) |
552 | 552 |
|
553 |
return self._SendRequest(HTTP_DELETE, "/2/instances/%s" % instance, query) |
|
553 |
return self._SendRequest(HTTP_DELETE, "/2/instances/%s" % instance, |
|
554 |
query, None) |
|
554 | 555 |
|
555 | 556 |
def GetInstanceTags(self, instance): |
556 | 557 |
"""Gets tags for an instance. |
... | ... | |
562 | 563 |
@return: tags for the instance |
563 | 564 |
|
564 | 565 |
""" |
565 |
return self._SendRequest(HTTP_GET, "/2/instances/%s/tags" % instance) |
|
566 |
return self._SendRequest(HTTP_GET, "/2/instances/%s/tags" % instance, |
|
567 |
None, None) |
|
566 | 568 |
|
567 | 569 |
def AddInstanceTags(self, instance, tags, dry_run=False): |
568 | 570 |
"""Adds tags to an instance. |
... | ... | |
582 | 584 |
if dry_run: |
583 | 585 |
query.append(("dry-run", 1)) |
584 | 586 |
|
585 |
return self._SendRequest(HTTP_PUT, "/2/instances/%s/tags" % instance, query) |
|
587 |
return self._SendRequest(HTTP_PUT, "/2/instances/%s/tags" % instance, |
|
588 |
query, None) |
|
586 | 589 |
|
587 | 590 |
def DeleteInstanceTags(self, instance, tags, dry_run=False): |
588 | 591 |
"""Deletes tags from an instance. |
... | ... | |
600 | 603 |
query.append(("dry-run", 1)) |
601 | 604 |
|
602 | 605 |
return self._SendRequest(HTTP_DELETE, "/2/instances/%s/tags" % instance, |
603 |
query) |
|
606 |
query, None)
|
|
604 | 607 |
|
605 | 608 |
def RebootInstance(self, instance, reboot_type=None, ignore_secondaries=None, |
606 | 609 |
dry_run=False): |
... | ... | |
626 | 629 |
query.append(("dry-run", 1)) |
627 | 630 |
|
628 | 631 |
return self._SendRequest(HTTP_POST, "/2/instances/%s/reboot" % instance, |
629 |
query) |
|
632 |
query, None)
|
|
630 | 633 |
|
631 | 634 |
def ShutdownInstance(self, instance, dry_run=False): |
632 | 635 |
"""Shuts down an instance. |
... | ... | |
642 | 645 |
query.append(("dry-run", 1)) |
643 | 646 |
|
644 | 647 |
return self._SendRequest(HTTP_PUT, "/2/instances/%s/shutdown" % instance, |
645 |
query) |
|
648 |
query, None)
|
|
646 | 649 |
|
647 | 650 |
def StartupInstance(self, instance, dry_run=False): |
648 | 651 |
"""Starts up an instance. |
... | ... | |
658 | 661 |
query.append(("dry-run", 1)) |
659 | 662 |
|
660 | 663 |
return self._SendRequest(HTTP_PUT, "/2/instances/%s/startup" % instance, |
661 |
query) |
|
664 |
query, None)
|
|
662 | 665 |
|
663 | 666 |
def ReinstallInstance(self, instance, os, no_startup=False): |
664 | 667 |
"""Reinstalls an instance. |
... | ... | |
675 | 678 |
if no_startup: |
676 | 679 |
query.append(("nostartup", 1)) |
677 | 680 |
return self._SendRequest(HTTP_POST, "/2/instances/%s/reinstall" % instance, |
678 |
query) |
|
681 |
query, None)
|
|
679 | 682 |
|
680 | 683 |
def ReplaceInstanceDisks(self, instance, disks, mode="replace_auto", |
681 | 684 |
remote_node=None, iallocator="hail", dry_run=False): |
... | ... | |
721 | 724 |
query.append(("dry-run", 1)) |
722 | 725 |
|
723 | 726 |
return self._SendRequest(HTTP_POST, |
724 |
"/2/instances/%s/replace-disks" % instance, query) |
|
727 |
"/2/instances/%s/replace-disks" % instance, |
|
728 |
query, None) |
|
725 | 729 |
|
726 | 730 |
def GetJobs(self): |
727 | 731 |
"""Gets all jobs for the cluster. |
... | ... | |
730 | 734 |
@return: job ids for the cluster |
731 | 735 |
|
732 | 736 |
""" |
733 |
return [int(j["id"]) for j in self._SendRequest(HTTP_GET, "/2/jobs")] |
|
737 |
return [int(j["id"]) |
|
738 |
for j in self._SendRequest(HTTP_GET, "/2/jobs", None, None)] |
|
734 | 739 |
|
735 | 740 |
def GetJobStatus(self, job_id): |
736 | 741 |
"""Gets the status of a job. |
... | ... | |
742 | 747 |
@return: job status |
743 | 748 |
|
744 | 749 |
""" |
745 |
return self._SendRequest(HTTP_GET, "/2/jobs/%d" % job_id) |
|
750 |
return self._SendRequest(HTTP_GET, "/2/jobs/%d" % job_id, None, None)
|
|
746 | 751 |
|
747 | 752 |
def DeleteJob(self, job_id, dry_run=False): |
748 | 753 |
"""Deletes a job. |
... | ... | |
757 | 762 |
if dry_run: |
758 | 763 |
query.append(("dry-run", 1)) |
759 | 764 |
|
760 |
return self._SendRequest(HTTP_DELETE, "/2/jobs/%d" % job_id, query) |
|
765 |
return self._SendRequest(HTTP_DELETE, "/2/jobs/%d" % job_id, query, None)
|
|
761 | 766 |
|
762 | 767 |
def GetNodes(self, bulk=False): |
763 | 768 |
"""Gets all nodes in the cluster. |
... | ... | |
774 | 779 |
if bulk: |
775 | 780 |
query.append(("bulk", 1)) |
776 | 781 |
|
777 |
nodes = self._SendRequest(HTTP_GET, "/2/nodes", query) |
|
782 |
nodes = self._SendRequest(HTTP_GET, "/2/nodes", query, None)
|
|
778 | 783 |
if bulk: |
779 | 784 |
return nodes |
780 | 785 |
else: |
... | ... | |
790 | 795 |
@return: info about the node |
791 | 796 |
|
792 | 797 |
""" |
793 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s" % node) |
|
798 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s" % node, None, None)
|
|
794 | 799 |
|
795 | 800 |
def EvacuateNode(self, node, iallocator=None, remote_node=None, |
796 | 801 |
dry_run=False): |
... | ... | |
822 | 827 |
if dry_run: |
823 | 828 |
query.append(("dry-run", 1)) |
824 | 829 |
|
825 |
return self._SendRequest(HTTP_POST, "/2/nodes/%s/evacuate" % node, query) |
|
830 |
return self._SendRequest(HTTP_POST, "/2/nodes/%s/evacuate" % node, |
|
831 |
query, None) |
|
826 | 832 |
|
827 | 833 |
def MigrateNode(self, node, live=True, dry_run=False): |
828 | 834 |
"""Migrates all primary instances from a node. |
... | ... | |
844 | 850 |
if dry_run: |
845 | 851 |
query.append(("dry-run", 1)) |
846 | 852 |
|
847 |
return self._SendRequest(HTTP_POST, "/2/nodes/%s/migrate" % node, query) |
|
853 |
return self._SendRequest(HTTP_POST, "/2/nodes/%s/migrate" % node, |
|
854 |
query, None) |
|
848 | 855 |
|
849 | 856 |
def GetNodeRole(self, node): |
850 | 857 |
"""Gets the current role for a node. |
... | ... | |
856 | 863 |
@return: the current role for a node |
857 | 864 |
|
858 | 865 |
""" |
859 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s/role" % node) |
|
866 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s/role" % node, None, None)
|
|
860 | 867 |
|
861 | 868 |
def SetNodeRole(self, node, role, force=False): |
862 | 869 |
"""Sets the role for a node. |
... | ... | |
878 | 885 |
raise InvalidNodeRole("%s is not a valid node role.", role) |
879 | 886 |
|
880 | 887 |
query = [("force", force)] |
881 |
return self._SendRequest(HTTP_PUT, "/2/nodes/%s/role" % node, query,
|
|
882 |
content=role)
|
|
888 |
return self._SendRequest(HTTP_PUT, "/2/nodes/%s/role" % node, |
|
889 |
query, role)
|
|
883 | 890 |
|
884 | 891 |
def GetNodeStorageUnits(self, node, storage_type, output_fields): |
885 | 892 |
"""Gets the storage units for a node. |
... | ... | |
902 | 909 |
raise InvalidStorageType("%s is an invalid storage type.", storage_type) |
903 | 910 |
|
904 | 911 |
query = [("storage_type", storage_type), ("output_fields", output_fields)] |
905 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s/storage" % node, query) |
|
912 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s/storage" % node, |
|
913 |
query, None) |
|
906 | 914 |
|
907 | 915 |
def ModifyNodeStorageUnits(self, node, storage_type, name, allocatable=True): |
908 | 916 |
"""Modifies parameters of storage units on the node. |
... | ... | |
930 | 938 |
("allocatable", allocatable) |
931 | 939 |
] |
932 | 940 |
return self._SendRequest(HTTP_PUT, "/2/nodes/%s/storage/modify" % node, |
933 |
query) |
|
941 |
query, None)
|
|
934 | 942 |
|
935 | 943 |
def RepairNodeStorageUnits(self, node, storage_type, name): |
936 | 944 |
"""Repairs a storage unit on the node. |
... | ... | |
953 | 961 |
|
954 | 962 |
query = [("storage_type", storage_type), ("name", name)] |
955 | 963 |
return self._SendRequest(HTTP_PUT, "/2/nodes/%s/storage/repair" % node, |
956 |
query) |
|
964 |
query, None)
|
|
957 | 965 |
|
958 | 966 |
def GetNodeTags(self, node): |
959 | 967 |
"""Gets the tags for a node. |
... | ... | |
965 | 973 |
@return: tags for the node |
966 | 974 |
|
967 | 975 |
""" |
968 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s/tags" % node) |
|
976 |
return self._SendRequest(HTTP_GET, "/2/nodes/%s/tags" % node, None, None)
|
|
969 | 977 |
|
970 | 978 |
def AddNodeTags(self, node, tags, dry_run=False): |
971 | 979 |
"""Adds tags to a node. |
... | ... | |
985 | 993 |
if dry_run: |
986 | 994 |
query.append(("dry-run", 1)) |
987 | 995 |
|
988 |
return self._SendRequest(HTTP_PUT, "/2/nodes/%s/tags" % node, query,
|
|
989 |
content=tags)
|
|
996 |
return self._SendRequest(HTTP_PUT, "/2/nodes/%s/tags" % node, |
|
997 |
query, tags)
|
|
990 | 998 |
|
991 | 999 |
def DeleteNodeTags(self, node, tags, dry_run=False): |
992 | 1000 |
"""Delete tags from a node. |
... | ... | |
1006 | 1014 |
if dry_run: |
1007 | 1015 |
query.append(("dry-run", 1)) |
1008 | 1016 |
|
1009 |
return self._SendRequest(HTTP_DELETE, "/2/nodes/%s/tags" % node, query) |
|
1017 |
return self._SendRequest(HTTP_DELETE, "/2/nodes/%s/tags" % node, |
|
1018 |
query, None) |
Also available in: Unified diff