Merge pull request #179 from cloud-inovasi/feat-responsehandler-delete-network

feat(network): define a response handler for image deletion
pull/180/head
Anthony Lapenna 2016-09-01 12:21:44 +12:00 committed by GitHub
commit 4125361fb5
4 changed files with 42 additions and 11 deletions

View File

@ -17,18 +17,21 @@ function ($scope, Network, Messages, $state, $stateParams, errorMsgFilter) {
$scope.remove = function remove(networkId) {
$('#loadingViewSpinner').show();
Network.remove({id: $stateParams.id}, function (d) {
var error = errorMsgFilter(d);
if (error) {
if (d.message) {
$('#loadingViewSpinner').hide();
Messages.send("Error", error);
Messages.send("Error", d.message);
} else {
$('#loadingViewSpinner').hide();
Messages.send("Network deleted", $stateParams.id);
Messages.send("Network removed", $stateParams.id);
$state.go('networks', {});
}
}, function (e) {
$('#loadingViewSpinner').hide();
Messages.error("Failure", e.data);
if (e.data.message) {
Messages.error("Failure", e.data.message);
} else {
Messages.error("Failure", 'Unable to remove network');
}
});
};

View File

@ -45,17 +45,21 @@ function ($scope, $state, Network, Config, Messages, errorMsgFilter) {
if (network.Checked) {
counter = counter + 1;
Network.remove({id: network.Id}, function (d) {
var error = errorMsgFilter(d);
if (error) {
Messages.send("Error", error);
if (d.message) {
Messages.send("Error", d.message);
} else {
Messages.send("Network deleted", network.Id);
Messages.send("Network removed", network.Id);
var index = $scope.networks.indexOf(network);
$scope.networks.splice(index, 1);
}
complete();
}, function (e) {
Messages.error("Failure", e.data);
console.log(JSON.stringify(e, null, 4));
if (e.data.message) {
Messages.error("Failure", e.data.message);
} else {
Messages.error("Failure", 'Unable to remove network');
}
complete();
});
}

View File

@ -33,3 +33,27 @@ function deleteImageHandler(data) {
response = angular.fromJson(data);
return response;
}
// Network delete API returns an empty string on success.
// On error, it returns either an error message as a string (Docker < 1.12) or a JSON object with the field message
// container the error (Docker = 1.12).
// This handler returns an empty object on success or a JSON object with the field message container the error message
// on failure.
function deleteNetworkHandler(data) {
console.log(JSON.stringify(data, null, 4));
var response = {};
// No data is returned when deletion is successful (Docker 1.9 -> 1.12)
if (!data) {
return response;
}
// A string is returned when an error occurs (Docker < 1.12)
else if (data && !isJSON(data)) {
response.message = data;
return response;
}
// Docker 1.12 returns a valid JSON object when an error occurs
else {
response = angular.fromJson(data);
}
return response;
}

View File

@ -135,7 +135,7 @@ angular.module('uifordocker.services', ['ngResource', 'ngSanitize'])
query: {method: 'GET', isArray: true},
get: {method: 'GET'},
create: {method: 'POST', params: {action: 'create'}},
remove: {method: 'DELETE'},
remove: { method: 'DELETE', transformResponse: deleteNetworkHandler },
connect: {method: 'POST', params: {action: 'connect'}},
disconnect: {method: 'POST', params: {action: 'disconnect'}}
});