update的入口代码在nova/api/openstack/compute/servers.py,方法是update(),如下
def update(self, req, id, body):
"""Update server then pass on to version-specific controller."""
ctxt = req.environ['nova.context']
//update_dict 表示要更新的字典
update_dict = {}
//根据id找到要更新的instance
instance = self._get_server(ctxt, req, id, is_detail=True)
//判断是否有权限权限更新instance
ctxt.can(server_policies.SERVERS % 'update',
target={'user_id': instance.user_id,
'project_id': instance.project_id})
server = body['server']
//更新display_name
if 'name' in server:
update_dict['display_name'] = common.normalize_name(
server['name'])
//更新description
if 'description' in server:
# This is allowed to be None (remove description)
update_dict['display_description'] = server['description']
helpers.translate_attributes(helpers.UPDATE, server, update_dict)
try:
instance = self.compute_api.update_instance(ctxt, instance,
update_dict)
return self._view_builder.show(req, instance,
extend_address=False)
except exception.InstanceNotFound:
msg = _("Instance could not be found")
raise exc.HTTPNotFound(explanation=msg)
在__init__ 函数中self.compute_api = compute.API(),因此这里就转到
def update_instance(self, context, instance, updates):
"""Updates a single Instance object with some updates dict.
Returns the updated instance.
"""
//如果instance设置了id的话,则直接调用instance.update 来更新,最后调用instance.save()来保存更新。
if instance.obj_attr_is_set('id'):
instance.update(updates)
inst_map = self._get_instance_map_or_none(context, instance.uuid)
if inst_map and (inst_map.cell_mapping is not None):
with nova_context.target_cell(context, inst_map.cell_mapping):
instance.save()
else:
# If inst_map.cell_mapping does not point at a cell then cell
# migration has not happened yet.
# TODO(alaski): Make this a failure case after we put in
# a block that requires migrating to cellsv2.
instance.save()
else:
//如果这个instance没有设置id的话,则从更具instance的uuid找到这个instance,同样通过instance.update(updates)更新后,通过build_req.save()来保存
try:
build_req = objects.BuildRequest.get_by_instance_uuid(
context, instance.uuid)
instance = build_req.instance
instance.update(updates)
build_req.save()
except exception.BuildRequestNotFound:
return instance
总结一下nova的update函数从nova/api/openstack/compute/servers.py 中开始到nova/compute/api.py中结束。看起来这个函数只会更新instance的display_name和description 这两个字符串
nova update
最新推荐文章于 2023-01-17 13:36:20 发布