+def _ParseModifyInstanceRequest(name, data):
+ """Parses a request for modifying an instance.
+
+ @rtype: L{opcodes.OpInstanceSetParams}
+ @return: Instance modify opcode
+
+ """
+ return baserlib.FillOpcode(opcodes.OpInstanceSetParams, data, {
+ "instance_name": name,
+ })
+
+
+class R_2_instances_name_modify(baserlib.R_Generic):
+ """/2/instances/[instance_name]/modify resource.
+
+ """
+ def PUT(self):
+ """Changes some parameters of an instance.
+
+ @return: a job id
+
+ """
+ baserlib.CheckType(self.request_body, dict, "Body contents")
+
+ op = _ParseModifyInstanceRequest(self.items[0], self.request_body)
+
+ return baserlib.SubmitJob([op])
+
+
+class R_2_instances_name_disk_grow(baserlib.R_Generic):
+ """/2/instances/[instance_name]/disk/[disk_index]/grow resource.
+
+ """
+ def POST(self):
+ """Increases the size of an instance disk.
+
+ @return: a job id
+
+ """
+ op = baserlib.FillOpcode(opcodes.OpInstanceGrowDisk, self.request_body, {
+ "instance_name": self.items[0],
+ "disk": int(self.items[1]),
+ })
+
+ return baserlib.SubmitJob([op])
+
+
+class R_2_instances_name_console(baserlib.R_Generic):
+ """/2/instances/[instance_name]/console resource.
+
+ """
+ GET_ACCESS = [rapi.RAPI_ACCESS_WRITE]
+
+ def GET(self):
+ """Request information for connecting to instance's console.
+
+ @return: Serialized instance console description, see
+ L{objects.InstanceConsole}
+
+ """
+ client = baserlib.GetClient()
+
+ ((console, ), ) = client.QueryInstances([self.items[0]], ["console"], False)
+
+ if console is None:
+ raise http.HttpServiceUnavailable("Instance console unavailable")
+
+ assert isinstance(console, dict)
+ return console
+
+
+def _GetQueryFields(args):
+ """
+
+ """
+ try:
+ fields = args["fields"]
+ except KeyError:
+ raise http.HttpBadRequest("Missing 'fields' query argument")
+
+ return _SplitQueryFields(fields[0])
+
+
+def _SplitQueryFields(fields):
+ """
+
+ """
+ return [i.strip() for i in fields.split(",")]
+
+
+class R_2_query(baserlib.R_Generic):
+ """/2/query/[resource] resource.
+
+ """
+ # Results might contain sensitive information
+ GET_ACCESS = [rapi.RAPI_ACCESS_WRITE]
+
+ def _Query(self, fields, filter_):
+ return baserlib.GetClient().Query(self.items[0], fields, filter_).ToDict()
+
+ def GET(self):
+ """Returns resource information.
+
+ @return: Query result, see L{objects.QueryResponse}
+
+ """
+ return self._Query(_GetQueryFields(self.queryargs), None)
+
+ def PUT(self):
+ """Submits job querying for resources.
+
+ @return: Query result, see L{objects.QueryResponse}
+
+ """
+ body = self.request_body
+
+ baserlib.CheckType(body, dict, "Body contents")
+
+ try:
+ fields = body["fields"]
+ except KeyError:
+ fields = _GetQueryFields(self.queryargs)
+
+ return self._Query(fields, self.request_body.get("filter", None))
+
+
+class R_2_query_fields(baserlib.R_Generic):
+ """/2/query/[resource]/fields resource.
+
+ """
+ def GET(self):
+ """Retrieves list of available fields for a resource.
+
+ @return: List of serialized L{objects.QueryFieldDefinition}
+
+ """
+ try:
+ raw_fields = self.queryargs["fields"]
+ except KeyError:
+ fields = None
+ else:
+ fields = _SplitQueryFields(raw_fields[0])
+
+ return baserlib.GetClient().QueryFields(self.items[0], fields).ToDict()
+
+