diff --git a/masakaridashboard/api/api.py b/masakaridashboard/api/api.py index bba4094..2e7904e 100644 --- a/masakaridashboard/api/api.py +++ b/masakaridashboard/api/api.py @@ -146,3 +146,8 @@ def create_host(request, data): def get_host_list(request, segment_id, filters): """Returns host list.""" return openstack_connection(request).hosts(segment_id, **filters) + + +def delete_host(request, host_id, segment_id): + return openstack_connection(request).delete_host( + host_id, segment_id, False) diff --git a/masakaridashboard/hosts/tables.py b/masakaridashboard/hosts/tables.py index 0f8bcd8..c5b0633 100644 --- a/masakaridashboard/hosts/tables.py +++ b/masakaridashboard/hosts/tables.py @@ -12,9 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ungettext_lazy + +from horizon import exceptions from horizon import tables +from masakaridashboard.api import api + + HOST_FILTER_CHOICES = ( ('failover_segment_id', _("Segment Id ="), True), ('type', _("Type ="), True), @@ -28,6 +35,35 @@ class HostFilterAction(tables.FilterAction): filter_choices = HOST_FILTER_CHOICES +class DeleteHost(tables.DeleteAction): + @staticmethod + def action_present(count): + return ungettext_lazy( + u"Delete Host", + u"Delete Hosts", + count + ) + + @staticmethod + def action_past(count): + return ungettext_lazy( + u"Deleted Host", + u"Deleted Hosts", + count + ) + + def delete(self, request, data): + row_data = data.split(',') + segment_uuid = row_data[1] + host_uuid = row_data[0] + try: + api.delete_host(request, host_uuid, segment_uuid) + except Exception: + msg = _('Unable to delete host.') + redirect = reverse('horizon:masakaridashboard:hosts:index') + exceptions.handle(self.request, msg, redirect=redirect) + + class HostTable(tables.DataTable): name = tables.Column('name', verbose_name=_("Name")) @@ -50,4 +86,4 @@ class HostTable(tables.DataTable): class Meta(object): name = "host" verbose_name = _("Host") - table_actions = (HostFilterAction,) + table_actions = (HostFilterAction, DeleteHost) diff --git a/masakaridashboard/hosts/tests.py b/masakaridashboard/hosts/tests.py index 5784097..3ca7eff 100644 --- a/masakaridashboard/hosts/tests.py +++ b/masakaridashboard/hosts/tests.py @@ -70,3 +70,26 @@ class HostTest(test.TestCase): mock.ANY, form_data ) + + def test_delete_ok(self): + host = self.masakari_host.list()[0] + data = {'object_ids': host.uuid + ',' + host.failover_segment_id, + 'action': 'host__delete'} + with mock.patch( + 'masakaridashboard.api.api.segment_list', + return_value=[self.masakari_segment.first( + )]), mock.patch( + 'masakaridashboard.api.api.get_host_list', + return_value=self.masakari_host.list()), mock.patch( + 'masakaridashboard.api.api.delete_host', + return_value=None + ) as mocked_delete: + res = self.client.post(INDEX_URL, data) + + self.assertNoFormErrors(res) + self.assertRedirectsNoFollow(res, INDEX_URL) + mocked_delete.assert_called_once_with( + mock.ANY, + host.uuid, + host.failover_segment_id, + )