From 13f1e6f41e88d7c904085b82e414078f181af5a2 Mon Sep 17 00:00:00 2001 From: Lingxian Kong Date: Thu, 22 Jun 2017 23:57:01 +1200 Subject: [PATCH] Support function update Change-Id: Ib184236b28a4aa3684970654f2788c9e3677f822 --- qinling/api/controllers/v1/function.py | 28 +++++++++++++++++++++++++ qinling/api/controllers/v1/resources.py | 4 ++-- qinling/db/sqlalchemy/api.py | 12 ++++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/qinling/api/controllers/v1/function.py b/qinling/api/controllers/v1/function.py index 63eb6bc8..c19a8795 100644 --- a/qinling/api/controllers/v1/function.py +++ b/qinling/api/controllers/v1/function.py @@ -39,6 +39,7 @@ CONF = cfg.CONF POST_REQUIRED = set(['name', 'code']) CODE_SOURCE = set(['package', 'swift', 'image']) +UPDATE_ALLOWED = set(['name', 'description', 'entry']) class FunctionsController(rest.RestController): @@ -182,3 +183,30 @@ class FunctionsController(rest.RestController): # This will also delete function service mapping as well. db_api.delete_function(id) + + @rest_utils.wrap_wsme_controller_exception + @wsme_pecan.wsexpose( + resources.Function, + types.uuid, + body=resources.Function + ) + def put(self, id, func): + """Update function. + + Currently, we only support update name, description, entry. + """ + values = {} + for key in UPDATE_ALLOWED: + if key in func.to_dict(): + values.update({key: func.to_dict().get(key)}) + + LOG.info('Update function [id=%s, values=%s]' % (id, values)) + + with db_api.transaction(): + func_db = db_api.update_function(id, values) + if 'entry' in values: + # Update entry will delete allocated resources in orchestrator. + db_api.delete_function_service_mapping(id) + self.engine_client.delete_function(id) + + return resources.Function.from_dict(func_db.to_dict()) diff --git a/qinling/api/controllers/v1/resources.py b/qinling/api/controllers/v1/resources.py index 2ba30221..e1b8f312 100644 --- a/qinling/api/controllers/v1/resources.py +++ b/qinling/api/controllers/v1/resources.py @@ -166,10 +166,10 @@ class Function(Resource): description = wtypes.text memory_size = int timeout = int - runtime_id = types.uuid + runtime_id = wsme.wsattr(types.uuid, readonly=True) code = types.jsontype entry = wtypes.text - count = int + count = wsme.wsattr(int, readonly=True) created_at = wtypes.text updated_at = wtypes.text diff --git a/qinling/db/sqlalchemy/api.py b/qinling/db/sqlalchemy/api.py index 580f1e75..5b493cdb 100644 --- a/qinling/db/sqlalchemy/api.py +++ b/qinling/db/sqlalchemy/api.py @@ -213,8 +213,11 @@ def create_function(values, session=None): @db_base.session_aware() -def update_function(id, values): - pass +def update_function(id, values, session=None): + function = get_function(id) + function.update(values.copy()) + + return function @db_base.session_aware() @@ -336,6 +339,9 @@ def get_function_service_mappings(session=None, **kwargs): @db_base.session_aware() def delete_function_service_mapping(id, session=None): - mapping = get_function_service_mapping(id) + try: + mapping = get_function_service_mapping(id) + except exc.DBEntityNotFoundError: + return session.delete(mapping)