~ubuntu-branches/ubuntu/trusty/horizon/trusty-updates

« back to all changes in this revision

Viewing changes to openstack_dashboard/dashboards/admin/aggregates/workflows.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-03-06 16:53:28 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20140306165328-w2vgmtfriqlhp27m
Tags: 1:2014.1~b3-0ubuntu1
* New upstream milestone release.
* d/static/*: Refreshed assets for new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
2
#    not use this file except in compliance with the License. You may obtain
 
3
#    a copy of the License at
 
4
#
 
5
#         http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
7
#    Unless required by applicable law or agreed to in writing, software
 
8
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
9
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
10
#    License for the specific language governing permissions and limitations
 
11
#    under the License.
 
12
 
 
13
from django.utils.translation import ugettext_lazy as _
 
14
 
 
15
from horizon import exceptions
 
16
from horizon import forms
 
17
from horizon import workflows
 
18
 
 
19
from openstack_dashboard import api
 
20
from openstack_dashboard.dashboards.admin.aggregates import constants
 
21
 
 
22
 
 
23
class SetAggregateInfoAction(workflows.Action):
 
24
    name = forms.CharField(label=_("Name"),
 
25
                           max_length=255)
 
26
 
 
27
    availability_zone = forms.CharField(label=_("Availability Zone"),
 
28
                                        max_length=255,
 
29
                                        required=False)
 
30
 
 
31
    class Meta:
 
32
        name = _("Host Aggregate Info")
 
33
        help_text = _("From here you can create a new "
 
34
                      "host aggregate to organize instances.")
 
35
        slug = "set_aggregate_info"
 
36
 
 
37
    def clean(self):
 
38
        cleaned_data = super(SetAggregateInfoAction, self).clean()
 
39
        name = cleaned_data.get('name')
 
40
 
 
41
        try:
 
42
            aggregates = api.nova.aggregate_details_list(self.request)
 
43
        except Exception:
 
44
            msg = _('Unable to get host aggregate list')
 
45
            exceptions.check_message(["Connection", "refused"], msg)
 
46
            raise
 
47
        if aggregates is not None:
 
48
            for aggregate in aggregates:
 
49
                if aggregate.name.lower() == name.lower():
 
50
                    raise forms.ValidationError(
 
51
                        _('The name "%s" is already used by '
 
52
                          'another host aggregate.')
 
53
                        % name
 
54
                    )
 
55
        return cleaned_data
 
56
 
 
57
 
 
58
class SetAggregateInfoStep(workflows.Step):
 
59
    action_class = SetAggregateInfoAction
 
60
    contributes = ("availability_zone",
 
61
                   "name")
 
62
 
 
63
 
 
64
class AddHostsToAggregateAction(workflows.MembershipAction):
 
65
    def __init__(self, request, *args, **kwargs):
 
66
        super(AddHostsToAggregateAction, self).__init__(request,
 
67
                                                        *args,
 
68
                                                        **kwargs)
 
69
        err_msg = _('Unable to get the available hosts')
 
70
 
 
71
        default_role_field_name = self.get_default_role_field_name()
 
72
        self.fields[default_role_field_name] = forms.CharField(required=False)
 
73
        self.fields[default_role_field_name].initial = 'member'
 
74
 
 
75
        field_name = self.get_member_field_name('member')
 
76
        self.fields[field_name] = forms.MultipleChoiceField(required=False)
 
77
 
 
78
        hosts = []
 
79
        try:
 
80
            hosts = api.nova.host_list(request)
 
81
        except Exception:
 
82
            exceptions.handle(request, err_msg)
 
83
 
 
84
        host_names = []
 
85
        for host in hosts:
 
86
            if host.host_name not in host_names and host.service == u'compute':
 
87
                host_names.append(host.host_name)
 
88
        host_names.sort()
 
89
 
 
90
        self.fields[field_name].choices = \
 
91
            [(host_name, host_name) for host_name in host_names]
 
92
 
 
93
    class Meta:
 
94
        name = _("Hosts within aggregate")
 
95
        slug = "add_host_to_aggregate"
 
96
 
 
97
 
 
98
class ManageAggregateHostsAction(workflows.MembershipAction):
 
99
    def __init__(self, request, *args, **kwargs):
 
100
        super(ManageAggregateHostsAction, self).__init__(request,
 
101
                                                         *args,
 
102
                                                         **kwargs)
 
103
        err_msg = _('Unable to get the available hosts')
 
104
 
 
105
        default_role_field_name = self.get_default_role_field_name()
 
106
        self.fields[default_role_field_name] = forms.CharField(required=False)
 
107
        self.fields[default_role_field_name].initial = 'member'
 
108
 
 
109
        field_name = self.get_member_field_name('member')
 
110
        self.fields[field_name] = forms.MultipleChoiceField(required=False)
 
111
 
 
112
        aggregate_id = self.initial['id']
 
113
        aggregate = api.nova.aggregate_get(request, aggregate_id)
 
114
        aggregate_hosts = aggregate.hosts
 
115
 
 
116
        hosts = []
 
117
        try:
 
118
            hosts = api.nova.host_list(request)
 
119
        except Exception:
 
120
            exceptions.handle(request, err_msg)
 
121
 
 
122
        host_names = []
 
123
        for host in hosts:
 
124
            if host.host_name not in host_names and host.service == u'compute':
 
125
                host_names.append(host.host_name)
 
126
        host_names.sort()
 
127
 
 
128
        self.fields[field_name].choices = \
 
129
            [(host_name, host_name) for host_name in host_names]
 
130
 
 
131
        self.fields[field_name].initial = aggregate_hosts
 
132
 
 
133
    class Meta:
 
134
        name = _("Hosts within aggregate")
 
135
 
 
136
 
 
137
class AddHostsToAggregateStep(workflows.UpdateMembersStep):
 
138
    action_class = AddHostsToAggregateAction
 
139
    help_text = _("You can add hosts to this aggregate. One host can be added "
 
140
                  "to one or more aggregate. You can also add the hosts later "
 
141
                  "by editing the aggregate.")
 
142
    available_list_title = _("All available hosts")
 
143
    members_list_title = _("Selected hosts")
 
144
    no_available_text = _("No hosts found.")
 
145
    no_members_text = _("No host selected.")
 
146
    show_roles = False
 
147
    contributes = ("hosts_aggregate",)
 
148
 
 
149
    def contribute(self, data, context):
 
150
        if data:
 
151
            member_field_name = self.get_member_field_name('member')
 
152
            context['hosts_aggregate'] = data.get(member_field_name, [])
 
153
        return context
 
154
 
 
155
 
 
156
class ManageAggregateHostsStep(workflows.UpdateMembersStep):
 
157
    action_class = ManageAggregateHostsAction
 
158
    help_text = _("You can add hosts to this aggregate, as well as remove "
 
159
                  "hosts from it.")
 
160
    available_list_title = _("All Available Hosts")
 
161
    members_list_title = _("Selected Hosts")
 
162
    no_available_text = _("No Hosts found.")
 
163
    no_members_text = _("No Host selected.")
 
164
    show_roles = False
 
165
    depends_on = ("id",)
 
166
    contributes = ("hosts_aggregate",)
 
167
 
 
168
    def contribute(self, data, context):
 
169
        if data:
 
170
            member_field_name = self.get_member_field_name('member')
 
171
            context['hosts_aggregate'] = data.get(member_field_name, [])
 
172
        return context
 
173
 
 
174
 
 
175
class CreateAggregateWorkflow(workflows.Workflow):
 
176
    slug = "create_aggregate"
 
177
    name = _("Create Host Aggregate")
 
178
    finalize_button_name = _("Create Host Aggregate")
 
179
    success_message = _('Created new host aggregate "%s".')
 
180
    failure_message = _('Unable to create host aggregate "%s".')
 
181
    success_url = constants.AGGREGATES_INDEX_URL
 
182
    default_steps = (SetAggregateInfoStep, AddHostsToAggregateStep)
 
183
 
 
184
    def format_status_message(self, message):
 
185
        return message % self.context['name']
 
186
 
 
187
    def handle(self, request, context):
 
188
        try:
 
189
            self.object = \
 
190
                api.nova.aggregate_create(
 
191
                    request,
 
192
                    name=context['name'],
 
193
                    availability_zone=context['availability_zone'])
 
194
        except Exception:
 
195
            exceptions.handle(request, _('Unable to create host aggregate.'))
 
196
            return False
 
197
 
 
198
        hosts = context['hosts_aggregate']
 
199
        for host in hosts:
 
200
            try:
 
201
                api.nova.add_host_to_aggregate(request, self.object.id, host)
 
202
            except Exception:
 
203
                exceptions.handle(
 
204
                    request, _('Error adding Hosts to the aggregate.'))
 
205
                return False
 
206
 
 
207
        return True
 
208
 
 
209
 
 
210
class ManageAggregateHostsWorkflow(workflows.Workflow):
 
211
    slug = "manage_hosts_aggregate"
 
212
    name = _("Add/Remove Hosts to Aggregate")
 
213
    finalize_button_name = _("Save")
 
214
    success_message = _('The Aggregate was updated.')
 
215
    failure_message = _('Unable to update the aggregate.')
 
216
    success_url = constants.AGGREGATES_INDEX_URL
 
217
    default_steps = (ManageAggregateHostsStep, )
 
218
 
 
219
    def format_status_message(self, message):
 
220
        return message
 
221
 
 
222
    def handle(self, request, context):
 
223
        hosts_aggregate = context['hosts_aggregate']
 
224
        aggregate_id = context['id']
 
225
        aggregate = api.nova.aggregate_get(request, aggregate_id)
 
226
        aggregate_hosts = aggregate.hosts
 
227
        for host in aggregate_hosts:
 
228
            api.nova.remove_host_from_aggregate(request, aggregate_id, host)
 
229
 
 
230
        for host in hosts_aggregate:
 
231
            try:
 
232
                api.nova.add_host_to_aggregate(request, aggregate_id, host)
 
233
            except Exception:
 
234
                exceptions.handle(
 
235
                    request, _('Error updating the aggregate.'))
 
236
                return False
 
237
 
 
238
        return True