Merge "Refactor the code of API controller/view"

This commit is contained in:
Zuul 2019-04-16 04:16:37 +00:00 committed by Gerrit Code Review
commit ac83e002c8
4 changed files with 107 additions and 29 deletions

View File

@ -26,6 +26,7 @@ from zun.api.controllers import base
from zun.api.controllers import link
from zun.api.controllers.v1 import collection
from zun.api.controllers.v1.schemas import containers as schema
from zun.api.controllers.v1.views import actions_view
from zun.api.controllers.v1.views import containers_view as view
from zun.api import utils as api_utils
from zun.api import validation
@ -84,32 +85,6 @@ class ContainerCollection(collection.Collection):
class ContainersActionsController(base.Controller):
"""Controller for Container Actions."""
def __init__(self):
super(ContainersActionsController, self).__init__()
self._action_keys = ['action', 'container_uuid', 'request_id',
'user_id', 'project_id', 'start_time',
'message']
self._event_keys = ['event', 'start_time', 'finish_time', 'result',
'traceback']
def _format_action(self, action_raw):
action = {}
action_dict = action_raw.as_dict()
for key in self._action_keys:
action[key] = action_dict.get(key)
return action
def _format_event(self, event_raw, show_traceback=False):
event = {}
event_dict = event_raw.as_dict()
for key in self._event_keys:
# By default, non-admins are not allowed to see traceback details.
if key == 'traceback' and not show_traceback:
event['traceback'] = None
continue
event[key] = event_dict.get(key)
return event
@pecan.expose('json')
@exception.wrap_pecan_controller_exception
def get_all(self, container_ident, **kwargs):
@ -120,7 +95,7 @@ class ContainersActionsController(base.Controller):
container = utils.get_container(container_ident)
actions_raw = objects.ContainerAction.get_by_container_uuid(
context, container.uuid)
actions = [self._format_action(action) for action in actions_raw]
actions = [actions_view.format_action(a) for a in actions_raw]
return {"containerActions": actions}
@ -144,7 +119,7 @@ class ContainersActionsController(base.Controller):
# etcd using action.uuid get the unique action instead of action.id
action_id = action.uuid
action = self._format_action(action)
action = actions_view.format_action(action)
show_traceback = False
if policy.enforce(context, "container:action:events",
do_raise=False, action="container:action:events"):
@ -152,7 +127,7 @@ class ContainersActionsController(base.Controller):
events_raw = objects.ContainerActionEvent.get_by_action(context,
action_id)
action['events'] = [self._format_event(evt, show_traceback)
action['events'] = [actions_view.format_event(evt, show_traceback)
for evt in events_raw]
return action

View File

@ -0,0 +1,60 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import itertools
_action_keys = (
'action',
'container_uuid',
'message',
'project_id',
'request_id',
'start_time',
'user_id',
)
_action_event_keys = (
'event',
'finish_time',
'result',
'start_time',
'traceback',
)
def format_action(action):
def transform(key, value):
if key not in _action_keys:
return
yield (key, value)
return dict(itertools.chain.from_iterable(
transform(k, v) for k, v in action.as_dict().items()))
def format_event(event, show_traceback=False):
def transform(key, value):
if key not in _action_event_keys:
return
if key == 'traceback' and not show_traceback:
# By default, non-admins are not allowed to see traceback details.
yield (key, None)
else:
yield (key, value)
return dict(itertools.chain.from_iterable(
transform(k, v) for k, v in event.as_dict().items()))

View File

@ -0,0 +1,41 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import itertools
_basic_keys = (
'availability_zone',
'binary',
'created_at',
'disabled',
'disabled_reason',
'forced_down',
'host',
'id',
'last_seen_up',
'report_count',
'state',
'updated_at',
)
def format_service(service):
def transform(key, value):
if key not in _basic_keys:
return
yield (key, value)
return dict(
itertools.chain.from_iterable(
transform(k, v)for k, v in service.items()))

View File

@ -16,6 +16,7 @@ import pecan
from zun.api.controllers import base
from zun.api.controllers.v1 import collection
from zun.api.controllers.v1.schemas import services as schema
from zun.api.controllers.v1.views import services_view as view
from zun.api import servicegroup as svcgrp_api
from zun.api import validation
from zun.common import exception
@ -48,6 +49,7 @@ class ZunServiceCollection(collection.Collection):
alive = servicegroup_api.service_is_up(p)
state = 'up' if alive else 'down'
service['state'] = state
service = view.format_service(service)
collection.services.append(service)
if not service['availability_zone']:
service['availability_zone'] = CONF.default_availability_zone