Commit d50b3059 authored by Oleksiy Mishchenko's avatar Oleksiy Mishchenko
Browse files

RAPI: Instance modify.

Split parameters filter to the separate function and reuse it in instance creation.
parent a143be68
......@@ -122,6 +122,33 @@ def MapBulkFields(itemslist, fields):
return items_details
def MakeParamsDict(opts, params):
""" Makes params dictionary out of a option set.
This function returns a dictionary needed for hv or be parameters. But only
those fields which provided in the option set. Takes parameters frozensets
from constants.
@type opts: dict
@param opts: selected options
@type params: frozenset
@param params: subset of options
@rtype: dict
@return: dictionary of options, filtered by given subset.
"""
result = {}
for p in params:
try:
value = opts[p]
except KeyError:
continue
result[p] = value
return result
class R_Generic(object):
"""Generic class for resources.
......
......@@ -285,6 +285,32 @@ class R_instances_name(baserlib.R_Generic):
return job_id
def POST(self):
"""Modify an instance.
"""
instance_name = self.items[0]
opts = {}
for key in self.queryargs:
opts[key] = self.queryargs[key][0]
beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
op = ganeti.opcodes.OpSetInstanceParams(
instance_name=instance_name,
ip=opts.get('ip', None),
bridge=opts.get('bridge', None),
mac=opts.get('mac', None),
hvparams=hvparams,
beparams=beparams,
force=opts.get('force', None))
job_id = ganeti.cli.SendJob([op])
return job_id
class R_instances_name_tags(baserlib.R_Generic):
"""/instances/[instance_name]/tags resource.
......
......@@ -205,18 +205,8 @@ class R_2_instances(baserlib.R_Generic):
"""
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
beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
op = ganeti.opcodes.OpCreateInstance(
instance_name=opts.get('name'),
......@@ -234,7 +224,7 @@ class R_2_instances(baserlib.R_Generic):
wait_for_sync=opts.get('wait_for_sync', True),
mac=opts.get('mac', 'auto'),
hypervisor=opts.get('hypervisor', None),
hvparams=opts.get('hvparams', {}),
hvparams=hvparams,
beparams=beparams,
iallocator=opts.get('iallocator', None),
file_storage_dir=opts.get('file_storage_dir', None),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment