zun-ui/zun_ui/api/rest_api.py
Shu Muto c16e7a4b3d Add container operations
This patch adds 'start', 'stop' and 'logs' operations
for each container.

'start' and 'stop' operations are added as item action
in each row on table view and details view.
'logs' are called while loading details view, then
logs are shown in 'Logs' tab.

Change-Id: I878728a2b05265d29a4d73918146083d90bec749
Partial-Implements: blueprint add-container-operations
2016-10-06 01:35:04 +00:00

96 lines
3.0 KiB
Python

# 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.
from django.views import generic
from zun_ui.api import client
from openstack_dashboard.api.rest import urls
from openstack_dashboard.api.rest import utils as rest_utils
def change_to_id(obj):
"""Change key named 'uuid' to 'id'
Zun returns objects with a field called 'uuid' many of Horizons
directives however expect objects to have a field called 'id'.
"""
obj['id'] = obj.pop('uuid')
return obj
@urls.register
class Container(generic.View):
"""API for retrieving a single container"""
url_regex = r'zun/containers/(?P<id>[^/]+)$'
@rest_utils.ajax()
def get(self, request, id):
"""Get a specific container"""
return change_to_id(client.container_show(request, id).to_dict())
@urls.register
class ContainerActions(generic.View):
"""API for retrieving a single container"""
url_regex = r'zun/containers/(?P<id>[^/]+)/(?P<action>[^/]+)$'
@rest_utils.ajax()
def get(self, request, id, action):
"""Get a specific container info"""
if action == 'logs':
return client.container_logs(request, id)
@rest_utils.ajax()
def post(self, request, id, action):
"""Execute a action of the Containers."""
if action == 'start':
return client.container_start(request, id)
elif action == 'stop':
return client.container_stop(request, id)
@urls.register
class Containers(generic.View):
"""API for Zun Containers"""
url_regex = r'zun/containers/$'
@rest_utils.ajax()
def get(self, request):
"""Get a list of the Containers for a project.
The returned result is an object with property 'items' and each
item under this is a Container.
"""
result = client.container_list(request)
return {'items': [change_to_id(n.to_dict()) for n in result]}
@rest_utils.ajax(data_required=True)
def delete(self, request):
"""Delete one or more Containers by id.
Returns HTTP 204 (no content) on successful deletion.
"""
for id in request.DATA:
client.container_delete(request, id)
@rest_utils.ajax(data_required=True)
def post(self, request):
"""Create a new Container.
Returns the new Container object on success.
"""
new_container = client.container_create(request, **request.DATA)
return rest_utils.CreatedResponse(
'/api/zun/container/%s' % new_container.uuid,
new_container.to_dict())