summaryrefslogtreecommitdiff
path: root/openstack_dashboard/static/app/core/server_groups/actions/delete.action.service.js
blob: 51b8b2285825bf59a16ecff8e671969a3fb168f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * 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.app.core.server_groups')
    .factory('horizon.app.core.server_groups.actions.delete.service', deleteServerGroupService);

  deleteServerGroupService.$inject = [
    '$location',
    'horizon.app.core.openstack-service-api.nova',
    'horizon.app.core.openstack-service-api.policy',
    'horizon.app.core.server_groups.resourceType',
    'horizon.framework.util.actions.action-result.service',
    'horizon.framework.util.i18n.ngettext',
    'horizon.framework.widgets.modal.deleteModalService'
  ];

  /*
   * @ngdoc factory
   * @name horizon.app.core.server_groups.actions.delete.service
   *
   * @Description
   * Brings up the delete server groups confirmation modal dialog.

   * On submit, delete given server groups.
   * On cancel, do nothing.
   */
  function deleteServerGroupService(
    $location,
    nova,
    policy,
    serverGroupResourceType,
    actionResultService,
    ngettext,
    deleteModal
  ) {
    return {
      allowed: allowed,
      perform: perform
    };

    //////////////

    function allowed() {
      return policy.ifAllowed(
        {rules: [['compute', 'os_compute_api:os-server-groups:delete']]});
    }

    function perform(items, scope) {
      var servergroups = angular.isArray(items) ? items : [items];
      var context = {
        labels: labelize(servergroups.length),
        deleteEntity: deleteServerGroup
      };
      return deleteModal.open(scope, servergroups, context).then(deleteResult);
    }

    function deleteResult(deleteModalResult) {
      // To make the result of this action generically useful, reformat the return
      // from the deleteModal into a standard form
      var actionResult = actionResultService.getActionResult();
      deleteModalResult.pass.forEach(function markDeleted(item) {
        actionResult.deleted(serverGroupResourceType, item.context.id);
      });
      deleteModalResult.fail.forEach(function markFailed(item) {
        actionResult.failed(serverGroupResourceType, item.context.id);
      });
      var path = '/project/server_groups';
      if ($location.url() !== path && actionResult.result.failed.length === 0 &&
          actionResult.result.deleted.length > 0) {
        $location.path(path);
      } else {
        return actionResult.result;
      }
    }

    function labelize(count) {
      return {
        title: ngettext(
          'Confirm Delete Server Group',
          'Confirm Delete Server Groups', count),
        message: ngettext(
          'You have selected "%s". Deleted Server Group is not recoverable.',
          'You have selected "%s". Deleted Server Groups are not recoverable.', count),
        submit: ngettext(
          'Delete Server Group',
          'Delete Server Groups', count),
        success: ngettext(
          'Deleted Server Group: %s.',
          'Deleted Server Groups: %s.', count),
        error: ngettext(
          'Unable to delete Server Group: %s.',
          'Unable to delete Server Groups: %s.', count)
      };

    }

    function deleteServerGroup(servergroup) {
      return nova.deleteServerGroup(servergroup, true);
    }
  }
})();