Convert cmdlib.py to _FieldSet
[ganeti-local] / lib / rapi / rlib2.py
index 9c4863b..1484c2b 100644 (file)
@@ -24,9 +24,9 @@
 """
 
 import ganeti.opcodes
-
+from ganeti import http
 from ganeti import luxi
-
+from ganeti import constants
 from ganeti.rapi import baserlib
 
 from ganeti.rapi.rlib1 import I_FIELDS, N_FIELDS
@@ -139,7 +139,6 @@ class R_2_instances(baserlib.R_Generic):
   """
   DOC_URI = "/2/instances"
 
-
   def GET(self):
     """Returns a list of all available instances.
 
@@ -197,6 +196,132 @@ class R_2_instances(baserlib.R_Generic):
       return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
                                    uri_fields=("id", "uri"))
 
+  def PUT(self):
+    """Create an instance.
+
+    Returns:
+      A job id.
+
+    """
+    opts = self.req.request_post_data
+
+    # beparams
+    mem = opts.get('mem', None)
+    vcpus = opts.get('vcpus', None)
+    auto_balance = opts.get('auto_balance', None)
+
+    beparams = {}
+
+    for key, const in [(mem, constants.BE_MEMORY),
+                       (vcpus, constants.BE_VCPUS),
+                       (auto_balance, constants.BE_AUTO_BALANCE)]:
+      if key is not None:
+        beparams[const] = key
+
+    op = ganeti.opcodes.OpCreateInstance(
+        instance_name=opts.get('name'),
+        disk_size=opts.get('size', 20 * 1024),
+        swap_size=opts.get('swap', 4 * 1024),
+        disk_template=opts.get('disk_template', None),
+        mode=constants.INSTANCE_CREATE,
+        os_type=opts.get('os'),
+        pnode=opts.get('pnode'),
+        snode=opts.get('snode'),
+        ip=opts.get('ip', 'none'),
+        bridge=opts.get('bridge', None),
+        start=opts.get('start', True),
+        ip_check=opts.get('ip_check', True),
+        wait_for_sync=opts.get('wait_for_sync', True),
+        mac=opts.get('mac', 'auto'),
+        hypervisor=opts.get('hypervisor', None),
+        hvparams=opts.get('hvparams', {}),
+        beparams=beparams,
+        iallocator=opts.get('iallocator', None),
+        file_storage_dir=opts.get('file_storage_dir', None),
+        file_driver=opts.get('file_driver', 'loop'),
+        )
+
+    job_id = ganeti.cli.SendJob([op])
+    return job_id
+
+
+class R_2_instances_name_reboot(baserlib.R_Generic):
+  """/2/instances/[instance_name]/reboot resource.
+
+  Implements an instance reboot.
+
+  """
+
+  DOC_URI = "/2/instances/[instance_name]/reboot"
+
+  def GET(self):
+    """Reboot an instance.
+
+    The URI takes type=[hard|soft|full] and
+    ignore_secondaries=[False|True] parameters.
+
+    """
+    instance_name = self.items[0]
+    reboot_type = self.queryargs.get('type',
+                                     [constants.INSTANCE_REBOOT_HARD])[0]
+    ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
+                                                 [False])[0])
+    op = ganeti.opcodes.OpRebootInstance(
+        instance_name=instance_name,
+        reboot_type=reboot_type,
+        ignore_secondaries=ignore_secondaries)
+
+    job_id = ganeti.cli.SendJob([op])
+
+    return job_id
+
+
+class R_2_instances_name_startup(baserlib.R_Generic):
+  """/2/instances/[instance_name]/startup resource.
+
+  Implements an instance startup.
+
+  """
+
+  DOC_URI = "/2/instances/[instance_name]/startup"
+
+  def GET(self):
+    """Startup an instance.
+
+    The URI takes force=[False|True] parameter to start the instance if even if
+    secondary disks are failing.
+
+    """
+    instance_name = self.items[0]
+    force_startup = bool(self.queryargs.get('force', [False])[0])
+    op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
+                                          force=force_startup)
+
+    job_id = ganeti.cli.SendJob([op])
+
+    return job_id
+
+
+class R_2_instances_name_shutdown(baserlib.R_Generic):
+  """/2/instances/[instance_name]/shutdown resource.
+
+  Implements an instance shutdown.
+
+  """
+
+  DOC_URI = "/2/instances/[instance_name]/shutdown"
+
+  def GET(self):
+    """Shutdown an instance.
+
+    """
+    instance_name = self.items[0]
+    op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
+
+    job_id = ganeti.cli.SendJob([op])
+
+    return job_id
+
 
 class R_2_instances_name_tags(baserlib.R_Generic):
   """/2/instances/[instance_name]/tags resource.
@@ -217,9 +342,23 @@ class R_2_instances_name_tags(baserlib.R_Generic):
   def POST(self):
     """Add a set of tags to the instance.
 
-    The reqest as a list of strings should be POST to this URI. And you'll have
+    The request as a list of strings should be POST to this URI. And you'll have
     back a job id.
 
     """
     return baserlib._Tags_POST(constants.TAG_INSTANCE,
                                self.post_data, name=self.items[0])
+
+  def DELETE(self):
+    """Delete a tag.
+
+    In order to delete a set of tags from a instance, DELETE request should be
+    addressed to URI like: /2/instances/[instance_name]/tags?tag=[tag]&tag=[tag]
+
+    """
+    if 'tag' not in self.queryargs:
+      # no we not gonna delete all tags from an instance
+      raise http.HTTPNotImplemented()
+    return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
+                                 self.queryargs['tag'],
+                                 name=self.items[0])