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
This commit is contained in:
Shu Muto 2016-10-05 12:26:11 +09:00
parent 543a2a02f4
commit c16e7a4b3d
10 changed files with 280 additions and 2 deletions

View File

@ -74,3 +74,15 @@ def container_list(request, limit=None, marker=None, sort_key=None,
def container_show(request, id):
return zunclient(request).containers.get(id)
def container_logs(request, id):
return zunclient(request).containers.logs(id)
def container_start(request, id):
return zunclient(request).containers.start(id)
def container_stop(request, id):
return zunclient(request).containers.stop(id)

View File

@ -39,6 +39,26 @@ class Container(generic.View):
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"""

View File

@ -30,6 +30,8 @@
'horizon.framework.util.i18n.gettext',
'horizon.dashboard.container.containers.create.service',
'horizon.dashboard.container.containers.delete.service',
'horizon.dashboard.container.containers.start.service',
'horizon.dashboard.container.containers.stop.service',
'horizon.dashboard.container.containers.resourceType',
];
@ -38,10 +40,26 @@
gettext,
createContainerService,
deleteContainerService,
startContainerService,
stopContainerService,
resourceType)
{
var containersResourceType = registry.getResourceType(resourceType);
containersResourceType.itemActions
.append({
id: 'startContainerAction',
service: startContainerService,
template: {
text: gettext('Start Container')
}
})
.append({
id: 'stopContainerAction',
service: stopContainerService,
template: {
text: gettext('Stop Container')
}
})
.append({
id: 'deleteContainerAction',
service: deleteContainerService,

View File

@ -0,0 +1,3 @@
.logs {
margin-top: 18px;
}

View File

@ -41,10 +41,16 @@
) {
registry.getResourceType(resourceType)
.setLoadFunction(loadFunction)
.detailsViews.append({
.detailsViews
.append({
id: 'containerDetailsOverview',
name: gettext('Overview'),
template: basePath + 'details/overview.html'
})
.append({
id: 'containerDetailsLogs',
name: gettext('Logs'),
template: basePath + 'details/logs.html'
});
function loadFunction(identifier) {

View File

@ -0,0 +1,51 @@
/*
* 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.
*/
(function() {
"use strict";
angular
.module('horizon.dashboard.container.containers')
.controller('horizon.dashboard.container.containers.LogsController', controller);
controller.$inject = [
'$scope',
'horizon.app.core.openstack-service-api.zun',
'horizon.dashboard.container.containers.resourceType',
'horizon.dashboard.container.containers.events',
'horizon.framework.conf.resource-type-registry.service'
];
function controller(
$scope,
zun,
resourceType,
events,
registry
) {
var ctrl = this;
ctrl.container = {};
ctrl.logs = {};
$scope.context.loadPromise.then(onGetContainer);
function onGetContainer(container) {
ctrl.container = container.data;
zun.logsContainer(ctrl.container.id).then(onGetLogs);
}
function onGetLogs(logs) {
ctrl.logs = logs.data;
}
}
})();

View File

@ -0,0 +1,7 @@
<div ng-controller="horizon.dashboard.container.containers.LogsController as ctrl">
<div class="row">
<div class="col-md-12 detail">
<pre class="logs">{$ ctrl.logs $}</pre>
</div>
</div>
</div>

View File

@ -0,0 +1,68 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use self 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.
*/
(function() {
'use strict';
angular
.module('horizon.dashboard.container.containers')
.factory('horizon.dashboard.container.containers.start.service', startService);
startService.$inject = [
'horizon.framework.util.q.extensions',
'horizon.framework.widgets.toast.service',
'horizon.app.core.openstack-service-api.zun'
];
/**
* @ngDoc factory
* @name horizon.dashboard.container.containers.start.service
* @Description
* Start container.
*/
function startService(
$qExtensions, toast, zun
) {
var message = {
success: gettext('Container %s was successfully started.')
};
var service = {
initScope: initScope,
allowed: allowed,
perform: perform
};
return service;
//////////////
// include this function in your service
// if you plan to emit events to the parent controller
function initScope() {
}
function allowed() {
return $qExtensions.booleanAsPromise(true);
}
function perform(selected) {
// start selected container
return zun.startContainer(selected.id).success(function(response) {
toast.add('success', interpolate(message.success, [selected.name]));
});
}
}
})();

View File

@ -0,0 +1,68 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use self 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.
*/
(function() {
'use strict';
angular
.module('horizon.dashboard.container.containers')
.factory('horizon.dashboard.container.containers.stop.service', stopService);
stopService.$inject = [
'horizon.framework.util.q.extensions',
'horizon.framework.widgets.toast.service',
'horizon.app.core.openstack-service-api.zun'
];
/**
* @ngDoc factory
* @name horizon.dashboard.container.containers.stop.service
* @Description
* Stop container.
*/
function stopService(
$qExtensions, toast, zun
) {
var message = {
success: gettext('Container %s was successfully stoped.')
};
var service = {
initScope: initScope,
allowed: allowed,
perform: perform
};
return service;
//////////////
// include this function in your service
// if you plan to emit events to the parent controller
function initScope() {
}
function allowed() {
return $qExtensions.booleanAsPromise(true);
}
function perform(selected) {
// start selected container
return zun.stopContainer(selected.id).success(function(response) {
toast.add('success', interpolate(message.success, [selected.name]));
});
}
}
})();

View File

@ -31,6 +31,9 @@
getContainers: getContainers,
deleteContainer: deleteContainer,
deleteContainers: deleteContainers,
startContainer: startContainer,
stopContainer: stopContainer,
logsContainer: logsContainer,
};
return service;
@ -73,7 +76,29 @@
return apiService.delete('/api/zun/containers/', ids)
.error(function() {
toastService.add('error', gettext('Unable to delete the Containers.'));
})
});
}
function startContainer(id) {
return apiService.post('/api/zun/containers/' + id + '/start')
.error(function() {
toastService.add('error', gettext('Unable to start Container'));
});
}
function stopContainer(id) {
return apiService.post('/api/zun/containers/' + id + '/stop')
.error(function() {
toastService.add('error', gettext('Unable to stop Container'));
});
}
function logsContainer(id) {
return apiService.get('/api/zun/containers/' + id + '/logs')
.error(function() {
toastService.add('error', gettext('Unable to get logs of Container'));
});
}
}
}());